From 577a2476eb6e75c301f593f9b1987ecda744a60c Mon Sep 17 00:00:00 2001 From: WanderingCat27 <51253586+WanderingCat27@users.noreply.github.com> Date: Mon, 10 Oct 2022 20:29:18 -0400 Subject: [PATCH 1/7] trusted players break block config Adds config that will allow trusted players of a chest to break the chest This defaults to false as it makes most sense to retain current behavior by default --- .../com/github/fabricservertools/htm/config/HTMConfig.java | 3 +++ .../fabricservertools/htm/listeners/PlayerEventListener.java | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/fabricservertools/htm/config/HTMConfig.java b/src/main/java/com/github/fabricservertools/htm/config/HTMConfig.java index aec906d..0cc46b9 100644 --- a/src/main/java/com/github/fabricservertools/htm/config/HTMConfig.java +++ b/src/main/java/com/github/fabricservertools/htm/config/HTMConfig.java @@ -22,6 +22,8 @@ public class HTMConfig { .setPrettyPrinting() .create(); + public boolean canTrustedPlayersBreakChests; + public final Map defaultFlags = new HashMap<>(); public final ArrayList autolockingContainers = new ArrayList<>(Arrays.asList( @@ -54,6 +56,7 @@ public class HTMConfig { public HTMConfig() { defaultFlags.put("hoppers", true); + canTrustedPlayersBreakChests = false; } diff --git a/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java b/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java index a1fd073..695983d 100644 --- a/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java +++ b/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java @@ -55,7 +55,7 @@ private static boolean onBeforeBreak(World world, PlayerEntity player, BlockPos if (!lock.isLocked()) return true; - if (lock.isOwner((ServerPlayerEntity) player)) { + if (lock.isOwner((ServerPlayerEntity) player) || (HTM.config.canTrustedPlayersBreakChests && lock.getTrusted().contains(((ServerPlayerEntity) player).getUuid()))) { if (state.getBlock() instanceof LockableChestBlock) { Optional unlocked = ((LockableChestBlock) state.getBlock()).getUnlockedPart(state, world, pos); if (unlocked.isPresent()) { @@ -65,7 +65,6 @@ private static boolean onBeforeBreak(World world, PlayerEntity player, BlockPos } } - Utility.sendMessage(playerEntity, Text.translatable("text.htm.unlocked")); return true; From 1c5ec1f43cacbc112d878eb1c9fc0d1f0fec3cae Mon Sep 17 00:00:00 2001 From: WanderingCat27 <51253586+WanderingCat27@users.noreply.github.com> Date: Mon, 10 Oct 2022 20:35:13 -0400 Subject: [PATCH 2/7] Update README.md Added config section to readme --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 7eecfbd..a478c56 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,13 @@ Toggles no message mode, which hides non-command messages like automatic protect Permission node: `htm.command.quiet` +### Config +`canTrustedPlayersBreakChests`: Toggles whether players trusted to a locked container can break the container, set to false by default meaning only the owner can break a locked container. + +`defaultFlags`: + - `hoppers`: Toggles whether hoppers can pull from locked containers by default, true by default meaning hoppers can pull from locked containers. +`autolockingContainers`: List of containers which will be set to PRIVATE by default, remove or comment out items in the list to make them set to public by default. + ### Additional permissions `htm.admin`: Allows unrestricted access to containers and other managerial permissions \ No newline at end of file From 469ff7fc95966492393a451f252120f39288281b Mon Sep 17 00:00:00 2001 From: WanderingCat27 <51253586+WanderingCat27@users.noreply.github.com> Date: Mon, 10 Oct 2022 20:37:01 -0400 Subject: [PATCH 3/7] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a478c56..09e3048 100644 --- a/README.md +++ b/README.md @@ -79,11 +79,13 @@ Toggles no message mode, which hides non-command messages like automatic protect Permission node: `htm.command.quiet` ### Config -`canTrustedPlayersBreakChests`: Toggles whether players trusted to a locked container can break the container, set to false by default meaning only the owner can break a locked container. +`canTrustedPlayersBreakChests`: Toggles whether players trusted to a locked container can break the container +(set to false by default meaning only the owner can break a locked container). `defaultFlags`: - `hoppers`: Toggles whether hoppers can pull from locked containers by default, true by default meaning hoppers can pull from locked containers. -`autolockingContainers`: List of containers which will be set to PRIVATE by default, remove or comment out items in the list to make them set to public by default. +`autolockingContainers`: List of containers which will be set to PRIVATE by default +(remove or comment out items in the list to make them set to public by default). ### Additional permissions From 7300e9715f97c382906eb21f9058dba0bc0796b2 Mon Sep 17 00:00:00 2001 From: WanderingCat27 <51253586+WanderingCat27@users.noreply.github.com> Date: Tue, 11 Oct 2022 09:35:48 -0400 Subject: [PATCH 4/7] Update README.md --- README.md | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 09e3048..100134a 100644 --- a/README.md +++ b/README.md @@ -80,13 +80,23 @@ Permission node: `htm.command.quiet` ### Config `canTrustedPlayersBreakChests`: Toggles whether players trusted to a locked container can break the container -(set to false by default meaning only the owner can break a locked container). + + (set to false by default meaning only the owner can break a locked container). + + `defaultFlags`: - - `hoppers`: Toggles whether hoppers can pull from locked containers by default, true by default meaning hoppers can pull from locked containers. + + - `hoppers`: Toggles whether hoppers can pull from locked containers by default + + (true by default meaning hoppers can pull from locked containers). + + + `autolockingContainers`: List of containers which will be set to PRIVATE by default -(remove or comment out items in the list to make them set to public by default). + + (remove or comment out items in the list to make them set to public by default). ### Additional permissions -`htm.admin`: Allows unrestricted access to containers and other managerial permissions \ No newline at end of file +`htm.admin`: Allows unrestricted access to containers and other managerial permissions From 976f9c89452e3720c2d0fd675b13f63ee0287812 Mon Sep 17 00:00:00 2001 From: WanderingCat27 <51253586+WanderingCat27@users.noreply.github.com> Date: Tue, 11 Oct 2022 15:38:05 -0400 Subject: [PATCH 5/7] Fixed Hopper flags In my testing hopper flags just did not work. The getInventoryAt() seems to be no longer reliable as a mixin to prevent hopper accessing the chests Injecting and canceling a specific one of the 3 HopperBlockEntity extract methods seems to stop hoppers properly, although, it only stops the hoppers from extracting from a locked and flagged chest while it still is able to insert into any chest logged/flagged or not --- .../htm/mixin/HopperBlockEntityMixin.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/com/github/fabricservertools/htm/mixin/HopperBlockEntityMixin.java b/src/main/java/com/github/fabricservertools/htm/mixin/HopperBlockEntityMixin.java index 6440417..7a1eab5 100644 --- a/src/main/java/com/github/fabricservertools/htm/mixin/HopperBlockEntityMixin.java +++ b/src/main/java/com/github/fabricservertools/htm/mixin/HopperBlockEntityMixin.java @@ -3,18 +3,23 @@ import com.github.fabricservertools.htm.HTMContainerLock; import com.github.fabricservertools.htm.interactions.InteractionManager; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.Hopper; import net.minecraft.block.entity.HopperBlockEntity; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import java.util.Objects; @Mixin(HopperBlockEntity.class) public abstract class HopperBlockEntityMixin { + /* @Redirect(method = "getInventoryAt(Lnet/minecraft/world/World;DDD)Lnet/minecraft/inventory/Inventory;", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;getBlockEntity(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/entity/BlockEntity;")) private static BlockEntity getProtectedInventory(World world, BlockPos pos) { BlockEntity blockEntity = Objects.requireNonNull(world.getBlockEntity(pos)); @@ -35,4 +40,24 @@ private static BlockEntity getProtectedInventory(World world, BlockPos pos) { return null; } + */ + + @Inject(method = "extract(Lnet/minecraft/world/World;Lnet/minecraft/block/entity/Hopper;)Z", at = @At("HEAD"), cancellable = true) + private static void extractHTMCheck(World world, Hopper hopper, CallbackInfoReturnable cir) { + // only checks extraction, so only needs to check block above hopper + if (!isBlockContainerHopperable(world, new BlockPos(hopper.getHopperX(), hopper.getHopperY(), hopper.getHopperZ()).offset(Direction.UP))) + cir.setReturnValue(true); // if block is not hopperable, cancel the extract method call + // otherwise continue the extract method as normal + } + + private static boolean isBlockContainerHopperable(World world, BlockPos pos) { + if (world.isClient) return true; + + BlockEntity blockEntity = world.getBlockEntity(pos); + if (blockEntity == null) // no block entity above + return true; + + HTMContainerLock lock = InteractionManager.getLock((ServerWorld) world, blockEntity); + return lock == null || !lock.isLocked() || lock.getFlags().get("hoppers"); + } } From 3edfd8824ca44c78e2c7d65e31acfbad90409e6c Mon Sep 17 00:00:00 2001 From: WanderingCat27 <51253586+WanderingCat27@users.noreply.github.com> Date: Tue, 11 Oct 2022 16:58:01 -0400 Subject: [PATCH 6/7] Me being forgettful you cannot comment out lines in a json. Though if u were to comment them out it would just be deleted, its not a good thing to encourage. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 100134a..904f782 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ Permission node: `htm.command.quiet` `autolockingContainers`: List of containers which will be set to PRIVATE by default - (remove or comment out items in the list to make them set to public by default). + (remove items in the list to make them set to public by default). ### Additional permissions From a8d0d292eaadfa940cd1bd086fd4e91e25725901 Mon Sep 17 00:00:00 2001 From: Potatoboy9999 <51728317+PotatoPresident@users.noreply.github.com> Date: Sun, 13 Nov 2022 14:56:25 -0800 Subject: [PATCH 7/7] Minor changes --- .../htm/config/HTMConfig.java | 4 +-- .../htm/listeners/PlayerEventListener.java | 2 +- .../htm/mixin/HopperBlockEntityMixin.java | 29 ++----------------- 3 files changed, 4 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/github/fabricservertools/htm/config/HTMConfig.java b/src/main/java/com/github/fabricservertools/htm/config/HTMConfig.java index 0cc46b9..e39d3dc 100644 --- a/src/main/java/com/github/fabricservertools/htm/config/HTMConfig.java +++ b/src/main/java/com/github/fabricservertools/htm/config/HTMConfig.java @@ -22,7 +22,7 @@ public class HTMConfig { .setPrettyPrinting() .create(); - public boolean canTrustedPlayersBreakChests; + public boolean canTrustedPlayersBreakChests = false; public final Map defaultFlags = new HashMap<>(); @@ -56,10 +56,8 @@ public class HTMConfig { public HTMConfig() { defaultFlags.put("hoppers", true); - canTrustedPlayersBreakChests = false; } - public static HTMConfig loadConfig(File file) { HTMConfig config; diff --git a/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java b/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java index 695983d..d67731c 100644 --- a/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java +++ b/src/main/java/com/github/fabricservertools/htm/listeners/PlayerEventListener.java @@ -55,7 +55,7 @@ private static boolean onBeforeBreak(World world, PlayerEntity player, BlockPos if (!lock.isLocked()) return true; - if (lock.isOwner((ServerPlayerEntity) player) || (HTM.config.canTrustedPlayersBreakChests && lock.getTrusted().contains(((ServerPlayerEntity) player).getUuid()))) { + if (lock.isOwner((ServerPlayerEntity) player) || (HTM.config.canTrustedPlayersBreakChests && lock.getTrusted().contains(player.getUuid()))) { if (state.getBlock() instanceof LockableChestBlock) { Optional unlocked = ((LockableChestBlock) state.getBlock()).getUnlockedPart(state, world, pos); if (unlocked.isPresent()) { diff --git a/src/main/java/com/github/fabricservertools/htm/mixin/HopperBlockEntityMixin.java b/src/main/java/com/github/fabricservertools/htm/mixin/HopperBlockEntityMixin.java index 7a1eab5..5389dca 100644 --- a/src/main/java/com/github/fabricservertools/htm/mixin/HopperBlockEntityMixin.java +++ b/src/main/java/com/github/fabricservertools/htm/mixin/HopperBlockEntityMixin.java @@ -12,37 +12,11 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import java.util.Objects; - @Mixin(HopperBlockEntity.class) public abstract class HopperBlockEntityMixin { - /* - @Redirect(method = "getInventoryAt(Lnet/minecraft/world/World;DDD)Lnet/minecraft/inventory/Inventory;", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;getBlockEntity(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/entity/BlockEntity;")) - private static BlockEntity getProtectedInventory(World world, BlockPos pos) { - BlockEntity blockEntity = Objects.requireNonNull(world.getBlockEntity(pos)); - if (world.isClient) return blockEntity; - - HTMContainerLock lock = InteractionManager.getLock((ServerWorld) world, blockEntity); - if (lock == null) { - return blockEntity; - } - - if (!lock.isLocked()) { - return blockEntity; - } - - if (lock.getFlags().get("hoppers")) { - return blockEntity; - } - - return null; - } - */ - - @Inject(method = "extract(Lnet/minecraft/world/World;Lnet/minecraft/block/entity/Hopper;)Z", at = @At("HEAD"), cancellable = true) + @Inject(method = "extract(Lnet/minecraft/world/World;Lnet/minecraft/block/entity/Hopper;)Z", at = @At(value = "FIELD", target = "Lnet/minecraft/util/math/Direction;DOWN:Lnet/minecraft/util/math/Direction;", shift = At.Shift.AFTER), cancellable = true) private static void extractHTMCheck(World world, Hopper hopper, CallbackInfoReturnable cir) { // only checks extraction, so only needs to check block above hopper if (!isBlockContainerHopperable(world, new BlockPos(hopper.getHopperX(), hopper.getHopperY(), hopper.getHopperZ()).offset(Direction.UP))) @@ -50,6 +24,7 @@ private static void extractHTMCheck(World world, Hopper hopper, CallbackInfoRetu // otherwise continue the extract method as normal } + //TODO optimize better private static boolean isBlockContainerHopperable(World world, BlockPos pos) { if (world.isClient) return true;