tcr/common/src/main/java/common/item/tool/ItemMagnet.java
2025-07-06 21:43:15 +02:00

71 lines
2.2 KiB
Java
Executable file

package common.item.tool;
import java.util.List;
import java.util.function.Predicate;
import common.entity.Entity;
import common.entity.animal.EntityChicken;
import common.entity.npc.EntityNPC;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.model.Transforms;
import common.util.BoundingBox;
import common.util.Vec3;
import common.world.World;
public class ItemMagnet extends Item {
private final boolean chicken;
public <T extends Entity> ItemMagnet(boolean chicken) {
this.setTab(CheatTab.TOOLS);
this.setUnstackable();
this.chicken = chicken;
}
// public boolean isFull3D() {
// return true;
// }
// public boolean shouldRotateAroundWhenRendering() {
// return true;
// }
public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityNPC playerIn) {
float speed = 1.0f;
if(!worldIn.client) {
List<Entity> list = worldIn
.getEntitiesWithinAABB(this.chicken ? EntityChicken.class : Entity.class,
new BoundingBox(playerIn.posX - 40.0f, playerIn.posY + playerIn.getEyeHeight() - 40.0f, playerIn.posZ - 40.0f,
playerIn.posX + 40.0f, playerIn.posY + playerIn.getEyeHeight() + 40.0f, playerIn.posZ + 40.0f),
new Predicate<Entity>() {
public boolean test(Entity entity) {
return (ItemMagnet.this.chicken || /* ? !((EntityLiving)entity).isAIDisabled() : */ entity.isMagnetic())
&& entity.getDistanceToEntity(playerIn) <= 38.0f;
}
});
for(Entity entity : list) {
if(entity.getDistance(playerIn.posX, playerIn.posY + playerIn.getEyeHeight(), playerIn.posZ) <= 2.0f) {
entity.motionX *= 0.1f;
entity.motionY *= 0.1f;
entity.motionZ *= 0.1f;
}
else {
Vec3 vec = new Vec3(playerIn.posX - entity.posX, playerIn.posY - entity.posY, playerIn.posZ - entity.posZ);
vec = vec.normalize();
entity.addVelocity(vec.xCoord * speed, vec.yCoord * speed, vec.zCoord * speed);
}
}
}
// playerIn.triggerAchievement(StatRegistry.objectUseStats[ItemRegistry.getIdFromItem(this)]);
return itemStackIn;
}
public boolean isMagnetic() {
return true;
}
public Transforms getTransform() {
return Transforms.TOOL_FLIP;
}
}