diff --git a/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java b/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java index 64da8b7..b27a3b4 100644 --- a/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java +++ b/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java @@ -36,10 +36,11 @@ public class JustEnoughCalculation { public static Logger logger = LogManager.getLogger(Reference.MODID); @Mod.EventHandler - public static void initPre(FMLPreInitializationEvent event) { + public static void preInit(FMLPreInitializationEvent event) { JecaConfig.preInit(event); GameRegistry.registerItem(JecaItem.INSTANCE, JecaItem.NAME); network = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MODID); + proxy.preInit(); } @Mod.EventHandler diff --git a/src/main/java/me/towdium/jecalculation/core/ItemStackHelper.java b/src/main/java/me/towdium/jecalculation/core/ItemStackHelper.java deleted file mode 100644 index 1c8dd32..0000000 --- a/src/main/java/me/towdium/jecalculation/core/ItemStackHelper.java +++ /dev/null @@ -1,13 +0,0 @@ -package me.towdium.jecalculation.core; - -import net.minecraft.item.ItemStack; - -public class ItemStackHelper { - public static boolean isTypeEqual(ItemStack one, ItemStack two) { - return one != null && two != null && one.getItem() == two.getItem() && getMetadata(one) == getMetadata(two); - } - - private static int getMetadata(ItemStack stack) { - return stack.getItemDamage(); - } -} diff --git a/src/main/java/me/towdium/jecalculation/gui/JecaContainer.java b/src/main/java/me/towdium/jecalculation/gui/JecaContainer.java index cbac81a..efd862c 100644 --- a/src/main/java/me/towdium/jecalculation/gui/JecaContainer.java +++ b/src/main/java/me/towdium/jecalculation/gui/JecaContainer.java @@ -1,5 +1,7 @@ package me.towdium.jecalculation.gui; +import me.towdium.jecalculation.utils.ItemStackHelper; +import me.towdium.jecalculation.utils.polyfill.ClickType; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.InventoryBasic; @@ -13,6 +15,8 @@ public abstract class JecaContainer extends Container { InventoryBasic inventory; List slotBuffer = new ArrayList<>(); + public enum EnumSlotType {SELECT, AMOUNT, DISABLED} + public JecaContainer() { addSlots(); int count = 0; @@ -40,6 +44,34 @@ public abstract class JecaContainer extends Container { return null; } + @Override + public ItemStack slotClick(int slotId, int dragType, int clickTypeIn, EntityPlayer player) { + // PICKUP = 0 + // QUICK_MOVE = 1 + // SWAP = 2 + // CLONE = 3 + // THROW = 4 + // QUICK_CRAFT = 5 + // PICKUP_ALL = 6 + if(slotId >=0 && getSlotType(slotId)==EnumSlotType.AMOUNT){ + ItemStack itemStack = getSlot(slotId).getStack(); + if(itemStack != null){ + itemStack = itemStack.copy(); + if(dragType == 0 && clickTypeIn == ClickType.PICKUP){ + itemStack = ItemStackHelper.Click.leftClick(itemStack); + }else if(dragType == 0 && clickTypeIn == ClickType.QUICK_MOVE){ + itemStack = ItemStackHelper.Click.leftShift(itemStack); + }else if(dragType == 1 && clickTypeIn == ClickType.PICKUP){ + itemStack = ItemStackHelper.Click.rightClick(itemStack); + }else if(dragType == 1 && clickTypeIn == ClickType.QUICK_MOVE){ + itemStack = ItemStackHelper.Click.rightShift(itemStack); + } + getSlot(slotId).putStack(itemStack); + } + } + return null; + } + protected void addSlotSingle(int left, int top){ slotBuffer.add(new int[]{left, top, 0, 0, 1, 1}); } @@ -50,4 +82,6 @@ public abstract class JecaContainer extends Container { protected abstract void addSlots(); + + public abstract EnumSlotType getSlotType(int index); } diff --git a/src/main/java/me/towdium/jecalculation/gui/JecaGuiContainer.java b/src/main/java/me/towdium/jecalculation/gui/JecaGuiContainer.java index ce4adeb..29c8249 100644 --- a/src/main/java/me/towdium/jecalculation/gui/JecaGuiContainer.java +++ b/src/main/java/me/towdium/jecalculation/gui/JecaGuiContainer.java @@ -1,10 +1,14 @@ package me.towdium.jecalculation.gui; +import me.towdium.jecalculation.utils.ItemStackHelper; +import me.towdium.jecalculation.utils.polyfill.SoundEvents; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.RenderHelper; +import net.minecraft.client.renderer.entity.RenderItem; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; @@ -12,11 +16,13 @@ import net.minecraft.item.ItemStack; import java.lang.reflect.Field; import java.util.Arrays; import java.util.List; +import java.util.function.Supplier; public abstract class JecaGuiContainer extends GuiContainer { GuiScreen parent; int activeSlot = -1; long timeStart = 0; + ItemStack temp; public JecaGuiContainer(Container inventorySlotsIn, GuiScreen parent) { super(inventorySlotsIn); @@ -24,7 +30,7 @@ public abstract class JecaGuiContainer extends GuiContainer { } public boolean setActiveSlot(int id) { - if (id == -1 || getSizeSlotActive(id) != 0) { + if (id == -1 || ((JecaContainer) inventorySlots).getSlotType(id) != JecaContainer.EnumSlotType.DISABLED) { activeSlot = id; return true; } else { @@ -32,8 +38,22 @@ public abstract class JecaGuiContainer extends GuiContainer { } } - public int getActiveSlot() { - return activeSlot; + @Override + public void setWorldAndResolution(Minecraft mc, int width, int height) { + super.setWorldAndResolution(mc, width, height); + //TODO check + // ModelManager tempMM = ReflectionHelper.getField(mc, "modelManager", "field_175617_aL"); + // if(tempMM != null){ + // itemRender = new RenderItem(mc.getTextureManager(), tempMM, mc.getItemColors()){ + // @Override + // public void renderItemOverlayIntoGUI(@SuppressWarnings("NullableProblems") FontRenderer fr, ItemStack stack, int xPosition, int yPosition, String text) { + // boolean b = fr.getUnicodeFlag(); + // fr.setUnicodeFlag(true); + // super.renderItemOverlayIntoGUI(fr, stack, xPosition, yPosition, stack == null ? "" : ItemStackHelper.NBT.getType(stack).getDisplayString(ItemStackHelper.NBT.getAmount(stack))); + // fr.setUnicodeFlag(b); + // } + // }; + // } } @Override @@ -47,7 +67,9 @@ public abstract class JecaGuiContainer extends GuiContainer { @Override protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { - drawActiveSlot(); + if (activeSlot != -1) { + drawSlotOverlay(activeSlot, 0x60aeff00); + } super.drawGuiContainerForegroundLayer(mouseX, mouseY); } @@ -55,6 +77,7 @@ public abstract class JecaGuiContainer extends GuiContainer { protected void keyTyped(char typedChar, int keyCode) { if (keyCode == 1) { if (activeSlot != -1) { + inventorySlots.getSlot(activeSlot).putStack(temp); setActiveSlot(-1); } else { mc.displayGuiScreen(parent); @@ -68,13 +91,30 @@ public abstract class JecaGuiContainer extends GuiContainer { protected void mouseClicked(int i1, int i2, int i3) { if (activeSlot == -1) { Slot slot = getSlotUnderMouse(); - if (slot != null && setActiveSlot(slot.getSlotIndex())) { - mc.thePlayer.playSound("gui.button.press", 0.2f, 1f); + if (slot != null) { + switch (((JecaContainer) inventorySlots).getSlotType(slot.getSlotIndex())) { + case SELECT: + temp = slot.getStack(); + if (setActiveSlot(slot.getSlotIndex())) { + mc.thePlayer.playSound(SoundEvents.UI_BUTTON_CLICK, 0.2f, 1f); + } + break; + case AMOUNT: + temp = slot.getStack(); + if (!slot.getHasStack() && setActiveSlot(slot.getSlotIndex())) { + mc.thePlayer.playSound(SoundEvents.UI_BUTTON_CLICK, 0.2f, 1f); + } + break; + } } super.mouseClicked(i1, i2, i3); } else { - activeSlot = -1; - mc.thePlayer.playSound("gui.button.press", 0.2f, 1f); + Slot active = inventorySlots.getSlot(activeSlot); + if(active.getHasStack()) { + active.putStack(ItemStackHelper.toItemStackJEC(active.getStack())); + activeSlot = -1; + mc.thePlayer.playSound(SoundEvents.UI_BUTTON_CLICK, 0.2f, 1f); + } } } @@ -85,7 +125,7 @@ public abstract class JecaGuiContainer extends GuiContainer { */ public void handleMouseOverNEIItemPanel(ItemStack stack) { if (activeSlot != -1) { - inventorySlots.getSlot(activeSlot).putStack(stack == null ? null : stack.copy()); + inventorySlots.getSlot(activeSlot).putStack(stack == null ? null : ItemStackHelper.toItemStackJEC(stack.copy())); } } @@ -119,14 +159,12 @@ public abstract class JecaGuiContainer extends GuiContainer { } } - protected void drawActiveSlot() { - if (activeSlot != -1) { - Slot slot = inventorySlots.getSlot(activeSlot); - int size = getSizeSlotActive(activeSlot); - int move = (size - 16) / 2; - drawRect(slot.xDisplayPosition - move, slot.yDisplayPosition - move, slot.xDisplayPosition + size - move, - slot.yDisplayPosition + size - move, 0x60aeff00); - } + protected void drawSlotOverlay(int index, int color) { + Slot slot = inventorySlots.getSlot(index); + int size = getSizeSlot(index); + int move = (size - 16) / 2; + drawRect(slot.xDisplayPosition - move, slot.yDisplayPosition - move, slot.xDisplayPosition + size - move, + slot.yDisplayPosition + size - move, color); } public void drawCenteredStringMultiLine(FontRenderer fontRendererIn, @@ -157,7 +195,7 @@ public abstract class JecaGuiContainer extends GuiContainer { protected abstract String getButtonTooltip(int buttonId); - protected abstract int getSizeSlotActive(int index); + protected abstract int getSizeSlot(int index); protected Slot getSlotUnderMouse() { Slot theSlot = null; @@ -171,4 +209,11 @@ public abstract class JecaGuiContainer extends GuiContainer { } return theSlot; } + + static class RenderItemSupplier implements Supplier { + @Override + public RenderItem get() { + return null; + } + } } diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiCalculator.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiCalculator.java index 6ac1f01..529cc9d 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/GuiCalculator.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiCalculator.java @@ -126,7 +126,7 @@ public class GuiCalculator extends JecaGuiContainer { } @Override - protected int getSizeSlotActive(int index) { + protected int getSizeSlot(int index) { return index == 0 ? 20: 0; } @@ -146,5 +146,10 @@ public class GuiCalculator extends JecaGuiContainer { EntityPlayer player) { return null; } + + @Override + public EnumSlotType getSlotType(int index) { + return index == 0 ? EnumSlotType.SELECT : EnumSlotType.DISABLED; + } } } diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiEditor.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiEditor.java index 576e070..fb83c4c 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/GuiEditor.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiEditor.java @@ -79,7 +79,7 @@ public class GuiEditor extends JecaGuiContainer { } @Override - protected int getSizeSlotActive(int index) { + protected int getSizeSlot(int index) { return 20; } @@ -97,5 +97,10 @@ public class GuiEditor extends JecaGuiContainer { addSlotGroup(46, 66, 21, 33, 1, 4); addSlotGroup(46, 99, 21, 32, 2, 6); } + + @Override + public EnumSlotType getSlotType(int index) { + return EnumSlotType.AMOUNT; + } } } diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipeList.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipeList.java index d619f39..e89c913 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipeList.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipeList.java @@ -13,7 +13,7 @@ public abstract class GuiRecipeList extends JecaGuiContainer { } @Override - protected int getSizeSlotActive(int index) { + protected int getSizeSlot(int index) { return 0; } diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipeSearch.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipeSearch.java index 253a0c9..f47073f 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipeSearch.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipeSearch.java @@ -13,21 +13,29 @@ public class GuiRecipeSearch extends GuiRecipeList { enum EnumMode { IN, OUT, IO; - EnumMode next(){ - switch (this){ - case OUT: return IN; - case IN: return IO; - case IO: return OUT; - default: return OUT; + EnumMode next() { + switch (this) { + case OUT: + return IN; + case IN: + return IO; + case IO: + return OUT; + default: + return OUT; } } - EnumMode last(){ - switch (this){ - case OUT: return IO; - case IN: return OUT; - case IO: return IN; - default: return OUT; + EnumMode last() { + switch (this) { + case OUT: + return IO; + case IN: + return OUT; + case IO: + return IN; + default: + return OUT; } } } @@ -39,6 +47,11 @@ public class GuiRecipeSearch extends GuiRecipeList { super.addSlots(); addSlotSingle(9, 9); } + + @Override + public EnumSlotType getSlotType(int index) { + return index == 20 ? EnumSlotType.SELECT : EnumSlotType.DISABLED; + } }, parent); } @@ -46,8 +59,8 @@ public class GuiRecipeSearch extends GuiRecipeList { @Override public void initGui() { super.initGui(); - buttonMode = new GuiButton(12, 31+guiLeft, 7+guiTop, 84, 20, "mode"); - buttonSearch = new GuiButton(13, 119+guiLeft, 7+guiTop, 50, 20, "search"); + buttonMode = new GuiButton(12, 31 + guiLeft, 7 + guiTop, 84, 20, "mode"); + buttonSearch = new GuiButton(13, 119 + guiLeft, 7 + guiTop, 50, 20, "search"); buttonList.add(buttonMode); buttonList.add(buttonSearch); } @@ -59,12 +72,13 @@ public class GuiRecipeSearch extends GuiRecipeList { @Override protected void drawGuiContainerBackgroundLayer(float v, int i, int i1) { - this.mc.getTextureManager().bindTexture(new ResourceLocation(JustEnoughCalculation.Reference.MODID, "textures/gui/guiRecipeSearch.png")); + this.mc.getTextureManager().bindTexture( + new ResourceLocation(JustEnoughCalculation.Reference.MODID, "textures/gui/guiRecipeSearch.png")); this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize); } @Override - protected int getSizeSlotActive(int index) { + protected int getSizeSlot(int index) { return index == 20 ? 20 : 0; } } diff --git a/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java b/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java index 6f47467..bdaec39 100644 --- a/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java +++ b/src/main/java/me/towdium/jecalculation/nei/JecaOverlayHandler.java @@ -3,7 +3,7 @@ package me.towdium.jecalculation.nei; import codechicken.nei.PositionedStack; import codechicken.nei.api.IOverlayHandler; import codechicken.nei.recipe.IRecipeHandler; -import me.towdium.jecalculation.core.ItemStackHelper; +import me.towdium.jecalculation.utils.ItemStackHelper; import me.towdium.jecalculation.gui.guis.GuiEditor; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.inventory.GuiContainer; diff --git a/src/main/java/me/towdium/jecalculation/network/IProxy.java b/src/main/java/me/towdium/jecalculation/network/IProxy.java index 53dd617..fcd0a74 100644 --- a/src/main/java/me/towdium/jecalculation/network/IProxy.java +++ b/src/main/java/me/towdium/jecalculation/network/IProxy.java @@ -1,5 +1,8 @@ package me.towdium.jecalculation.network; public interface IProxy { + void preInit(); + void init(); + } diff --git a/src/main/java/me/towdium/jecalculation/network/ProxyClient.java b/src/main/java/me/towdium/jecalculation/network/ProxyClient.java index 9272719..9f1011e 100644 --- a/src/main/java/me/towdium/jecalculation/network/ProxyClient.java +++ b/src/main/java/me/towdium/jecalculation/network/ProxyClient.java @@ -1,12 +1,21 @@ package me.towdium.jecalculation.network; import cpw.mods.fml.common.network.NetworkRegistry; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; import me.towdium.jecalculation.JustEnoughCalculation; import me.towdium.jecalculation.gui.GuiHandler; +@SideOnly(Side.CLIENT) public class ProxyClient implements IProxy { + @Override + public void preInit() { + + } + @Override public void init() { NetworkRegistry.INSTANCE.registerGuiHandler(JustEnoughCalculation.INSTANCE, new GuiHandler()); } + } diff --git a/src/main/java/me/towdium/jecalculation/network/ProxyServer.java b/src/main/java/me/towdium/jecalculation/network/ProxyServer.java index 014d40f..0f4ea29 100644 --- a/src/main/java/me/towdium/jecalculation/network/ProxyServer.java +++ b/src/main/java/me/towdium/jecalculation/network/ProxyServer.java @@ -1,8 +1,14 @@ package me.towdium.jecalculation.network; -public class ProxyServer implements IProxy{ +public class ProxyServer implements IProxy { + @Override + public void preInit() { + + } + @Override public void init() { } + } diff --git a/src/main/java/me/towdium/jecalculation/utils/ItemStackHelper.java b/src/main/java/me/towdium/jecalculation/utils/ItemStackHelper.java new file mode 100644 index 0000000..4c4fff9 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/utils/ItemStackHelper.java @@ -0,0 +1,299 @@ +package me.towdium.jecalculation.utils; + +import me.towdium.jecalculation.JustEnoughCalculation; +import me.towdium.jecalculation.utils.polyfill.Polyfill; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; + +public class ItemStackHelper { + public static final String keyAmount = "amount"; + public static final String keyType = "type"; + + public enum EnumStackType { + INVALID, NUMBER, PERCENTAGE, FLUID; + + public String getDisplayString(long l) { + switch (this) { + case NUMBER: + if (l < 999) { + return String.valueOf(l); + } else if (l < 9999) { + return String.format("%.2fK", (double) l / 1000); + } else if (l < 99999) { + return String.format("%.1fK", (double) l / 1000); + } else if (l < 999999) { + return String.format("%.0fK", (double) l / 1000); + } else if (l < 9999999) { + return String.format("%.2fM", (double) l / 1000000); + } else if (l < 99999999) { + return String.format("%.1fM", (double) l / 1000000); + } else if (l < 999999999) { + return String.format("%.0fB", (double) l / 1000000); + } else if (l < 9999999999L) { + return String.format("%.2fB", (double) l / 1000000000); + } else if (l < 99999999999L) { + return String.format("%.1fB", (double) l / 1000000000); + } else if (l < 999999999999L) { + return String.format("%.0fT", (double) l / 1000000000); + } else if (l < 9999999999999L) { + return String.format("%.2fT", (double) l / 1000000000000L); + } else if (l < 99999999999999L) { + return String.format("%.1fT", (double) l / 1000000000000L); + } else if (l < 999999999999999L) { + return String.format("%.0fG", (double) l / 1000000000000L); + } else if (l < 9999999999999999L) { + return String.format("%.2fG", (double) l / 1000000000000000L); + } else if (l < 99999999999990999L) { + return String.format("%.1fG", (double) l / 1000000000000000L); + } else { + return String.format("%.0fG", (double) l / 1000000000000000L); + } + case PERCENTAGE: + if (l < 999) { + return String.valueOf(l); + } else if (l < 9999) { + return String.format("≈%.1fK", (double) l / 1000); + } else if (l < 99999) { + return String.format("≈%.0fK", (double) l / 1000); + } else if (l < 999999) { + return String.format("≈%.1fM", (double) l / 1000000); + } else if (l < 9999999) { + return String.format("≈%.1fM", (double) l / 1000000); + } else if (l < 99999999) { + return String.format("≈%.0fM", (double) l / 1000000); + } else if (l < 999999999) { + return String.format("≈%.1fB", (double) l / 1000000000); + } else if (l < 9999999999L) { + return String.format("≈%.1fB", (double) l / 1000000000); + } else if (l < 99999999999L) { + return String.format("≈%.0fB", (double) l / 1000000000); + } else if (l < 999999999999L) { + return String.format("≈%.1fT", (double) l / 1000000000000L); + } else if (l < 9999999999999L) { + return String.format("≈%.1fT", (double) l / 1000000000000L); + } else if (l < 99999999999999L) { + return String.format("≈%.0fT", (double) l / 1000000000000L); + } else if (l < 999999999999999L) { + return String.format("≈%.1fG", (double) l / 1000000000000000L); + } else if (l < 9999999999999999L) { + return String.format("≈%.1fG", (double) l / 1000000000000000L); + } else { + return String.format("≈%.0fG", (double) l / 1000000000000000L); + } + case FLUID: + if (l < 999) { + return l + "mb"; + } else if (l < 9999) { + return String.format("%.2fb", (double) l / 1000); + } else if (l < 99999) { + return String.format("%.1fb", (double) l / 1000); + } else if (l < 999999) { + return String.format("%.0fb", (double) l / 1000); + } else if (l < 9999999) { + return String.format("%.1fKb", (double) l / 1000000); + } else if (l < 99999999) { + return String.format("%.0fKb", (double) l / 1000000); + } else if (l < 999999999) { + return String.format("%.1fMb", (double) l / 1000000000); + } else if (l < 9999999999L) { + return String.format("%.1fMb", (double) l / 1000000000); + } else if (l < 99999999999L) { + return String.format("%.0fMb", (double) l / 1000000000); + } else if (l < 999999999999L) { + return String.format("%.1fBb", (double) l / 1000000000000L); + } else if (l < 9999999999999L) { + return String.format("%.1fBb", (double) l / 1000000000000L); + } else if (l < 99999999999999L) { + return String.format("%.0fBb", (double) l / 1000000000000L); + } else if (l < 999999999999999L) { + return String.format("%.1fTb", (double) l / 1000000000000000L); + } else if (l < 9999999999999999L) { + return String.format("%.1fTb", (double) l / 1000000000000000L); + } else { + return String.format("%.0fTb", (double) l / 1000000000000000L); + } + default: + return "N/A"; + } + } + } + + + public static boolean isTypeEqual(ItemStack one, ItemStack two) { + return one != null && two != null && one.getItem() == two.getItem() && getMetadata(one) == getMetadata(two) && + NBT.equalsIgnoreJEC(one.getTagCompound(), two.getTagCompound()); + } + + + public static ItemStack toItemStackJEC(ItemStack itemStack) { + NBT.setData(itemStack, EnumStackType.NUMBER, itemStack.stackSize); + itemStack.stackSize = 1; + return itemStack; + } + + + private static int getMetadata(ItemStack stack) { + return stack.getItemDamage(); + } + + public static class NBT { + public static boolean equalsIgnoreJEC(@Nullable NBTTagCompound a, @Nullable NBTTagCompound b) { + if (a == null && b == null) { + return true; + } else if (a == null || b == null) { + return false; + } else { + NBTTagCompound aNew = ((NBTTagCompound) a.copy()); + aNew.removeTag(JustEnoughCalculation.Reference.MODID); + NBTTagCompound bNew = ((NBTTagCompound) b.copy()); + bNew.removeTag(JustEnoughCalculation.Reference.MODID); + return aNew.equals(bNew); + } + } + + public static ItemStack setAmount(ItemStack itemStack, long amount) { + return setLong(itemStack, true, keyAmount, amount); + } + + public static ItemStack setType(ItemStack itemStack, EnumStackType type) { + return setInteger(itemStack, true, keyType, type.ordinal()); + } + + public static ItemStack setData(ItemStack itemStack, EnumStackType type, long amount) { + setInteger(itemStack, true, keyType, type.ordinal()); + return setLong(itemStack, true, keyAmount, amount); + } + + public static ItemStack setInteger(ItemStack itemStack, boolean isolate, String key, int value) { + return setValue(itemStack, isolate, (nbtTagCompound -> nbtTagCompound.setInteger(key, value))); + } + + public static ItemStack setLong(ItemStack itemStack, boolean isolate, String key, long value) { + return setValue(itemStack, isolate, (nbtTagCompound -> nbtTagCompound.setLong(key, value))); + } + + public static ItemStack setBoolean(ItemStack itemStack, boolean isolate, String key, boolean value) { + return setValue(itemStack, isolate, (nbtTagCompound -> nbtTagCompound.setBoolean(key, value))); + } + + static ItemStack setValue(ItemStack itemStack, boolean isolate, Consumer func) { + NBTTagCompound tagCompound = itemStack.getTagCompound(); + if (tagCompound == null) { + tagCompound = new NBTTagCompound(); + itemStack.setTagCompound(tagCompound); + } + if (isolate) { + if (!tagCompound.hasKey(JustEnoughCalculation.Reference.MODID)) { + tagCompound.setTag(JustEnoughCalculation.Reference.MODID, new NBTTagCompound()); + } + tagCompound = tagCompound.getCompoundTag(JustEnoughCalculation.Reference.MODID); + } + func.accept(tagCompound); + return itemStack; + } + + public static EnumStackType getType(ItemStack itemStack) { + return EnumStackType.values()[getInteger(itemStack, true, keyType)]; + } + + public static long getAmount(ItemStack itemStack) { + return getLong(itemStack, true, keyAmount); + } + + public static long getAmountItem(ItemStack itemStack) { + long amount = getAmount(itemStack); + switch (getType(itemStack)) { + case NUMBER: + return amount; + case PERCENTAGE: + return (amount + 99) % 100; + case FLUID: + return (amount + 999) % 1000; + default: + return 0; + } + } + + public static long getAmountInternal(ItemStack itemStack) { + long amount = getAmount(itemStack); + switch (getType(itemStack)) { + case NUMBER: + return amount * 100; + case PERCENTAGE: + return amount; + case FLUID: + return amount; + default: + return 0; + } + } + + public static int getInteger(ItemStack itemStack, boolean isolate, String key) { + return getTag(itemStack, isolate).getInteger(key); + } + + public static long getLong(ItemStack itemStack, boolean isolate, String key) { + return getTag(itemStack, isolate).getLong(key); + } + + @SuppressWarnings("TrivialFunctionalExpressionUsage") + static NBTTagCompound getTag(@Nonnull ItemStack itemStack, boolean isolate) { + return isolate ? + Polyfill.getSubCompound(itemStack, JustEnoughCalculation.Reference.MODID, true) : + itemStack.hasTagCompound() ? itemStack.getTagCompound() : ((Supplier) () -> { + itemStack.setTagCompound(new NBTTagCompound()); + return itemStack.getTagCompound(); + }).get(); + } + } + + + public static class Click { + @Nullable + public static ItemStack leftClick(@Nullable ItemStack itemStack) { + return getClick(itemStack, (l -> l+1), (l -> l+1), (l -> 0L)); + } + + @Nullable + public static ItemStack leftShift(@Nullable ItemStack itemStack) { + return getClick(itemStack, (l -> l==1 ? 10 : l+10), (l -> l==1 ? 10 : l+10), (l -> 0L)); + } + + @Nullable + public static ItemStack rightClick(@Nullable ItemStack itemStack) { + return getClick(itemStack, (l -> l-1), (l -> l-1), (l -> 0L)); + } + + @Nullable + public static ItemStack rightShift(@Nullable ItemStack itemStack) { + return getClick(itemStack, (l -> l==1 ? 0 : l<=10 ? 1 : l-10), (l -> l==1 ? 0 : l<=10 ? 1 : l-10), (l -> 0L)); + } + + @Nullable + static ItemStack getClick(@Nullable ItemStack itemStack, Function funcNumber, + Function funcPercentage, Function funcFluid){ + if(itemStack == null){ + return null; + } else { + long amount = NBT.getAmount(itemStack); + switch (NBT.getType(itemStack)){ + case INVALID: + amount = 0; break; + case NUMBER: + amount = funcNumber.apply(amount); break; + case PERCENTAGE: + amount = funcPercentage.apply(amount); break; + case FLUID: + amount = funcFluid.apply(amount); break; + } + return amount == 0 ? null : NBT.setAmount(itemStack, amount); + } + } + } +} diff --git a/src/main/java/me/towdium/jecalculation/utils/ReflectionHelper.java b/src/main/java/me/towdium/jecalculation/utils/ReflectionHelper.java new file mode 100644 index 0000000..5c72eed --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/utils/ReflectionHelper.java @@ -0,0 +1,42 @@ +package me.towdium.jecalculation.utils; + +import java.lang.reflect.Field; + +/** + * Author: Towdium + * Date: 2016/6/25. + */ +public class ReflectionHelper { + @SuppressWarnings("unchecked") + public static T getField(C o, String... names){ + Field field = null; + boolean flag = false; + for(String name : names){ + try { + field = o.getClass().getDeclaredField(name); + } catch (NoSuchFieldException e) { + continue; + } + flag = true; + break; + } + if(!flag){ + StringBuilder buffer = new StringBuilder( + "Field not found in class " + o.getClass().getCanonicalName() + ":"); + for(String s : names){ + buffer.append(" "); + buffer.append(s); + } + throw new NoSuchFieldError(buffer.toString()); + } else { + field.setAccessible(true); + try { + Object temp = field.get(o); + return (T) temp; + } catch (IllegalAccessException e) { + return null; + } + } + + } +} diff --git a/src/main/java/me/towdium/jecalculation/utils/polyfill/ClickType.java b/src/main/java/me/towdium/jecalculation/utils/polyfill/ClickType.java new file mode 100644 index 0000000..791f0e9 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/utils/polyfill/ClickType.java @@ -0,0 +1,11 @@ +package me.towdium.jecalculation.utils.polyfill; + +public class ClickType { + public static final int PICKUP = 0; + public static final int QUICK_MOVE = 1; + public static final int SWAP = 2; + public static final int CLONE = 3; + public static final int THROW = 4; + public static final int QUICK_CRAFT = 5; + public static final int PICKUP_ALL = 6; +} diff --git a/src/main/java/me/towdium/jecalculation/utils/polyfill/Polyfill.java b/src/main/java/me/towdium/jecalculation/utils/polyfill/Polyfill.java new file mode 100644 index 0000000..4131573 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/utils/polyfill/Polyfill.java @@ -0,0 +1,26 @@ +package me.towdium.jecalculation.utils.polyfill; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +public class Polyfill { + /** + * Get an NBTTagCompound from this stack's NBT data. + * + * @param itemStack item stack + * @param key key + * @param create create if not exist + * @return NBTTagCompound + */ + public static NBTTagCompound getSubCompound(ItemStack itemStack, String key, boolean create) { + if (itemStack.stackTagCompound != null && itemStack.stackTagCompound.hasKey(key, 10)) { + return itemStack.stackTagCompound.getCompoundTag(key); + } else if (create) { + NBTTagCompound nbtTagCompound = new NBTTagCompound(); + itemStack.setTagInfo(key, nbtTagCompound); + return nbtTagCompound; + } else { + return null; + } + } +} diff --git a/src/main/java/me/towdium/jecalculation/utils/polyfill/SoundEvents.java b/src/main/java/me/towdium/jecalculation/utils/polyfill/SoundEvents.java new file mode 100644 index 0000000..5a659d9 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/utils/polyfill/SoundEvents.java @@ -0,0 +1,5 @@ +package me.towdium.jecalculation.utils.polyfill; + +public class SoundEvents { + public static final String UI_BUTTON_CLICK = "gui.button.press"; +}