``` [17:06:57] [Server thread/WARN]: [FML]: Java has been asked to exit (code -2) [17:06:57] [Server thread/WARN]: [FML]: This is an abortive exit and could cause world corruption or other things [17:06:57] [Server thread/WARN]: [FML]: Exit trace: [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.nbt.NBTTagCompound.handler$checkNullTag$bdj000(NBTTagCompound.java:1158) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.nbt.NBTTagCompound.func_74782_a(NBTTagCompound.java) [17:06:57] [Server thread/WARN]: [FML]: tschipp.carryon.common.item.ItemEntity.storeEntityData(ItemEntity.java:84) [17:06:57] [Server thread/WARN]: [FML]: tschipp.carryon.common.event.ItemEntityEvents.onEntityRightClick(ItemEntityEvents.java:114) [17:06:57] [Server thread/WARN]: [FML]: net.minecraftforge.fml.common.eventhandler.ASMEventHandler_1235_ItemEntityEvents_onEntityRightClick_EntityInteract.invoke(.dynamic) [17:06:57] [Server thread/WARN]: [FML]: net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90) [17:06:57] [Server thread/WARN]: [FML]: net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:750) [17:06:57] [Server thread/WARN]: [FML]: net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:700) [17:06:57] [Server thread/WARN]: [FML]: net.minecraftforge.common.ForgeHooks.onInteractEntity(ForgeHooks.java:1116) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.entity.player.EntityPlayer.func_190775_a(EntityPlayer.java:1194) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.network.NetHandlerPlayServer.func_147340_a(NetHandlerPlayServer.java:2691) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.network.play.client.CPacketUseEntity.func_148833_a(SourceFile:69) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.network.play.client.CPacketUseEntity.func_148833_a(SourceFile:13) [17:06:57] [Server thread/WARN]: [FML]: org.spongepowered.common.event.tracking.phase.packet.PacketPhaseUtil.onProcessPacket(PacketPhaseUtil.java:193) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.network.PacketThreadUtil$1.redirect$onProcessPacket$zll000(SourceFile:539) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.network.PacketThreadUtil$1.run(SourceFile:13) [17:06:57] [Server thread/WARN]: [FML]: java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [17:06:57] [Server thread/WARN]: [FML]: java.util.concurrent.FutureTask.run(FutureTask.java:266) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.util.Util.func_181617_a(SourceFile:46) [17:06:57] [Server thread/WARN]: [FML]: org.spongepowered.common.SpongeImplHooks.onUtilRunTask(SpongeImplHooks.java:305) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.server.MinecraftServer.redirect$onRun$zjp000(MinecraftServer.java:3965) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:723) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:397) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:668) [17:06:57] [Server thread/WARN]: [FML]: net.minecraft.server.MinecraftServer.run(MinecraftServer.java:526) [17:06:57] [Server thread/WARN]: [FML]: java.lang.Thread.run(Thread.java:748) ```
225 lines
5.7 KiB
Java
225 lines
5.7 KiB
Java
package tschipp.carryon.common.item;
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import net.minecraft.block.Block;
|
|
import net.minecraft.entity.Entity;
|
|
import net.minecraft.entity.EntityList;
|
|
import net.minecraft.entity.EntityLiving;
|
|
import net.minecraft.entity.EntityLivingBase;
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
import net.minecraft.init.MobEffects;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.potion.PotionEffect;
|
|
import net.minecraft.util.EnumActionResult;
|
|
import net.minecraft.util.EnumFacing;
|
|
import net.minecraft.util.EnumHand;
|
|
import net.minecraft.util.ResourceLocation;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.util.text.translation.I18n;
|
|
import net.minecraft.world.World;
|
|
import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint;
|
|
import net.minecraftforge.fml.common.registry.GameRegistry;
|
|
import tschipp.carryon.CarryOn;
|
|
import tschipp.carryon.common.config.CarryOnConfig;
|
|
import tschipp.carryon.network.client.CarrySlotPacket;
|
|
|
|
public class ItemEntity extends Item
|
|
{
|
|
|
|
public static final String ENTITY_DATA_KEY = "entityData";
|
|
|
|
public ItemEntity()
|
|
{
|
|
this.setUnlocalizedName("entity_item");
|
|
this.setRegistryName(CarryOn.MODID, "entity_item");
|
|
GameRegistry.register(this);
|
|
this.setMaxStackSize(1);
|
|
}
|
|
|
|
@Override
|
|
public String getItemStackDisplayName(ItemStack stack)
|
|
{
|
|
if (hasEntityData(stack))
|
|
{
|
|
return I18n.translateToLocal("entity."+EntityList.getTranslationName(new ResourceLocation(getEntityName(stack))) + ".name");
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
public static boolean hasEntityData(ItemStack stack)
|
|
{
|
|
if (stack.hasTagCompound())
|
|
{
|
|
NBTTagCompound tag = stack.getTagCompound();
|
|
return tag.hasKey(ENTITY_DATA_KEY) && tag.hasKey("entity");
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static boolean storeEntityData(@Nonnull Entity entity, World world, ItemStack stack)
|
|
{
|
|
if (entity == null)
|
|
return false;
|
|
|
|
if (stack.isEmpty())
|
|
return false;
|
|
|
|
NBTTagCompound entityData = new NBTTagCompound();
|
|
entityData = entity.writeToNBT(entityData);
|
|
|
|
if (entityData == null)
|
|
return false;
|
|
|
|
String name = EntityList.getKey(entity).toString();
|
|
|
|
NBTTagCompound tag = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound();
|
|
if (tag.hasKey(ENTITY_DATA_KEY))
|
|
return false;
|
|
|
|
tag.setTag(ENTITY_DATA_KEY, entityData);
|
|
tag.setString("entity", name);
|
|
stack.setTagCompound(tag);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
|
|
{
|
|
ItemStack stack = player.getHeldItem(hand);
|
|
Block block = world.getBlockState(pos).getBlock();
|
|
|
|
if (hasEntityData(stack))
|
|
{
|
|
BlockPos finalPos = pos;
|
|
|
|
if (!block.isReplaceable(world, pos))
|
|
{
|
|
finalPos = pos.offset(facing);
|
|
}
|
|
|
|
Entity entity = getEntity(stack, world);
|
|
if (entity != null)
|
|
{
|
|
if (!world.isRemote)
|
|
{
|
|
entity.setPositionAndRotation(finalPos.getX() + 0.5, finalPos.getY(), finalPos.getZ() + 0.5, 180 + player.rotationYawHead, 0.0f);
|
|
world.spawnEntity(entity);
|
|
if (entity instanceof EntityLiving)
|
|
{
|
|
((EntityLiving) entity).playLivingSound();
|
|
}
|
|
clearEntityData(stack);
|
|
player.setHeldItem(hand, ItemStack.EMPTY);
|
|
CarryOn.network.sendToAllAround(new CarrySlotPacket(9, player.getEntityId()), new TargetPoint(world.provider.getDimension(), player.posX, player.posY, player.posZ, 256));
|
|
}
|
|
player.getEntityData().removeTag("overrideKey");
|
|
return EnumActionResult.SUCCESS;
|
|
}
|
|
}
|
|
|
|
return EnumActionResult.FAIL;
|
|
}
|
|
|
|
@Override
|
|
public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected)
|
|
{
|
|
if (hasEntityData(stack))
|
|
{
|
|
if(getEntity(stack, world) == null)
|
|
stack = ItemStack.EMPTY;
|
|
|
|
if (entity instanceof EntityLivingBase)
|
|
{
|
|
if(entity instanceof EntityPlayer && CarryOnConfig.settings.slownessInCreative ? false : ((EntityPlayer)entity).isCreative())
|
|
return;
|
|
|
|
((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.SLOWNESS, 1, potionLevel(stack, world), false, false));
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
stack = ItemStack.EMPTY;
|
|
}
|
|
}
|
|
|
|
public static void clearEntityData(ItemStack stack)
|
|
{
|
|
if (stack.hasTagCompound())
|
|
{
|
|
NBTTagCompound tag = stack.getTagCompound();
|
|
tag.removeTag(ENTITY_DATA_KEY);
|
|
tag.removeTag("entity");
|
|
}
|
|
}
|
|
|
|
public static NBTTagCompound getEntityData(ItemStack stack)
|
|
{
|
|
if (stack.hasTagCompound())
|
|
{
|
|
NBTTagCompound tag = stack.getTagCompound();
|
|
return tag.getCompoundTag(ENTITY_DATA_KEY);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static Entity getEntity(ItemStack stack, World world)
|
|
{
|
|
if (world == null)
|
|
return null;
|
|
|
|
String name = getEntityName(stack);
|
|
|
|
NBTTagCompound e = getEntityData(stack);
|
|
Entity entity = EntityList.createEntityByIDFromName(new ResourceLocation(name), world);
|
|
if (entity != null)
|
|
entity.readFromNBT(e);
|
|
|
|
return entity;
|
|
}
|
|
|
|
public static String getEntityName(ItemStack stack)
|
|
{
|
|
if (stack.hasTagCompound())
|
|
{
|
|
NBTTagCompound tag = stack.getTagCompound();
|
|
return tag.getString("entity");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static String getCustomName(ItemStack stack)
|
|
{
|
|
if (stack.hasTagCompound())
|
|
{
|
|
NBTTagCompound tag = stack.getTagCompound();
|
|
if (tag.hasKey("CustomName") && !tag.getString("CustomName").isEmpty()) {
|
|
return tag.toString();
|
|
} else {
|
|
return tag.toString();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private int potionLevel(ItemStack stack, World world)
|
|
{
|
|
Entity e = getEntity(stack, world);
|
|
if(e == null)
|
|
return 1;
|
|
|
|
int i = (int)(e.height * e.width);
|
|
if (i > 4)
|
|
i = 4;
|
|
|
|
if (!CarryOnConfig.settings.heavyEntities)
|
|
i = 1;
|
|
|
|
return (int) (i * CarryOnConfig.settings.entitySlownessMultiplier);
|
|
}
|
|
}
|