diff --git a/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java b/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java index fb75b644..35a7fe70 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/ItemImpl.java @@ -1,15 +1,26 @@ package com.minelittlepony.unicopia.entity; +import java.util.Random; + import com.minelittlepony.unicopia.Owned; import com.minelittlepony.unicopia.Race; +import com.minelittlepony.unicopia.item.enchantment.UEnchantments; +import com.minelittlepony.unicopia.util.VecHelper; +import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.ItemEntity; +import net.minecraft.entity.MovementType; import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.data.TrackedData; import net.minecraft.entity.data.TrackedDataHandlerRegistry; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; +import net.minecraft.particle.ParticleEffect; +import net.minecraft.particle.ParticleTypes; import net.minecraft.util.ActionResult; +import net.minecraft.util.math.Vec3d; public class ItemImpl implements Equine, Owned { private static final TrackedData ITEM_RACE = DataTracker.registerData(ItemEntity.class, TrackedDataHandlerRegistry.INTEGER); @@ -42,8 +53,41 @@ public class ItemImpl implements Equine, Owned { ItemStack stack = owner.getStack(); - if (!stack.isEmpty() && stack.getItem() instanceof TickableItem) { - return ((TickableItem)stack.getItem()).onGroundTick((IItemEntity)owner) == ActionResult.SUCCESS; + if (!stack.isEmpty()) { + + Item item = stack.getItem(); + ClingyItem clingy = item instanceof ClingyItem ? (ClingyItem)item : ClingyItem.DEFAULT; + + IItemEntity i = (IItemEntity)owner; + + if (clingy.isClingy(stack)) { + Random rng = owner.world.random; + + owner.world.addParticle(clingy.getParticleEffect((IItemEntity)owner), + owner.getX() + rng.nextFloat() - 0.5, + owner.getY() + rng.nextFloat() - 0.5, + owner.getZ() + rng.nextFloat() - 0.5, + 0, 0, 0 + ); + + Vec3d position = owner.getPos(); + VecHelper.findInRange(owner, owner.world, owner.getPos(), clingy.getFollowDistance(i), e -> e instanceof PlayerEntity) + .stream() + .sorted((a, b) -> (int)(a.getPos().distanceTo(position) - b.getPos().distanceTo(position))) + .findFirst() + .ifPresent(player -> { + owner.move(MovementType.SELF, player.getPos().subtract(owner.getPos()).multiply(clingy.getFollowSpeed(i))); + if (owner.horizontalCollision) { + owner.move(MovementType.SELF, new Vec3d(0, owner.verticalCollision ? -0.3 : 0.3, 0)); + } + + clingy.interactWithPlayer(i, (PlayerEntity)player); + }); + } + + if (stack.getItem() instanceof TickableItem) { + return ((TickableItem)stack.getItem()).onGroundTick(i) == ActionResult.SUCCESS; + } } return false; @@ -70,7 +114,6 @@ public class ItemImpl implements Equine, Owned { physics.toNBT(compound); } - @Override public void fromNBT(CompoundTag compound) { setSpecies(Race.fromName(compound.getString("owner_species"))); @@ -90,4 +133,28 @@ public class ItemImpl implements Equine, Owned { public interface TickableItem { ActionResult onGroundTick(IItemEntity entity); } + + public interface ClingyItem { + ClingyItem DEFAULT = stack -> { + return EnchantmentHelper.getLevel(UEnchantments.CLINGY, stack) > 0; + }; + + boolean isClingy(ItemStack stack); + + default ParticleEffect getParticleEffect(IItemEntity entity) { + return ParticleTypes.AMBIENT_ENTITY_EFFECT; + } + + default float getFollowDistance(IItemEntity entity) { + return 6 * (1 + EnchantmentHelper.getLevel(UEnchantments.CLINGY, entity.get().getMaster().getStack())); + } + + default float getFollowSpeed(IItemEntity entity) { + return Math.min(1, 0.02F * (1 + EnchantmentHelper.getLevel(UEnchantments.CLINGY, entity.get().getMaster().getStack()))); + } + + default void interactWithPlayer(IItemEntity entity, PlayerEntity player) { + + } + } } diff --git a/src/main/java/com/minelittlepony/unicopia/item/enchantment/UEnchantments.java b/src/main/java/com/minelittlepony/unicopia/item/enchantment/UEnchantments.java index 60db4c5f..e2a61d6b 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/enchantment/UEnchantments.java +++ b/src/main/java/com/minelittlepony/unicopia/item/enchantment/UEnchantments.java @@ -75,7 +75,7 @@ public interface UEnchantments { /** * This item just wants to be held. */ - Enchantment CLINGY = register("clingy", new SimpleEnchantment(Rarity.VERY_RARE, true, 1, EquipmentSlot.values())); + Enchantment CLINGY = register("clingy", new SimpleEnchantment(Rarity.VERY_RARE, true, 6, EquipmentSlot.values())); static void bootstrap() { } diff --git a/src/main/resources/assets/unicopia/lang/en_us.json b/src/main/resources/assets/unicopia/lang/en_us.json index d8243fec..6419dbf2 100644 --- a/src/main/resources/assets/unicopia/lang/en_us.json +++ b/src/main/resources/assets/unicopia/lang/en_us.json @@ -166,6 +166,8 @@ "enchantment.unicopia.gem_location": "Gem Locator", "enchantment.unicopia.padded": "Padded", + "enchantment.unicopia.clingy": "Clings", + "enchantment.unicopia.repulsion": "Repulsive", "enchantment.unicopia.heavy": "Heavy", "enchantment.unicopia.collaborator": "Collaborator", "enchantment.unicopia.desired": "Want It Need It",