From 95b094968d601ccd0a2f881cecc5d985cdf77b6a Mon Sep 17 00:00:00 2001 From: xkball <45330674+xkball@users.noreply.github.com> Date: Thu, 5 Sep 2024 22:34:19 +0800 Subject: [PATCH 1/2] add tonk item Took 1 hour 30 minutes --- .../powertool/entity/FenceKnotEntity.java | 3 +- .../teacon/powertool/item/PowerToolItems.java | 3 ++ .../{ItemEventHandler.java => TonkItem.java} | 46 +++++++++++-------- .../powertool/models/item/command_rune.json | 6 +++ .../assets/powertool/models/item/tonk.json | 6 +++ 5 files changed, 42 insertions(+), 22 deletions(-) rename src/main/java/org/teacon/powertool/item/{ItemEventHandler.java => TonkItem.java} (50%) create mode 100644 src/main/resources/assets/powertool/models/item/command_rune.json create mode 100644 src/main/resources/assets/powertool/models/item/tonk.json diff --git a/src/main/java/org/teacon/powertool/entity/FenceKnotEntity.java b/src/main/java/org/teacon/powertool/entity/FenceKnotEntity.java index 29bf08b..4a6a408 100644 --- a/src/main/java/org/teacon/powertool/entity/FenceKnotEntity.java +++ b/src/main/java/org/teacon/powertool/entity/FenceKnotEntity.java @@ -26,7 +26,6 @@ import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.decoration.HangingEntity; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.Vec3; @@ -106,7 +105,7 @@ public InteractionResult interact(Player p, InteractionHand hand) { } var held = p.getItemInHand(hand); - if (held.getItem() == Items.LEAD) { + if (held.getItem() == PowerToolItems.TONK.get()) { var data = held.get(PowerToolItems.KNOT_DATA); if (data == null) { // Connection start. diff --git a/src/main/java/org/teacon/powertool/item/PowerToolItems.java b/src/main/java/org/teacon/powertool/item/PowerToolItems.java index 1fa72da..56d2ba9 100644 --- a/src/main/java/org/teacon/powertool/item/PowerToolItems.java +++ b/src/main/java/org/teacon/powertool/item/PowerToolItems.java @@ -88,6 +88,8 @@ public class PowerToolItems { "cycle", () -> DataComponentType.builder().persistent(Codec.INT).build() ); + public static DeferredHolder TONK; + public static void register(IEventBus bus) { ITEMS.register(bus); ITEMS.register("useless_stick", () -> new Item(new Item.Properties()) { @@ -102,6 +104,7 @@ public boolean isFoil(ItemStack stack) { ITEMS.register("transparent_brush",TransparentBrushItem::new); ITEMS.register("examine_holo_glass",ExamineHoloGlass::new); ITEMS.register("command_rune", () -> new CommandRune(new Item.Properties())); + TONK = ITEMS.register("tonk", () -> new TonkItem(new Item.Properties())); CREATIVE_MODE_TABS.register(bus); DATA_COMPONENTS.register(bus); ARMOR_MATERIAL.register(bus); diff --git a/src/main/java/org/teacon/powertool/item/ItemEventHandler.java b/src/main/java/org/teacon/powertool/item/TonkItem.java similarity index 50% rename from src/main/java/org/teacon/powertool/item/ItemEventHandler.java rename to src/main/java/org/teacon/powertool/item/TonkItem.java index 780c119..801a5bb 100644 --- a/src/main/java/org/teacon/powertool/item/ItemEventHandler.java +++ b/src/main/java/org/teacon/powertool/item/TonkItem.java @@ -1,40 +1,45 @@ package org.teacon.powertool.item; +import net.minecraft.MethodsReturnNonnullByDefault; +import net.minecraft.tags.BlockTags; +import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Mob; -import net.minecraft.world.item.Items; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.gameevent.GameEvent; import net.minecraft.world.phys.AABB; -import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.fml.LogicalSide; -import net.neoforged.fml.common.EventBusSubscriber; -import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent; -import org.teacon.powertool.PowerTool; import org.teacon.powertool.entity.FenceKnotEntity; -@EventBusSubscriber(modid = PowerTool.MODID, bus = EventBusSubscriber.Bus.GAME) -public class ItemEventHandler { +import javax.annotation.ParametersAreNonnullByDefault; - @SubscribeEvent - public static void on(PlayerInteractEvent.RightClickBlock event) { - if (event.getSide() == LogicalSide.SERVER) { - var player = event.getEntity(); - var held = player.getItemInHand(event.getHand()); - if (held.getItem() != Items.LEAD) { - return; +@MethodsReturnNonnullByDefault +@ParametersAreNonnullByDefault +public class TonkItem extends Item { + + public TonkItem(Properties properties) { + super(properties); + } + + @Override + public InteractionResult useOn(UseOnContext context) { + if (!context.getLevel().isClientSide() && context.getPlayer() != null) { + var player = context.getPlayer(); + var held = context.getItemInHand(); + var level = context.getLevel(); + var pos = context.getClickedPos(); + if(!level.getBlockState(pos).is(BlockTags.FENCES)){ + return InteractionResult.PASS; } - var level = event.getLevel(); - var pos = event.getPos(); var range = new AABB(pos.getX() - 7, pos.getY() - 7, pos.getZ() - 7, pos.getX() + 7, pos.getY() + 7, pos.getZ() + 7); for (var mob : level.getEntitiesOfClass(Mob.class, range)) { if (mob.getLeashHolder() == player) { - return; + return InteractionResult.PASS; } } range = new AABB(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1); if (!level.getEntitiesOfClass(FenceKnotEntity.class, range).isEmpty()) { - return; + return InteractionResult.PASS; } - event.setCanceled(true); var knot = new FenceKnotEntity(level, pos); level.addFreshEntity(knot); knot.playPlacementSound(); @@ -43,5 +48,6 @@ public static void on(PlayerInteractEvent.RightClickBlock event) { held.shrink(1); } } + return InteractionResult.SUCCESS; } } diff --git a/src/main/resources/assets/powertool/models/item/command_rune.json b/src/main/resources/assets/powertool/models/item/command_rune.json new file mode 100644 index 0000000..284bf83 --- /dev/null +++ b/src/main/resources/assets/powertool/models/item/command_rune.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:item/handheld", + "textures": { + "layer0": "powertool:item/command_rune" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/powertool/models/item/tonk.json b/src/main/resources/assets/powertool/models/item/tonk.json new file mode 100644 index 0000000..7f0f181 --- /dev/null +++ b/src/main/resources/assets/powertool/models/item/tonk.json @@ -0,0 +1,6 @@ +{ + "parent": "minecraft:item/handheld", + "textures": { + "layer0": "powertool:item/tonk" + } +} \ No newline at end of file From 1bfa7a0063774a5609d3b08080ebd2469f55c00b Mon Sep 17 00:00:00 2001 From: xkball <45330674+xkball@users.noreply.github.com> Date: Thu, 5 Sep 2024 22:36:04 +0800 Subject: [PATCH 2/2] bump version to 1.4.5 Took 2 minutes --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 389a297..01d412e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -34,7 +34,7 @@ mod_name=Power Tool # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=GPL-3.0 # The mod version. See https://semver.org/ -mod_version=1.4.4 +mod_version=1.4.5 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html