diff --git a/gradle.properties b/gradle.properties index de9990c..5763f71 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,7 +6,7 @@ minecraft_version=1.21.8 yarn_mappings=1.21.8+build.1 loader_version=0.17.2 # Mod Properties -mod_version=0.2.3 +mod_version=0.2.4 maven_group=moe.sebiann archives_base_name=qol27 # Dependencies diff --git a/src/main/java/moe/sebiann/qol27/client/CarpetSafety.java b/src/main/java/moe/sebiann/qol27/client/CarpetSafety.java new file mode 100644 index 0000000..634023a --- /dev/null +++ b/src/main/java/moe/sebiann/qol27/client/CarpetSafety.java @@ -0,0 +1,63 @@ +package moe.sebiann.qol27.client; + +import moe.sebiann.qol27.config.QoL27Config; +import net.fabricmc.fabric.api.event.player.UseBlockCallback; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.block.CarpetBlock; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ActionResult; +import net.minecraft.util.math.BlockPos; + +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + + +public class CarpetSafety { + public static void initialize() { + QoL27Config config = QoL27Client.getConfig(); + + UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> { + if (!config.noCarpetsOnCarpetsEnabled()) { + return ActionResult.PASS; + } + if (!world.isClient) return ActionResult.PASS; + + ItemStack heldItem = player.getStackInHand(hand); + if (!CARPET_ITEMS.contains(heldItem.getItem())) return ActionResult.PASS; + + BlockPos pos = hitResult.getBlockPos(); + BlockState state = world.getBlockState(pos); + + if (config.sneakOverridesCarpetPlacing() && player.isSneaking()) { + return ActionResult.PASS; + } else if (state.getBlock() instanceof CarpetBlock) { + return ActionResult.FAIL; + } else { + return ActionResult.PASS; + } + }); + } + + private static final Set CARPET_ITEMS = Stream.of( + Blocks.WHITE_CARPET, + Blocks.LIGHT_GRAY_CARPET, + Blocks.GRAY_CARPET, + Blocks.BLACK_CARPET, + Blocks.BROWN_CARPET, + Blocks.ORANGE_CARPET, + Blocks.MAGENTA_CARPET, + Blocks.LIGHT_BLUE_CARPET, + Blocks.YELLOW_CARPET, + Blocks.LIME_CARPET, + Blocks.PINK_CARPET, + Blocks.CYAN_CARPET, + Blocks.PURPLE_CARPET, + Blocks.BLUE_CARPET, + Blocks.GREEN_CARPET, + Blocks.RED_CARPET + ).map(Block::asItem).collect(Collectors.toSet()); +} diff --git a/src/main/java/moe/sebiann/qol27/client/QoL27Client.java b/src/main/java/moe/sebiann/qol27/client/QoL27Client.java index 00b3cea..bc319dd 100644 --- a/src/main/java/moe/sebiann/qol27/client/QoL27Client.java +++ b/src/main/java/moe/sebiann/qol27/client/QoL27Client.java @@ -14,6 +14,7 @@ public class QoL27Client implements ClientModInitializer { public void onInitializeClient() { NoSilkDetection.initialize(); WoodStrippingDetection.initialize(); + CarpetSafety.initialize(); LOGGER.info("{} initialized!", MOD_ID); } diff --git a/src/main/java/moe/sebiann/qol27/config/Config27.java b/src/main/java/moe/sebiann/qol27/config/Config27.java index b93077b..81da1cd 100644 --- a/src/main/java/moe/sebiann/qol27/config/Config27.java +++ b/src/main/java/moe/sebiann/qol27/config/Config27.java @@ -17,4 +17,9 @@ public class Config27 { // @SectionHeader("saveCoordsOnLogout") // public boolean saveCoordsOnLogout = true; + + @SectionHeader("noCarpetsOnCarpets") + public boolean noCarpetsOnCarpetsEnabled = true; + public boolean sneakOverridesCarpetPlacing = true; + } diff --git a/src/main/resources/assets/qol27/lang/en_us.json b/src/main/resources/assets/qol27/lang/en_us.json index cbbe27e..afc2abf 100644 --- a/src/main/resources/assets/qol27/lang/en_us.json +++ b/src/main/resources/assets/qol27/lang/en_us.json @@ -7,5 +7,8 @@ "text.config.qol27-config.option.noStrippingEnabled":"No Stripping Detection Enabled", "text.config.qol27-config.option.sneakOverridesStripping":"Sneak Overrides Stripping", "text.config.qol27-config.section.saveCoordsOnLogout":"Save Coordinates on Logout", - "text.config.qol27-config.option.saveCoordsOnLogout":"Save Coordinates on Logout" + "text.config.qol27-config.option.saveCoordsOnLogout":"Save Coordinates on Logout", + "text.config.qol27-config.section.noCarpetsOnCarpets":"No Carpets on Carpets", + "text.config.qol27-config.option.noCarpetsOnCarpetsEnabled":"No Carpets on Carpets Enabled", + "text.config.qol27-config.option.sneakOverridesCarpetPlacing":"Sneak Overrides Carpet Placing" } \ No newline at end of file