2014-04-29 10:54:20 +10:00
|
|
|
package codechicken.nei;
|
|
|
|
|
|
2023-09-25 00:35:24 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
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;
|
2025-06-15 12:09:55 +02:00
|
|
|
import net.minecraft.util.EnumChatFormatting;
|
2014-04-29 10:54:20 +10:00
|
|
|
import net.minecraft.util.IIcon;
|
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
import net.minecraftforge.client.MinecraftForgeClient;
|
|
|
|
|
|
2025-06-15 12:09:55 +02:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public class ItemMobSpawner extends ItemBlock {
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2023-07-16 14:29:31 +02:00
|
|
|
private static final Map<Integer, EntityLiving> entityHashMap = new HashMap<>();
|
|
|
|
|
private static final Map<Integer, String> IDtoNameMap = new HashMap<>();
|
2014-04-29 10:54:20 +10:00
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public boolean onItemUse(ItemStack itemstack, EntityPlayer entityplayer, World world, int x, int y, int z, int par7,
|
|
|
|
|
float par8, float par9, float par10) {
|
2014-04-29 10:54:20 +10:00
|
|
|
if (super.onItemUse(itemstack, entityplayer, world, x, y, z, par7, par8, par9, par10) && world.isRemote) {
|
2022-06-24 12:32:13 +01:00
|
|
|
TileEntityMobSpawner tileentitymobspawner = (TileEntityMobSpawner) world
|
|
|
|
|
.getTileEntity(placedX, placedY, placedZ);
|
2014-04-29 10:54:20 +10:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-15 12:09:55 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
2014-04-29 10:54:20 +10:00
|
|
|
@Override
|
2024-08-29 20:07:14 +02:00
|
|
|
public void addInformation(ItemStack itemstack, EntityPlayer par2EntityPlayer, List<String> list, boolean par4) {
|
2014-04-29 10:54:20 +10:00
|
|
|
setDefaultTag(itemstack);
|
|
|
|
|
int meta = itemstack.getItemDamage();
|
|
|
|
|
if (meta == 0) {
|
|
|
|
|
meta = idPig;
|
|
|
|
|
}
|
|
|
|
|
Entity e = getEntity(meta);
|
2025-06-15 12:09:55 +02:00
|
|
|
list.add(
|
|
|
|
|
(e instanceof IMob ? EnumChatFormatting.DARK_RED : EnumChatFormatting.DARK_AQUA)
|
|
|
|
|
+ IDtoNameMap.get(meta));
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static EntityLiving getEntity(int ID) {
|
2014-06-29 20:31:37 +10:00
|
|
|
EntityLiving e = entityHashMap.get(ID);
|
|
|
|
|
if (e == null) {
|
2025-06-15 12:09:55 +02:00
|
|
|
loadSpawners();
|
|
|
|
|
Class<?> clazz = EntityList.IDtoClassMapping.get(ID);
|
2014-04-29 10:54:20 +10:00
|
|
|
World world = NEIClientUtils.mc().theWorld;
|
|
|
|
|
try {
|
2022-06-24 12:32:13 +01:00
|
|
|
e = (EntityLiving) clazz.getConstructor(new Class[] { World.class }).newInstance(world);
|
2014-04-29 10:54:20 +10:00
|
|
|
} catch (Throwable t) {
|
2022-06-24 12:32:13 +01: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
|
|
|
}
|
|
|
|
|
|
2025-06-15 12:09:55 +02:00
|
|
|
public static void clearEntityReferences() {
|
|
|
|
|
entityHashMap.clear();
|
2022-06-06 11:05:02 -04:00
|
|
|
}
|
|
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
private void setDefaultTag(ItemStack itemstack) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!IDtoNameMap.containsKey(itemstack.getItemDamage())) itemstack.setItemDamage(idPig);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
2025-06-15 12:09:55 +02:00
|
|
|
public static void loadSpawners() {
|
2014-04-29 10:54:20 +10:00
|
|
|
if (loaded) return;
|
|
|
|
|
loaded = true;
|
2025-06-15 12:09:55 +02:00
|
|
|
for (Map.Entry<Class<? extends Entity>, String> entry : EntityList.classToStringMapping.entrySet()) {
|
|
|
|
|
final Class<? extends Entity> clazz = entry.getKey();
|
|
|
|
|
if (EntityLiving.class.isAssignableFrom(clazz)) {
|
|
|
|
|
Integer id = (Integer) EntityList.classToIDMapping.get(clazz);
|
|
|
|
|
if (id == null) continue;
|
|
|
|
|
String name = entry.getValue();
|
|
|
|
|
if (name == null) continue;
|
2022-06-24 12:32:13 +01:00
|
|
|
if (name.equals("EnderDragon")) continue;
|
|
|
|
|
if (name.equals("Pig")) idPig = id;
|
2025-06-15 12:09:55 +02:00
|
|
|
if (clazz != EntityPig.class || name.equals("Pig")) {
|
|
|
|
|
IDtoNameMap.put(id, name);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2024-08-29 20:07:14 +02:00
|
|
|
public void getSubItems(Item item, CreativeTabs tab, List<ItemStack> list) {
|
2022-06-24 12:32:13 +01: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
|
|
|
}
|
|
|
|
|
}
|