Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ItemEntityCatcher
- public abstract class ItemEntityCatcher extends Item {
- public ItemEntityCatcher(String name) {
- setMaxStackSize(1);
- setUnlocalizedName(EntityCatcher.MODID + "_" + name);
- setCreativeTab(EntityCatcher.tab);
- setRegistryName(name);
- GameRegistry.register(this);
- }
- @Override
- public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
- if (!world.isRemote) {
- NBTTagCompound nbt = NBTTagCompounds.getOrCreateTag(stack);
- NBTTagCompound entityNBT = nbt.getCompoundTag("WrittenEntity");
- Entity entityFromNBT = EntityList.createEntityFromNBT(entityNBT, world);
- world.spawnEntityInWorld(entityFromNBT);
- stack.damageItem(1, player);
- }
- return EnumActionResult.SUCCESS;
- }
- @Override
- public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) {
- super.addInformation(stack, playerIn, tooltip, advanced);
- }
- public static class SingleUseEntityCatcher extends ItemEntityCatcher {
- public SingleUseEntityCatcher() {
- super("single_use_catcher");
- setMaxDamage(1);
- }
- }
- public static class MultiUseEntityCatcher extends ItemEntityCatcher {
- public MultiUseEntityCatcher() {
- super("multi_use_catcher");
- setMaxDamage(25);
- }
- }
- }
- The Event Handler:
- public class ECEventHandler {
- @SubscribeEvent
- public void interactionEvent(PlayerInteractEvent.EntityInteract event) {
- EntityPlayer player = event.getEntityPlayer();
- if (!player.worldObj.isRemote) {
- Entity entity = event.getTarget();
- if (entity != null && !entity.isRiding()) {
- ItemStack held = player.getHeldItemMainhand();
- if (held != null && held.getItem() instanceof ItemEntityCatcher) {
- NBTTagCompound nbt = NBTTagCompounds.getOrCreateTag(held);
- NBTTagCompound entityNBT = new NBTTagCompound();
- entity.writeToNBTOptional(entityNBT);
- nbt.setTag("WrittenEntity", entityNBT);
- entity.setGlowing(true);
- entity.attackEntityFrom(EntityDamageSource.outOfWorld, Float.MAX_VALUE);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement