Skip to content

Instantly share code, notes, and snippets.

Created May 31, 2014 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/cf4dd49d5de35af5a8a3 to your computer and use it in GitHub Desktop.
Save anonymous/cf4dd49d5de35af5a8a3 to your computer and use it in GitHub Desktop.
package wesserboysMod.eventHandlers;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.passive.EntitySquid;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import wesserboysMod.client.particles.EntityItemFX;
import wesserboysMod.client.particles.ParticleHelper;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class LivingUpdateEventHandler {
ArrayList<Class> blacklist = new ArrayList<Class>();
@SubscribeEvent
public void onLivingUpdateEvent(LivingUpdateEvent event) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Entity entity = event.entity;
if(entity instanceof EntityLiving){
try{
Class<?> c = entity.getClass();
if(!blacklist.contains(c)){
updateParticleTimer(entity);
if(readParticleTimer(entity) == 0){
Method m = c.getDeclaredMethod("getDropItem");
m.setAccessible(true);
Object o = m.invoke(entity);
if(o instanceof Item){
Item item = (Item)o;
if(!(item instanceof ItemBlock)){
spawnItemParticle(item, entity);
}else{
if(!blacklist.contains(entity.getClass())){
blacklist.add(entity.getClass());
}
}
}else{
if(!blacklist.contains(entity.getClass())){
blacklist.add(entity.getClass());
}
}
}
}
} catch (NoSuchMethodException x) {
x.printStackTrace();
if(!blacklist.contains(entity.getClass())){
blacklist.add(entity.getClass());
}
} catch (InvocationTargetException x) {
x.printStackTrace();
} catch (IllegalAccessException x) {
x.printStackTrace();
}
}
}
private void spawnItemParticle(Item item, Entity entity){
float particleX = ((int)Math.round(entity.posX)) + entity.worldObj.rand.nextFloat();
float particleY = ((int)Math.round(entity.posY)) + entity.worldObj.rand.nextFloat();
float particleZ = ((int)Math.round(entity.posZ)) + entity.worldObj.rand.nextFloat();
float motionX = -0.5F + entity.worldObj.rand.nextFloat();
float motionY = 0.5F + entity.worldObj.rand.nextFloat();
float motionZ = -0.5F + entity.worldObj.rand.nextFloat();
ParticleHelper.spawnParticle(new EntityItemFX(item, entity.worldObj, particleX, particleY, particleZ, motionX, motionY, motionZ));
}
private int readParticleTimer(Entity entity){
NBTTagCompound compound = entity.getEntityData();
if(!compound.hasKey("particleTimer")){
compound.setInteger("particleTimer", 50);
}
return compound.getInteger("particleTimer");
}
private void updateParticleTimer(Entity entity){
NBTTagCompound compound = entity.getEntityData();
int particleTimer = readParticleTimer(entity);
if(particleTimer > 0){
particleTimer--;
}else{
particleTimer = 100;
}
compound.setInteger("particleTimer", particleTimer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment