From ee7bf1db0ffdc1ec0c04fa7bbbaff5f6b2db86af Mon Sep 17 00:00:00 2001 From: FaeWulfs Date: Thu, 14 Nov 2024 12:39:02 +0700 Subject: [PATCH] + Add: Shears defuses TNT. --- .../diversity/event/shearDefusesTnt.java | 64 +++++++++++++++++++ .../diversity/util/config/ModConfigs.java | 3 + .../assets/diversity/lang/en_us.json | 1 + .../data/diversity/tags/item/tnt_defuser.json | 6 ++ .../java/xyz/faewulf/diversity/Diversity.java | 1 + .../event_handler/shearDefusesTnt.java | 10 +++ .../event_handler/shearDefusesTnt.java | 18 ++++++ 7 files changed, 103 insertions(+) create mode 100644 common/src/main/java/xyz/faewulf/diversity/event/shearDefusesTnt.java create mode 100644 common/src/main/resources/data/diversity/tags/item/tnt_defuser.json create mode 100644 fabric/src/main/java/xyz/faewulf/diversity/event_handler/shearDefusesTnt.java create mode 100644 neoforge/src/main/java/xyz/faewulf/diversity/event_handler/shearDefusesTnt.java diff --git a/common/src/main/java/xyz/faewulf/diversity/event/shearDefusesTnt.java b/common/src/main/java/xyz/faewulf/diversity/event/shearDefusesTnt.java new file mode 100644 index 0000000..2771509 --- /dev/null +++ b/common/src/main/java/xyz/faewulf/diversity/event/shearDefusesTnt.java @@ -0,0 +1,64 @@ +package xyz.faewulf.diversity.event; + +import net.minecraft.server.level.ServerLevel; +import net.minecraft.sounds.SoundEvents; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.item.ItemEntity; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.gameevent.GameEvent; +import net.minecraft.world.phys.HitResult; +import xyz.faewulf.diversity.inter.ICustomSniffer; +import xyz.faewulf.diversity.inter.typeSnort; +import xyz.faewulf.diversity.util.compare; +import xyz.faewulf.diversity.util.config.ModConfigs; + +import java.util.ArrayList; +import java.util.List; + +public class shearDefusesTnt { + public static InteractionResult run(Level level, Player player, InteractionHand hand, Entity entity, HitResult hitResult) { + + + //if not enable in config file + if (!ModConfigs.shear_defuses_tnt) + return InteractionResult.PASS; + + Item item = player.getItemInHand(hand).getItem(); + + //if not mainhand + if ((entity.getType() == EntityType.TNT) + && hand == InteractionHand.MAIN_HAND + && hitResult == null + && level instanceof ServerLevel serverLevel + && compare.isHasTag(item, "diversity:tnt_defuser") + ) { + + if (entity.getRandom().nextFloat() < 0.15f) + entity.remove(Entity.RemovalReason.KILLED); + +// ItemStack itemStack = new ItemStack(Items.TNT, 1); +// ItemEntity item1 = new ItemEntity(level, entity.getX(), entity.getY(), entity.getZ(), itemStack); +// item1.setDefaultPickUpDelay(); +// level.addFreshEntity(item1); + + player.swing(hand, true); + player.getItemInHand(hand).hurtAndBreak(1, player, LivingEntity.getSlotForHand(hand)); + entity.playSound(SoundEvents.SHEEP_SHEAR, 1.0f, 1.0f); + + //game event + level.gameEvent(player, GameEvent.ITEM_INTERACT_FINISH, entity.position()); + + return InteractionResult.SUCCESS; + } + + return InteractionResult.PASS; + } +} diff --git a/common/src/main/java/xyz/faewulf/diversity/util/config/ModConfigs.java b/common/src/main/java/xyz/faewulf/diversity/util/config/ModConfigs.java index 735d392..23007f5 100644 --- a/common/src/main/java/xyz/faewulf/diversity/util/config/ModConfigs.java +++ b/common/src/main/java/xyz/faewulf/diversity/util/config/ModConfigs.java @@ -101,6 +101,9 @@ public class ModConfigs { @Entry(category = "item", name = "Shear can pickpocket villager") public static boolean shear_can_pickpocket_villager = true; + @Entry(category = "item", name = "Shear can defuse TNT") + public static boolean shear_defuses_tnt = true; + @Entry(category = "item", name = "Shear prevents plant from growing") public static boolean shear_prevent_growing = true; diff --git a/common/src/main/resources/assets/diversity/lang/en_us.json b/common/src/main/resources/assets/diversity/lang/en_us.json index e60ffdc..683461d 100644 --- a/common/src/main/resources/assets/diversity/lang/en_us.json +++ b/common/src/main/resources/assets/diversity/lang/en_us.json @@ -41,6 +41,7 @@ "diversity.config.clock_shows_time.tooltip": "- Use Clock to show current time.", "diversity.config.shear_prevent_growing.tooltip": "- Shear can be used on Saplings, Bamboo, Vine and Sugar cane to prevent it from growing", "diversity.config.shear_can_pickpocket_villager.tooltip": "- Pickpocket villager using Shear while sneaking.\n- Pickpocket from the front has a high chance of making them angry; reduce it by pickpocketing from behind, or completely nullify it with invisibility potion.\n- Pickpocketing has a chance to reset their profession. The higher the profession level, the lower the chance of it resetting.", + "diversity.config.shear_defuses_tnt.tooltip": "- Shears can be used on primed TNT to give a chance to defuse it.", "diversity.config.spyglass_what_is_that.tooltip": "- See additional block/entity information with a Spyglass, default radius is 5 blocks.\n- Zoom with Spyglass will extend to 32 blocks.", "diversity.config.invisible_frame.tooltip": "- Make item frames invisible by sneak + right-clicking a non-dyed glass pane into them.\n- They stay invisible when holding an item, otherwise, they emit particles as indicators.", "diversity.config.faster_minecart.tooltip": "- Rail placed on `Gravel` will make Minecart and its variants travel faster.", diff --git a/common/src/main/resources/data/diversity/tags/item/tnt_defuser.json b/common/src/main/resources/data/diversity/tags/item/tnt_defuser.json new file mode 100644 index 0000000..7aeb6cb --- /dev/null +++ b/common/src/main/resources/data/diversity/tags/item/tnt_defuser.json @@ -0,0 +1,6 @@ +{ + "replace": false, + "values": [ + "minecraft:shears" + ] +} \ No newline at end of file diff --git a/fabric/src/main/java/xyz/faewulf/diversity/Diversity.java b/fabric/src/main/java/xyz/faewulf/diversity/Diversity.java index b549b8c..9790876 100644 --- a/fabric/src/main/java/xyz/faewulf/diversity/Diversity.java +++ b/fabric/src/main/java/xyz/faewulf/diversity/Diversity.java @@ -43,6 +43,7 @@ private void loadEvent() { useClockOnBlock.register(); useShearOnBlock.register(); onRightClickCropBlocks.register(); + shearDefusesTnt.register(); }); } diff --git a/fabric/src/main/java/xyz/faewulf/diversity/event_handler/shearDefusesTnt.java b/fabric/src/main/java/xyz/faewulf/diversity/event_handler/shearDefusesTnt.java new file mode 100644 index 0000000..ebc0d67 --- /dev/null +++ b/fabric/src/main/java/xyz/faewulf/diversity/event_handler/shearDefusesTnt.java @@ -0,0 +1,10 @@ +package xyz.faewulf.diversity.event_handler; + +import net.fabricmc.fabric.api.event.player.UseEntityCallback; + +public class shearDefusesTnt { + + public static void register() { + UseEntityCallback.EVENT.register(((player, world, hand, entity, hitResult) -> xyz.faewulf.diversity.event.shearDefusesTnt.run(world, player, hand, entity, hitResult))); + } +} diff --git a/neoforge/src/main/java/xyz/faewulf/diversity/event_handler/shearDefusesTnt.java b/neoforge/src/main/java/xyz/faewulf/diversity/event_handler/shearDefusesTnt.java new file mode 100644 index 0000000..23dfe75 --- /dev/null +++ b/neoforge/src/main/java/xyz/faewulf/diversity/event_handler/shearDefusesTnt.java @@ -0,0 +1,18 @@ +package xyz.faewulf.diversity.event_handler; + +import net.minecraft.world.InteractionResult; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent; +import xyz.faewulf.diversity.Constants; + +@EventBusSubscriber(modid = Constants.MOD_ID) +public class shearDefusesTnt { + @SubscribeEvent + public static void onEntityInteract(PlayerInteractEvent.EntityInteract event) { + InteractionResult interactionResult = xyz.faewulf.diversity.event.shearDefusesTnt.run(event.getLevel(), event.getEntity(), event.getHand(), event.getTarget(), null); + + if (interactionResult.consumesAction()) + event.setCanceled(true); + } +}