mirror of
https://github.com/GTNewHorizons/Gadomancy.git
synced 2026-11-28 23:39:39 +00:00
Transfer ASM to Mixins (#41)
This commit is contained in:
1 parent
eb35d92a6d
commit
0e8572ca64
27 files changed
+235
-777
No files matched your search
@@ -23,7 +23,6 @@ import makeo.gadomancy.common.data.config.ModConfig;
|
||||
import makeo.gadomancy.common.entities.golems.ItemAdditionalGolemPlacer;
|
||||
import makeo.gadomancy.common.entities.golems.nbt.ExtendedGolemProperties;
|
||||
import makeo.gadomancy.common.registration.RegisteredBlocks;
|
||||
import makeo.gadomancy.common.utils.GolemEnumHelper;
|
||||
import makeo.gadomancy.common.utils.NBTHelper;
|
||||
import makeo.gadomancy.common.utils.StringHelper;
|
||||
import thaumcraft.api.aspects.Aspect;
|
||||
@@ -39,20 +38,17 @@ public class DefaultApiHandler implements IApiHandler {
|
||||
|
||||
private static final Logger log = LogManager.getLogger("Gadomancy_API");
|
||||
|
||||
private static Map<String, AdditionalGolemType> additionalGolemTypes = new HashMap<String, AdditionalGolemType>();
|
||||
private static final Map<String, AdditionalGolemType> additionalGolemTypes = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public boolean registerAdditionalGolemType(String name, String modId, AdditionalGolemType newType) {
|
||||
String uniqueName = name.toUpperCase();
|
||||
if (!DefaultApiHandler.additionalGolemTypes.containsKey(uniqueName)) {
|
||||
GolemEnumHelper.addGolemType(uniqueName, newType);
|
||||
|
||||
ItemAdditionalGolemPlacer placerItem = new ItemAdditionalGolemPlacer(newType);
|
||||
GameRegistry
|
||||
.registerItem(placerItem, "item" + StringHelper.firstToUpper(name.toLowerCase()) + "GolemPlacer");
|
||||
newType.setModId(modId);
|
||||
newType.setPlacerItem(placerItem);
|
||||
|
||||
DefaultApiHandler.additionalGolemTypes.put(uniqueName, newType);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import net.minecraft.util.ResourceLocation;
|
||||
import makeo.gadomancy.api.golems.AdditionalGolemType;
|
||||
import makeo.gadomancy.common.Gadomancy;
|
||||
import makeo.gadomancy.common.utils.SimpleResourceLocation;
|
||||
import thaumcraft.common.entities.golems.EnumGolemType;
|
||||
|
||||
/**
|
||||
* This class is part of the Gadomancy Mod Gadomancy is Open Source and distributed under the GNU LESSER GENERAL PUBLIC
|
||||
@@ -18,7 +19,7 @@ import makeo.gadomancy.common.utils.SimpleResourceLocation;
|
||||
public class ObsidianGolemType extends AdditionalGolemType {
|
||||
|
||||
public ObsidianGolemType() {
|
||||
super(50, 18, 0.27F, true, 2, 32, 100, 6);
|
||||
super(EnumGolemType.valueOf("OBSIDIAN"), 50, 18, 0.27F, true, 2, 32, 100, 6);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
package makeo.gadomancy.common.entities.golems.types;
|
||||
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import makeo.gadomancy.api.golems.AdditionalGolemType;
|
||||
|
||||
/**
|
||||
* This class is part of the Gadomancy Mod Gadomancy is Open Source and distributed under the GNU LESSER GENERAL PUBLIC
|
||||
* LICENSE for more read the LICENSE file
|
||||
*
|
||||
* Created by makeo @ 29.07.2015 00:15
|
||||
*/
|
||||
public class RemovedGolemType extends AdditionalGolemType {
|
||||
|
||||
public RemovedGolemType() {
|
||||
super(0, 0, 0, false, 0, 0, 1000, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUnlocalizedName() {
|
||||
return "";
|
||||
}
|
||||
|
||||
private IIcon icon;
|
||||
|
||||
@Override
|
||||
public void registerIcons(IIconRegister ir) {
|
||||
this.icon = ir.registerIcon("");
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIcon getIcon(ItemStack stack, int pass) {
|
||||
return this.icon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getEntityTexture() {
|
||||
return new ResourceLocation("");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getInvSlotTexture() {
|
||||
return new ResourceLocation("");
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import net.minecraft.util.ResourceLocation;
|
||||
import makeo.gadomancy.api.golems.AdditionalGolemType;
|
||||
import makeo.gadomancy.common.Gadomancy;
|
||||
import makeo.gadomancy.common.utils.SimpleResourceLocation;
|
||||
import thaumcraft.common.entities.golems.EnumGolemType;
|
||||
|
||||
/**
|
||||
* This class is part of the Gadomancy Mod Gadomancy is Open Source and distributed under the GNU LESSER GENERAL PUBLIC
|
||||
@@ -18,7 +19,7 @@ import makeo.gadomancy.common.utils.SimpleResourceLocation;
|
||||
public class SilverwoodGolemType extends AdditionalGolemType {
|
||||
|
||||
public SilverwoodGolemType() {
|
||||
super(20, 9, 0.38f, false, 3, 8, 75, 1);
|
||||
super(EnumGolemType.valueOf("SILVERWOOD"), 20, 9, 0.38f, false, 3, 8, 75, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -11,9 +11,7 @@ import makeo.gadomancy.common.data.DataFamiliar;
|
||||
import makeo.gadomancy.common.data.SyncDataHolder;
|
||||
import makeo.gadomancy.common.network.PacketHandler;
|
||||
import makeo.gadomancy.common.network.packets.PacketSyncConfigs;
|
||||
import makeo.gadomancy.common.network.packets.PacketUpdateGolemTypeOrder;
|
||||
import makeo.gadomancy.common.network.packets.PacketUpdateOnlineState;
|
||||
import makeo.gadomancy.common.utils.GolemEnumHelper;
|
||||
import makeo.gadomancy.common.utils.world.TCMazeHandler;
|
||||
|
||||
/**
|
||||
@@ -28,7 +26,6 @@ public class EventHandlerNetwork {
|
||||
@SubscribeEvent
|
||||
public void on(PlayerEvent.PlayerLoggedInEvent e) {
|
||||
EntityPlayerMP p = (EntityPlayerMP) e.player;
|
||||
PacketHandler.INSTANCE.sendTo(new PacketUpdateGolemTypeOrder(GolemEnumHelper.getCurrentMapping()), p);
|
||||
PacketHandler.INSTANCE.sendTo(new PacketSyncConfigs(), p);
|
||||
PacketHandler.INSTANCE
|
||||
.sendTo(new PacketUpdateOnlineState(MinecraftServer.getServer().isServerInOnlineMode()), p);
|
||||
|
||||
@@ -1,23 +1,15 @@
|
||||
package makeo.gadomancy.common.events;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.enchantment.Enchantment;
|
||||
import net.minecraft.enchantment.EnchantmentHelper;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.client.event.DrawBlockHighlightEvent;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import makeo.gadomancy.common.registration.RegisteredEnchantments;
|
||||
import makeo.gadomancy.common.registration.RegisteredPotions;
|
||||
import makeo.gadomancy.common.utils.MiscUtils;
|
||||
import thaumcraft.api.aspects.Aspect;
|
||||
import thaumcraft.common.config.ConfigItems;
|
||||
|
||||
/**
|
||||
@@ -29,7 +21,6 @@ import thaumcraft.common.config.ConfigItems;
|
||||
public class EventHandlerRedirect {
|
||||
|
||||
private static final ItemStack ITEM_GOGGLES = new ItemStack(ConfigItems.itemGoggles);
|
||||
|
||||
private static boolean hasChanged;
|
||||
private static ItemStack oldItem;
|
||||
|
||||
@@ -58,94 +49,13 @@ public class EventHandlerRedirect {
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public static void preNodeRender(TileEntity tile) {
|
||||
public static void preNodeRender() {
|
||||
EventHandlerRedirect.addGoggles(Minecraft.getMinecraft().renderViewEntity);
|
||||
}
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
public static void postNodeRender(TileEntity tile) {
|
||||
public static void postNodeRender() {
|
||||
EventHandlerRedirect.removeGoggles(Minecraft.getMinecraft().renderViewEntity);
|
||||
}
|
||||
|
||||
public static void preBlockHighlight(DrawBlockHighlightEvent event) {
|
||||
EventHandlerRedirect.addGoggles(event.player);
|
||||
}
|
||||
|
||||
public static void postBlockHighlight(DrawBlockHighlightEvent event) {
|
||||
EventHandlerRedirect.removeGoggles(event.player);
|
||||
}
|
||||
|
||||
public static int getAdditionalVisDiscount(EntityPlayer player, Aspect aspect, int currentTotalDiscount) {
|
||||
if (player.isPotionActive(RegisteredPotions.VIS_DISCOUNT)) {
|
||||
currentTotalDiscount += (player.getActivePotionEffect(RegisteredPotions.VIS_DISCOUNT).getAmplifier() + 1)
|
||||
* 8;
|
||||
}
|
||||
return currentTotalDiscount;
|
||||
}
|
||||
|
||||
public static int getFortuneLevel(EntityLivingBase entity) {
|
||||
int fortuneLevel = EventHandlerRedirect
|
||||
.getRealEnchantmentLevel(Enchantment.fortune.effectId, entity.getHeldItem());
|
||||
if (entity.isPotionActive(RegisteredPotions.POTION_LUCK)) {
|
||||
int lvl = entity.getActivePotionEffect(RegisteredPotions.POTION_LUCK).getAmplifier() + 1; // Amplifier
|
||||
// 0-indexed
|
||||
fortuneLevel += lvl;
|
||||
}
|
||||
return fortuneLevel;
|
||||
}
|
||||
|
||||
public static int getLootingLevel(EntityLivingBase entity) {
|
||||
int lootingLevel = EventHandlerRedirect
|
||||
.getRealEnchantmentLevel(Enchantment.looting.effectId, entity.getHeldItem());
|
||||
if (entity.isPotionActive(RegisteredPotions.POTION_LUCK)) {
|
||||
int lvl = entity.getActivePotionEffect(RegisteredPotions.POTION_LUCK).getAmplifier() + 1; // Amplifier
|
||||
// 0-indexed
|
||||
lootingLevel += lvl;
|
||||
}
|
||||
return lootingLevel;
|
||||
}
|
||||
|
||||
public static int onGetEnchantmentLevel(int enchantmentId, ItemStack stack) {
|
||||
EntityPlayer possiblePlayer = null;
|
||||
if (stack != null) {
|
||||
MinecraftServer server = MinecraftServer.getServer();
|
||||
if (server != null && server.getConfigurationManager() != null) {
|
||||
for (EntityPlayer player : server.getConfigurationManager().playerEntityList) {
|
||||
if (player != null && player.getHeldItem() == stack) {
|
||||
possiblePlayer = player;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (possiblePlayer != null) {
|
||||
if (enchantmentId == Enchantment.fortune.effectId) {
|
||||
return EventHandlerRedirect.getFortuneLevel(possiblePlayer);
|
||||
} else if (enchantmentId == Enchantment.looting.effectId) {
|
||||
return EventHandlerRedirect.getLootingLevel(possiblePlayer);
|
||||
}
|
||||
}
|
||||
|
||||
return EventHandlerRedirect.getRealEnchantmentLevel(enchantmentId, stack);
|
||||
}
|
||||
|
||||
private static int getRealEnchantmentLevel(int enchantmentId, ItemStack stack) {
|
||||
if (stack == null) {
|
||||
return 0;
|
||||
} else {
|
||||
NBTTagList nbttaglist = stack.getEnchantmentTagList();
|
||||
if (nbttaglist == null) {
|
||||
return 0;
|
||||
} else {
|
||||
for (int j = 0; j < nbttaglist.tagCount(); ++j) {
|
||||
short id = nbttaglist.getCompoundTagAt(j).getShort("id");
|
||||
if (id == enchantmentId) {
|
||||
return nbttaglist.getCompoundTagAt(j).getShort("lvl");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,6 @@ import makeo.gadomancy.common.entities.EntityItemElement;
|
||||
import makeo.gadomancy.common.items.ItemElement;
|
||||
import makeo.gadomancy.common.registration.RegisteredBlocks;
|
||||
import makeo.gadomancy.common.registration.RegisteredItems;
|
||||
import makeo.gadomancy.common.utils.GolemEnumHelper;
|
||||
import makeo.gadomancy.common.utils.ItemUtils;
|
||||
import makeo.gadomancy.common.utils.MiscUtils;
|
||||
import makeo.gadomancy.common.utils.NBTHelper;
|
||||
@@ -161,10 +160,6 @@ public class EventHandlerWorld {
|
||||
public void on(WorldEvent.Load e) {
|
||||
if (!e.world.isRemote && e.world.provider.dimensionId == 0) {
|
||||
Gadomancy.loadModData();
|
||||
|
||||
GolemEnumHelper.validateSavedMapping();
|
||||
GolemEnumHelper.reorderEnum();
|
||||
|
||||
TCMazeHandler.init();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,10 @@
|
||||
package makeo.gadomancy.common.integration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import makeo.gadomancy.common.items.ItemFakeGolemPlacer;
|
||||
import codechicken.nei.api.API;
|
||||
import makeo.gadomancy.common.registration.RegisteredBlocks;
|
||||
import makeo.gadomancy.common.registration.RegisteredItems;
|
||||
import makeo.gadomancy.common.utils.Injector;
|
||||
|
||||
/**
|
||||
* This class is part of the Gadomancy Mod Gadomancy is Open Source and distributed under the GNU LESSER GENERAL PUBLIC
|
||||
@@ -25,45 +19,16 @@ public class IntegrationNEI extends IntegrationMod {
|
||||
return "NotEnoughItems";
|
||||
}
|
||||
|
||||
private Injector nei;
|
||||
|
||||
@Override
|
||||
protected void doInit() {
|
||||
this.nei = new Injector("codechicken.nei.api.API");
|
||||
|
||||
this.hideItem(new ItemStack(RegisteredItems.itemFakeModIcon));
|
||||
this.hideItem(new ItemStack(RegisteredBlocks.blockStickyJar, 1, Short.MAX_VALUE));
|
||||
this.hideItem(new ItemStack(RegisteredItems.itemPackage, 1, Short.MAX_VALUE));
|
||||
this.hideItem(new ItemStack(RegisteredItems.itemFakeLootbag, 1, Short.MAX_VALUE));
|
||||
this.hideItem(new ItemStack(RegisteredBlocks.blockExtendedNodeJar, 1, Short.MAX_VALUE));
|
||||
this.hideItem(new ItemStack(RegisteredItems.itemExtendedNodeJar, 1, Short.MAX_VALUE));
|
||||
this.hideItem(new ItemStack(RegisteredItems.itemFakeGolemPlacer, 1, Short.MAX_VALUE)); // This does... nothing?
|
||||
// ffs.
|
||||
this.hideItem(new ItemStack(RegisteredItems.itemTransformationFocus, 1, Short.MAX_VALUE));
|
||||
this.hideItem(new ItemStack(RegisteredItems.itemFamiliar_old, 1, Short.MAX_VALUE));
|
||||
}
|
||||
|
||||
public static void checkItems(ArrayList items) {
|
||||
Iterator iterator = items.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Object item = iterator.next();
|
||||
if (item != null && item instanceof ItemStack
|
||||
&& ((ItemStack) item).getItem() != null
|
||||
&& ((ItemStack) item).getItem() instanceof ItemFakeGolemPlacer) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void hideItem(ItemStack item) {
|
||||
this.nei.invokeMethod("hideItem", ItemStack.class, item);
|
||||
}
|
||||
|
||||
public void setItemListEntries(Block block, Iterable<ItemStack> items) {
|
||||
this.setItemListEntries(Item.getItemFromBlock(block), items);
|
||||
}
|
||||
|
||||
public void setItemListEntries(Item item, Iterable<ItemStack> items) {
|
||||
this.nei.invokeMethod("setItemListEntries", new Class[] { Item.class, Iterable.class }, item, items);
|
||||
API.hideItem(new ItemStack(RegisteredItems.itemFakeModIcon));
|
||||
API.hideItem(new ItemStack(RegisteredBlocks.blockStickyJar, 1, Short.MAX_VALUE));
|
||||
API.hideItem(new ItemStack(RegisteredItems.itemPackage, 1, Short.MAX_VALUE));
|
||||
API.hideItem(new ItemStack(RegisteredItems.itemFakeLootbag, 1, Short.MAX_VALUE));
|
||||
API.hideItem(new ItemStack(RegisteredBlocks.blockExtendedNodeJar, 1, Short.MAX_VALUE));
|
||||
API.hideItem(new ItemStack(RegisteredItems.itemExtendedNodeJar, 1, Short.MAX_VALUE));
|
||||
API.hideItem(new ItemStack(RegisteredItems.itemFakeGolemPlacer, 1, Short.MAX_VALUE));
|
||||
API.hideItem(new ItemStack(RegisteredItems.itemTransformationFocus, 1, Short.MAX_VALUE));
|
||||
API.hideItem(new ItemStack(RegisteredItems.itemFamiliar_old, 1, Short.MAX_VALUE));
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,6 @@ import makeo.gadomancy.common.network.packets.PacketSyncData;
|
||||
import makeo.gadomancy.common.network.packets.PacketTCNodeBolt;
|
||||
import makeo.gadomancy.common.network.packets.PacketTCNotificationText;
|
||||
import makeo.gadomancy.common.network.packets.PacketTCWispyLine;
|
||||
import makeo.gadomancy.common.network.packets.PacketUpdateGolemTypeOrder;
|
||||
import makeo.gadomancy.common.network.packets.PacketUpdateOnlineState;
|
||||
|
||||
/**
|
||||
@@ -30,8 +29,6 @@ public class PacketHandler {
|
||||
int id = 0;
|
||||
|
||||
// Random stuff
|
||||
PacketHandler.INSTANCE
|
||||
.registerMessage(PacketUpdateGolemTypeOrder.class, PacketUpdateGolemTypeOrder.class, id++, Side.CLIENT);
|
||||
PacketHandler.INSTANCE
|
||||
.registerMessage(PacketStartAnimation.class, PacketStartAnimation.class, id++, Side.CLIENT);
|
||||
PacketHandler.INSTANCE
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
package makeo.gadomancy.common.network.packets;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Map;
|
||||
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessage;
|
||||
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import makeo.gadomancy.common.utils.GolemEnumHelper;
|
||||
|
||||
/**
|
||||
* This class is part of the Gadomancy Mod Gadomancy is Open Source and distributed under the GNU LESSER GENERAL PUBLIC
|
||||
* LICENSE for more read the LICENSE file
|
||||
*
|
||||
* Created by makeo @ 30.07.2015 18:07
|
||||
*/
|
||||
public class PacketUpdateGolemTypeOrder implements IMessage, IMessageHandler<PacketUpdateGolemTypeOrder, IMessage> {
|
||||
|
||||
private Map<String, Integer> mapping;
|
||||
|
||||
public PacketUpdateGolemTypeOrder() {}
|
||||
|
||||
public PacketUpdateGolemTypeOrder(Map<String, Integer> mapping) {
|
||||
this.mapping = mapping;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
if (FMLCommonHandler.instance().getSide().isServer()) return;
|
||||
byte[] data = new byte[buf.readInt()];
|
||||
buf.readBytes(data);
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(data);
|
||||
|
||||
try {
|
||||
this.mapping = (Map<String, Integer>) new ValidatingObjectInputStream(in).readObject();
|
||||
in.close();
|
||||
} catch (Exception ignored) {} // IOException | ClassNotFoundException ignored
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try {
|
||||
new ObjectOutputStream(out).writeObject(this.mapping);
|
||||
} catch (IOException ignored) {}
|
||||
|
||||
byte[] data = out.toByteArray();
|
||||
|
||||
buf.writeInt(data.length);
|
||||
buf.writeBytes(data);
|
||||
|
||||
try {
|
||||
out.close();
|
||||
} catch (IOException ignored) {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(PacketUpdateGolemTypeOrder message, MessageContext ctx) {
|
||||
if (message.mapping != null) {
|
||||
GolemEnumHelper.reorderEnum(message.mapping);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,227 +0,0 @@
|
||||
package makeo.gadomancy.common.utils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraftforge.common.util.EnumHelper;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import makeo.gadomancy.api.GadomancyApi;
|
||||
import makeo.gadomancy.api.golems.AdditionalGolemType;
|
||||
import makeo.gadomancy.client.events.ResourceReloadListener;
|
||||
import makeo.gadomancy.common.Gadomancy;
|
||||
import makeo.gadomancy.common.entities.golems.types.RemovedGolemType;
|
||||
import thaumcraft.common.entities.golems.EnumGolemType;
|
||||
|
||||
/**
|
||||
* This class is part of the Gadomancy Mod Gadomancy is Open Source and distributed under the GNU LESSER GENERAL PUBLIC
|
||||
* LICENSE for more read the LICENSE file
|
||||
*
|
||||
* Created by makeo @ 29.07.2015 01:07
|
||||
*/
|
||||
public class GolemEnumHelper {
|
||||
|
||||
private static final RemovedGolemType REMOVED_GOLEM_TYPE = new RemovedGolemType();
|
||||
private static final Injector INJECTOR = new Injector(EnumGolemType.class);
|
||||
private static final Injector ENUM_INJECTOR = new Injector(Enum.class);
|
||||
private static final Injector CLASS_INJECTOR = new Injector(Class.class);
|
||||
|
||||
private static Field valuesField;
|
||||
|
||||
private static Field getValuesField() {
|
||||
if (GolemEnumHelper.valuesField == null) {
|
||||
for (Field field : EnumGolemType.class.getDeclaredFields()) {
|
||||
if (field.getType().equals(EnumGolemType[].class)) {
|
||||
field.setAccessible(true);
|
||||
GolemEnumHelper.valuesField = field;
|
||||
return field;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("Couldn't find the values$ field of EnumGolemType");
|
||||
}
|
||||
return GolemEnumHelper.valuesField;
|
||||
}
|
||||
|
||||
private static final Class<?>[] ENUM_PARAMS = { String.class, int.class, int.class, int.class, float.class,
|
||||
boolean.class, int.class, int.class, int.class, int.class };
|
||||
|
||||
private static Field ordinalField;
|
||||
|
||||
public static Field getOrdinalField() {
|
||||
if (GolemEnumHelper.ordinalField == null) {
|
||||
try {
|
||||
GolemEnumHelper.ordinalField = Enum.class.getDeclaredField("ordinal");
|
||||
} catch (NoSuchFieldException ignored) {}
|
||||
}
|
||||
return GolemEnumHelper.ordinalField;
|
||||
}
|
||||
|
||||
private static EnumGolemType createEnum(String name, int ordinal, AdditionalGolemType type) {
|
||||
resetEnumCache();
|
||||
return INJECTOR.invokeUnsafeConstructor(
|
||||
ENUM_PARAMS,
|
||||
new Object[] { name, ordinal, type.maxHealth, type.armor, type.movementSpeed, type.fireResist,
|
||||
type.upgradeAmount, type.carryLimit, type.regenDelay, type.strength });
|
||||
}
|
||||
|
||||
private static void resetEnumCache() {
|
||||
CLASS_INJECTOR.setObject(EnumGolemType.class);
|
||||
try {
|
||||
Field enumConstants = EnumGolemType.class.getClass().getDeclaredField("enumConstants");
|
||||
CLASS_INJECTOR.setField(enumConstants, null);
|
||||
} catch (Exception e) {
|
||||
// no-op, worst case there is an outdated cache on new JVM versions
|
||||
}
|
||||
try {
|
||||
Field enumConstantDirectory = EnumGolemType.class.getClass().getDeclaredField("enumConstantDirectory");
|
||||
CLASS_INJECTOR.setField(enumConstantDirectory, null);
|
||||
} catch (Exception e) {
|
||||
// no-op, worst case there is an outdated cache on new JVM versions
|
||||
}
|
||||
}
|
||||
|
||||
private static void addEnum(int ordinal, EnumGolemType type) {
|
||||
EnumGolemType[] values = GolemEnumHelper.resizeValues(ordinal + 1);
|
||||
values[ordinal] = type;
|
||||
resetEnumCache();
|
||||
}
|
||||
|
||||
private static EnumGolemType addEnum(String name, int ordinal, AdditionalGolemType type) {
|
||||
EnumGolemType enumEntry = GolemEnumHelper.createEnum(name, ordinal, type);
|
||||
GolemEnumHelper.addEnum(ordinal, enumEntry);
|
||||
type.setEnumEntry(enumEntry);
|
||||
resetEnumCache();
|
||||
return enumEntry;
|
||||
}
|
||||
|
||||
private static EnumGolemType[] resizeValues(int size) {
|
||||
EnumGolemType[] values = GolemEnumHelper.INJECTOR.getField(GolemEnumHelper.getValuesField());
|
||||
if (size > values.length) {
|
||||
EnumGolemType[] newValues = new EnumGolemType[size];
|
||||
System.arraycopy(values, 0, newValues, 0, values.length);
|
||||
|
||||
for (int i = values.length; i < newValues.length; i++) {
|
||||
newValues[i] = GolemEnumHelper.createEnum("REMOVED", i, GolemEnumHelper.REMOVED_GOLEM_TYPE);
|
||||
}
|
||||
GolemEnumHelper.setValues(newValues);
|
||||
resetEnumCache();
|
||||
return newValues;
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
private static void setValues(EnumGolemType[] values) {
|
||||
try {
|
||||
EnumHelper.setFailsafeFieldValue(GolemEnumHelper.getValuesField(), null, values);
|
||||
resetEnumCache();
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
|
||||
public static EnumGolemType addGolemType(String name, AdditionalGolemType type) {
|
||||
return GolemEnumHelper.addEnum(name, GolemEnumHelper.getOrdinal(name), type);
|
||||
}
|
||||
|
||||
private static int getOrdinal(String name) {
|
||||
Map<String, Integer> map = GolemEnumHelper.getCurrentMapping();
|
||||
|
||||
if (map.containsKey(name)) {
|
||||
return map.get(name);
|
||||
}
|
||||
|
||||
int returnVal = -1;
|
||||
int i = GolemEnumHelper.calcDefaultGolemCount();
|
||||
do {
|
||||
boolean contains = false;
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
if (entry.getValue() == i) {
|
||||
contains = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!contains) {
|
||||
returnVal = i;
|
||||
}
|
||||
i++;
|
||||
} while (returnVal < 0);
|
||||
|
||||
map.put(name, returnVal);
|
||||
GolemEnumHelper.saveCurrentMapping(map);
|
||||
|
||||
return returnVal;
|
||||
}
|
||||
|
||||
public static void reorderEnum() {
|
||||
GolemEnumHelper.reorderEnum(GolemEnumHelper.getCurrentMapping());
|
||||
}
|
||||
|
||||
public static void reorderEnum(Map<String, Integer> mapping) {
|
||||
EnumGolemType[] oldValues = EnumGolemType.values();
|
||||
GolemEnumHelper.resetEnum();
|
||||
for (Map.Entry<String, Integer> entry : mapping.entrySet()) {
|
||||
for (EnumGolemType type : oldValues) {
|
||||
if (type.name().equals(entry.getKey())) {
|
||||
GolemEnumHelper.ENUM_INJECTOR.setObject(type);
|
||||
GolemEnumHelper.ENUM_INJECTOR.setFieldInt(GolemEnumHelper.getOrdinalField(), entry.getValue());
|
||||
GolemEnumHelper.addEnum(entry.getValue(), type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new Injector(EnumGolemType.class).setField("codeToTypeMapping", null);
|
||||
resetEnumCache();
|
||||
if (Gadomancy.proxy.getSide() == Side.CLIENT) {
|
||||
ResourceReloadListener.getInstance().reloadGolemResources();
|
||||
}
|
||||
}
|
||||
|
||||
private static void resetEnum() {
|
||||
EnumGolemType[] newValues = Arrays
|
||||
.copyOfRange(EnumGolemType.values(), 0, GolemEnumHelper.calcDefaultGolemCount());
|
||||
GolemEnumHelper.setValues(newValues);
|
||||
resetEnumCache();
|
||||
}
|
||||
|
||||
private static int calcDefaultGolemCount() {
|
||||
EnumGolemType[] values = EnumGolemType.values();
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (GadomancyApi.isAdditionalGolemType(values[i])) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return values.length;
|
||||
}
|
||||
|
||||
public static void validateSavedMapping() {
|
||||
if (GolemEnumHelper.hasCurrentMapping()) {
|
||||
Map<String, Integer> mapping = GolemEnumHelper.getCurrentMapping();
|
||||
for (Map.Entry<String, Integer> entry : GolemEnumHelper.defaultMapping.entrySet()) {
|
||||
if (!mapping.containsKey(entry.getKey())) {
|
||||
mapping.put(entry.getKey(), GolemEnumHelper.getOrdinal(entry.getKey()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, Integer> defaultMapping = new HashMap<>();
|
||||
|
||||
public static Map<String, Integer> getCurrentMapping() {
|
||||
if (GolemEnumHelper.hasCurrentMapping()) {
|
||||
return Gadomancy.getModData().get("GolemTypeMapping", new HashMap<>());
|
||||
} else if (Gadomancy.getModData() != null) {
|
||||
Gadomancy.getModData().set("GolemTypeMapping", GolemEnumHelper.defaultMapping);
|
||||
}
|
||||
return GolemEnumHelper.defaultMapping;
|
||||
}
|
||||
|
||||
public static boolean hasCurrentMapping() {
|
||||
return Gadomancy.getModData() != null && Gadomancy.getModData().contains("GolemTypeMapping");
|
||||
}
|
||||
|
||||
private static void saveCurrentMapping(Map<String, Integer> map) {
|
||||
if (Gadomancy.getModData() != null) {
|
||||
Gadomancy.getModData().set("GolemTypeMapping", map);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,6 @@ import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
|
||||
import cpw.mods.fml.relauncher.ReflectionHelper;
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
@@ -97,17 +95,6 @@ public class Injector {
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> T invokeUnsafeConstructor(Class<?>[] paramTypes, Object... params) {
|
||||
try {
|
||||
final Method constructor = this.clazz.getMethod("gadomancyRawCreate", paramTypes);
|
||||
Object created = constructor.invoke(null, params);
|
||||
return (T) created;
|
||||
} catch (Throwable e) {
|
||||
Throwables.propagate(e);
|
||||
}
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
public <T> T invokeMethod(String name, Object... params) {
|
||||
return this.invokeMethod(name, this.extractClasses(params), params);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user