Dartcraft/src/main/java/ley/modding/dartcraft/event/EntityBottleHandler.java

150 lines
7.2 KiB
Java
Raw Normal View History

2021-04-04 16:10:26 +02:00
package ley.modding.dartcraft.event;
import cpw.mods.fml.common.Mod.EventHandler;
import ley.modding.dartcraft.Dartcraft;
2021-04-07 12:45:02 +02:00
import ley.modding.dartcraft.item.DartItems;
2021-04-04 16:10:26 +02:00
import ley.modding.dartcraft.util.EntityUtils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityAgeable;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.*;
import net.minecraft.entity.passive.*;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
public class EntityBottleHandler {
@EventHandler
public void handleTaggedPickup(EntityItemPickupEvent event) {
try {
if (((PlayerEvent) event).entityPlayer == null || event.item == null
|| event.item.getEntityItem() == null)
2021-04-04 16:10:26 +02:00
return;
boolean trigger = false;
2021-04-07 12:45:02 +02:00
if (event.item.getEntityItem().getItem() == DartItems.entitybottle)
if (meshBottles(
((PlayerEvent) event).entityPlayer, event.item.getEntityItem()
)) {
2021-04-04 16:10:26 +02:00
event.setCanceled(true);
trigger = true;
}
if (Dartcraft.proxy.isSimulating(
((Entity) ((PlayerEvent) event).entityPlayer).worldObj
)
&& trigger) {
((Entity) ((PlayerEvent) event).entityPlayer)
.worldObj.playSoundAtEntity(
(Entity) ((PlayerEvent) event).entityPlayer,
"random.pop",
0.25F,
EntityUtils.randomPitch()
);
((Entity) ((PlayerEvent) event).entityPlayer)
.worldObj.removeEntity((Entity) event.item);
2021-04-04 16:10:26 +02:00
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean meshBottles(EntityPlayer player, ItemStack item) {
try {
EntityLivingBase entityOne = (EntityLivingBase
) EntityList.createEntityFromNBT(item.getTagCompound(), null);
if (!getHandleEntity((Entity) entityOne))
2021-04-04 16:10:26 +02:00
return false;
String nameOne = entityOne.getClass().getCanonicalName();
ItemStack[] equipOne = new ItemStack[5];
int color = 0;
boolean child = false;
boolean villager = false;
String owner = "";
String nameTagName = entityOne.getEntityData().getString("nameTagName");
if (entityOne instanceof EntitySheep)
color = ((EntitySheep) entityOne).getFleeceColor();
if (entityOne instanceof EntityAgeable
&& ((EntityAgeable) entityOne).isChild())
2021-04-04 16:10:26 +02:00
child = true;
if (entityOne instanceof EntityZombie && ((EntityZombie) entityOne).isChild())
2021-04-04 16:10:26 +02:00
child = true;
if (entityOne instanceof EntityZombie
&& ((EntityZombie) entityOne).isVillager())
2021-04-04 16:10:26 +02:00
villager = true;
if (entityOne instanceof EntityTameable) {
EntityLivingBase owningEntity = ((EntityTameable) entityOne).getOwner();
owner
= (owningEntity != null) ? owningEntity.getCommandSenderName() : null;
2021-04-04 16:10:26 +02:00
}
int i;
for (i = 0; i < 5; i++) {
equipOne[i] = entityOne.getEquipmentInSlot(i);
if (equipOne[i] != null
&& (!(entityOne instanceof EntityPigZombie) || i != 0
|| equipOne[i].getItem() != net.minecraft.init.Items.golden_sword
))
2021-04-04 16:10:26 +02:00
return false;
}
for (i = 0; i < player.inventory.mainInventory.length; i++) {
ItemStack invStack = player.inventory.mainInventory[i];
if (invStack != null && invStack.getItem() == DartItems.entitybottle
&& invStack.hasTagCompound()
&& invStack.stackSize < invStack.getMaxStackSize()) {
EntityLivingBase entityTwo = (EntityLivingBase
) EntityList.createEntityFromNBT(invStack.getTagCompound(), null);
2021-04-04 16:10:26 +02:00
if (nameOne.equals(entityTwo.getClass().getCanonicalName()))
if (!(entityTwo instanceof EntitySheep)
|| ((EntitySheep) entityTwo).getFleeceColor() == color)
if (!(entityTwo instanceof EntityAgeable)
|| ((EntityAgeable) entityTwo).isChild() == child)
if (!(entityTwo instanceof EntityZombie)
|| ((EntityZombie) entityTwo).isChild() == child)
if (!(entityTwo instanceof EntityZombie)
|| ((EntityZombie) entityTwo).isVillager()
== villager)
if (!(entityTwo instanceof EntityTameable)
|| ((EntityTameable) entityTwo)
.getOwner()
.equals(owner)) {
2021-04-04 16:10:26 +02:00
for (int j = 0; j < 5; j++) {
ItemStack checkStack
= entityTwo.getEquipmentInSlot(j);
if (checkStack != null
&& (!(entityOne
instanceof EntityPigZombie)
|| j != 0
|| checkStack.getItem()
!= net.minecraft.init.Items
.golden_sword))
2021-04-04 16:10:26 +02:00
return false;
}
invStack.stackSize++;
invStack.animationsToGo = 10;
return true;
}
}
}
} catch (Exception e) {}
return false;
}
private static boolean getHandleEntity(Entity entity) {
try {
String name = entity.getClass().getCanonicalName();
Class[] accepted
= { EntityCow.class, EntityChicken.class, EntitySheep.class,
EntityPig.class, EntityWolf.class, EntityZombie.class,
EntityEnderman.class, EntityGhast.class, EntityBlaze.class,
EntityPigZombie.class, EntitySkeleton.class };
2021-04-04 16:10:26 +02:00
for (Class check : accepted) {
if (name.equals(check.getCanonicalName()))
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}