diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/operations/SingleOperation.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/operations/SingleOperation.java index f2075d2a4..980c329b9 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/operations/SingleOperation.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/operations/SingleOperation.java @@ -10,6 +10,7 @@ public enum SingleOperation implements IPeripheralOperation { DIG(1000, 1), USE_ON_BLOCK(5000, 1), + UPDATE_BLOCK(500, 1), SUCK(1000, 1), USE_ON_ANIMAL(2500, 10), CAPTURE_ANIMAL(50_000, 100), diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/BlockEntityPeripheralOwner.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/BlockEntityPeripheralOwner.java index a909d1eda..81250b6f2 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/BlockEntityPeripheralOwner.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/BlockEntityPeripheralOwner.java @@ -1,5 +1,6 @@ package de.srendi.advancedperipherals.common.addons.computercraft.owner; +import dan200.computercraft.api.peripheral.IPeripheral; import de.srendi.advancedperipherals.common.blocks.base.BaseBlock; import de.srendi.advancedperipherals.common.blocks.blockentities.InventoryManagerEntity; import de.srendi.advancedperipherals.common.util.DataStorageUtil; @@ -20,7 +21,6 @@ import org.jetbrains.annotations.Nullable; import java.util.Objects; -import java.util.function.Function; public class BlockEntityPeripheralOwner extends BasePeripheralOwner { @@ -90,7 +90,7 @@ public void markDataStorageDirty() { } @Override - public T1 withPlayer(Function function) { + public T1 withPlayer(APFakePlayer.Action function) { throw new NotImplementedException(); } @@ -124,4 +124,9 @@ public BlockEntityPeripheralOwner attachFuel() { attachAbility(PeripheralOwnerAbility.FUEL, new TileEntityFuelAbility<>(this)); return this; } + + @Override + public U getConnectedPeripheral(Class type) { + throw new NotImplementedException(); + } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/IPeripheralOwner.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/IPeripheralOwner.java index 287c94e4e..2dd333a9d 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/IPeripheralOwner.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/IPeripheralOwner.java @@ -1,5 +1,6 @@ package de.srendi.advancedperipherals.common.addons.computercraft.owner; +import dan200.computercraft.api.peripheral.IPeripheral; import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer; import de.srendi.advancedperipherals.lib.peripherals.IPeripheralOperation; import net.minecraft.core.BlockPos; @@ -14,7 +15,6 @@ import org.jetbrains.annotations.Nullable; import java.util.Collection; -import java.util.function.Function; public interface IPeripheralOwner { @@ -39,7 +39,7 @@ default Vec3 getCenterPos() { void markDataStorageDirty(); - T withPlayer(Function function); + T withPlayer(APFakePlayer.Action function); ItemStack getToolInMainHand(); @@ -70,4 +70,10 @@ default void attachOperation(Collection> operations) { for (IPeripheralOperation operation : operations) operationAbility.registerOperation(operation); } + + T getConnectedPeripheral(Class type); + + default boolean hasConnectedPeripheral(Class type) { + return getConnectedPeripheral(type) != null; + } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/PocketPeripheralOwner.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/PocketPeripheralOwner.java index 7c3f431ec..ffafe5d54 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/PocketPeripheralOwner.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/PocketPeripheralOwner.java @@ -1,9 +1,11 @@ package de.srendi.advancedperipherals.common.addons.computercraft.owner; +import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.pocket.IPocketAccess; import de.srendi.advancedperipherals.common.configuration.APConfig; import de.srendi.advancedperipherals.common.util.DataStorageUtil; import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer; +import de.srendi.advancedperipherals.lib.peripherals.IBasePeripheral; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.FrontAndTop; @@ -17,8 +19,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.function.Function; - public class PocketPeripheralOwner extends BasePeripheralOwner { private final IPocketAccess pocket; @@ -97,7 +97,7 @@ public void markDataStorageDirty() { } @Override - public T withPlayer(Function function) { + public T withPlayer(APFakePlayer.Action function) { throw new NotImplementedException(); } @@ -125,4 +125,18 @@ public boolean isMovementPossible(@NotNull Level level, @NotNull BlockPos pos) { public boolean move(@NotNull Level level, @NotNull BlockPos pos) { return false; } + + @Override + public T getConnectedPeripheral(Class type) { + IPeripheral foundPeripheral = pocket.getUpgrades().values().stream() + .filter(peripheral -> { + if (peripheral == null || type.isInstance(peripheral)) { + return false; + } + return peripheral instanceof IBasePeripheral basePeripheral ? basePeripheral.isEnabled() : true; + }) + .findFirst() + .orElse(null); + return (T) foundPeripheral; + } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/TurtlePeripheralOwner.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/TurtlePeripheralOwner.java index aee1a67c7..17dd0e3fb 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/TurtlePeripheralOwner.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/owner/TurtlePeripheralOwner.java @@ -2,6 +2,7 @@ import com.mojang.authlib.GameProfile; import dan200.computercraft.ComputerCraft; +import dan200.computercraft.api.peripheral.IPeripheral; import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.TurtleSide; import dan200.computercraft.shared.TurtlePermissions; @@ -9,6 +10,7 @@ import de.srendi.advancedperipherals.common.util.DataStorageUtil; import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer; import de.srendi.advancedperipherals.common.util.fakeplayer.FakePlayerProviderTurtle; +import de.srendi.advancedperipherals.lib.peripherals.IBasePeripheral; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.FrontAndTop; @@ -20,7 +22,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.function.Function; +import java.util.stream.Stream; public class TurtlePeripheralOwner extends BasePeripheralOwner { public final ITurtleAccess turtle; @@ -82,7 +84,7 @@ public void markDataStorageDirty() { } @Override - public T withPlayer(Function function) { + public T withPlayer(APFakePlayer.Action function) { return FakePlayerProviderTurtle.withPlayer(turtle, function); } @@ -132,4 +134,19 @@ public TurtlePeripheralOwner attachFuel(int maxFuelConsumptionLevel) { attachAbility(PeripheralOwnerAbility.FUEL, new TurtleFuelAbility(this, maxFuelConsumptionLevel)); return this; } + + @Override + public T getConnectedPeripheral(Class type) { + IPeripheral foundPeripheral = Stream.of(TurtleSide.values()) + .map(side -> turtle.getPeripheral(side)) + .filter(peripheral -> { + if (peripheral == null || type.isInstance(peripheral)) { + return false; + } + return peripheral instanceof IBasePeripheral basePeripheral ? basePeripheral.isEnabled() : true; + }) + .findFirst() + .orElse(null); + return (T) foundPeripheral; + } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredEndAutomataCorePeripheral.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredEndAutomataCorePeripheral.java index 392dcfaf2..c925da8c1 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredEndAutomataCorePeripheral.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredEndAutomataCorePeripheral.java @@ -2,9 +2,7 @@ import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.TurtleSide; -import de.srendi.advancedperipherals.AdvancedPeripherals; import de.srendi.advancedperipherals.common.addons.computercraft.operations.AutomataCoreTier; -import de.srendi.advancedperipherals.common.configuration.APConfig; public class OverpoweredEndAutomataCorePeripheral extends EndAutomataCorePeripheral { @@ -12,12 +10,10 @@ public class OverpoweredEndAutomataCorePeripheral extends EndAutomataCorePeriphe public OverpoweredEndAutomataCorePeripheral(ITurtleAccess turtle, TurtleSide side) { super(TYPE, turtle, side, AutomataCoreTier.OVERPOWERED_TIER2); - setAttribute(ATTR_STORING_TOOL_DURABILITY); } - public void addRotationCycle(int count) { - super.addRotationCycle(count); - if (AdvancedPeripherals.RANDOM.nextDouble() <= APConfig.METAPHYSICS_CONFIG.overpoweredAutomataBreakChance.get()) - owner.destroyUpgrade(); + @Override + public boolean canActiveOverpower() { + return true; } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredHusbandryAutomataCorePeripheral.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredHusbandryAutomataCorePeripheral.java index 220e9ce49..2fee520d5 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredHusbandryAutomataCorePeripheral.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredHusbandryAutomataCorePeripheral.java @@ -2,9 +2,7 @@ import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.TurtleSide; -import de.srendi.advancedperipherals.AdvancedPeripherals; import de.srendi.advancedperipherals.common.addons.computercraft.operations.AutomataCoreTier; -import de.srendi.advancedperipherals.common.configuration.APConfig; public class OverpoweredHusbandryAutomataCorePeripheral extends HusbandryAutomataCorePeripheral { @@ -12,13 +10,10 @@ public class OverpoweredHusbandryAutomataCorePeripheral extends HusbandryAutomat public OverpoweredHusbandryAutomataCorePeripheral(ITurtleAccess turtle, TurtleSide side) { super(TYPE, turtle, side, AutomataCoreTier.OVERPOWERED_TIER2); - setAttribute(ATTR_STORING_TOOL_DURABILITY); } @Override - public void addRotationCycle(int count) { - super.addRotationCycle(count); - if (AdvancedPeripherals.RANDOM.nextDouble() <= APConfig.METAPHYSICS_CONFIG.overpoweredAutomataBreakChance.get()) - owner.destroyUpgrade(); + public boolean canActiveOverpower() { + return true; } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredWeakAutomataCorePeripheral.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredWeakAutomataCorePeripheral.java index 1b19eb40e..d8ed7a2c7 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredWeakAutomataCorePeripheral.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/OverpoweredWeakAutomataCorePeripheral.java @@ -2,9 +2,7 @@ import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.TurtleSide; -import de.srendi.advancedperipherals.AdvancedPeripherals; import de.srendi.advancedperipherals.common.addons.computercraft.operations.AutomataCoreTier; -import de.srendi.advancedperipherals.common.configuration.APConfig; public class OverpoweredWeakAutomataCorePeripheral extends WeakAutomataCorePeripheral { @@ -12,13 +10,10 @@ public class OverpoweredWeakAutomataCorePeripheral extends WeakAutomataCorePerip public OverpoweredWeakAutomataCorePeripheral(ITurtleAccess turtle, TurtleSide side) { super(TYPE, turtle, side, AutomataCoreTier.OVERPOWERED_TIER1); - setAttribute(ATTR_STORING_TOOL_DURABILITY); } @Override - public void addRotationCycle(int count) { - super.addRotationCycle(count); - if (AdvancedPeripherals.RANDOM.nextDouble() <= APConfig.METAPHYSICS_CONFIG.overpoweredAutomataBreakChance.get()) - owner.destroyUpgrade(); + public boolean canActiveOverpower() { + return true; } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java index 1d69afece..f7befdfe0 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/metaphysics/WeakAutomataCorePeripheral.java @@ -48,4 +48,9 @@ public static void addIntegrationPlugin(Function opts = arguments.count() > 0 ? arguments.getTable(0) : Collections.emptyMap(); + float yaw = opts != null ? (float) TableHelper.optNumberField(opts, "yaw", 0) : 0; + float pitch = opts != null ? (float) TableHelper.optNumberField(opts, "pitch", 0) : 0; + return automataCore.withOperation(UPDATE_BLOCK, context -> { + TurtlePeripheralOwner owner = automataCore.getPeripheralOwner(); + ItemStack selectedTool = owner.getToolInMainHand(); + int previousDamageValue = selectedTool.getDamageValue(); + InteractionResult result = owner.withPlayer(APFakePlayer.wrapActionWithRot(yaw, pitch, (player) -> this.updateBlock(player, opts))); + if (result.consumesAction() && automataCore.canActiveOverpower() && automataCore.afterOverpowerAction()) { + selectedTool.setDamageValue(previousDamageValue); + } + return MethodResult.of(result.consumesAction(), result.toString()); + }); + } + + private InteractionResult updateBlock(APFakePlayer player, Map options) { + Level world = player.getLevel(); + HitResult hit = player.findHit(true, false); + if (!(hit instanceof BlockHitResult blockHit)) { + return InteractionResult.PASS; + } + BlockPos pos = blockHit.getBlockPos(); + BlockEntity block = world.getBlockEntity(pos); + if (block instanceof SignBlockEntity sign) { + String text; + try { + text = TableHelper.optStringField(options, "text", null); + } catch (LuaException e) { + // Why not allow empty catch block? TAT + text = null; + } + if (text != null) { + setSignText(world, sign, StringUtil.convertAndToSectionMark(text)); + return InteractionResult.CONSUME; + } + } + return InteractionResult.PASS; + } + /** * placeBlock method will let turtle place a block with more details when compass has equipped. - * It should not able to place fluids / use any item, because compass do not recognize them. + * It should not able to place fluids / use any item, because compass does not recognize them. * * @param options A table contains how to place the block: * x: the x offset relative to the turtle. Default 0 @@ -108,10 +164,9 @@ public final MethodResult useOnBlock(@NotNull IArguments arguments) throws LuaEx * text: the text going to write on the sign. Default is null */ @LuaFunction(mainThread = true) - public MethodResult placeBlock(@NotNull Map options) throws LuaException { + public final MethodResult placeBlock(@NotNull Map options) throws LuaException { ITurtleAccess turtle = automataCore.getPeripheralOwner().getTurtle(); - CompassPeripheral compassPeripheral = Stream.of(TurtleSide.values()).map(side -> turtle.getPeripheral(side) instanceof CompassPeripheral compass ? compass : null).filter(peripheral -> peripheral != null).findFirst().orElse(null); - if (compassPeripheral == null || !compassPeripheral.isEnabled()) { + if (!automataCore.getPeripheralOwner().hasConnectedPeripheral(CompassPeripheral.class)) { return MethodResult.of(false, "COMPASS_NOT_EQUIPPED"); } int x = TableHelper.optIntField(options, "x", 0); @@ -174,12 +229,12 @@ private String deployOn(ItemStack stack, BlockPos position, Direction anchor, Di if (!(item instanceof BlockItem)) { return "NOT_BLOCK"; } - BlockItem block = (BlockItem) item; - InteractionResult res = block.place(context); + BlockItem blockItem = (BlockItem) item; + InteractionResult res = blockItem.place(context); if (!res.consumesAction()) { return "CANNOT_PLACE"; } - if (block instanceof SignItem) { + if (blockItem instanceof SignItem) { BlockEntity blockEntity = world.getBlockEntity(position); if (blockEntity instanceof SignBlockEntity sign) { String text = StringUtil.convertAndToSectionMark(TableHelper.optStringField(options, "text", null)); diff --git a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java index 821b9b876..b7ab0aba4 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java +++ b/src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/plugins/AutomataEntityHandPlugin.java @@ -53,9 +53,9 @@ public final MethodResult useOnAnimal(@NotNull IArguments arguments) throws LuaE ItemStack selectedTool = owner.getToolInMainHand(); int previousDamageValue = selectedTool.getDamageValue(); InteractionResult result = owner.withPlayer(APFakePlayer.wrapActionWithShiftKey(sneak, APFakePlayer.wrapActionWithRot(yaw, pitch, p -> p.useOnFilteredEntity(suitableEntity)))); - if (automataCore.hasAttribute(AutomataCorePeripheral.ATTR_STORING_TOOL_DURABILITY)) + if (automataCore.canActiveOverpower() && automataCore.afterOverpowerAction()) { selectedTool.setDamageValue(previousDamageValue); - + } return MethodResult.of(result.consumesAction(), result.toString()); }); } diff --git a/src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/DistanceDetectorEntity.java b/src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/DistanceDetectorEntity.java index 267107797..2a719f949 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/DistanceDetectorEntity.java +++ b/src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/DistanceDetectorEntity.java @@ -11,6 +11,7 @@ import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; +import net.minecraft.world.level.ClipContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityType; @@ -228,8 +229,8 @@ private HitResult getHitResult(Vec3 to, Vec3 from) { Level level = this.getLevel(); return switch (this.detectionType) { case ENTITY -> HitResultUtil.getEntityHitResult(to, from, level); - case BLOCK -> HitResultUtil.getBlockHitResult(to, from, level, this.ignoreTransparent, this.getBlockPos()); - case BOTH -> HitResultUtil.getHitResult(to, from, level, this.ignoreTransparent, this.getBlockPos()); + case BLOCK -> HitResultUtil.getBlockHitResult(to, from, level, this.ignoreTransparent ? HitResultUtil.IgnoreNoOccludedContext.INSTANCE : ClipContext.Block.COLLIDER, this.getBlockPos()); + case BOTH -> HitResultUtil.getHitResult(to, from, level, this.ignoreTransparent ? HitResultUtil.IgnoreNoOccludedContext.INSTANCE : ClipContext.Block.COLLIDER, this.getBlockPos()); }; } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/ModulePeripheralOwner.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/ModulePeripheralOwner.java index 67bf2b6f0..62d22cdeb 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/ModulePeripheralOwner.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/ModulePeripheralOwner.java @@ -1,8 +1,11 @@ package de.srendi.advancedperipherals.common.smartglasses.modules; +import dan200.computercraft.api.peripheral.IPeripheral; +import dan200.computercraft.core.computer.ComputerSide; import de.srendi.advancedperipherals.common.addons.computercraft.owner.BasePeripheralOwner; import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesComputer; import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer; +import de.srendi.advancedperipherals.lib.peripherals.IBasePeripheral; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.FrontAndTop; @@ -15,7 +18,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.function.Function; +import java.util.stream.Stream; public class ModulePeripheralOwner extends BasePeripheralOwner { @@ -82,7 +85,7 @@ public void markDataStorageDirty() { } @Override - public T withPlayer(Function function) { + public T withPlayer(APFakePlayer.Action function) { throw new NotImplementedException(); } @@ -111,4 +114,18 @@ public boolean move(@NotNull Level level, @NotNull BlockPos pos) { return false; } + @Override + public T getConnectedPeripheral(Class type) { + IPeripheral foundPeripheral = Stream.of(ComputerSide.values()) + .map(side -> computer.getPeripheral(side)) + .filter(peripheral -> { + if (peripheral == null || type.isInstance(peripheral)) { + return false; + } + return peripheral instanceof IBasePeripheral basePeripheral ? basePeripheral.isEnabled() : true; + }) + .findFirst() + .orElse(null); + return (T) foundPeripheral; + } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java b/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java index dd3398362..801937e2c 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/HitResultUtil.java @@ -26,31 +26,31 @@ public class HitResultUtil { /** * This method is used to get the hit result of an entity from the start position of a block * - * @param to the target position/max position - * @param from the source position like a block - * @param level the level - * @param ignoreTransparent if transparent blocks should be ignored + * @param to the target position/max position + * @param from the source position like a block + * @param level the level + * @param shapeGetter the block collision shape getter * @return the hit result. {@link BlockHitResult#miss(Vec3, Direction, BlockPos)} if nothing found */ @NotNull - public static HitResult getHitResult(Vec3 to, Vec3 from, Level level, boolean ignoreTransparent) { - return getHitResult(to, from, level, ignoreTransparent, null); + public static HitResult getHitResult(Vec3 to, Vec3 from, Level level, ClipContext.ShapeGetter shapeGetter) { + return getHitResult(to, from, level, shapeGetter, null); } /** * This method is used to get the hit result of an entity from the start position of a block * - * @param to the target position/max position - * @param from the source position like a block - * @param level the level - * @param ignoreTransparent if transparent blocks should be ignored - * @param source the source Entity/BlockPos that will be ignored + * @param to the target position/max position + * @param from the source position like a block + * @param level the level + * @param shapeGetter the block collision shape getter + * @param source the source Entity/BlockPos that will be ignored * @return the hit result. {@link BlockHitResult#miss(Vec3, Direction, BlockPos)} if nothing found */ @NotNull - public static HitResult getHitResult(Vec3 to, Vec3 from, Level level, boolean ignoreTransparent, Object source) { + public static HitResult getHitResult(Vec3 to, Vec3 from, Level level, ClipContext.ShapeGetter shapeGetter, Object source) { EntityHitResult entityResult = getEntityHitResult(to, from, level, source instanceof Entity ? (Entity) source : null); - BlockHitResult blockResult = getBlockHitResult(to, from, level, ignoreTransparent, source instanceof BlockPos ? (BlockPos) source : null); + BlockHitResult blockResult = getBlockHitResult(to, from, level, shapeGetter, source instanceof BlockPos ? (BlockPos) source : null); if (entityResult.getType() == HitResult.Type.MISS) { if (blockResult.getType() == HitResult.Type.MISS) { @@ -124,30 +124,30 @@ public static EntityHitResult getEntityHitResult(Vec3 to, Vec3 from, Level level /** * This method is used to get the hit result of a block from the start position of a block * - * @param to the target position/max position - * @param from the source position - * @param level the world - * @param ignoreNoOccluded if true, the method will ignore blocks which are not occluding like glass + * @param to the target position/max position + * @param from the source position + * @param level the world + * @param shapeGetter the block collision shape getter * @return the block hit result. {@link BlockHitResult#miss(Vec3, Direction, BlockPos)} if nothing found */ @NotNull - public static BlockHitResult getBlockHitResult(Vec3 to, Vec3 from, Level level, boolean ignoreNoOccluded) { - return getBlockHitResult(to, from, level, ignoreNoOccluded, null); + public static BlockHitResult getBlockHitResult(Vec3 to, Vec3 from, Level level, ClipContext.ShapeGetter shapeGetter) { + return getBlockHitResult(to, from, level, shapeGetter, null); } /** * This method is used to get the hit result of a block from the start position of a block * - * @param to the target position/max position - * @param from the source position - * @param level the world - * @param ignoreNoOccluded if true, the method will ignore blocks which are not occluding like glass - * @param source the source BlockPos that will be ignored + * @param to the target position/max position + * @param from the source position + * @param level the world + * @param shapeGetter the block collision shape getter + * @param source the source BlockPos that will be ignored * @return the block hit result. {@link BlockHitResult#miss(Vec3, Direction, BlockPos)} if nothing found */ @NotNull - public static BlockHitResult getBlockHitResult(Vec3 to, Vec3 from, Level level, boolean ignoreNoOccluded, BlockPos source) { - return level.clip(new AdvancedClipContext(from, to, ignoreNoOccluded ? IgnoreNoOccludedContext.INSTANCE : ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, null, source)); + public static BlockHitResult getBlockHitResult(Vec3 to, Vec3 from, Level level, ClipContext.ShapeGetter shapeGetter, BlockPos source) { + return level.clip(new AdvancedClipContext(from, to, shapeGetter, ClipContext.Fluid.NONE, null, source)); } public static class EmptyEntityHitResult extends EntityHitResult { @@ -170,7 +170,7 @@ public Type getType() { /** * A shape getter which ignores blocks which are not occluding like glass */ - private static class IgnoreNoOccludedContext implements ClipContext.ShapeGetter { + public static class IgnoreNoOccludedContext implements ClipContext.ShapeGetter { public static final IgnoreNoOccludedContext INSTANCE = new IgnoreNoOccludedContext(); private IgnoreNoOccludedContext() {} diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java b/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java index 396aec423..32eea9135 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/LuaConverter.java @@ -13,6 +13,7 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.animal.Animal; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; @@ -79,11 +80,15 @@ public static Map livingEntityToLua(LivingEntity entity, boolean return data; } - public static Map animalToLua(Animal animal, ItemStack itemInHand, boolean detailed) { + public static Map mobToLua(Mob animal, boolean detailed) { Map data = livingEntityToLua(animal, detailed); - data.put("baby", animal.isBaby()); - data.put("inLove", animal.isInLove()); data.put("aggressive", animal.isAggressive()); + return data; + } + + public static Map animalToLua(Animal animal, ItemStack itemInHand, boolean detailed) { + Map data = mobToLua(animal, detailed); + data.put("inLove", animal.isInLove()); if (animal instanceof IForgeShearable shareable && !itemInHand.isEmpty()) { data.put("shareable", shareable.isShearable(itemInHand, animal.level, animal.blockPosition())); } @@ -124,6 +129,7 @@ public static Map completeEntityToLua(Entity entity, ItemStack i public static Map completeEntityToLua(Entity entity, ItemStack itemInHand, boolean detailed) { if (entity instanceof Player player) return playerToLua(player, detailed); if (entity instanceof Animal animal) return animalToLua(animal, itemInHand, detailed); + if (entity instanceof Mob mob) return mobToLua(mob, detailed); if (entity instanceof LivingEntity livingEntity) return livingEntityToLua(livingEntity, detailed); return entityToLua(entity); } diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java index 1bffaa4be..bc7c6bbf2 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/APFakePlayer.java @@ -24,6 +24,7 @@ import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.UseOnContext; +import net.minecraft.world.level.ClipContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; @@ -47,7 +48,6 @@ import java.util.List; import java.util.UUID; -import java.util.function.Function; import java.util.function.Predicate; public class APFakePlayer extends FakePlayer { @@ -112,11 +112,11 @@ public float getEyeHeight(@NotNull Pose pose) { return 0; } - public static Function wrapActionWithRot(float yaw, float pitch, Function action) { + public static Action wrapActionWithRot(float yaw, float pitch, Action action) { return player -> player.doActionWithRot(yaw, pitch, action); } - public T doActionWithRot(float yaw, float pitch, Function action) { + public T doActionWithRot(float yaw, float pitch, Action action) { final float yRot = this.getYRot(), xRot = this.getXRot(); this.setRot(yRot + yaw, xRot + pitch); try { @@ -126,11 +126,11 @@ public T doActionWithRot(float yaw, float pitch, Function a } } - public static Function wrapActionWithShiftKey(boolean shift, Function action) { + public static Action wrapActionWithShiftKey(boolean shift, Action action) { return player -> player.doActionWithShiftKey(shift, action); } - public T doActionWithShiftKey(boolean shift, Function action) { + public T doActionWithShiftKey(boolean shift, Action action) { boolean old = this.isShiftKeyDown(); this.setShiftKeyDown(shift); try { @@ -294,7 +294,7 @@ public HitResult findHit(boolean skipEntity, boolean skipBlock, @Nullable Predic Direction traceDirection = Direction.getNearest(look.x, look.y, look.z); blockHit = BlockHitResult.miss(target, traceDirection, new BlockPos(target)); } else { - blockHit = HitResultUtil.getBlockHitResult(target, origin, level, false, this.source); + blockHit = HitResultUtil.getBlockHitResult(target, origin, level, ClipContext.Block.OUTLINE, this.source); } if (skipEntity) { @@ -353,4 +353,9 @@ public HitResult findHit(boolean skipEntity, boolean skipBlock, @Nullable Predic } return blockHit; } + + @FunctionalInterface + public interface Action { + T apply(APFakePlayer player); + } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java index 26fd8d024..159fcda5c 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java +++ b/src/main/java/de/srendi/advancedperipherals/common/util/fakeplayer/FakePlayerProviderTurtle.java @@ -20,7 +20,6 @@ import org.valkyrienskies.core.api.ships.Ship; import java.util.WeakHashMap; -import java.util.function.Function; public final class FakePlayerProviderTurtle { @@ -55,8 +54,8 @@ public static void load(APFakePlayer player, ITurtleAccess turtle) { direction = new Vec3(newDir.x, newDir.y, newDir.z); } } + player.setPosRaw(position.x, position.y, position.z); player.lookAt(EntityAnchorArgument.Anchor.FEET, position.add(direction)); - player.moveTo(position.x, position.y, position.z, player.getYRot(), player.getXRot()); // Player inventory Inventory playerInventory = player.getInventory(); @@ -115,7 +114,7 @@ public static void unload(APFakePlayer player, ITurtleAccess turtle) { } } - public static T withPlayer(ITurtleAccess turtle, Function function) { + public static T withPlayer(ITurtleAccess turtle, APFakePlayer.Action function) { APFakePlayer player = getPlayer(turtle, turtle.getOwningPlayer()); load(player, turtle); T result = function.apply(player); diff --git a/src/main/java/de/srendi/advancedperipherals/lib/peripherals/AutomataCorePeripheral.java b/src/main/java/de/srendi/advancedperipherals/lib/peripherals/AutomataCorePeripheral.java index 9f827a232..097e855d4 100644 --- a/src/main/java/de/srendi/advancedperipherals/lib/peripherals/AutomataCorePeripheral.java +++ b/src/main/java/de/srendi/advancedperipherals/lib/peripherals/AutomataCorePeripheral.java @@ -4,6 +4,7 @@ import dan200.computercraft.api.lua.MethodResult; import dan200.computercraft.api.turtle.ITurtleAccess; import dan200.computercraft.api.turtle.TurtleSide; +import de.srendi.advancedperipherals.AdvancedPeripherals; import de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation; import de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperationContext; import de.srendi.advancedperipherals.common.addons.computercraft.owner.TurtlePeripheralOwner; @@ -18,11 +19,9 @@ import java.util.Map; public abstract class AutomataCorePeripheral extends BasePeripheral { - - public static final String ATTR_STORING_TOOL_DURABILITY = "storingToolDurability"; - private final IAutomataCoreTier tier; private final Map attributes = new HashMap<>(); + protected boolean destroyed = false; protected AutomataCorePeripheral(String type, ITurtleAccess turtle, TurtleSide side, IAutomataCoreTier tier) { super(type, new TurtlePeripheralOwner(turtle, side)); @@ -85,4 +84,25 @@ public void setAttribute(String attribute) { public Direction validateSide(String direction) throws LuaException { return super.validateSide(direction); } + + public boolean isDestroyed() { + return this.destroyed; + } + + public boolean canActiveOverpower() { + return false; + } + + public abstract double getBreakChance(); + + public boolean afterOverpowerAction() { + if (isDestroyed() || !canActiveOverpower()) { + return false; + } + if (AdvancedPeripherals.RANDOM.nextDouble() <= getBreakChance()) { + this.destroyed = true; + owner.destroyUpgrade(); + } + return true; + } } diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 33142efab..f4a652f24 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -29,7 +29,7 @@ side = "BOTH" modId = "computercraft" mandatory = true versionRange = "[${cc_version},)" -ordering = "NONE" +ordering = "AFTER" side = "BOTH" [[dependencies.${mod_id}]] modId = "curios"