From 90ef92b453f9f5749d1e533f3f07e58f4544864f Mon Sep 17 00:00:00 2001 From: Jenny Date: Mon, 21 Apr 2025 19:09:01 +0200 Subject: [PATCH] WandBoost onGround() check + Attraction Wand --- .../java/com/jenny/magic/items/items.java | 6 +-- .../com/jenny/magic/items/wands/BaseWand.java | 6 +-- .../magic/items/wands/WandAttraction.java | 8 +++ .../jenny/magic/items/wands/WandBoost.java | 2 +- .../com/jenny/magic/mixins/TemptGoal.java | 54 +++++++++++++++++++ src/main/resources/magic.mixins.json | 3 +- 6 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/jenny/magic/items/wands/WandAttraction.java create mode 100644 src/main/java/com/jenny/magic/mixins/TemptGoal.java diff --git a/src/main/java/com/jenny/magic/items/items.java b/src/main/java/com/jenny/magic/items/items.java index d05d831..a6f81d4 100644 --- a/src/main/java/com/jenny/magic/items/items.java +++ b/src/main/java/com/jenny/magic/items/items.java @@ -3,10 +3,7 @@ package com.jenny.magic.items; import com.jenny.magic.entities.entities; import com.jenny.magic.items.amulets.*; import com.jenny.magic.items.scrolls.*; -import com.jenny.magic.items.wands.ProjectileWand; -import com.jenny.magic.items.wands.WandBoost; -import com.jenny.magic.items.wands.WandPush; -import com.jenny.magic.items.wands.WandVacuum; +import com.jenny.magic.items.wands.*; import net.minecraft.ChatFormatting; import net.minecraft.world.effect.MobEffects; import net.minecraft.world.item.Item; @@ -26,6 +23,7 @@ public class items { public static final RegistryObject WAND_VACUUM = ITEMS.register("wand_vacuum", () -> new WandVacuum(new Item.Properties())); public static final RegistryObject WAND_BOOST = ITEMS.register("wand_boost", () -> new WandBoost(new Item.Properties())); public static final RegistryObject WAND_PUSH = ITEMS.register("wand_push", () -> new WandPush(new Item.Properties())); + public static final RegistryObject WAND_ATTRACTION = ITEMS.register("wand_attraction", () -> new WandAttraction(new Item.Properties())); // SCROLLS public static final RegistryObject SCROLL_TELEPORT = ITEMS.register("scroll_teleport", () -> new ScrollTeleportPersistent(new Item.Properties().stacksTo(1))); public static final RegistryObject SCROLL_TELEPORT_BRITTLE = ITEMS.register("scroll_teleport_brittle", () -> new ScrollTeleportConsumable(new Item.Properties().stacksTo(16))); diff --git a/src/main/java/com/jenny/magic/items/wands/BaseWand.java b/src/main/java/com/jenny/magic/items/wands/BaseWand.java index 25cf018..3be97f8 100644 --- a/src/main/java/com/jenny/magic/items/wands/BaseWand.java +++ b/src/main/java/com/jenny/magic/items/wands/BaseWand.java @@ -4,7 +4,7 @@ import com.jenny.magic.config.ConfigServer; import net.minecraft.ChatFormatting; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; -import net.minecraft.world.entity.player.Player; +import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; @@ -21,9 +21,9 @@ public abstract class BaseWand extends Item { super(properties.durability(100)); } - public static boolean tryUse(ItemStack itemStack, Player player) { + public static boolean tryUse(ItemStack itemStack, LivingEntity livingEntity) { if (!ConfigServer.C_PREVENT_BREAKING.get() || itemStack.getMaxDamage() - itemStack.getDamageValue() > 1) { - itemStack.hurtAndBreak(1, player, (player2) -> player2.broadcastBreakEvent(player2.getUsedItemHand())); + itemStack.hurtAndBreak(1, livingEntity, (player2) -> player2.broadcastBreakEvent(player2.getUsedItemHand())); return true; } return false; diff --git a/src/main/java/com/jenny/magic/items/wands/WandAttraction.java b/src/main/java/com/jenny/magic/items/wands/WandAttraction.java new file mode 100644 index 0000000..cc3ae53 --- /dev/null +++ b/src/main/java/com/jenny/magic/items/wands/WandAttraction.java @@ -0,0 +1,8 @@ +package com.jenny.magic.items.wands; + +public class WandAttraction extends BaseWand { + public WandAttraction(Properties properties) { + super(properties); + } + +} diff --git a/src/main/java/com/jenny/magic/items/wands/WandBoost.java b/src/main/java/com/jenny/magic/items/wands/WandBoost.java index 71058a5..5d37047 100644 --- a/src/main/java/com/jenny/magic/items/wands/WandBoost.java +++ b/src/main/java/com/jenny/magic/items/wands/WandBoost.java @@ -28,7 +28,7 @@ public class WandBoost extends BaseWand { public @NotNull InteractionResultHolder use(@NotNull Level pLevel, Player pPlayer, @NotNull InteractionHand pHand) { ItemStack itemStack = pPlayer.getItemInHand(pHand); - if (tryUse(itemStack, pPlayer)) { + if (pPlayer.onGround() && tryUse(itemStack, pPlayer)) { pPlayer.startUsingItem(pHand); return InteractionResultHolder.consume(itemStack); } diff --git a/src/main/java/com/jenny/magic/mixins/TemptGoal.java b/src/main/java/com/jenny/magic/mixins/TemptGoal.java new file mode 100644 index 0000000..7ec68b9 --- /dev/null +++ b/src/main/java/com/jenny/magic/mixins/TemptGoal.java @@ -0,0 +1,54 @@ +package com.jenny.magic.mixins; + +import com.jenny.magic.items.items; +import com.jenny.magic.items.wands.BaseWand; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.PathfinderMob; +import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(net.minecraft.world.entity.ai.goal.TemptGoal.class) +public abstract class TemptGoal { + @Shadow + @Final + protected PathfinderMob mob; + + @Unique + private static @Nullable ItemStack getStack(@NotNull LivingEntity livingEntity) { + if (livingEntity.getItemInHand(InteractionHand.MAIN_HAND).getItem().asItem().equals(items.WAND_ATTRACTION.get())) { + return livingEntity.getItemInHand(InteractionHand.MAIN_HAND); + } else { + if (items.WAND_ATTRACTION.get().equals(livingEntity.getItemInHand(InteractionHand.OFF_HAND).getItem().asItem())) { + return livingEntity.getItemInHand(InteractionHand.OFF_HAND); + } + } + return null; + } + + /** + * @author xJenny69 + * @reason Attraction Wand + */ + @Inject(method = "shouldFollow", at = @At("RETURN"), cancellable = true) + private void shouldFollow(LivingEntity livingEntity, CallbackInfoReturnable cir) { + ItemStack itemStack = getStack(livingEntity); + if (itemStack != null) { + boolean usable = true; + if (livingEntity.level().getRandom().nextInt(30) == 0 && livingEntity.distanceTo(mob) < 10f) { + usable = BaseWand.tryUse(itemStack, livingEntity); + } + if (usable) { + cir.setReturnValue(true); + } + } + } +} diff --git a/src/main/resources/magic.mixins.json b/src/main/resources/magic.mixins.json index adbed21..5fb3162 100644 --- a/src/main/resources/magic.mixins.json +++ b/src/main/resources/magic.mixins.json @@ -5,7 +5,8 @@ "compatibilityLevel": "JAVA_17", "refmap": "magic.refmap.json", "mixins": [ - "PiglinAi" + "PiglinAi", + "TemptGoal" ], "client": [], "injectors": {