mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-18 19:39:06 +00:00
Compare commits
29
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83c54ec6c6 | ||
|
|
688c2c1f36 | ||
|
|
22280ea320 | ||
|
|
6df14438c5 | ||
|
|
0b1019bdf0 | ||
|
|
a811d20299 | ||
|
|
4e1835ed12 | ||
|
|
1db5809cde | ||
|
|
fa4411ad2e | ||
|
|
80f7455e7f | ||
|
|
b9cac67ec7 | ||
|
|
41a6cc0711 | ||
|
|
319ed409f1 | ||
|
|
ad153f1c0a | ||
|
|
e340423b28 | ||
|
|
7b0feb16f9 | ||
|
|
e0b34e8b9a | ||
|
|
c533f6f1b5 | ||
|
|
140977be2e | ||
|
|
8a13af1969 | ||
|
|
d6d50a968d | ||
|
|
43459d4b41 | ||
|
|
2a1aff073b | ||
|
|
73eea57d11 | ||
|
|
9b84ea7804 | ||
|
|
de42a43b2c | ||
|
|
b89734f3e6 | ||
|
|
7445d96531 | ||
|
|
dc997626c1 |
@@ -4,6 +4,7 @@ import java.lang.reflect.Modifier;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
@@ -17,7 +18,6 @@ import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.tileentity.TileEntityMobSpawner;
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
import net.minecraft.util.IIcon;
|
||||
@@ -29,26 +29,18 @@ import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
public class ItemMobSpawner extends ItemBlock {
|
||||
|
||||
private static final Map<Integer, EntityLiving> entityHashMap = new ConcurrentHashMap<>();
|
||||
private static final Map<Integer, EntityLiving> entityHashMap = Collections.synchronizedMap(new WeakHashMap<>());
|
||||
private static final Map<Integer, String> IDtoNameMap = new ConcurrentHashMap<>();
|
||||
private static final Map<Class<? extends Entity>, String> ENTITY_CLASS_TO_NAME_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
public static int idPig = 90;
|
||||
private static volatile boolean loaded = false;
|
||||
private static boolean loaded = false;
|
||||
|
||||
// For asm
|
||||
public static int placedX;
|
||||
public static int placedY;
|
||||
public static int placedZ;
|
||||
private static final ThreadLocal<PlacementContext> placementContext = ThreadLocal
|
||||
.withInitial(PlacementContext::new);
|
||||
|
||||
public static void setLastPlacedPosition(int x, int y, int z) {
|
||||
placedX = x;
|
||||
placedY = y;
|
||||
placedZ = z;
|
||||
}
|
||||
private static class PlacementContext {
|
||||
|
||||
public static int[] getLastPlacedPosition() {
|
||||
return new int[] { placedX, placedY, placedZ };
|
||||
int x, y, z;
|
||||
}
|
||||
|
||||
public ItemMobSpawner() {
|
||||
@@ -62,25 +54,17 @@ public class ItemMobSpawner extends ItemBlock {
|
||||
return Blocks.mob_spawner.getBlockTextureFromSide(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onItemUse(ItemStack itemstack, EntityPlayer entityplayer, World world, int x, int y, int z, int side,
|
||||
float hitX, float hitY, float hitZ) {
|
||||
|
||||
placedX = x;
|
||||
placedY = y;
|
||||
placedZ = z;
|
||||
|
||||
boolean placed = super.onItemUse(itemstack, entityplayer, world, x, y, z, side, hitX, hitY, hitZ);
|
||||
|
||||
if (placed && !world.isRemote) {
|
||||
TileEntity tileEntity = world.getTileEntity(x, y, z);
|
||||
if (tileEntity instanceof TileEntityMobSpawner) {
|
||||
TileEntityMobSpawner spawner = (TileEntityMobSpawner) tileEntity;
|
||||
if (placed && world.isRemote) {
|
||||
TileEntityMobSpawner spawner = (TileEntityMobSpawner) world.getTileEntity(x, y, z);
|
||||
if (spawner != null) {
|
||||
String mobType = getMobTypeFromItemStack(itemstack);
|
||||
if (mobType != null) {
|
||||
spawner.func_145881_a().setEntityName(mobType);
|
||||
world.markBlockForUpdate(x, y, z);
|
||||
NEICPH.sendMobSpawnerID(x, y, z, mobType);
|
||||
spawner.func_145881_a().setEntityName(mobType);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -123,7 +107,7 @@ public class ItemMobSpawner extends ItemBlock {
|
||||
|
||||
loadSpawners();
|
||||
|
||||
Class<?> entityClass = EntityList.IDtoClassMapping.get(id);
|
||||
Class<?> entityClass = (Class<?>) EntityList.IDtoClassMapping.get(id);
|
||||
World world = NEIClientUtils.mc() != null ? NEIClientUtils.mc().theWorld : null;
|
||||
|
||||
if (isInvalidEntityClass(entityClass)) {
|
||||
@@ -140,33 +124,25 @@ public class ItemMobSpawner extends ItemBlock {
|
||||
|
||||
private static boolean isInvalidEntityClass(Class<?> entityClass) {
|
||||
if (entityClass == null) {
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
int modifiers = entityClass.getModifiers();
|
||||
if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers)) {
|
||||
NEIClientConfig.logger.warn("Skipping abstract/interface entity class: {}", entityClass.getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!EntityLiving.class.isAssignableFrom(entityClass)) {
|
||||
NEIClientConfig.logger.warn("Skipping non-EntityLiving class: {}", entityClass.getName());
|
||||
NEIClientConfig.logger.warn("Skipping abstract entity class: " + entityClass.getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static EntityLiving createEntityInstance(Class<?> entityClass, World world, int id) {
|
||||
if (entityClass == null || world == null) {
|
||||
NEIClientConfig.logger.debug("World or entity class is null, using fallback for ID: {}", id);
|
||||
return getFallbackEntity(id);
|
||||
}
|
||||
|
||||
try {
|
||||
Class<? extends EntityLiving> livingClass = (Class<? extends EntityLiving>) entityClass;
|
||||
return livingClass.getConstructor(World.class).newInstance(world);
|
||||
return (EntityLiving) entityClass.getConstructor(World.class).newInstance(world);
|
||||
} catch (Throwable t) {
|
||||
logEntityCreationError(entityClass, id, t);
|
||||
return getFallbackEntity(id);
|
||||
@@ -174,17 +150,11 @@ public class ItemMobSpawner extends ItemBlock {
|
||||
}
|
||||
|
||||
private static void logEntityCreationError(Class<?> entityClass, int id, Throwable t) {
|
||||
String entityName = IDtoNameMap.getOrDefault(id, "unknown");
|
||||
if (entityClass == null) {
|
||||
NEIClientConfig.logger
|
||||
.error("Failed to create entity instance: Class is null for ID {}, Name {}", id, entityName, t);
|
||||
.error("Null class for entity (ID: {}, Name: {})", id, IDtoNameMap.getOrDefault(id, "unknown"));
|
||||
} else {
|
||||
NEIClientConfig.logger.error(
|
||||
"Failed to create instance of entity: Class {}, ID {}, Name {}",
|
||||
entityClass.getName(),
|
||||
id,
|
||||
entityName,
|
||||
t);
|
||||
NEIClientConfig.logger.error("Error creating instance of entity: " + entityClass.getName(), t);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,62 +175,60 @@ public class ItemMobSpawner extends ItemBlock {
|
||||
try {
|
||||
pig = new EntityPig(world);
|
||||
entityHashMap.put(idPig, pig);
|
||||
NEIClientConfig.logger.debug("Created fallback pig entity for ID: {}", originalId);
|
||||
} catch (Exception e) {
|
||||
NEIClientConfig.logger
|
||||
.error("Failed to create fallback pig entity for original ID: {}", originalId, e);
|
||||
NEIClientConfig.logger.error("Failed to create fallback pig entity", e);
|
||||
}
|
||||
} else {
|
||||
NEIClientConfig.logger.warn("World is null, cannot create fallback entity for ID: {}", originalId);
|
||||
}
|
||||
}
|
||||
|
||||
if (pig == null && originalId != idPig) {
|
||||
NEIClientConfig.logger
|
||||
.warn("Using null fallback for entity ID: {}, original fallback ID: {}", originalId, idPig);
|
||||
}
|
||||
|
||||
return pig;
|
||||
}
|
||||
|
||||
public static void clearEntityReferences() {
|
||||
entityHashMap.clear();
|
||||
NEIClientConfig.logger.debug("Cleared all entity references from cache");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void loadSpawners() {
|
||||
if (loaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
synchronized (ItemMobSpawner.class) {
|
||||
try {
|
||||
for (Map.Entry<Class<? extends Entity>, String> entry : ((Map<Class<? extends Entity>, String>) EntityList.classToStringMapping)
|
||||
.entrySet()) {
|
||||
if (loaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
Class<? extends Entity> clazz = entry.getKey();
|
||||
String name = entry.getValue();
|
||||
try {
|
||||
for (Object entry : EntityList.classToStringMapping.entrySet()) {
|
||||
Map.Entry<Class<? extends Entity>, String> mapEntry = (Map.Entry<Class<? extends Entity>, String>) entry;
|
||||
|
||||
Class<? extends Entity> clazz = mapEntry.getKey();
|
||||
String name = mapEntry.getValue();
|
||||
|
||||
if (shouldRegisterEntity(clazz, name)) {
|
||||
Integer id = (Integer) EntityList.classToIDMapping.get(clazz);
|
||||
if (id != null) {
|
||||
IDtoNameMap.put(id, name);
|
||||
ENTITY_CLASS_TO_NAME_CACHE.put(clazz, name);
|
||||
|
||||
if ("Pig".equals(name)) {
|
||||
idPig = id;
|
||||
NEIClientConfig.logger.debug("Set pig entity ID to: {}", id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
loaded = true;
|
||||
NEIClientConfig.logger.info("Loaded {} spawner entity mappings", IDtoNameMap.size());
|
||||
|
||||
} catch (Exception e) {
|
||||
NEIClientConfig.logger.error("Error loading spawners from entity list", e);
|
||||
NEIClientConfig.logger.error("Error loading spawners", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean shouldRegisterEntity(Class<? extends Entity> clazz, String name) {
|
||||
if (clazz == null || name == null || name.trim().isEmpty()) {
|
||||
if (clazz == null || name == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -268,7 +236,7 @@ public class ItemMobSpawner extends ItemBlock {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ("EnderDragon".equals(name) || "Giant".equals(name)) {
|
||||
if ("EnderDragon".equals(name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -280,10 +248,8 @@ public class ItemMobSpawner extends ItemBlock {
|
||||
if (!NEIClientConfig.hasSMPCounterPart()) {
|
||||
list.add(new ItemStack(item));
|
||||
} else {
|
||||
loadSpawners();
|
||||
for (int id : IDtoNameMap.keySet()) {
|
||||
ItemStack stack = new ItemStack(item, 1, id);
|
||||
list.add(stack);
|
||||
list.add(new ItemStack(item, 1, id));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -297,20 +263,4 @@ public class ItemMobSpawner extends ItemBlock {
|
||||
loadSpawners();
|
||||
return IDtoNameMap.containsKey(meta) || meta == 0;
|
||||
}
|
||||
|
||||
public static Map<Integer, String> getRegisteredSpawnerEntities() {
|
||||
loadSpawners();
|
||||
return Collections.unmodifiableMap(IDtoNameMap);
|
||||
}
|
||||
|
||||
public static boolean isEntityValidForSpawner(Class<? extends Entity> entityClass) {
|
||||
if (entityClass == null) return false;
|
||||
|
||||
String name = ENTITY_CLASS_TO_NAME_CACHE.get(entityClass);
|
||||
if (name == null) {
|
||||
name = (String) EntityList.classToStringMapping.get(entityClass);
|
||||
}
|
||||
|
||||
return name != null && shouldRegisterEntity(entityClass, name);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user