From 7f525ce264def7e99f664556315a07969f0c10e7 Mon Sep 17 00:00:00 2001 From: Discreater Date: Sat, 1 May 2021 23:15:00 +0800 Subject: [PATCH] feat: math and Chinese --- .../jecalculation/command/Commands.java | 8 +- .../commands/{CUuid.java => CCraft.java} | 13 +- .../commands/{COreDict.java => CMath.java} | 14 +- .../command/commands/CState.java | 2 +- .../jecalculation/data/Controller.java | 144 +++++++------ .../jecalculation/data/structure/Recents.java | 46 ----- .../jecalculation/data/structure/Recipes.java | 16 +- .../data/structure/RecordCraft.java | 58 ++++++ .../data/structure/RecordMath.java | 93 +++++++++ .../data/structure/RecordPlayer.java | 31 +++ .../event/handlers/FMLBusEventHandler.java | 9 +- .../me/towdium/jecalculation/gui/JecaGui.java | 53 ++++- .../towdium/jecalculation/gui/Resource.java | 24 +++ .../{GuiCalculator.java => GuiCraft.java} | 74 ++++--- ...{GuiDisambiguation.java => GuiDisamb.java} | 4 +- .../jecalculation/gui/guis/GuiMath.java | 189 ++++++++++++++++++ .../jecalculation/gui/guis/GuiRecipe.java | 10 +- .../jecalculation/gui/guis/GuiSearch.java | 2 +- .../jecalculation/gui/widgets/WButton.java | 32 ++- .../gui/widgets/WButtonText.java | 9 +- .../jecalculation/gui/widgets/WLabel.java | 9 +- .../jecalculation/gui/widgets/WLcd.java | 148 ++++++++++++++ .../jecalculation/gui/widgets/WLine.java | 34 ++++ .../jecalculation/gui/widgets/WPanel.java | 6 +- .../jecalculation/gui/widgets/WTooltip.java | 3 +- .../jecalculation/network/ClientHandler.java | 8 +- .../jecalculation/utils/Utilities.java | 9 +- .../assets/jecalculation/lang/en_US.lang | 37 ++-- .../assets/jecalculation/lang/zh_CN.lang | 88 ++++++++ src/test/java/JecaTest.java | 8 - 30 files changed, 970 insertions(+), 211 deletions(-) rename src/main/java/me/towdium/jecalculation/command/commands/{CUuid.java => CCraft.java} (72%) rename src/main/java/me/towdium/jecalculation/command/commands/{COreDict.java => CMath.java} (70%) delete mode 100644 src/main/java/me/towdium/jecalculation/data/structure/Recents.java create mode 100644 src/main/java/me/towdium/jecalculation/data/structure/RecordCraft.java create mode 100644 src/main/java/me/towdium/jecalculation/data/structure/RecordMath.java create mode 100644 src/main/java/me/towdium/jecalculation/data/structure/RecordPlayer.java rename src/main/java/me/towdium/jecalculation/gui/guis/{GuiCalculator.java => GuiCraft.java} (80%) rename src/main/java/me/towdium/jecalculation/gui/guis/{GuiDisambiguation.java => GuiDisamb.java} (92%) create mode 100644 src/main/java/me/towdium/jecalculation/gui/guis/GuiMath.java create mode 100644 src/main/java/me/towdium/jecalculation/gui/widgets/WLcd.java create mode 100644 src/main/resources/assets/jecalculation/lang/zh_CN.lang delete mode 100644 src/test/java/JecaTest.java diff --git a/src/main/java/me/towdium/jecalculation/command/Commands.java b/src/main/java/me/towdium/jecalculation/command/Commands.java index 4d7b84c..bc7e525 100644 --- a/src/main/java/me/towdium/jecalculation/command/Commands.java +++ b/src/main/java/me/towdium/jecalculation/command/Commands.java @@ -1,9 +1,9 @@ package me.towdium.jecalculation.command; import me.towdium.jecalculation.command.commands.CHelp; -import me.towdium.jecalculation.command.commands.COreDict; +import me.towdium.jecalculation.command.commands.CMath; import me.towdium.jecalculation.command.commands.CState; -import me.towdium.jecalculation.command.commands.CUuid; +import me.towdium.jecalculation.command.commands.CCraft; import javax.annotation.ParametersAreNonnullByDefault; import java.util.HashMap; @@ -19,8 +19,8 @@ public class Commands { static { add(new CHelp()); add(new CState()); - add(new COreDict()); - add(new CUuid()); + add(new CMath()); + add(new CCraft()); } static void add(ISubCommand c) { diff --git a/src/main/java/me/towdium/jecalculation/command/commands/CUuid.java b/src/main/java/me/towdium/jecalculation/command/commands/CCraft.java similarity index 72% rename from src/main/java/me/towdium/jecalculation/command/commands/CUuid.java rename to src/main/java/me/towdium/jecalculation/command/commands/CCraft.java index 64110dc..4fabbce 100644 --- a/src/main/java/me/towdium/jecalculation/command/commands/CUuid.java +++ b/src/main/java/me/towdium/jecalculation/command/commands/CCraft.java @@ -1,6 +1,7 @@ package me.towdium.jecalculation.command.commands; import me.towdium.jecalculation.command.ISubCommand; +import me.towdium.jecalculation.data.Controller; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.entity.Entity; @@ -9,24 +10,24 @@ import net.minecraft.util.ChatComponentText; import javax.annotation.ParametersAreNonnullByDefault; /** - * Author: towdium - * Date: 17-10-15. + * Author: Towdium + * Date: 19-1-20 */ @ParametersAreNonnullByDefault -public class CUuid implements ISubCommand { +public class CCraft implements ISubCommand { @Override public String getName() { - return "uuid"; + return "craft"; } @Override public String getUsage(ICommandSender sender) { - return "/jeca uuid"; + return "/jeca craft"; } @Override public void execute(ICommandSender sender, String[] args) { - sender.addChatMessage(new ChatComponentText(sender.getCommandSenderName())); + Controller.openGuiCraft(); } } diff --git a/src/main/java/me/towdium/jecalculation/command/commands/COreDict.java b/src/main/java/me/towdium/jecalculation/command/commands/CMath.java similarity index 70% rename from src/main/java/me/towdium/jecalculation/command/commands/COreDict.java rename to src/main/java/me/towdium/jecalculation/command/commands/CMath.java index 75480a5..292a017 100644 --- a/src/main/java/me/towdium/jecalculation/command/commands/COreDict.java +++ b/src/main/java/me/towdium/jecalculation/command/commands/CMath.java @@ -1,8 +1,10 @@ package me.towdium.jecalculation.command.commands; import me.towdium.jecalculation.command.ISubCommand; +import me.towdium.jecalculation.data.Controller; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; +import net.minecraft.server.MinecraftServer; import net.minecraft.util.ChatComponentText; import net.minecraftforge.oredict.OreDictionary; @@ -10,23 +12,23 @@ import javax.annotation.ParametersAreNonnullByDefault; import java.util.Arrays; /** - * Author: towdium - * Date: 17-9-11. + * Author: Towdium + * Date: 19-1-21 */ @ParametersAreNonnullByDefault -public class COreDict implements ISubCommand { +public class CMath implements ISubCommand { @Override public String getName() { - return "ore"; + return "math"; } @Override public String getUsage(ICommandSender sender) { - return "/jeca ore"; + return "/jeca math"; } @Override public void execute(ICommandSender sender, String[] args) { - Arrays.stream(OreDictionary.getOreNames()).forEach(s -> sender.addChatMessage(new ChatComponentText(s))); + Controller.openGuiMath(); } } diff --git a/src/main/java/me/towdium/jecalculation/command/commands/CState.java b/src/main/java/me/towdium/jecalculation/command/commands/CState.java index faadb69..d3a4ec2 100644 --- a/src/main/java/me/towdium/jecalculation/command/commands/CState.java +++ b/src/main/java/me/towdium/jecalculation/command/commands/CState.java @@ -26,6 +26,6 @@ public class CState implements ISubCommand { @Override public void execute(ICommandSender sender, String[] args) { - sender.addChatMessage(new ChatComponentTranslation(getKey("desc"), "inactive")); + sender.addChatMessage(new ChatComponentTranslation("inactive")); } } diff --git a/src/main/java/me/towdium/jecalculation/data/Controller.java b/src/main/java/me/towdium/jecalculation/data/Controller.java index 063a339..d2b38a8 100644 --- a/src/main/java/me/towdium/jecalculation/data/Controller.java +++ b/src/main/java/me/towdium/jecalculation/data/Controller.java @@ -4,15 +4,18 @@ import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import me.towdium.jecalculation.JecaConfig; import me.towdium.jecalculation.JustEnoughCalculation; -import me.towdium.jecalculation.data.label.ILabel; -import me.towdium.jecalculation.data.structure.Recents; -import me.towdium.jecalculation.data.structure.Recipe; -import me.towdium.jecalculation.data.structure.Recipes; +import me.towdium.jecalculation.data.structure.*; +import me.towdium.jecalculation.gui.JecaGui; +import me.towdium.jecalculation.gui.guis.GuiCraft; +import me.towdium.jecalculation.gui.guis.GuiMath; import me.towdium.jecalculation.utils.Utilities; import me.towdium.jecalculation.utils.wrappers.Pair; +import net.minecraft.client.Minecraft; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.ChatComponentTranslation; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import java.io.File; import java.util.*; @@ -28,16 +31,28 @@ import java.util.stream.Stream; @SideOnly(Side.CLIENT) public class Controller { public static final String KEY_RECIPES = "recipes"; - public static final String KEY_RECENTS = "recents"; - public static final String KEY_AMOUNT = "amount"; - public static final String KEY_INVENTORY = "inventory"; - static Recipes recipesClient; - static Recents recentsClient; - static boolean inventoryClient; - static String amountClient; + public static final String KEY_MATH = "math"; + public static final String KEY_CRAFT = "craft"; + public static final String KEY_PLAYER = "player"; - static Recipes getRecord() { - return recipesClient; + static RecordPlayer rPlayerServer; + static RecordPlayer rPlayerClient; + static RecordCraft rCraftClient; + static RecordMath rMathClient; + + public static void setRecordsServer(RecordPlayer r) { + rPlayerServer = r; + } + + public static boolean isServerActive() { + return rPlayerServer != null; + } + + static Recipes getRecipes() { + if (isServerActive()) + return rPlayerServer.recipes; + else + return rPlayerClient.recipes; } static Optional getStack() { @@ -62,7 +77,7 @@ public class Controller { public static void inport(Recipes recipes, String group) { ArrayList buffer = new ArrayList<>(); recipes.getGroup(group) - .forEach(i -> getRecord().getGroup(group).stream().filter(j -> j.equals(i)).findAny().orElseGet(() -> { + .forEach(i -> getRecipes().getGroup(group).stream().filter(j -> j.equals(i)).findAny().orElseGet(() -> { buffer.add(i); return null; })); @@ -72,82 +87,85 @@ public class Controller { public static File export(String group) { File f = JecaConfig.getDataFile(group); - Utilities.Json.write(getRecord().serialize(Collections.singleton(group)), f); + Utilities.Json.write(getRecipes().serialize(Collections.singleton(group)), f); return f; } public static File export() { File f = JecaConfig.getDataFile("groups"); - Utilities.Json.write(getRecord().serialize(), f); + Utilities.Json.write(getRecipes().serialize(), f); return f; } + @Nullable + public static String getLast() { + return isServerActive() ? rPlayerServer.last : rPlayerClient.last; + } + + static void setLast(String last) { + if (isServerActive()) rPlayerServer.last = last; + else rPlayerServer.last = last; + } + public static List getGroups() { - return getRecord().getGroups(); + return getRecipes().getGroups(); } public static void addRecipe(String group, Recipe recipe) { - getRecord().add(group, recipe); + getRecipes().add(group, recipe); + setLast(group); writeToLocal(); } public static void setRecipe(String group, int index, Recipe recipe) { - getRecord().set(group, index, recipe); + getRecipes().set(group, index, recipe); + setLast(group); writeToLocal(); } public static void removeRecipe(String group, int index) { - getRecord().remove(group, index); + getRecipes().remove(group, index); + setLast(group); writeToLocal(); } public static Recipe getRecipe(String group, int index) { - return getRecord().getRecipe(group, index); + return getRecipes().getRecipe(group, index); } public static Stream>> stream() { - return getRecord().stream(); + return getRecipes().stream(); } public static Recipes.RecipeIterator recipeIterator() { - return getRecord().recipeIterator(); + return getRecipes().recipeIterator(); } - public static List getRecipes() { - return getRecord().getRecipes(); + public static Recipes.RecipeIterator recipeIterator(String group) { + return getRecipes().recipeIterator(group); } - public static List getRecipes(String group) { - return getRecord().getRecipes(group); + public static void setRMath(RecordMath math) { + rMathClient = math; } - public static String getAmount() { - return amountClient; + public static RecordCraft getRCraft() { + return rCraftClient; + } - public static void setAmount(String amount) { - amountClient = amount; + public static void setRCraft(RecordCraft rc) { + rCraftClient = rc; + } - public static boolean getDetectInv() { - return inventoryClient; + public static RecordMath getRMath() { + return rMathClient; } - public static void setDetectInv(boolean b) { - inventoryClient = b; - } - - public static List getRecent() { - return recentsClient.getRecords(); - } - - public static void setRecent(ILabel label, boolean replace) { - recentsClient.push(label, replace); - writeToLocal(); - } public static boolean hasDuplicate(Recipe r) { - return getRecord().hasDuplicate(r); + return getRecipes().hasDuplicate(r); } public static void loadFromLocal() { @@ -156,25 +174,35 @@ public class Controller { File file = JecaConfig.recordFile; NBTTagCompound nbt = Utilities.Json.read(file); if (nbt != null) { - recipesClient = nbt.hasKey(KEY_RECIPES) ? new Recipes(nbt.getCompoundTag(KEY_RECIPES)) : new Recipes(); - recentsClient = nbt.hasKey(KEY_RECENTS) ? new Recents(nbt.getTagList(KEY_RECENTS, 10)) : new Recents(); - amountClient = nbt.hasKey(KEY_AMOUNT) ? nbt.getString(KEY_AMOUNT) : ""; - inventoryClient = !nbt.hasKey(KEY_INVENTORY) || nbt.getBoolean(KEY_INVENTORY); + rCraftClient = new RecordCraft(nbt.getCompoundTag(KEY_CRAFT)); + rMathClient = new RecordMath(nbt.getCompoundTag(KEY_MATH)); + rPlayerClient = nbt.hasKey(KEY_PLAYER) ? new RecordPlayer(nbt.getCompoundTag(KEY_PLAYER)) : new RecordPlayer(); } else { - recipesClient = new Recipes(); - recentsClient = new Recents(); - amountClient = ""; - inventoryClient = true; + rPlayerClient = new RecordPlayer(); + rCraftClient = new RecordCraft(new NBTTagCompound()); + rMathClient = new RecordMath(new NBTTagCompound()); } } public static void writeToLocal() { File file = JecaConfig.recordFile; NBTTagCompound nbt = new NBTTagCompound(); - nbt.setTag(KEY_RECENTS, recentsClient.serialize()); - nbt.setTag(KEY_RECIPES, recipesClient.serialize()); - nbt.setString(KEY_AMOUNT, amountClient); - nbt.setBoolean(KEY_INVENTORY, inventoryClient); + nbt.setTag(KEY_CRAFT, rCraftClient.serialize()); + nbt.setTag(KEY_PLAYER, rPlayerClient.serialize()); + nbt.setTag(KEY_MATH, rMathClient.serialize()); Utilities.Json.write(nbt, file); } + + public static void openGuiCraft() { + if (!Controller.isServerActive()) JecaGui.displayGui(true, true, new GuiCraft()); + else Minecraft.getMinecraft().thePlayer.addChatMessage( + new ChatComponentTranslation("jecalculation.chat.server_mode")); + } + + public static void openGuiMath() { + if (!Controller.isServerActive()) JecaGui.displayGui(true, true, new GuiMath()); + else Minecraft.getMinecraft().thePlayer.addChatMessage( + new ChatComponentTranslation("jecalculation.chat.server_mode")); + } + } diff --git a/src/main/java/me/towdium/jecalculation/data/structure/Recents.java b/src/main/java/me/towdium/jecalculation/data/structure/Recents.java deleted file mode 100644 index 385db0d..0000000 --- a/src/main/java/me/towdium/jecalculation/data/structure/Recents.java +++ /dev/null @@ -1,46 +0,0 @@ -package me.towdium.jecalculation.data.structure; - -import me.towdium.jecalculation.data.label.ILabel; -import me.towdium.jecalculation.polyfill.NBTHelper; -import me.towdium.jecalculation.utils.Utilities; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; - -import javax.annotation.ParametersAreNonnullByDefault; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; - -@ParametersAreNonnullByDefault -public class Recents { - Utilities.Recent record = new Utilities.Recent<>((a, b) -> a == ILabel.EMPTY || a.equals(b), 9); - - public Recents(NBTTagList nbt) { - List ls = StreamSupport.stream(NBTHelper.spliterator(nbt), false) - .filter(n -> n instanceof NBTTagCompound) - .map(n -> ILabel.SERIALIZER.deserialize((NBTTagCompound) n)) - .collect(Collectors.toList()); - new Utilities.ReversedIterator<>(ls).forEachRemaining(l -> record.push(l, false)); - } - - public Recents() { - } - - public void push(ILabel label, boolean replace) { - record.push(label, replace); - } - - public ILabel getLatest() { - return record.toList().get(0); - } - - public List getRecords() { - return record.toList(); - } - - public NBTTagList serialize() { - NBTTagList ret = new NBTTagList(); - record.toList().forEach(l -> ret.appendTag(ILabel.SERIALIZER.serialize(l))); - return ret; - } -} diff --git a/src/main/java/me/towdium/jecalculation/data/structure/Recipes.java b/src/main/java/me/towdium/jecalculation/data/structure/Recipes.java index b357dd8..3769aae 100644 --- a/src/main/java/me/towdium/jecalculation/data/structure/Recipes.java +++ b/src/main/java/me/towdium/jecalculation/data/structure/Recipes.java @@ -135,10 +135,24 @@ public class Recipes { return new RecipeIterator(); } + public RecipeIterator recipeIterator(String group) { + return new RecipeIterator(group); + } + public class RecipeIterator implements Iterator { - Iterator>> i = records.entrySet().iterator(); + Iterator>> i; Iterator j; + public RecipeIterator() { + i = records.entrySet().iterator(); + } + + public RecipeIterator(String group) { + HashMap> tmp = new HashMap<>(); + tmp.put(group, records.get(group)); + i = tmp.entrySet().iterator(); + } + @Override public boolean hasNext() { while (j == null || !j.hasNext()) { diff --git a/src/main/java/me/towdium/jecalculation/data/structure/RecordCraft.java b/src/main/java/me/towdium/jecalculation/data/structure/RecordCraft.java new file mode 100644 index 0000000..c57c466 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/data/structure/RecordCraft.java @@ -0,0 +1,58 @@ +package me.towdium.jecalculation.data.structure; + +import me.towdium.jecalculation.data.label.ILabel; +import me.towdium.jecalculation.utils.Utilities; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +/** + * Author: Towdium + * Date: 19-1-20 + */ +public class RecordCraft { + public static final String KEY_RECENTS = "recents"; + public static final String KEY_AMOUNT = "amount"; + public static final String KEY_INVENTORY = "inventory"; + + Utilities.Recent record = new Utilities.Recent<>((a, b) -> + a == ILabel.EMPTY || a.equals(b), 9); + public String amount; + public boolean inventory; + + public RecordCraft(NBTTagCompound nbt) { + List ls = StreamSupport.stream(nbt.getTagList(KEY_RECENTS, 10).spliterator(), false) + .filter(n -> n instanceof NBTTagCompound) + .map(n -> ILabel.SERIALIZER.deserialize((NBTTagCompound) n)) + .collect(Collectors.toList()); + new Utilities.ReversedIterator<>(ls).forEachRemaining(l -> record.push(l, false)); + amount = nbt.getString(KEY_AMOUNT); + inventory = nbt.getBoolean(KEY_INVENTORY); + } + + public boolean push(ILabel label, boolean replace) { + return record.push(label, replace); + } + + public ILabel getLatest() { + return record.size() == 0 ? ILabel.EMPTY : record.toList().get(0); + } + + public List getHistory() { + return record.size() > 1 ? record.toList().subList(1, record.size()) : new ArrayList<>(); + } + + public NBTTagCompound serialize() { + NBTTagCompound ret = new NBTTagCompound(); + ret.setBoolean(KEY_INVENTORY, inventory); + ret.setString(KEY_AMOUNT, amount); + NBTTagList recent = new NBTTagList(); + record.toList().forEach(l -> recent.appendTag(ILabel.SERIALIZER.serialize(l))); + ret.setTag(KEY_RECENTS, recent); + return ret; + } +} diff --git a/src/main/java/me/towdium/jecalculation/data/structure/RecordMath.java b/src/main/java/me/towdium/jecalculation/data/structure/RecordMath.java new file mode 100644 index 0000000..aa7fc60 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/data/structure/RecordMath.java @@ -0,0 +1,93 @@ +package me.towdium.jecalculation.data.structure; + +import me.towdium.jecalculation.utils.IllegalPositionException; +import net.minecraft.nbt.NBTTagCompound; + +import java.math.BigDecimal; +import java.math.MathContext; +import java.math.RoundingMode; +import java.util.LinkedList; +import java.util.List; + +/** + * Author: Towdium + * Date: 19-1-20 + */ +public class RecordMath { + public static MathContext context = new MathContext(7, RoundingMode.HALF_UP); + public static final int DOT_NONE = -1; + static final String KEY_CURRENT = "current"; + static final String KEY_LAST = "last"; + static final String KEY_OPERATOR = "operator"; + static final String KEY_STATE = "state"; + + public BigDecimal last; + public State state; + public Operator operator; + String current; + + public enum State {INPUT, OUTPUT, ERROR} + + public enum Operator { + EQUALS, PLUS, MINUS, TIMES, DIVIDE; + + public BigDecimal operate(BigDecimal a, BigDecimal b) { + switch (this) { + case PLUS: return a.add(b); + case MINUS: return a.subtract(b); + case TIMES: return a.multiply(b); + case DIVIDE: return a.divide(b, context); + case EQUALS: return b; + default: throw new IllegalPositionException(); + } + } + } + + public RecordMath(State state, Operator operator, BigDecimal last, + boolean sign, int dot, List numbers) { + this.state = state; + this.operator = operator; + this.last = last; + StringBuilder sb = new StringBuilder(); + if (!sign) sb.append('-'); + numbers.forEach(i -> sb.append(i.toString())); + if (dot != DOT_NONE) sb.insert(sb.length() - dot, '.'); + current = sb.toString(); + } + + public RecordMath(NBTTagCompound nbt) { + state = State.values()[nbt.getInteger(KEY_STATE)]; + operator = Operator.values()[nbt.getInteger(KEY_OPERATOR)]; + last = nbt.hasKey(KEY_LAST) ? new BigDecimal(nbt.getString(KEY_LAST)) : BigDecimal.ZERO; + current = nbt.getString(KEY_CURRENT); + } + + public NBTTagCompound serialize() { + NBTTagCompound ret = new NBTTagCompound(); + ret.setString(KEY_CURRENT, current); + ret.setString(KEY_LAST, last.toString()); + ret.setInteger(KEY_OPERATOR, operator.ordinal()); + ret.setInteger(KEY_STATE, state.ordinal()); + return ret; + } + + public boolean getSign() { + return current == null || current.isEmpty() || current.charAt(0) != '-'; + } + + public LinkedList getNumbers() { + LinkedList ret = new LinkedList<>(); + if (current == null) return ret; + for (int i = 0; i < current.length(); i++) { + char ch = current.charAt(i); + if (ch != '-' && ch != '.') ret.add(new BigDecimal(ch - '0')); + } + return ret; + } + + public int getDot() { + if (current == null) return DOT_NONE; + int index = current.indexOf('.'); + return index == -1 ? DOT_NONE : current.length() - index - 1; + } +} diff --git a/src/main/java/me/towdium/jecalculation/data/structure/RecordPlayer.java b/src/main/java/me/towdium/jecalculation/data/structure/RecordPlayer.java new file mode 100644 index 0000000..cf3e6ff --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/data/structure/RecordPlayer.java @@ -0,0 +1,31 @@ +package me.towdium.jecalculation.data.structure; + +import net.minecraft.nbt.NBTTagCompound; + +/** + * Author: Towdium + * Date: 19-1-21 + */ +public class RecordPlayer { + public static final String KEY_RECIPES = "recipes"; + public static final String KEY_LAST = "last"; + + public Recipes recipes; + public String last; + + public RecordPlayer() { + recipes = new Recipes(); + } + + public RecordPlayer(NBTTagCompound nbt) { + recipes = new Recipes(nbt.getCompoundTag(KEY_RECIPES)); + last = nbt.getString(KEY_LAST); + } + + public NBTTagCompound serialize() { + NBTTagCompound ret = new NBTTagCompound(); + if (last != null) ret.setString(KEY_LAST, last); + ret.setTag(KEY_RECIPES, recipes.serialize()); + return ret; + } +} diff --git a/src/main/java/me/towdium/jecalculation/event/handlers/FMLBusEventHandler.java b/src/main/java/me/towdium/jecalculation/event/handlers/FMLBusEventHandler.java index 93491d5..abead3a 100644 --- a/src/main/java/me/towdium/jecalculation/event/handlers/FMLBusEventHandler.java +++ b/src/main/java/me/towdium/jecalculation/event/handlers/FMLBusEventHandler.java @@ -9,10 +9,8 @@ import me.towdium.jecalculation.JustEnoughCalculation; import me.towdium.jecalculation.data.Controller; import me.towdium.jecalculation.data.label.labels.LPlaceholder; import me.towdium.jecalculation.gui.JecaGui; -import me.towdium.jecalculation.gui.guis.GuiCalculator; +import me.towdium.jecalculation.gui.guis.GuiCraft; import me.towdium.jecalculation.network.ClientHandler; -import net.minecraft.client.Minecraft; -import net.minecraft.util.ChatComponentTranslation; /** * Author: towdium @@ -23,9 +21,8 @@ public class FMLBusEventHandler { @SideOnly(Side.CLIENT) @SubscribeEvent(receiveCanceled = true) public void onKey(InputEvent.KeyInputEvent event) { - if (ClientHandler.keyOpenGui.isPressed()) { - JecaGui.displayGui(true, true, new GuiCalculator()); - } + if (ClientHandler.keyOpenGuiCraft.isPressed()) Controller.openGuiCraft(); + if (ClientHandler.keyOpenGuiMath.isPressed()) Controller.openGuiMath(); } @SubscribeEvent diff --git a/src/main/java/me/towdium/jecalculation/gui/JecaGui.java b/src/main/java/me/towdium/jecalculation/gui/JecaGui.java index ae07640..88ddfdf 100644 --- a/src/main/java/me/towdium/jecalculation/gui/JecaGui.java +++ b/src/main/java/me/towdium/jecalculation/gui/JecaGui.java @@ -11,6 +11,7 @@ import me.towdium.jecalculation.nei.NEIPlugin; import me.towdium.jecalculation.polyfill.mc.client.renderer.GlStateManager; import me.towdium.jecalculation.utils.Utilities; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.RenderHelper; @@ -152,6 +153,49 @@ public class JecaGui extends GuiContainer { GlStateManager.enableDepth(); } + @Override + // modified from vanilla + public void drawHoveringText(List lines, int x, int y, FontRenderer font) { + List textLines = (List) lines; + if (!textLines.isEmpty()) { + GlStateManager.disableRescaleNormal(); + RenderHelper.disableStandardItemLighting(); + GlStateManager.disableLighting(); + GlStateManager.disableDepth(); + int i = 0; + for (String s : textLines) { + int j = this.fontRendererObj.getStringWidth(s); + if (j > i) i = j; + } + int l1 = x + 12; + int i2 = y - 12; + int k = 8 + (textLines.size() - 1) * 10; + if (l1 + i > this.width) l1 -= 28 + i; + if (i2 + k + 6 > this.height) i2 = this.height - k - 6; + zLevel = 300.0F; + itemRender.zLevel = 300.0F; + drawGradientRect(l1 - 3, i2 - 4, l1 + i + 3, i2 - 3, -267386864, -267386864); + drawGradientRect(l1 - 3, i2 + k + 3, l1 + i + 3, i2 + k + 4, -267386864, -267386864); + drawGradientRect(l1 - 3, i2 - 3, l1 + i + 3, i2 + k + 3, -267386864, -267386864); + drawGradientRect(l1 - 4, i2 - 3, l1 - 3, i2 + k + 3, -267386864, -267386864); + drawGradientRect(l1 + i + 3, i2 - 3, l1 + i + 4, i2 + k + 3, -267386864, -267386864); + drawGradientRect(l1 - 3, i2 - 3 + 1, l1 - 3 + 1, i2 + k + 3 - 1, 1347420415, 1344798847); + drawGradientRect(l1 + i + 2, i2 - 3 + 1, l1 + i + 3, i2 + k + 3 - 1, 1347420415, 1344798847); + drawGradientRect(l1 - 3, i2 - 3, l1 + i + 3, i2 - 3 + 1, 1347420415, 1347420415); + drawGradientRect(l1 - 3, i2 + k + 2, l1 + i + 3, i2 + k + 3, 1344798847, 1344798847); + for (String s1 : textLines) { + fontRendererObj.drawStringWithShadow(s1, l1, i2, -1); + i2 += 10; + } + zLevel = 0.0F; + itemRender.zLevel = 0.0F; + GlStateManager.enableLighting(); + GlStateManager.enableDepth(); + RenderHelper.enableStandardItemLighting(); + GlStateManager.enableRescaleNormal(); + } + } + /** * This function handles events within the interface. * Specifically, handles mouse wheel within interface. @@ -292,7 +336,7 @@ public class JecaGui extends GuiContainer { boolean unicode = fontRendererObj.getUnicodeFlag(); if (f.half) fontRendererObj.setUnicodeFlag(false); GlStateManager.pushMatrix(); - GlStateManager.translate(unicode && f.half ? xPos - 5 : xPos, yPos, 0); + GlStateManager.translate(xPos, yPos, 0); if (f.half) GlStateManager.scale(0.5, 0.5, 1); r.run(); GlStateManager.popMatrix(); @@ -349,7 +393,12 @@ public class JecaGui extends GuiContainer { } public int getTextWidth(String s) { - return (int) Math.ceil(getCurrent().fontRendererObj.getStringWidth(s) * (half ? 0.5f : 1)); + FontRenderer fr = getCurrent().fontRendererObj; + boolean flag = fr.getUnicodeFlag(); + if (half) fr.setUnicodeFlag(false); + int ret = (int) Math.ceil(fr.getStringWidth(s) * (half ? 0.5f : 1)); + fr.setUnicodeFlag(flag); + return ret; } public int getTextHeight() { diff --git a/src/main/java/me/towdium/jecalculation/gui/Resource.java b/src/main/java/me/towdium/jecalculation/gui/Resource.java index 2981a56..15ddbbc 100644 --- a/src/main/java/me/towdium/jecalculation/gui/Resource.java +++ b/src/main/java/me/towdium/jecalculation/gui/Resource.java @@ -104,6 +104,26 @@ public class Resource { public static final Resource WGT_SCROLL = new Resource(133, 132, 14, 17); public static final Resource WGT_HELP_F = new Resource(147, 132, 20, 20); public static final Resource WGT_HELP_N = new Resource(167, 132, 20, 20); + public static final Resource WGT_LCD_UL_F = new Resource(187, 132, 4, 16); + public static final Resource WGT_LCD_UR_F = new Resource(191, 132, 4, 16); + public static final Resource WGT_LCD_LL_F = new Resource(195, 132, 4, 16); + public static final Resource WGT_LCD_LR_F = new Resource(199, 132, 4, 16); + public static final Resource WGT_LCD_H_F = new Resource(187, 148, 12, 4); + public static final Resource WGT_LCD_DO_F = new Resource(199, 148, 4, 4); + public static final Resource WGT_LCD_UL_N = new Resource(203, 132, 4, 16); + public static final Resource WGT_LCD_UR_N = new Resource(207, 132, 4, 16); + public static final Resource WGT_LCD_LL_N = new Resource(211, 132, 4, 16); + public static final Resource WGT_LCD_LR_N = new Resource(215, 132, 4, 16); + public static final Resource WGT_LCD_H_N = new Resource(203, 148, 12, 4); + public static final Resource WGT_LCD_DO_N = new Resource(215, 148, 4, 4); + public static final Resource WGT_LCD_P_F = new Resource(219, 132, 7, 7); + public static final Resource WGT_LCD_M_F = new Resource(226, 132, 7, 7); + public static final Resource WGT_LCD_T_F = new Resource(233, 132, 7, 7); + public static final Resource WGT_LCD_D_F = new Resource(240, 132, 7, 7); + public static final Resource WGT_LCD_P_N = new Resource(219, 139, 7, 7); + public static final Resource WGT_LCD_M_N = new Resource(226, 139, 7, 7); + public static final Resource WGT_LCD_T_N = new Resource(233, 139, 7, 7); + public static final Resource WGT_LCD_D_N = new Resource(240, 139, 7, 7); public static final Resource ICN_RECENT_N = getIcon(0, 7); public static final Resource ICN_RECENT_F = getIcon(0, 6); @@ -184,6 +204,10 @@ public class Resource { this(xPos, yPos, xSize, ySize, location); } + public Resource sub(int xPos, int yPos, int xSize, int ySize) { + return new Resource(this.xPos + xPos, this.yPos + yPos, xSize, ySize); + } + public int getXPos() { return xPos; } diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiCalculator.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiCraft.java similarity index 80% rename from src/main/java/me/towdium/jecalculation/gui/guis/GuiCalculator.java rename to src/main/java/me/towdium/jecalculation/gui/guis/GuiCraft.java index 7962c89..907b9c9 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/GuiCalculator.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiCraft.java @@ -5,6 +5,7 @@ import cpw.mods.fml.relauncher.SideOnly; import me.towdium.jecalculation.data.Controller; import me.towdium.jecalculation.data.label.ILabel; import me.towdium.jecalculation.data.structure.CostList; +import me.towdium.jecalculation.data.structure.RecordCraft; import me.towdium.jecalculation.gui.JecaGui; import me.towdium.jecalculation.gui.Resource; import me.towdium.jecalculation.gui.widgets.*; @@ -27,13 +28,14 @@ import java.util.stream.Stream; */ @ParametersAreNonnullByDefault @SideOnly(Side.CLIENT) -public class GuiCalculator extends WContainer implements IGui { +public class GuiCraft extends WContainer implements IGui { Mode mode = Mode.INPUT; CostList.Calculator calculator = null; + RecordCraft record = Controller.getRCraft(); WLabelGroup recent = new WLabelGroup(7, 31, 8, 1, WLabel.Mode.PICKER) .setListener((i, v) -> JecaGui.getCurrent().hand = i.get(v)); WLabelScroll result = new WLabelScroll(7, 87, 8, 4, WLabel.Mode.RESULT, true); - WButton steps = new WButtonIcon(64, 62, 20, 20, Resource.BTN_LIST, "calculator.step") + WButton steps = new WButtonIcon(64, 62, 20, 20, Resource.BTN_LIST, "craft.step") .setListener(i -> setMode(Mode.STEPS)); WButton catalyst = new WButtonIcon(45, 62, 20, 20, Resource.BTN_CAT, "common.catalyst") .setListener(i -> setMode(Mode.CATALYST)); @@ -41,38 +43,43 @@ public class GuiCalculator extends WContainer implements IGui { .setListener(i -> setMode(Mode.OUTPUT)); WButton input = new WButtonIcon(7, 62, 20, 20, Resource.BTN_IN, "common.input") .setListener(i -> setMode(Mode.INPUT)); - WTextField amount = new WTextField(60, 7, 65).setText(Controller.getAmount()).setListener(i -> { - Controller.setAmount(i.getText()); + + WLabel label = new WLabel(31, 7, 20, 20, WLabel.Mode.SELECTOR).setListener((i, v) -> refreshLabel(v, false, true)); + WButton invE = new WButtonIcon(149, 62, 20, 20, Resource.BTN_INV_E, "craft.inventory_enabled"); + WButton invD = new WButtonIcon(149, 62, 20, 20, Resource.BTN_INV_D, "craft.inventory_disabled"); + WTextField amount = new WTextField(60, 7, 65).setText(record.amount).setListener(i -> { + record.amount = i.getText(); + Controller.setRCraft(record); refreshCalculator(); }); - WLabel label = new WLabel(31, 7, 20, 20, WLabel.Mode.SELECTOR).setListener((i, v) -> refreshLabel(v, false)); - WButton invE = new WButtonIcon(149, 62, 20, 20, Resource.BTN_INV_E, "calculator.inventory_enabled"); - WButton invD = new WButtonIcon(149, 62, 20, 20, Resource.BTN_INV_D, "calculator.inventory_disabled"); - public GuiCalculator() { - add(new WHelp("calculator")); + + public GuiCraft() { + add(new WHelp("craft")); add(new WPanel()); - add(new WButtonIcon(7, 7, 20, 20, Resource.BTN_LABEL, "calculator.label") + add(new WButtonIcon(7, 7, 20, 20, Resource.BTN_LABEL, "craft.label") .setListener(i -> JecaGui.displayGui(new GuiLabel(l -> { JecaGui.displayParent(); JecaGui.getCurrent().hand = l; })))); - add(new WButtonIcon(130, 7, 20, 20, Resource.BTN_NEW, "calculator.recipe") + add(new WButtonIcon(130, 7, 20, 20, Resource.BTN_NEW, "craft.recipe") .setListener(i -> JecaGui.displayGui(true, true, new GuiRecipe()))); - add(new WButtonIcon(149, 7, 20, 20, Resource.BTN_SEARCH, "calculator.search") + add(new WButtonIcon(149, 7, 20, 20, Resource.BTN_SEARCH, "craft.search") .setListener(i -> JecaGui.displayGui(new GuiSearch()))); add(new WText(53, 13, JecaGui.Font.PLAIN, "x")); add(new WLine(55)); - add(new WIcon(151, 31, 18, 18, Resource.ICN_RECENT, "calculator.history")); - add(recent, label, input, output, catalyst, steps, result, amount, Controller.getDetectInv() ? invE : invD); + add(new WIcon(151, 31, 18, 18, Resource.ICN_RECENT, "craft.history")); + add(recent, label, input, output, catalyst, steps, result, amount, record.inventory ? invE : invD); invE.setListener(i -> { - Controller.setDetectInv(false); + record.inventory = false; + Controller.setRCraft(record); remove(invE); add(invD); refreshCalculator(); }); invD.setListener(i -> { - Controller.setDetectInv(true); + record.inventory = true; + Controller.setRCraft(record); remove(invD); add(invE); refreshCalculator(); @@ -96,9 +103,8 @@ public class GuiCalculator extends WContainer implements IGui { } void refreshRecent() { - List labels = Controller.getRecent(); - label.setLabel(labels.isEmpty() ? ILabel.EMPTY : labels.get(0)); - recent.setLabel(labels.isEmpty() ? new ArrayList<>() : labels.subList(1, labels.size()), 0); + label.setLabel(record.getLatest()); + recent.setLabel(record.getHistory(), 0); } void refreshCalculator() { @@ -107,7 +113,7 @@ public class GuiCalculator extends WContainer implements IGui { long i = s.isEmpty() ? 1 : Long.parseLong(amount.getText()); amount.setColor(JecaGui.COLOR_TEXT_WHITE); List dest = Collections.singletonList(label.getLabel().copy().setAmount(i)); - CostList list = Controller.getDetectInv() ? new CostList(getInventory(), dest) : new CostList(dest); + CostList list = record.inventory ? new CostList(getInventory(), dest) : new CostList(dest); calculator = list.calculate(); } catch (NumberFormatException | ArithmeticException e) { amount.setColor(JecaGui.COLOR_TEXT_RED); @@ -149,11 +155,12 @@ public class GuiCalculator extends WContainer implements IGui { } } - private void refreshLabel(ILabel l, boolean replace) { - Controller.setRecent(l, replace); + private void refreshLabel(ILabel l, boolean replace, boolean suggest) { + boolean dup = record.push(l, replace); + Controller.setRCraft(record); refreshRecent(); refreshCalculator(); - if (findRecipe(l).isEmpty()) { + if (suggest && findRecipe(l).isEmpty()) { Pair, List> guess = ILabel.CONVERTER.guess(Collections.singletonList(l)); LinkedHashSet match = new LinkedHashSet<>(); List fuzzy = new ArrayList<>(); @@ -164,7 +171,7 @@ public class GuiCalculator extends WContainer implements IGui { }); match.addAll(fuzzy); List list = new ArrayList<>(match); - if (!match.isEmpty()) add(new Suggest(list.size() > 3 ? list.subList(0, 3) : list)); + if (!match.isEmpty()) add(new Suggest(list.size() > 3 ? list.subList(0, 3) : list, !dup)); } } @@ -180,12 +187,15 @@ public class GuiCalculator extends WContainer implements IGui { INPUT, OUTPUT, CATALYST, STEPS } class Suggest extends WContainer { - public Suggest(List labels) { + boolean replace; + + public Suggest(List labels, boolean replace) { + this.replace = replace; int width = labels.size() * 20; add(new WPanel(0 - width, 2, 56 + width, 30)); - add(new WLabel(31, 7, 20, 20, WLabel.Mode.SELECTOR).setLabel(label.getLabel()) - .setListener((i, v) -> refresh(v))); - add(new WIcon(5 - width, 7, 18, 20, Resource.ICN_HELP, "calculator.suggest")); + add(new WLabel(31, 7, 20, 20, WLabel.Mode.PICKER).setLabel(label.getLabel()) + .setListener((i, v) -> refresh(v))); + add(new WIcon(5 - width, 7, 18, 20, Resource.ICN_HELP, "craft.suggest")); add(new WLine(26, 7, 20, false)); for (int i = 0; i < labels.size(); i++) { add(new WLabel(3 - i * 20, 7, 20, 20, WLabel.Mode.PICKER).setLabel(labels.get(i)) @@ -194,13 +204,13 @@ public class GuiCalculator extends WContainer implements IGui { } public void refresh(ILabel l) { - GuiCalculator.this.remove(this); - refreshLabel(l, true); + GuiCraft.this.remove(this); + refreshLabel(l, replace, false); } @Override public boolean onClicked(JecaGui gui, int xMouse, int yMouse, int button) { - if (!super.onClicked(gui, xMouse, yMouse, button)) GuiCalculator.this.remove(this); + if (!super.onClicked(gui, xMouse, yMouse, button)) GuiCraft.this.remove(this); return true; } @@ -208,7 +218,7 @@ public class GuiCalculator extends WContainer implements IGui { public boolean onKey(JecaGui gui, char ch, int code) { if (!super.onKey(gui, ch, code)) { if (code == Keyboard.KEY_ESCAPE) { - GuiCalculator.this.remove(this); + GuiCraft.this.remove(this); return true; } else return false; } else return true; diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiDisambiguation.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiDisamb.java similarity index 92% rename from src/main/java/me/towdium/jecalculation/gui/guis/GuiDisambiguation.java rename to src/main/java/me/towdium/jecalculation/gui/guis/GuiDisamb.java index 75d3b8b..5f4ab51 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/GuiDisambiguation.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiDisamb.java @@ -19,12 +19,12 @@ import static me.towdium.jecalculation.gui.Resource.*; */ @ParametersAreNonnullByDefault @SideOnly(Side.CLIENT) -public class GuiDisambiguation extends IPicker.Impl implements IGui { +public class GuiDisamb extends IPicker.Impl implements IGui { protected WLabelScroll lsUp = new WLabelScroll(25, 48, 7, 3, WLabel.Mode.PICKER, true); protected WLabelScroll lsDown = new WLabelScroll(25, 105, 7, 3, WLabel.Mode.PICKER, true); protected List> record; - public GuiDisambiguation(List> record) { + public GuiDisamb(List> record) { this.record = record; WSwitcher switcher = new WSwitcher(7, 7, 162, this.record.size()).setListener(i -> setPage(i.getIndex())); ListenerValue consumer = (i, v) -> callback.accept(i.get(v).copy().multiply(-1)); diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiMath.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiMath.java new file mode 100644 index 0000000..c9f3966 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiMath.java @@ -0,0 +1,189 @@ +package me.towdium.jecalculation.gui.guis; + +import me.towdium.jecalculation.data.Controller; +import me.towdium.jecalculation.data.structure.RecordMath; +import me.towdium.jecalculation.data.structure.RecordMath.Operator; +import me.towdium.jecalculation.data.structure.RecordMath.State; +import me.towdium.jecalculation.gui.Resource; +import me.towdium.jecalculation.gui.widgets.*; +import me.towdium.jecalculation.utils.Utilities; +import org.lwjgl.input.Keyboard; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.math.BigDecimal; +import java.util.LinkedList; +import java.util.stream.Collectors; + +import static me.towdium.jecalculation.data.structure.RecordMath.DOT_NONE; +import static me.towdium.jecalculation.data.structure.RecordMath.context; + +/** + * Author: Towdium + * Date: 19-1-3 + */ +@ParametersAreNonnullByDefault +public class GuiMath extends WContainer implements IGui { + WLcd lcd = new WLcd(7); + LinkedList numbers; + BigDecimal last; + int dot; + boolean sign; + Operator operator; + State state; + + public GuiMath() { + add(new WHelp("math")); + add(new WPanel(), lcd); + add(new WButtonText(7, 67, 28, 20, "7").setListener(i -> append(7)) + .setKeyBind(Keyboard.KEY_7, Keyboard.KEY_NUMPAD7)); + add(new WButtonText(39, 67, 28, 20, "8").setListener(i -> append(8)) + .setKeyBind(Keyboard.KEY_8, Keyboard.KEY_NUMPAD8)); + add(new WButtonText(71, 67, 28, 20, "9").setListener(i -> append(9)) + .setKeyBind(Keyboard.KEY_9, Keyboard.KEY_NUMPAD9)); + add(new WButtonText(7, 91, 28, 20, "4").setListener(i -> append(4)) + .setKeyBind(Keyboard.KEY_4, Keyboard.KEY_NUMPAD4)); + add(new WButtonText(39, 91, 28, 20, "5").setListener(i -> append(5)) + .setKeyBind(Keyboard.KEY_5, Keyboard.KEY_NUMPAD5)); + add(new WButtonText(71, 91, 28, 20, "6").setListener(i -> append(6)) + .setKeyBind(Keyboard.KEY_6, Keyboard.KEY_NUMPAD6)); + add(new WButtonText(7, 115, 28, 20, "1").setListener(i -> append(1)) + .setKeyBind(Keyboard.KEY_1, Keyboard.KEY_NUMPAD1)); + add(new WButtonText(39, 115, 28, 20, "2").setListener(i -> append(2)) + .setKeyBind(Keyboard.KEY_2, Keyboard.KEY_NUMPAD2)); + add(new WButtonText(71, 115, 28, 20, "3").setListener(i -> append(3)) + .setKeyBind(Keyboard.KEY_3, Keyboard.KEY_NUMPAD3)); + add(new WButtonText(7, 139, 28, 20, "0").setListener(i -> append(0)) + .setKeyBind(Keyboard.KEY_0, Keyboard.KEY_NUMPAD0)); + add(new WButtonText(39, 139, 28, 20, "00").setListener(i -> { + append(0); + append(0); + })); + add(new WButtonText(71, 139, 28, 20, ".").setListener(i -> dot()) + .setKeyBind(Keyboard.KEY_PERIOD, Keyboard.KEY_DECIMAL)); + add(new WButtonIcon(109, 67, 28, 20, Resource.WGT_ARR_L).setListener(i -> remove()) + .setKeyBind(Keyboard.KEY_BACK)); + add(new WButtonText(141, 67, 28, 20, "+").setListener(i -> operate(Operator.PLUS)) + .setKeyBind(Keyboard.KEY_EQUALS, Keyboard.KEY_ADD)); + add(new WButtonText(109, 91, 28, 20, "C").setListener(i -> reset()).setKeyBind(Keyboard.KEY_DELETE)); + add(new WButtonText(141, 91, 28, 20, "-").setListener(i -> operate(Operator.MINUS)) + .setKeyBind(Keyboard.KEY_SUBTRACT, Keyboard.KEY_MINUS)); + add(new WButtonText(109, 115, 28, 44, "=").setListener(i -> operate(Operator.EQUALS)) + .setKeyBind(Keyboard.KEY_RETURN, Keyboard.KEY_NUMPADENTER, + Keyboard.KEY_NUMPADEQUALS)); + add(new WButtonText(141, 115, 28, 20, "x").setListener(i -> operate(Operator.TIMES)) + .setKeyBind(Keyboard.KEY_APOSTROPHE, Keyboard.KEY_MULTIPLY)); + add(new WButtonText(141, 139, 28, 20, "/").setListener(i -> operate(Operator.DIVIDE)) + .setKeyBind(Keyboard.KEY_SLASH, Keyboard.KEY_DIVIDE)); + add(new WLine(61), new WLine(103, 61, 98, false), new WLine.Joint(103, 61, false, true, true, true)); + RecordMath recordMath = Controller.getRMath(); + state = recordMath.state; + operator = recordMath.operator; + last = recordMath.last; + dot = recordMath.getDot(); + sign = recordMath.getSign(); + numbers = recordMath.getNumbers(); + print(); + } + + private void print() { + if (state == State.INPUT) { + String s = numbers.isEmpty() ? "0" : numbers.stream().map(Object::toString).collect(Collectors.joining()); + if (!sign) + s = '-' + Utilities.repeat(" ", 6 - s.length()) + s; + lcd.text = s; + lcd.dot = dot; + } else if (state == State.ERROR) { + lcd.text = "Error"; + lcd.dot = DOT_NONE; + } else if (state == State.OUTPUT) { + lcd.print(last); + } + lcd.operator = operator; + } + + private void append(int i) { + if (state != State.INPUT) + state = State.INPUT; + if ((i != 0 || !numbers.isEmpty()) && numbers.size() < (sign ? 7 : 6)) { + numbers.add(new BigDecimal(i)); + if (dot != DOT_NONE) + dot++; + } + print(); + sync(); + } + + private void sync() { + Controller.setRMath(new RecordMath(state, operator, last, sign, dot, numbers)); + } + + private void operate(Operator operator) { + if (state == State.INPUT && numbers.isEmpty() && operator == Operator.MINUS) + sign = !sign; + else { + if (state == State.INPUT) { + try { + last = this.operator.operate(last, convert()); + } catch (ArithmeticException e) { + state = State.ERROR; + this.operator = Operator.EQUALS; + print(); + sync(); + return; + } finally { + numbers.clear(); + dot = DOT_NONE; + sign = true; + } + } + this.operator = operator; + state = State.OUTPUT; + } + print(); + sync(); + } + + private void dot() { + if (state != State.INPUT) + state = State.INPUT; + if (numbers.size() == 0) + numbers.add(BigDecimal.ZERO); + dot = 0; + print(); + sync(); + } + + + private void remove() { + if (state != State.INPUT) + reset(); + if (numbers.size() > 0) { + numbers.removeLast(); + if (dot != DOT_NONE) + dot--; + } + print(); + sync(); + } + + private void reset() { + last = BigDecimal.ZERO; + operator = Operator.EQUALS; + numbers.clear(); + dot = DOT_NONE; + sign = true; + state = State.INPUT; + print(); + sync(); + } + + private BigDecimal convert() { + BigDecimal f = new BigDecimal(0); + for (BigDecimal i : numbers) + f = f.add(i).multiply(BigDecimal.TEN); + f = f.divide(BigDecimal.TEN.pow(Math.max(dot, 0) + 1), context); + if (!sign) + f = f.negate(); + return f; + } +} diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipe.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipe.java index 9e14f77..8983955 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipe.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiRecipe.java @@ -1,6 +1,5 @@ package me.towdium.jecalculation.gui.guis; -import codechicken.nei.PositionedStack; import codechicken.nei.recipe.IRecipeHandler; import me.towdium.jecalculation.data.Controller; import me.towdium.jecalculation.data.label.ILabel; @@ -16,7 +15,7 @@ import net.minecraft.item.ItemStack; import java.util.*; import java.util.stream.Collectors; -import java.util.stream.Stream; + import static me.towdium.jecalculation.gui.Resource.*; /** @@ -41,13 +40,14 @@ public class GuiRecipe extends WContainer implements IGui { refresh(); }); WButton buttonDisamb = new WButtonIcon(121, 31, 20, 20, BTN_DISAMB, "recipe.disamb").setListener(i -> { - if (disambiguation != null) JecaGui.displayGui(new GuiDisambiguation(new ArrayList<>(disambiguation.values())) + if (disambiguation != null) JecaGui.displayGui(new GuiDisamb(new ArrayList<>(disambiguation.values())) .setCallback(l -> { JecaGui.displayParent(); JecaGui.getCurrent().hand = l; })); }); WButton buttonClear = new WButtonIcon(64, 31, 20, 20, BTN_DEL, "recipe.clear").setListener(i -> clear()); + // check duplicate and valid WButton buttonCopy = new WButtonIcon(83, 31, 20, 20, BTN_COPY, "recipe.copy").setListener(i -> { Controller.addRecipe(switcherGroup.getText(), toRecipe()); JecaGui.displayParent(); @@ -101,6 +101,10 @@ public class GuiRecipe extends WContainer implements IGui { add(new WLine(57)); add(groupInput, groupCatalyst, groupOutput, switcherGroup); if (switcherGroup.getTexts().isEmpty()) switcherGroup.setTemp(Utilities.I18n.get("gui.common.default")); + String last = Controller.getLast(); + int index = -1; + if (last != null) index = switcherGroup.getTexts().indexOf(last); + if (index != -1) switcherGroup.setIndex(index); setNewGroup(false); buttonCopy.setDisabled(true); buttonDel.setDisabled(true); diff --git a/src/main/java/me/towdium/jecalculation/gui/guis/GuiSearch.java b/src/main/java/me/towdium/jecalculation/gui/guis/GuiSearch.java index 0348f85..7541ddb 100644 --- a/src/main/java/me/towdium/jecalculation/gui/guis/GuiSearch.java +++ b/src/main/java/me/towdium/jecalculation/gui/guis/GuiSearch.java @@ -58,7 +58,7 @@ public class GuiSearch extends WContainer implements IGui { .forEach(add); } else { String s = group.getText(); - Streams.mapWithIndex(Controller.getRecipes(s).stream(), (j, k) -> new Trio<>(j, s, (int) k)) + Streams.mapWithIndex(Controller.recipeIterator(s).stream(), (j, k) -> new Trio<>(j, s, (int) k)) .forEach(add); } } diff --git a/src/main/java/me/towdium/jecalculation/gui/widgets/WButton.java b/src/main/java/me/towdium/jecalculation/gui/widgets/WButton.java index deb75cb..459e97f 100644 --- a/src/main/java/me/towdium/jecalculation/gui/widgets/WButton.java +++ b/src/main/java/me/towdium/jecalculation/gui/widgets/WButton.java @@ -5,6 +5,9 @@ import cpw.mods.fml.relauncher.SideOnly; import me.towdium.jecalculation.gui.JecaGui; import me.towdium.jecalculation.gui.Resource; import me.towdium.jecalculation.utils.ClientUtils; +import net.minecraft.client.Minecraft; +import net.minecraft.client.audio.PositionedSoundRecord; +import org.lwjgl.input.Keyboard; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; @@ -23,6 +26,7 @@ public abstract class WButton extends WTooltip { protected int xPos, yPos, xSize, ySize; protected ListenerAction listener; protected boolean disabled; + protected int[] keys; public WButton(int xPos, int yPos, int xSize, int ySize, @Nullable String name) { super(name); @@ -41,16 +45,31 @@ public abstract class WButton extends WTooltip { public void onDraw(JecaGui gui, int xMouse, int yMouse) { super.onDraw(gui, xMouse, yMouse); boolean hovered = JecaGui.mouseIn(xPos + 1, yPos + 1, xSize - 2, ySize - 2, xMouse, yMouse); + if (keys != null) for (int i : keys) if (Keyboard.isKeyDown(i)) hovered = true; gui.drawResourceContinuous(disabled ? WGT_BUTTON_D : (hovered ? WGT_BUTTON_F : WGT_BUTTON_N) , xPos, yPos, xSize, ySize, 5, 5, 5, 5); } @Override public boolean onClicked(JecaGui gui, int xMouse, int yMouse, int button) { - if (!disabled && JecaGui.mouseIn(xPos + 1, yPos + 1, xSize - 2, ySize - 2, xMouse, yMouse)) { - if (button == 0 && listener != null) { - listener.invoke(this); - ClientUtils.playClickSound(1.0F); + if (JecaGui.mouseIn(xPos + 1, yPos + 1, xSize - 2, ySize - 2, xMouse, yMouse) && + !disabled && button == 0 && listener != null) { + trigger(); + return true; + } else return false; + } + + private void trigger() { + listener.invoke(this); + ClientUtils.playClickSound(1.0F); + } + + @Override + public boolean onKey(JecaGui gui, char ch, int code) { + // JustEnoughCalculation.logger.info(code); + if (keys != null) for (int i : keys) { + if (i == code) { + trigger(); return true; } } @@ -72,4 +91,9 @@ public abstract class WButton extends WTooltip { protected List getSuffix() { return disabled ? Arrays.asList("disabled", "enabled", "") : Arrays.asList("enabled", ""); } + + public WButton setKeyBind(int... keys) { + this.keys = keys; + return this; + } } diff --git a/src/main/java/me/towdium/jecalculation/gui/widgets/WButtonText.java b/src/main/java/me/towdium/jecalculation/gui/widgets/WButtonText.java index ce1767a..93d0664 100644 --- a/src/main/java/me/towdium/jecalculation/gui/widgets/WButtonText.java +++ b/src/main/java/me/towdium/jecalculation/gui/widgets/WButtonText.java @@ -4,6 +4,7 @@ import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import me.towdium.jecalculation.gui.JecaGui; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; /** @@ -12,12 +13,16 @@ import javax.annotation.ParametersAreNonnullByDefault; */ @ParametersAreNonnullByDefault @SideOnly(Side.CLIENT) -public class WButtonText extends WButton { // TODO need rework for text without need for localization +public class WButtonText extends WButton { public static final JecaGui.Font focused = new JecaGui.Font(0xFFFFA0, true, false); public static final JecaGui.Font normal = new JecaGui.Font(0xFFFFFF, true, false); public String text; - public WButtonText(int xPos, int yPos, int xSize, int ySize, String name, String text) { + public WButtonText(int xPos, int yPos, int xSize, int ySize, String text) { + this(xPos, yPos, xSize, ySize, text, null); + } + + public WButtonText(int xPos, int yPos, int xSize, int ySize, String text, @Nullable String name) { super(xPos, yPos, xSize, ySize, name); this.text = text; } diff --git a/src/main/java/me/towdium/jecalculation/gui/widgets/WLabel.java b/src/main/java/me/towdium/jecalculation/gui/widgets/WLabel.java index 53fb5a8..d5eaaae 100644 --- a/src/main/java/me/towdium/jecalculation/gui/widgets/WLabel.java +++ b/src/main/java/me/towdium/jecalculation/gui/widgets/WLabel.java @@ -99,7 +99,7 @@ public class WLabel implements IWidget { @Override public boolean onClicked(JecaGui gui, int xMouse, int yMouse, int button) { - if (!mouseIn(xMouse, yMouse)) + if (!mouseIn(xMouse, yMouse) || (button == 1 && mode != Mode.RESULT)) return false; switch (mode) { case EDITOR: @@ -160,7 +160,7 @@ public class WLabel implements IWidget { public enum Mode { EDITOR, // Slots in editor gui. Can use to edit amount. Exact amount displayed. - RESULT, // Slots to display calculate/getRecipes result. Rounded amount displayed. + RESULT, // Slots to display calculate result. Rounded amount displayed. PICKER, // Slots that can pick items from. No amount displayed. SELECTOR // Slots to put labels into. No amount displayed. } @@ -168,10 +168,10 @@ public class WLabel implements IWidget { class WAmount extends WContainer { long old = label.getAmount(); - WButton bAmount = new WButtonText(xPos + xSize + 60, yPos, 20, 20, "general.to_percent", "#") + WButton bAmount = new WButtonText(xPos + xSize + 60, yPos, 20, 20, "#", "general.to_percent") .setListener(i -> setPercent(true)); WTextField wtf = new WTextField(xPos + xSize + 10, yPos + ySize / 2 - WTextField.HEIGHT / 2, 50); - WButton bPercent = new WButtonText(xPos + xSize + 60, yPos, 20, 20, "general.to_percent", "%") + WButton bPercent = new WButtonText(xPos + xSize + 60, yPos, 20, 20, "%", "general.to_percent") .setListener(i -> setPercent(false)); WLabel wl = new WLabel(xPos, yPos, xSize, ySize, Mode.SELECTOR).setListener((i, v) -> update()); WButton bYes = new WButtonIcon(xPos + xSize + 83, yPos, 20, 20, BTN_YES) @@ -205,6 +205,7 @@ public class WLabel implements IWidget { } private void update() { + label = wl.label; bAmount.setDisabled(!label.acceptPercent()); if (label.isPercent()) { remove(bAmount); diff --git a/src/main/java/me/towdium/jecalculation/gui/widgets/WLcd.java b/src/main/java/me/towdium/jecalculation/gui/widgets/WLcd.java new file mode 100644 index 0000000..c4d9e3c --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/gui/widgets/WLcd.java @@ -0,0 +1,148 @@ +package me.towdium.jecalculation.gui.widgets; + +import me.towdium.jecalculation.data.structure.RecordMath.Operator; +import me.towdium.jecalculation.gui.JecaGui; +import me.towdium.jecalculation.gui.Resource; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.HashMap; +import java.util.Objects; + +import static me.towdium.jecalculation.data.structure.RecordMath.DOT_NONE; + + +/** + * Author: Towdium + * Date: 19-1-16 + */ +@ParametersAreNonnullByDefault +public class WLcd implements IWidget { + static HashMap PATTERN = new HashMap<>(); + + + public int yPos; + public String text = ""; + public int dot = DOT_NONE; + static NumberFormat format = new DecimalFormat("0.0E0"); + public Operator operator = Operator.EQUALS; + + static { + PATTERN.put('0', new boolean[]{true, true, true, true, true, true, false}); + PATTERN.put('1', new boolean[]{false, true, true, false, false, false, false}); + PATTERN.put('2', new boolean[]{true, true, false, true, true, false, true}); + PATTERN.put('3', new boolean[]{true, true, true, true, false, false, true}); + PATTERN.put('4', new boolean[]{false, true, true, false, false, true, true}); + PATTERN.put('5', new boolean[]{true, false, true, true, false, true, true}); + PATTERN.put('6', new boolean[]{true, false, true, true, true, true, true}); + PATTERN.put('7', new boolean[]{true, true, true, false, false, false, false}); + PATTERN.put('8', new boolean[]{true, true, true, true, true, true, true}); + PATTERN.put('9', new boolean[]{true, true, true, true, false, true, true}); + PATTERN.put('E', new boolean[]{true, false, false, true, true, true, true}); + PATTERN.put('o', new boolean[]{false, false, true, true, true, false, true}); + PATTERN.put('r', new boolean[]{false, false, false, false, true, false, true}); + PATTERN.put('-', new boolean[]{false, false, false, false, false, false, true}); + PATTERN.put(' ', new boolean[]{false, false, false, false, false, false, false}); + //format.setMinimumIntegerDigits(0); + } + + public WLcd(int yPos) { + this.yPos = yPos; + } + + @Override + public void onDraw(JecaGui gui, int xMouse, int yMouse) { + drawBackground(gui); + drawStr(gui, text); + if (dot != DOT_NONE) + drawDot(gui, 6 - dot); + drawOperator(gui, operator); + } + + protected void drawBackground(JecaGui gui) { + gui.drawResourceContinuous(Resource.WGT_LCD_BG, 7, yPos, 162, 50, 8); + for (int i = 0; i < 7; i++) { + gui.drawResource(Resource.WGT_LCD_UL_N, 16 + i * 21, yPos + 14); + gui.drawResource(Resource.WGT_LCD_UR_N, 30 + i * 21, yPos + 14); + gui.drawResource(Resource.WGT_LCD_LL_N, 16 + i * 21, yPos + 28); + gui.drawResource(Resource.WGT_LCD_LR_N, 30 + i * 21, yPos + 28); + gui.drawResource(Resource.WGT_LCD_H_N, 19 + i * 21, yPos + 14); + gui.drawResource(Resource.WGT_LCD_H_N, 19 + i * 21, yPos + 27); + gui.drawResource(Resource.WGT_LCD_H_N, 19 + i * 21, yPos + 40); + gui.drawResource(Resource.WGT_LCD_DO_N, 33 + i * 21, yPos + 40); + } + gui.drawResource(Resource.WGT_LCD_P_N, 132, yPos + 6); + gui.drawResource(Resource.WGT_LCD_M_N, 139, yPos + 6); + gui.drawResource(Resource.WGT_LCD_T_N, 146, yPos + 6); + gui.drawResource(Resource.WGT_LCD_D_N, 153, yPos + 6); + } + + protected void drawChar(JecaGui gui, char ch, int index) { + boolean[] pattern = PATTERN.get(ch); + Objects.requireNonNull(pattern, "Unsupported char: " + ch + "."); + if (pattern[0]) + gui.drawResource(Resource.WGT_LCD_H_F, 19 + index * 21, yPos + 14); + if (pattern[1]) + gui.drawResource(Resource.WGT_LCD_UR_F, 30 + index * 21, yPos + 14); + if (pattern[2]) + gui.drawResource(Resource.WGT_LCD_LR_F, 30 + index * 21, yPos + 28); + if (pattern[3]) + gui.drawResource(Resource.WGT_LCD_H_F, 19 + index * 21, yPos + 40); + if (pattern[4]) + gui.drawResource(Resource.WGT_LCD_LL_F, 16 + index * 21, yPos + 28); + if (pattern[5]) + gui.drawResource(Resource.WGT_LCD_UL_F, 16 + index * 21, yPos + 14); + if (pattern[6]) + gui.drawResource(Resource.WGT_LCD_H_F, 19 + index * 21, yPos + 27); + } + + protected void drawStr(JecaGui gui, String str) { + int offset = 7 - str.length(); + for (int i = offset; i < 7; i++) + drawChar(gui, str.charAt(i - offset), i); + } + + protected void drawDot(JecaGui gui, int index) { + gui.drawResource(Resource.WGT_LCD_DO_F, 33 + index * 21, yPos + 40); + } + + protected void drawOperator(JecaGui gui, Operator operator) { + switch (operator) { + case PLUS: + gui.drawResource(Resource.WGT_LCD_P_F, 132, yPos + 6); + break; + case MINUS: + gui.drawResource(Resource.WGT_LCD_M_F, 139, yPos + 6); + break; + case TIMES: + gui.drawResource(Resource.WGT_LCD_T_F, 146, yPos + 6); + break; + case DIVIDE: + gui.drawResource(Resource.WGT_LCD_D_F, 153, yPos + 6); + break; + } + } + + public void print(BigDecimal num) { + String s = num.stripTrailingZeros().toPlainString(); + double f = num.floatValue(); + int len = f > 0 ? 7 : 6; + int sca = num.unscaledValue().abs().toString().length() - num.scale() - 1; + if (Math.abs(sca) > len) { + String scale = String.valueOf(sca); + len -= scale.length() + 2; + format.setMaximumFractionDigits(len); + s = format.format(num); + } + int d = s.indexOf('.'); + if (d >= 0 && d <= 7) { + text = s.substring(0, d) + s.substring(d + 1, Math.min(s.length(), 8)); + dot = text.length() - d; + } else { + text = s; + dot = DOT_NONE; + } + } +} diff --git a/src/main/java/me/towdium/jecalculation/gui/widgets/WLine.java b/src/main/java/me/towdium/jecalculation/gui/widgets/WLine.java index ac12f0c..2d1c7b0 100644 --- a/src/main/java/me/towdium/jecalculation/gui/widgets/WLine.java +++ b/src/main/java/me/towdium/jecalculation/gui/widgets/WLine.java @@ -36,4 +36,38 @@ public class WLine implements IWidget { public void onDraw(JecaGui gui, int xMouse, int yMouse) { gui.drawResourceContinuous(Resource.WGT_SLOT, xPos, yPos, xSize, ySize, 1); } + + public static class Joint implements IWidget { + int xPos, yPos; + boolean up, down, left, right; + public static Resource u = Resource.WGT_SLOT.sub(9, 0, 2, 1); + public static Resource d = Resource.WGT_SLOT.sub(9, 19, 2, 1); + public static Resource l = Resource.WGT_SLOT.sub(0, 9, 1, 2); + public static Resource r = Resource.WGT_SLOT.sub(19, 9, 1, 2); + public static Resource ul = Resource.WGT_SLOT.sub(0, 0, 1, 1); + public static Resource ur = Resource.WGT_SLOT.sub(19, 0, 1, 1); + public static Resource ll = Resource.WGT_SLOT.sub(0, 19, 1, 1); + public static Resource lr = Resource.WGT_SLOT.sub(19, 19, 1, 1); + + public Joint(int xPos, int yPos, boolean up, boolean down, boolean left, boolean right) { + this.xPos = xPos; + this.yPos = yPos; + this.up = up; + this.down = down; + this.left = left; + this.right = right; + } + + @Override + public void onDraw(JecaGui gui, int xMouse, int yMouse) { + if (!up) gui.drawResource(u, xPos, yPos); + if (!down) gui.drawResource(d, xPos, yPos + 1); + if (!left) gui.drawResource(l, xPos, yPos); + if (!right) gui.drawResource(r, xPos + 1, yPos); + if (left == up) gui.drawResource(ul, xPos, yPos); + if (right == up) gui.drawResource(ur, xPos + 1, yPos); + if (left == down) gui.drawResource(ll, xPos, 1 + yPos); + if (right == down) gui.drawResource(lr, xPos + 1, yPos + 1); + } + } } diff --git a/src/main/java/me/towdium/jecalculation/gui/widgets/WPanel.java b/src/main/java/me/towdium/jecalculation/gui/widgets/WPanel.java index e36c0d0..80155d5 100644 --- a/src/main/java/me/towdium/jecalculation/gui/widgets/WPanel.java +++ b/src/main/java/me/towdium/jecalculation/gui/widgets/WPanel.java @@ -2,6 +2,7 @@ package me.towdium.jecalculation.gui.widgets; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import me.towdium.jecalculation.data.label.ILabel; import me.towdium.jecalculation.gui.JecaGui; import me.towdium.jecalculation.gui.Resource; @@ -42,9 +43,10 @@ public class WPanel implements IWidget { @Override public boolean onClicked(JecaGui gui, int xMouse, int yMouse, int button) { - return mouseIn(xMouse, yMouse); + boolean ret = mouseIn(xMouse, yMouse); + if (ret) gui.hand = ILabel.EMPTY; + return ret; } - public boolean mouseIn(int x, int y) { int xx = x - xPos; int yy = y - yPos; diff --git a/src/main/java/me/towdium/jecalculation/gui/widgets/WTooltip.java b/src/main/java/me/towdium/jecalculation/gui/widgets/WTooltip.java index 5c8d97d..5a51497 100644 --- a/src/main/java/me/towdium/jecalculation/gui/widgets/WTooltip.java +++ b/src/main/java/me/towdium/jecalculation/gui/widgets/WTooltip.java @@ -43,8 +43,7 @@ public abstract class WTooltip implements IWidget { .collect(Collectors.toList()); String str = suffix.stream().filter(p -> p.two).findFirst().map(p -> p.one) .orElse(JecaGui.ALWAYS_TOOLTIP ? suffix.get(0).one : null); - if (str != null) - tooltip.add(str); + if (str != null) Collections.addAll(tooltip, str.split("\n")); } return false; } diff --git a/src/main/java/me/towdium/jecalculation/network/ClientHandler.java b/src/main/java/me/towdium/jecalculation/network/ClientHandler.java index 3482fc7..1cad613 100644 --- a/src/main/java/me/towdium/jecalculation/network/ClientHandler.java +++ b/src/main/java/me/towdium/jecalculation/network/ClientHandler.java @@ -11,8 +11,11 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraftforge.client.ClientCommandHandler; import org.lwjgl.input.Keyboard; +import static org.lwjgl.input.Keyboard.KEY_NONE; + public class ClientHandler { - public static final KeyBinding keyOpenGui = new KeyBinding("jecalculation.key.open_gui", Keyboard.KEY_NONE, "jecalculation.key.category"); + public static final KeyBinding keyOpenGuiCraft = new KeyBinding("jecalculation.key.gui_craft", KEY_NONE, "jecalculation.key.category"); + public static final KeyBinding keyOpenGuiMath = new KeyBinding("jecalculation.key.gui_math", KEY_NONE, "jecalculation.key.category"); public void initPre() { Handlers.register(); @@ -26,7 +29,8 @@ public class ClientHandler { } public void init() { - ClientRegistry.registerKeyBinding(keyOpenGui); + ClientRegistry.registerKeyBinding(keyOpenGuiCraft); + ClientRegistry.registerKeyBinding(keyOpenGuiMath); } diff --git a/src/main/java/me/towdium/jecalculation/utils/Utilities.java b/src/main/java/me/towdium/jecalculation/utils/Utilities.java index 36eb866..79b174c 100644 --- a/src/main/java/me/towdium/jecalculation/utils/Utilities.java +++ b/src/main/java/me/towdium/jecalculation/utils/Utilities.java @@ -317,11 +317,12 @@ public class Utilities { this.limit = limit; } - public void push(T obj, boolean replace) { + public boolean push(T obj, boolean replace) { if (replace) data.pop(); - data.removeIf(t -> tester != null ? tester.test(t, obj) : t.equals(obj)); + boolean ret = data.removeIf(t -> tester != null ? tester.test(t, obj) : t.equals(obj)); data.addFirst(obj); if (data.size() > limit) data.removeLast(); + return ret; } public List toList() { @@ -332,6 +333,10 @@ public class Utilities { public void clear() { data.clear(); } + + public int size() { + return data.size(); + } } public static class Json { diff --git a/src/main/resources/assets/jecalculation/lang/en_US.lang b/src/main/resources/assets/jecalculation/lang/en_US.lang index c94ddb7..914bc55 100644 --- a/src/main/resources/assets/jecalculation/lang/en_US.lang +++ b/src/main/resources/assets/jecalculation/lang/en_US.lang @@ -1,28 +1,31 @@ -# suppress inspection "UnusedProperty" for whole file jecalculation.key.category=Just Enough Calculation -jecalculation.key.open_gui=Open GUI +jecalculation.key.gui_craft=Open crafting calculator +jecalculation.key.gui_math=Open math calculator jecalculation.command.help.list=Here is the list of all commands available: -jecalculation.command.state.desc=Server %s. -jecalculation.command.reload.done=Reloading finished +jecalculation.command.state.active=Server active. +jecalculation.command.state.inactive=Server inactive. jecalculation.chat.max_loop=Sorry, it seems the calculation does not converge in 1000 iterations. Please contact the developer if you think this process should have a result. -jecalculation.chat.server_mode=The mod is not working in client mode. Key binding will not work. +jecalculation.chat.server_mode=The mod is not working in client mode, so you cannot open the interface without an calculator item jecalculation.chat.export=Selected recipes has been exported to "%s". -jecalculation.gui.calculator.inventory_enabled=Inventory detection enabled -jecalculation.gui.calculator.inventory_disabled=Inventory detection disabled -jecalculation.gui.calculator.recipeNew recipe -jecalculation.gui.calculator.help=Welcome to the main interface of the mod. The first thing suggested to do is to use the two buttons in the upper right to input the recipes, which allows you to create and edit recipes. All of the calculations require recipes to be recorded.\fTo calculate the cost of an item, simply pick the item from JEIs item list then put it in the slot in the upper left of the interface with all of the recipes to use recorded. The result will then be displayed in the lower slots of the interface. The four buttons provide options to display input, output, catalyst and crafting steps.\fThe button in the upper left allows you to edit and get a label. Labels are a very important part of the mod. They are basically things that can be put into a slot, including ItemStack, FluidStack, OreDict entry, or simply a placeholder. Lables are used in the calculation process. Most of time they are quite clever as long as you configure them properly. -jecalculation.gui.calculator.title=Crafting Calculator -jecalculation.gui.calculator.label=Add label -jecalculation.gui.calculator.step=Steps -jecalculation.gui.calculator.recipe=Add recipe -jecalculation.gui.calculator.search=Search recipe -jecalculation.gui.calculator.suggest=Suggestions +jecalculation.gui.craft.inventory_enabled=Inventory detection enabled +jecalculation.gui.craft.inventory_disabled=Inventory detection disabled +jecalculation.gui.craft.recipe=New recipe +jecalculation.gui.craft.history=History +jecalculation.gui.craft.help=Welcome to the main interface of the mod. The first thing suggested to do is to use the two buttons in the upper right to input the recipes, which allows you to create and edit recipes. All of the calculations require recipes to be recorded.\fTo calculate the cost of an item, simply pick the item from JEIs item list then put it in the slot in the upper left of the interface with all of the recipes to use recorded. The result will then be displayed in the lower slots of the interface. The four buttons provide options to display input, output, catalyst and crafting steps.\fThe button in the upper left allows you to edit and get a label. Labels are a very important part of the mod. They are basically things that can be put into a slot, including ItemStack, FluidStack, OreDict entry, or simply a placeholder. Lables are used in the calculation process. Most of time they are quite clever as long as you configure them properly. +jecalculation.gui.craft.title=Crafting Calculator +jecalculation.gui.craft.label=Add label +jecalculation.gui.craft.step=Steps +jecalculation.gui.craft.recipe=Add recipe +jecalculation.gui.craft.search=Search recipe +jecalculation.gui.craft.suggest=Suggestions +jecalculation.gui.math.title=Math Calculator +jecalculation.gui.math.help=You can use both buttons and keyboard (numpad or main keyboard) to use it. For main keyboard, the buttons are mapped to these keys:\n- plus: = / +\n- minus: - / --\n- times: ' / "\n- divide by: / / ?\n- equals: Enter\n- clear: Delete\n- backspace: Backspace jecalculation.gui.disamb.list=Raw types jecalculation.gui.disamb.label=Inferred types jecalculation.gui.recipe.save.enabled=Save jecalculation.gui.recipe.save.disabled=Cannot save recipe because\nrecipe is invalid or you\nalready have same recipe jecalculation.gui.recipe.copy.enabled=Save as new recipe -jecalculation.gui.recipe.copy.disabled=Cannot save as new recipe\nbecause you are not editing a recipe +jecalculation.gui.recipe.copy.disabled=Cannot save as new recipe\nbecause you are not editing\na recipe jecalculation.gui.recipe.delete.enabled=Delete recipe jecalculation.gui.recipe.delete.disabled=Cannot delete recipe because\nyou are not editing a recipe jecalculation.gui.recipe.clear=Clear grid @@ -30,7 +33,7 @@ jecalculation.gui.recipe.label=Add label jecalculation.gui.recipe.confirm.enabled=Confirm jecalculation.gui.recipe.confirm.disabled=Cannot use empty string as group name jecalculation.gui.recipe.disamb.enabled=Open disambiguate settings -jecalculation.gui.recipe.disamb.disabled=Disambiguate settings not available\nbecause of no ambiguous element +jecalculation.gui.recipe.disamb.disabled=Disambiguation settings are only\navailable when transferring recipes\nwith multiple options from JEI jecalculation.gui.recipe.new=New group jecalculation.gui.recipe.title=Recipe Editing jecalculation.gui.recipe.help=This interface allows you to create or edit recipes. To create one recipe, you can pick items from the JEI item list, then put it into the slots. When finished, simply click the save button. You can also fast transer recipes from JEI by clicking "+" when openning recipe view.\fTo edit one label, you can simply click it, then a toolbar will pop up to help you, where you can switch between amount and percentage mode, type the amount, or remove the label. In addition, you can use mouse scroll to quickly change the amount. Clicking the "Clear" button will remove all of the labels.\fUsing recipe fast transfer, some labels will automatically setup for you, like oreDict labels. If you do not want this behavoir, simply use "Disambiguous" button to select manually. You can also add labels by ckicking the "Add label" button. By the way, normally you shouldn't put fuzzy labels as output.\fYou can easily manage recipes using groups. Click the "New group" button, type the group name and hit the tick, you are done. The new group name will show in the top bar. If you already have groups set up and you want to use an existing one, you can use the arrows to select between them.\fWhen you are editing an existing recipe, you will have two extra buttons, "Save as" and "Delete". "Save as" allows you to save the current recipe as a new recipe, without changing the existing one. "Delete" simply allows you to delete the recipe. diff --git a/src/main/resources/assets/jecalculation/lang/zh_CN.lang b/src/main/resources/assets/jecalculation/lang/zh_CN.lang new file mode 100644 index 0000000..3f7e57e --- /dev/null +++ b/src/main/resources/assets/jecalculation/lang/zh_CN.lang @@ -0,0 +1,88 @@ +# suppress inspection "UnusedProperty" for whole file +jecalculation.key.category=Just Enough Calculation +jecalculation.key.gui_craft=打开合成计算器 +jecalculation.key.gui_math=打开数学计算器 +jecalculation.command.help.list=所有可用指令: +jecalculation.command.state.active=服务端在线 +jecalculation.command.state.inactive=服务端离线 +jecalculation.chat.max_loop=抱歉,由于计算过程已经超过1000次迭代,计算已经被强制终止。如果你认为该计算存在合理的结果,请联系开发者。 +jecalculation.chat.server_mode=抱歉,本模组目前不在客户端模式下运行,所以你无法使用右击计算器以外的方式打开界面。 +jecalculation.chat.export=选中的配方已经导出到“%s”。 +jecalculation.gui.craft.inventory_enabled=启用物品栏检测 +jecalculation.gui.craft.inventory_disabled=禁用物品栏检测 +jecalculation.gui.craft.recipe=新建配方 +jecalculation.gui.craft.history=历史记录 +jecalculation.gui.craft.help=欢迎来到本模组的主界面!首先,我建议你使用右上角的两个按钮来添加/编辑配方。配方是本模组的核心,而且,抱歉,只能手动载入。当你添加了计算所需的所有配方,你只需要从JEI物品列表中抓取所需计算的物品,放入左上角的槽位,结果就会自动显示在下方的列表中。通过下方的四个按钮,你可以在输入,额外输出,催化剂和合成步骤中切换。\f你可以使用左上角的按钮来插入标签。“标签”是本模组运算的的核心,本质上任何可以放入物品槽的东西都是一种标签。最简单的是物品栈,但是你还可以创建更高级的标签,诸如流体栈,矿物词典或者仅仅是一个占位符。在运算过程中,相同的标签会自动关联。除此之外,矿物词典标签或者带有模糊设定的物品栈标签还会智能地与其他匹配的标签相关联。 +jecalculation.gui.craft.title=合成计算器 +jecalculation.gui.craft.label=添加标签 +jecalculation.gui.craft.step=步骤 +jecalculation.gui.craft.recipe=添加配方 +jecalculation.gui.craft.search=查找配方 +jecalculation.gui.craft.suggest=建议选项 +jecalculation.gui.math.title=数学计算器 +jecalculation.gui.math.help=按钮或者键盘都可以与计算器(主键盘或小键盘)进行交互。对于主键盘而言,各项功能以下列关系被映射到键盘按键上:\n- 加: = / +\n- 减: - / --\n- 乘: ' / "\n- 除: / / ?\n- 等于: Enter\n- 清零: Delete\n- 退格: Backspace +jecalculation.gui.disamb.list=原始类型 +jecalculation.gui.disamb.label=推测类型 +jecalculation.gui.recipe.save.enabled=保存配方 +jecalculation.gui.recipe.save.disabled=因为当前配方无效或者\n你已经保存过相同的配方\n所以无法保存当前配方 +jecalculation.gui.recipe.copy.enabled=另存为新配方 +jecalculation.gui.recipe.copy.disabled=因为你不在编辑已有配方\n所以无法使用另存为 +jecalculation.gui.recipe.delete.enabled=删除配方 +jecalculation.gui.recipe.delete.disabled=因为你不在编辑已有配方\n所以无法使用删除 +jecalculation.gui.recipe.clear=清空所有槽位 +jecalculation.gui.recipe.label=添加标签 +jecalculation.gui.recipe.confirm.enabled=确认 +jecalculation.gui.recipe.confirm.disabled=组名不能为空 +jecalculation.gui.recipe.disamb.enabled=打开消歧义设定 +jecalculation.gui.recipe.disamb.disabled=消歧义设定只在从JEI转移配方\n并且含有歧义项时生效 +jecalculation.gui.recipe.new=新建编组 +jecalculation.gui.recipe.title=编辑配方 +jecalculation.gui.recipe.help=你可以使用本界面编辑或者创建配方。你可以手动从JEI物品列表抓取物品,然后放入槽位中。除此之外,你也可以使用JEI查询,然后点击“+”来快速转移配方。完成编辑后,点击“保存配方”就可以将该配方添加至你个人的配方库中。\n当你点击一个标签,一个工具栏会弹出来帮助你编辑。你可以使用这个工具栏来输入数量,切换百分比设定,或是删除标签。除此之外,你也可以直接使用鼠标滚轮来编辑标签的数量。\f在你从JEI快速转移配方的时候,系统会帮你自动配置好一些标签,诸如矿辞或是流体之类。如果你不想系统自动匹配,你可以使用消歧义选项来手动选取你需要的标签类型。与此同时,你也可以通过“添加标签”按钮手动选取并添加标签。值得一提的是,虽然本模组不会阻止你在输出列表中使用模糊标签,但是这通常是不必要的。\f你可以使用编组来轻松地管理你的配方。要添加一个新组,你只需要点击“新建编组”,键入名称,再点击确认即可。如果你想要使用已有的编组,可以使用两侧的箭头按钮进行切换。\n在你编辑已有的合成表时,会有两个额外的按钮。使用“另存为”,你可以将当前的合成表保存为新的合成表,而不改动原有合成表。使用“删除”将从合成表库中删除当前合成表。 +jecalculation.gui.label.title=编辑标签 +jecalculation.gui.label.help=你可以使用该界面创建标签。本模组目前有四类标签:流体栈,矿物词典,占位符和物品栈。要创建流体或矿辞标签,你只需要搜索并抓取即可。物品栈标签可以表示游戏中的物品栈,但是可以对Metadata,NBT和Capability使用额外的模糊选项。\n占位符并不表示任何实际内容,但可以用来链接配方,代表多方块结构,能量,或者任何你能想到的东西。只需要键入名字,就可以创建占位符标签。另外地,你也可以在已有的占位符中直接搜索并抓取。 +jecalculation.gui.search.all=全部组 +jecalculation.gui.search.import=导入配方 +jecalculation.gui.search.export_group=导出当前组内的配方 +jecalculation.gui.search.export_all.enabled=导出所有配方 +jecalculation.gui.search.export_all.disabled=没有配方记录,所以无法导出 +jecalculation.gui.search.title=配方管理 +jecalculation.gui.search.help=你可以使用该界面查找,导入和导出配方。你只需要点击其中的标签,就可以编辑对应的配方。使用右上方的按钮,你可以轻松地导入和导出配方。 +jecalculation.gui.import.title=导入配方 +jecalculation.gui.import.help=你可以使用该界面导入配方。首先,请将需要导入的文件放置于"config/ JustEnoughCalculation/data/"。在检测到合适的文件时,编组将会以“文件名/组名”的格式显示在列表中。最后选取需要导入的编组,点击确定就完成了。\n对于整合包作者,你可以将符合格式的文件放置于"config/JustEnoughCalculation/ default.json"。在玩家新建世界或者加入服务器时,其中的内容会做为默认值自动载入。 +jecalculation.gui.import.confirm.enabled=确认 +jecalculation.gui.import.confirm.disabled=因为没有选中编组\n所以无法执行导入 +jecalculation.gui.import.tick.unselected=点击以选中编组 +jecalculation.gui.import.tick.selected=点击以取消选择 +jecalculation.gui.placeholder.create=新建占位符 +jecalculation.gui.item_stack.confirm.enabled=确认 +jecalculation.gui.item_stack.confirm.disabled=因为没有选择物品\n所以无法新建标签 +jecalculation.gui.item_stack.nbt_normal.enabled=匹配NBT +jecalculation.gui.item_stack.nbt_normal.disabled=请先选择物品 +jecalculation.gui.item_stack.nbt_fuzzy.enabled=忽略NBT +jecalculation.gui.item_stack.capability_normal.enabled=匹配Capability +jecalculation.gui.item_stack.capability_normal.disabled=请先选择物品 +jecalculation.gui.item_stack.capability_fuzzy.enabled=忽略Capability +jecalculation.gui.item_stack.meta_normal.enabled=匹配Metadata +jecalculation.gui.item_stack.meta_normal.disabled=请先选择物品 +jecalculation.gui.item_stack.meta_fuzzy.enabled=忽略Metadata +jecalculation.gui.common.label.ore=矿物辞典 +jecalculation.gui.common.label.fluid=流体栈 +jecalculation.gui.common.label.placeholder=占位符 +jecalculation.gui.common.label.item=物品栈 +jecalculation.gui.common.search=搜索 +jecalculation.gui.common.input=输入 +jecalculation.gui.common.output=输出 +jecalculation.gui.common.catalyst=催化剂 +jecalculation.gui.common.default=默认 +jecalculation.gui.common.help=帮助 +jecalculation.gui.common.close=关闭 +jecalculation.gui.common.confirm=确认 +jecalculation.gui.common.cancel=取消 +jecalculation.config.force_client=强制运行在客户端模式 +jecalculation.label.ore_dict.name=矿物辞典: %s +jecalculation.label.placeholder.name=占位符: %s +jecalculation.label.item_stack.fuzzy_meta=忽略Metadata +jecalculation.label.item_stack.fuzzy_nbt=忽略NBT +jecalculation.label.item_stack.fuzzy_cap=忽略Capabilities +jecalculation.label.common.amount=数量: %s + diff --git a/src/test/java/JecaTest.java b/src/test/java/JecaTest.java deleted file mode 100644 index f5ae1cd..0000000 --- a/src/test/java/JecaTest.java +++ /dev/null @@ -1,8 +0,0 @@ -import org.junit.jupiter.api.Test; - -public class JecaTest { - @Test - public void test() { - - } -}