2014-04-29 10:54:20 +10:00
|
|
|
package codechicken.nei;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.creativetab.CreativeTabs;
|
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
|
import net.minecraft.entity.EntityList;
|
|
|
|
|
import net.minecraft.entity.EntityLiving;
|
|
|
|
|
import net.minecraft.entity.monster.IMob;
|
2014-08-10 12:02:35 +10:00
|
|
|
import net.minecraft.entity.passive.EntityPig;
|
2014-04-29 10:54:20 +10:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
|
import net.minecraft.init.Blocks;
|
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
|
import net.minecraft.item.ItemBlock;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.tileentity.TileEntityMobSpawner;
|
|
|
|
|
import net.minecraft.util.IIcon;
|
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
import net.minecraftforge.client.MinecraftForgeClient;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class ItemMobSpawner extends ItemBlock
|
|
|
|
|
{
|
|
|
|
|
private static Map<Integer, EntityLiving> entityHashMap;
|
|
|
|
|
private static Map<Integer, String> IDtoNameMap;
|
|
|
|
|
public static int idPig;
|
|
|
|
|
private static boolean loaded;
|
|
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
public ItemMobSpawner() {
|
2014-04-29 10:54:20 +10:00
|
|
|
super(Blocks.mob_spawner);
|
|
|
|
|
|
|
|
|
|
hasSubtypes = true;
|
|
|
|
|
MinecraftForgeClient.registerItemRenderer(this, new SpawnerRenderer());
|
|
|
|
|
|
2019-12-30 20:19:16 -08:00
|
|
|
entityHashMap = new HashMap<>();
|
|
|
|
|
IDtoNameMap = new HashMap<>();
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* These are ASM translated from BlockMobSpawner
|
|
|
|
|
*/
|
|
|
|
|
public static int placedX;
|
|
|
|
|
public static int placedY;
|
|
|
|
|
public static int placedZ;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public IIcon getIconFromDamage(int par1) {
|
|
|
|
|
return Blocks.mob_spawner.getBlockTextureFromSide(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean onItemUse(ItemStack itemstack, EntityPlayer entityplayer, World world, int x, int y, int z, int par7, float par8, float par9, float par10) {
|
|
|
|
|
if (super.onItemUse(itemstack, entityplayer, world, x, y, z, par7, par8, par9, par10) && world.isRemote) {
|
|
|
|
|
TileEntityMobSpawner tileentitymobspawner = (TileEntityMobSpawner) world.getTileEntity(placedX, placedY, placedZ);
|
|
|
|
|
if (tileentitymobspawner != null) {
|
|
|
|
|
setDefaultTag(itemstack);
|
|
|
|
|
String mobtype = IDtoNameMap.get(itemstack.getItemDamage());
|
|
|
|
|
if (mobtype != null) {
|
|
|
|
|
NEICPH.sendMobSpawnerID(placedX, placedY, placedZ, mobtype);
|
|
|
|
|
tileentitymobspawner.func_145881_a().setEntityName(mobtype);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addInformation(ItemStack itemstack, EntityPlayer par2EntityPlayer, List list, boolean par4) {
|
|
|
|
|
setDefaultTag(itemstack);
|
|
|
|
|
int meta = itemstack.getItemDamage();
|
|
|
|
|
if (meta == 0) {
|
|
|
|
|
meta = idPig;
|
|
|
|
|
}
|
|
|
|
|
Entity e = getEntity(meta);
|
|
|
|
|
list.add("\u00A7" + (e instanceof IMob ? "4" : "3") + IDtoNameMap.get(meta));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static EntityLiving getEntity(int ID) {
|
2014-06-29 20:31:37 +10:00
|
|
|
EntityLiving e = entityHashMap.get(ID);
|
|
|
|
|
if (e == null) {
|
2014-04-29 10:54:20 +10:00
|
|
|
World world = NEIClientUtils.mc().theWorld;
|
|
|
|
|
loadSpawners(world);
|
2014-06-29 20:31:37 +10:00
|
|
|
Class<?> clazz = (Class<?>) EntityList.IDtoClassMapping.get(ID);
|
2014-04-29 10:54:20 +10:00
|
|
|
try {
|
2014-06-29 20:31:37 +10:00
|
|
|
e = (EntityLiving) clazz.getConstructor(new Class[]{World.class}).newInstance(world);
|
2014-04-29 10:54:20 +10:00
|
|
|
} catch (Throwable t) {
|
2014-08-10 12:02:35 +10:00
|
|
|
if(clazz == null)
|
|
|
|
|
NEIClientConfig.logger.error("Null class for entity ("+ID+", "+IDtoNameMap.get(ID));
|
|
|
|
|
else
|
|
|
|
|
NEIClientConfig.logger.error("Error creating instance of entity: " + clazz.getName(), t);
|
2014-06-29 20:31:37 +10:00
|
|
|
e = getEntity(idPig);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
entityHashMap.put(ID, e);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
return e;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setDefaultTag(ItemStack itemstack) {
|
|
|
|
|
if (!IDtoNameMap.containsKey(itemstack.getItemDamage()))
|
|
|
|
|
itemstack.setItemDamage(idPig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void loadSpawners(World world) {
|
|
|
|
|
if (loaded) return;
|
|
|
|
|
loaded = true;
|
2014-06-29 20:31:37 +10:00
|
|
|
HashMap<Class<Entity>, String> classToStringMapping = (HashMap<Class<Entity>, String>) EntityList.classToStringMapping;
|
|
|
|
|
HashMap<Class<Entity>, Integer> classToIDMapping = (HashMap<Class<Entity>, Integer>) EntityList.classToIDMapping;
|
|
|
|
|
for (Class<Entity> eclass : classToStringMapping.keySet()) {
|
|
|
|
|
if (!EntityLiving.class.isAssignableFrom(eclass))
|
|
|
|
|
continue;
|
|
|
|
|
try {
|
|
|
|
|
EntityLiving entityliving = (EntityLiving) eclass.getConstructor(new Class[]{World.class}).newInstance(world);
|
|
|
|
|
entityliving.isChild();
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
int id = classToIDMapping.get(eclass);
|
|
|
|
|
String name = classToStringMapping.get(eclass);
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
if (name.equals("EnderDragon"))
|
|
|
|
|
continue;
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
IDtoNameMap.put(id, name);
|
2014-04-29 10:54:20 +10:00
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
if (name.equals("Pig"))
|
|
|
|
|
idPig = id;
|
|
|
|
|
} catch (Throwable ignored) {
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-10 12:02:35 +10:00
|
|
|
|
2019-12-24 13:37:41 -08:00
|
|
|
IDtoNameMap.entrySet().removeIf(e -> getEntity(e.getKey()).getClass() == EntityPig.class && !e.getValue().equals("Pig"));
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void getSubItems(Item item, CreativeTabs tab, List list) {
|
2014-12-30 21:05:22 +10:00
|
|
|
if(!NEIClientConfig.hasSMPCounterPart())
|
|
|
|
|
list.add(new ItemStack(item));
|
|
|
|
|
else
|
|
|
|
|
for(int i : IDtoNameMap.keySet())
|
|
|
|
|
list.add(new ItemStack(item, 1, i));
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
}
|