add help function, can't show number of item

This commit is contained in:
Discreater committed 2021-03-02 21:46:50 +08:00
1 parent 6d99e53ada
commit 87158fadbe
9 files changed
+259 -79

No files matched your search

@@ -0,0 +1,143 @@
package me.towdium.jecalculation.core;
import cpw.mods.fml.common.network.ByteBufUtils;
import io.netty.buffer.ByteBuf;
import me.towdium.jecalculation.utils.ItemStackHelper;
import me.towdium.jecalculation.utils.function.TriConsumer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import javax.annotation.Nullable;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
public class Recipe {
ItemStack[] output;
ItemStack[] catalyst;
ItemStack[] input;
enum EnumStackIOType {
OUTPUT, CATALYST, INPUT;
public int getLength() {
switch (this) {
case OUTPUT:
return 4;
case CATALYST:
return 4;
case INPUT:
return 12;
default:
return 0;
}
}
}
public Recipe(ItemStack[] output, ItemStack[] catalyst, ItemStack[] input) {
BiFunction<ItemStack[], Integer, Boolean> checkArray = (itemStackIn, amount) -> {
if (itemStackIn.length != amount)
return false;
for (ItemStack itemStack : itemStackIn) {
if (!ItemStackHelper.isItemStackJEC(itemStack))
return false;
}
return true;
};
Function<ItemStack[], ItemStack[]> copyArray = itemStacks -> {
ItemStack[] buffer = new ItemStack[itemStacks.length];
int count = -1;
for (ItemStack itemStack : itemStacks) {
buffer[++count] = itemStack.copy();
}
return buffer;
};
if (!checkArray.apply(output, 4) || !checkArray.apply(catalyst, 4) || !checkArray.apply(input, 12)) {
throw new IllegalArgumentException();
}
this.output = copyArray.apply(output);
this.catalyst = copyArray.apply(catalyst);
this.input = copyArray.apply(input);
}
@Nullable
public ItemStack getItemStackInput(ItemStack itemStack) {
return getItemSack(input, itemStack);
}
@Nullable
public ItemStack getItemStackCatalyst(ItemStack itemStack) {
return getItemSack(catalyst, itemStack);
}
@Nullable
public ItemStack getItemStackOutput(ItemStack itemStack) {
return getItemSack(output, itemStack);
}
@Nullable
ItemStack getItemSack(ItemStack[] itemStacks, ItemStack itemStackIn) {
for (ItemStack itemStack : itemStacks) {
if (ItemStackHelper.isTypeEqual(itemStack, itemStackIn)) {
return itemStack;
}
}
return null;
}
public static class IOUtl {
public static Recipe fromByte(ByteBuf buf) {
return fromContainer((enumStackIOType, integer) -> ByteBufUtils.readItemStack(buf));
}
public static Recipe fromNbt(NBTTagCompound tagCompound) {
return fromContainer((enumStackIOType, integer) -> {
NBTTagCompound temp = tagCompound.getCompoundTag(String.valueOf(enumStackIOType.ordinal()));
temp = temp.getCompoundTag(String.valueOf(integer));
return ItemStack.loadItemStackFromNBT(temp);
});
}
public static void toByte(ByteBuf buf, Recipe recipe) {
toContainer((enumStackIOType, integer, itemStack) -> ByteBufUtils.writeItemStack(buf, itemStack), recipe);
}
public static void toNbt(Recipe recipe) {
NBTTagCompound tagCompound = new NBTTagCompound();
toContainer((enumStackIOType, integer, itemStack) -> {
NBTTagCompound stackInfo = new NBTTagCompound();
itemStack.writeToNBT(stackInfo);
String temp = String.valueOf(enumStackIOType.ordinal());
if (integer == 0) {
tagCompound.setTag(temp, new NBTTagCompound());
}
tagCompound.getCompoundTag(temp).setTag(String.valueOf(integer), stackInfo);
}, recipe);
}
static Recipe fromContainer(BiFunction<EnumStackIOType, Integer, ItemStack> func) {
Function<EnumStackIOType, ItemStack[]> readArray = (type) -> {
int len = type.getLength();
ItemStack[] buffer = new ItemStack[len];
for (int i = 0; i < len; i++) {
buffer[i] = func.apply(type, i);
}
return buffer;
};
return new Recipe(readArray.apply(EnumStackIOType.OUTPUT), readArray.apply(EnumStackIOType.CATALYST),
readArray.apply(EnumStackIOType.INPUT));
}
static void toContainer(TriConsumer<EnumStackIOType, Integer, ItemStack> func, Recipe recipe) {
BiConsumer<EnumStackIOType, ItemStack[]> writeArray = ((type, itemStacks) -> {
int len = type.getLength();
for (int i = 0; i < len; i++) {
func.accept(type, i, itemStacks[i]);
}
});
writeArray.accept(EnumStackIOType.OUTPUT, recipe.output);
writeArray.accept(EnumStackIOType.CATALYST, recipe.catalyst);
writeArray.accept(EnumStackIOType.INPUT, recipe.input);
}
}
}
@@ -21,14 +21,15 @@ public abstract class JecaContainer extends Container {
addSlots();
int count = 0;
int current = -1;
for(int[] record: slotBuffer) {
for (int[] record : slotBuffer) {
count += (record[4] * record[5]);
}
inventory = new InventoryBasic("Temp", false, count);
for(int[] record: slotBuffer) {
for(int a = 0; a < record[4]; a++) {
for(int b=0; b < record[5];b++) {
addSlotToContainer(new Slot(inventory, ++current, record[0]+b*record[2], record[1]+a*record[3]));
for (int[] record : slotBuffer) {
for (int a = 0; a < record[4]; a++) {
for (int b = 0; b < record[5]; b++) {
addSlotToContainer(
new Slot(inventory, ++current, record[0] + b * record[2], record[1] + a * record[3]));
}
}
}
@@ -53,17 +54,17 @@ public abstract class JecaContainer extends Container {
// THROW = 4
// QUICK_CRAFT = 5
// PICKUP_ALL = 6
if(slotId >=0 && getSlotType(slotId)==EnumSlotType.AMOUNT){
if (slotId >= 0 && getSlotType(slotId) == EnumSlotType.AMOUNT) {
ItemStack itemStack = getSlot(slotId).getStack();
if(itemStack != null){
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){
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){
} else if (dragType == 1 && clickTypeIn == ClickType.PICKUP) {
itemStack = ItemStackHelper.Click.rightClick(itemStack);
}else if(dragType == 1 && clickTypeIn == ClickType.QUICK_MOVE){
} else if (dragType == 1 && clickTypeIn == ClickType.QUICK_MOVE) {
itemStack = ItemStackHelper.Click.rightShift(itemStack);
}
getSlot(slotId).putStack(itemStack);
@@ -72,11 +73,11 @@ public abstract class JecaContainer extends Container {
return null;
}
protected void addSlotSingle(int left, int top){
protected void addSlotSingle(int left, int top) {
slotBuffer.add(new int[]{left, top, 0, 0, 1, 1});
}
protected void addSlotGroup(int left, int top, int intervalH, int intervalV, int sizeH, int sizeV){
protected void addSlotGroup(int left, int top, int intervalH, int intervalV, int sizeH, int sizeV) {
slotBuffer.add(new int[]{left, top, intervalH, intervalV, sizeH, sizeV});
}
@@ -9,6 +9,7 @@ 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.client.renderer.texture.TextureManager;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
@@ -42,26 +43,16 @@ public abstract class JecaGuiContainer extends GuiContainer {
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);
// }
// };
// }
itemRender = new JecaRenderItem();
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
super.drawScreen(mouseX, mouseY, partialTicks);
RenderHelper.disableStandardItemLighting(); // ?
drawToolTipScreen(mouseX, mouseY);
drawTooltipScreen(mouseX, mouseY);
RenderHelper.enableStandardItemLighting(); // >
}
@@ -110,11 +101,9 @@ public abstract class JecaGuiContainer extends GuiContainer {
super.mouseClicked(i1, i2, i3);
} else {
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);
}
active.putStack(ItemStackHelper.toItemStackJEC(active.getStack()));
activeSlot = -1;
mc.thePlayer.playSound(SoundEvents.UI_BUTTON_CLICK, 0.2f, 1f);
}
}
@@ -125,11 +114,12 @@ public abstract class JecaGuiContainer extends GuiContainer {
*/
public void handleMouseOverNEIItemPanel(ItemStack stack) {
if (activeSlot != -1) {
inventorySlots.getSlot(activeSlot).putStack(stack == null ? null : ItemStackHelper.toItemStackJEC(stack.copy()));
inventorySlots.getSlot(activeSlot)
.putStack(stack == null ? null : ItemStackHelper.toItemStackJEC(stack.copy()));
}
}
protected void drawToolTipScreen(int mouseX, int mouseY) {
protected void drawTooltipScreen(int mouseX, int mouseY) {
boolean flagUnicode = mc.fontRenderer.getUnicodeFlag();
boolean flagOver = false;
mc.fontRenderer.setUnicodeFlag(true);
@@ -0,0 +1,27 @@
package me.towdium.jecalculation.gui;
import me.towdium.jecalculation.utils.ItemStackHelper;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.item.ItemStack;
public class JecaRenderItem extends RenderItem {
@Override
public void renderItemOverlayIntoGUI(FontRenderer fr,
TextureManager textureManager,
ItemStack stack,
int xPosition,
int yPosition,
String text) {
boolean b = fr.getUnicodeFlag();
fr.setUnicodeFlag(true);
String customText = stack == null ?
"" :
ItemStackHelper.NBT.getType(stack)
.getDisplayString(ItemStackHelper.NBT.getAmount(stack));
System.out.println("render " + customText);
super.renderItemOverlayIntoGUI(fr, textureManager, stack, xPosition, yPosition, customText);
fr.setUnicodeFlag(b);
}
}
@@ -9,6 +9,7 @@ import net.minecraft.client.gui.GuiTextField;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
import javax.annotation.Nullable;
@@ -27,6 +28,7 @@ public class GuiCalculator extends JecaGuiContainer {
int activeSlot = -1;
int page = 1;
int total = 0;
EnumMode mode = EnumMode.INPUT;
@SuppressWarnings("DuplicateBranchesInSwitch")
public enum EnumMode {
@@ -90,16 +92,10 @@ public class GuiCalculator extends JecaGuiContainer {
textFieldAmount = new GuiTextField(fontRendererObj, guiLeft + 39, guiTop + 8, 75, 18);
}
@Override
protected void actionPerformed(GuiButton button) {
//noinspection SwitchStatementWithTooFewBranches
switch (button.id) {
case 1: mc.displayGuiScreen(new GuiRecipeSearch(this));
}
}
@Override
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(
new ResourceLocation(JustEnoughCalculation.Reference.MODID, "textures/gui/guiCalculator.png"));
this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
@@ -114,6 +110,13 @@ public class GuiCalculator extends JecaGuiContainer {
drawCenteredString(fontRendererObj, page + "/" + total, 33, 145, 0xFFFFFF);
}
@Override
protected void actionPerformed(GuiButton button) {
//noinspection SwitchStatementWithTooFewBranches
switch (button.id) {
case 1: mc.displayGuiScreen(new GuiRecipeSearch(this));
}
}
@SuppressWarnings("SwitchStatementWithTooFewBranches")
@Override
protected String getButtonTooltip(int buttonId) {
@@ -138,15 +141,6 @@ public class GuiCalculator extends JecaGuiContainer {
addSlotGroup(8, 82, 18, 18, 3, 9);
}
@Nullable
@Override
public ItemStack slotClick(int slotId,
int dragType,
int clickTypeIn,
EntityPlayer player) {
return null;
}
@Override
public EnumSlotType getSlotType(int index) {
return index == 0 ? EnumSlotType.SELECT : EnumSlotType.DISABLED;
@@ -71,7 +71,7 @@ public class GuiRecipeSearch extends GuiRecipeList {
}
@Override
protected void drawGuiContainerBackgroundLayer(float v, int i, int i1) {
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
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);
@@ -79,6 +79,6 @@ public class GuiRecipeSearch extends GuiRecipeList {
@Override
protected int getSizeSlot(int index) {
return index == 20 ? 20 : 0;
return index == 20 ? 20 : 18;
}
}
@@ -9,13 +9,12 @@ 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 {
public enum EnumStackAmountType {
INVALID, NUMBER, PERCENTAGE, FLUID;
public String getDisplayString(long l) {
@@ -125,18 +124,25 @@ public class ItemStackHelper {
}
public static boolean isTypeEqual(ItemStack one, ItemStack two) {
public static boolean isTypeEqual(@Nullable ItemStack one, @Nullable ItemStack two) {
return one != null && two != null && one.getItem() == two.getItem() && getMetadata(one) == getMetadata(two) &&
NBT.equalsIgnoreJEC(one.getTagCompound(), two.getTagCompound());
}
@Nullable
public static ItemStack toItemStackJEC(ItemStack itemStack) {
NBT.setData(itemStack, EnumStackType.NUMBER, itemStack.stackSize);
if (itemStack == null)
return null;
NBT.setData(itemStack, EnumStackAmountType.NUMBER, itemStack.stackSize);
itemStack.stackSize = 1;
return itemStack;
}
public static boolean isItemStackJEC(@Nullable ItemStack itemStack) {
return itemStack != null && itemStack.getTagCompound() != null &&
itemStack.getTagCompound().hasKey(JustEnoughCalculation.Reference.MODID);
}
private static int getMetadata(ItemStack stack) {
return stack.getItemDamage();
@@ -161,11 +167,11 @@ public class ItemStackHelper {
return setLong(itemStack, true, keyAmount, amount);
}
public static ItemStack setType(ItemStack itemStack, EnumStackType type) {
public static ItemStack setType(ItemStack itemStack, EnumStackAmountType type) {
return setInteger(itemStack, true, keyType, type.ordinal());
}
public static ItemStack setData(ItemStack itemStack, EnumStackType type, long amount) {
public static ItemStack setData(ItemStack itemStack, EnumStackAmountType type, long amount) {
setInteger(itemStack, true, keyType, type.ordinal());
return setLong(itemStack, true, keyAmount, amount);
}
@@ -198,8 +204,8 @@ public class ItemStackHelper {
return itemStack;
}
public static EnumStackType getType(ItemStack itemStack) {
return EnumStackType.values()[getInteger(itemStack, true, keyType)];
public static EnumStackAmountType getType(ItemStack itemStack) {
return EnumStackAmountType.values()[getInteger(itemStack, true, keyType)];
}
public static long getAmount(ItemStack itemStack) {
@@ -242,14 +248,15 @@ public class ItemStackHelper {
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<NBTTagCompound>) () -> {
itemStack.setTagCompound(new NBTTagCompound());
return itemStack.getTagCompound();
}).get();
if (isolate) {
return Polyfill.getSubCompound(itemStack, JustEnoughCalculation.Reference.MODID, true);
} else {
if (!itemStack.hasTagCompound()) {
itemStack.setTagCompound(new NBTTagCompound());
}
return itemStack.getTagCompound();
}
}
}
@@ -257,40 +264,47 @@ public class ItemStackHelper {
public static class Click {
@Nullable
public static ItemStack leftClick(@Nullable ItemStack itemStack) {
return getClick(itemStack, (l -> l+1), (l -> l+1), (l -> 0L));
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));
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));
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));
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<Long, Long> funcNumber,
Function<Long, Long> funcPercentage, Function<Long, Long> funcFluid){
if(itemStack == null){
static ItemStack getClick(@Nullable ItemStack itemStack,
Function<Long, Long> funcNumber,
Function<Long, Long> funcPercentage,
Function<Long, Long> funcFluid) {
if (itemStack == null) {
return null;
} else {
long amount = NBT.getAmount(itemStack);
switch (NBT.getType(itemStack)){
switch (NBT.getType(itemStack)) {
case INVALID:
amount = 0; break;
amount = 0;
break;
case NUMBER:
amount = funcNumber.apply(amount); break;
amount = funcNumber.apply(amount);
break;
case PERCENTAGE:
amount = funcPercentage.apply(amount); break;
amount = funcPercentage.apply(amount);
break;
case FLUID:
amount = funcFluid.apply(amount); break;
amount = funcFluid.apply(amount);
break;
}
return amount == 0 ? null : NBT.setAmount(itemStack, amount);
}
@@ -0,0 +1,5 @@
package me.towdium.jecalculation.utils.function;
public interface TriConsumer<T, U, V> {
void accept(T t, U u, V v);
}
@@ -0,0 +1,6 @@
package me.towdium.jecalculation.utils.function;
public interface TriFunction <T, U, V, R> {
R apply(T t, U u, V v);
}