From cce76168c06d86c2430ce65a101071ba94c9220b Mon Sep 17 00:00:00 2001 From: Discreater Date: Fri, 30 Apr 2021 14:46:38 +0800 Subject: [PATCH] feat: reorganize --- .../jecalculation/JustEnoughCalculation.java | 1 + .../client/gui/drawables/WContainer.java | 1 - .../client/gui/drawables/WLabel.java | 17 +++ .../client/gui/guis/GuiCalculator.java | 32 ++++- .../client/gui/guis/GuiDisambiguation.java | 5 +- .../client/gui/guis/pickers/PickerSimple.java | 8 +- .../gui/guis/pickers/PickerUniversal.java | 4 +- .../jecalculation/command/Commands.java | 4 +- .../{SubCommand.java => ISubCommand.java} | 2 +- .../jecalculation/command/JecaCommand.java | 6 +- .../command/commands/CommandHelp.java | 8 +- .../command/commands/CommandOreDict.java | 6 +- .../command/commands/CommandState.java | 7 +- .../command/commands/CommandUuid.java | 8 +- .../jecalculation/data/label/ILabel.java | 124 ++++++------------ ...{LabelFluidStack.java => LFluidStack.java} | 22 ++-- .../{LabelItemStack.java => LItemStack.java} | 25 ++-- .../{LabelOreDict.java => LOreDict.java} | 29 ++-- .../{LabelUniversal.java => LString.java} | 23 +++- .../jecalculation/data/structure/Recipe.java | 4 +- .../jecalculation/data/structure/User.java | 61 ++++----- .../event/handlers/ControllerClient.java | 17 ++- .../event/handlers/InputEventHandler.java | 4 +- .../towdium/jecalculation/nei/NEIPlugin.java | 5 +- .../jecalculation/network/ProxyClient.java | 15 ++- .../jecalculation/network/ProxyServer.java | 11 +- .../{Polyfill.java => NBTHelper.java} | 12 +- .../jecalculation/utils/ItemStackHelper.java | 4 +- .../jecalculation/utils/Utilities.java | 34 +++-- .../assets/jecalculation/lang/en_US.lang | 4 +- 30 files changed, 286 insertions(+), 217 deletions(-) rename src/main/java/me/towdium/jecalculation/command/{SubCommand.java => ISubCommand.java} (95%) rename src/main/java/me/towdium/jecalculation/data/label/labels/{LabelFluidStack.java => LFluidStack.java} (80%) rename src/main/java/me/towdium/jecalculation/data/label/labels/{LabelItemStack.java => LItemStack.java} (78%) rename src/main/java/me/towdium/jecalculation/data/label/labels/{LabelOreDict.java => LOreDict.java} (84%) rename src/main/java/me/towdium/jecalculation/data/label/labels/{LabelUniversal.java => LString.java} (73%) rename src/main/java/me/towdium/jecalculation/polyfill/{Polyfill.java => NBTHelper.java} (74%) diff --git a/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java b/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java index d24bca9..5aa2576 100644 --- a/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java +++ b/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java @@ -10,6 +10,7 @@ import cpw.mods.fml.common.network.NetworkCheckHandler; import cpw.mods.fml.common.network.NetworkRegistry; import cpw.mods.fml.common.network.simpleimpl.SimpleNetworkWrapper; import cpw.mods.fml.relauncher.Side; +import me.towdium.jecalculation.client.gui.JecGui; import me.towdium.jecalculation.command.JecaCommand; import me.towdium.jecalculation.nei.NEIPlugin; import me.towdium.jecalculation.network.IProxy; diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WContainer.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WContainer.java index d02cab3..57fd323 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WContainer.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WContainer.java @@ -17,7 +17,6 @@ import java.util.List; @SideOnly(Side.CLIENT) public class WContainer implements IWidget { protected List widgets = new ArrayList<>(); - protected JecGui gui; public void add(IWidget w) { widgets.add(w); diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabel.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabel.java index 5770ba8..928f6ea 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabel.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WLabel.java @@ -11,6 +11,7 @@ import me.towdium.jecalculation.utils.Utilities; import javax.annotation.ParametersAreNonnullByDefault; import java.util.ArrayList; +import java.util.function.Consumer; import java.util.stream.IntStream; /** @@ -30,6 +31,7 @@ public class WLabel implements IWidget { public int xPos, yPos, xSize, ySize; public ILabel label; public enumMode mode; + public Consumer lsnrUpdate; protected Utilities.Timer timer = new Utilities.Timer(); public WLabel(int xPos, int yPos, int xSize, int ySize, enumMode mode) { @@ -47,6 +49,7 @@ public class WLabel implements IWidget { public void setLabel(ILabel label) { this.label = label; + notifyLsnr(); } @Override @@ -79,6 +82,7 @@ public class WLabel implements IWidget { if (JecGui.isShiftDown()) label = diff > 0 ? label.increaseAmountLarge() : label.decreaseAmountLarge(); else label = diff > 0 ? label.increaseAmount() : label.decreaseAmount(); }); + notifyLsnr(); return true; } else return false; } @@ -92,6 +96,7 @@ public class WLabel implements IWidget { if (gui.hand != label.EMPTY) { label = gui.hand; gui.hand = label.EMPTY; + notifyLsnr(); return true; } else if (label != label.EMPTY) { if (button == 0) { @@ -99,12 +104,14 @@ public class WLabel implements IWidget { label = label.increaseAmountLarge(); else label = label.increaseAmount(); + notifyLsnr(); return true; } else if (button == 1) { if (JecGui.isShiftDown()) label = label.decreaseAmountLarge(); else label = label.decreaseAmount(); + notifyLsnr(); return true; } } else @@ -120,6 +127,7 @@ public class WLabel implements IWidget { case SELECTOR: label = gui.hand; gui.hand = label.EMPTY; + notifyLsnr(); return true; default: throw new IllegalPositionException(); @@ -128,12 +136,21 @@ public class WLabel implements IWidget { return false; } + public WLabel setLsnrUpdate(Consumer lsnr) { + lsnrUpdate = lsnr; + return this; + } + public boolean mouseIn(int x, int y) { int xx = x - xPos; int yy = y - yPos; return xx >= 0 && xx < xSize && yy >= 0 && yy < ySize; } + void notifyLsnr() { + if (lsnrUpdate != null) lsnrUpdate.accept(label); + } + public enum enumMode { /** * Slots in editor gui. Can use to edit amount. Exact amount displayed. diff --git a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiCalculator.java b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiCalculator.java index f126d2a..aac3689 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiCalculator.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiCalculator.java @@ -5,9 +5,18 @@ import cpw.mods.fml.relauncher.SideOnly; import me.towdium.jecalculation.client.gui.JecGui; import me.towdium.jecalculation.client.gui.Resource; import me.towdium.jecalculation.client.gui.drawables.*; +import me.towdium.jecalculation.data.label.ILabel; +import me.towdium.jecalculation.data.structure.User; +import me.towdium.jecalculation.event.handlers.ControllerClient; +import me.towdium.jecalculation.item.ItemCalculator; +import me.towdium.jecalculation.utils.Utilities; +import net.minecraft.client.Minecraft; +import net.minecraft.item.ItemStack; import javax.annotation.ParametersAreNonnullByDefault; +import java.util.Optional; + import static me.towdium.jecalculation.client.gui.drawables.WLabel.enumMode.*; /** @@ -17,6 +26,10 @@ import static me.towdium.jecalculation.client.gui.drawables.WLabel.enumMode.*; @ParametersAreNonnullByDefault @SideOnly(Side.CLIENT) public class GuiCalculator extends WContainer { + WLabelGroup wRecent = new WLabelGroup(7, 31, 8, 1, PICKER); + User.Recent recent; + + public GuiCalculator() { add(new WPanel()); add(new WTextField(61, 7, 64)); @@ -29,10 +42,25 @@ public class GuiCalculator extends WContainer { .setListenerLeft(() -> JecGui.displayGui(true, true, new GuiRecipe()))); add(new WButtonIcon(149, 7, 20, 20, Resource.BTN_SEARCH_N, Resource.BTN_SEARCH_F, "calculator.search")); add(new WLabelGroup(7, 87, 9, 4, RESULT)); - add(new WLabelGroup(7, 31, 8, 1, PICKER)); - add(new WLabel(31, 7, 20, 20, SELECTOR)); + add(wRecent); + add(new WLabel(31, 7, 20, 20, SELECTOR).setLsnrUpdate(this::setRecent)); add(new WLine(52)); add(new WIcon(151, 31, 18, 18, Resource.ICN_RECENT_N, Resource.ICN_RECENT_F, "calculator.history")); add(new WSwitcher(7, 56, 162, 5)); + Optional ois = getStack(); + recent = ois.map(is -> new User.Recent(Utilities.getTag(is).getTagList(User.Recent.IDENTIFIER, 9))) + .orElseGet(ControllerClient::getRecent); + } + + void setRecent(ILabel label) { + recent.push(label); + Optional ois = getStack(); + ois.ifPresent(is -> Utilities.getTag(is).setTag(User.Recent.IDENTIFIER, recent.serialize())); + wRecent.setLabel(recent.getRecords().subList(1, recent.getRecords().size()), 0); + } + + Optional getStack() { + ItemStack is = Minecraft.getMinecraft().thePlayer.inventory.getCurrentItem(); + return Optional.ofNullable(is.getItem() instanceof ItemCalculator ? is : null); } } diff --git a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiDisambiguation.java b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiDisambiguation.java index de10692..e7807f1 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiDisambiguation.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/guis/GuiDisambiguation.java @@ -9,7 +9,6 @@ import me.towdium.jecalculation.data.label.ILabel; import net.minecraft.item.ItemStack; import javax.annotation.ParametersAreNonnullByDefault; -import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -27,8 +26,8 @@ public class GuiDisambiguation extends IWPicker.Simple { public GuiDisambiguation(List> record) { lsUp = new WLabelScroll(25, 48, 7, 3, WLabel.enumMode.PICKER, true); lsDown = new WLabelScroll(25, 105, 7, 3, WLabel.enumMode.PICKER, true); - this.record = new ArrayList<>(record.stream().filter(iss -> !ILabel.CONVERTER_ITEM.toLabel(iss).isEmpty()) - .collect(Collectors.toSet())); + this.record = record.stream().filter(iss -> !ILabel.CONVERTER_ITEM.toLabel(iss).isEmpty()).distinct() + .collect(Collectors.toList()); WTextField tf = new WTextField(25, 24, 90); add(new WPanel()); diff --git a/src/main/java/me/towdium/jecalculation/client/gui/guis/pickers/PickerSimple.java b/src/main/java/me/towdium/jecalculation/client/gui/guis/pickers/PickerSimple.java index c33a286..037c2ff 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/guis/pickers/PickerSimple.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/guis/pickers/PickerSimple.java @@ -6,8 +6,8 @@ import me.towdium.jecalculation.client.gui.IWPicker; import me.towdium.jecalculation.client.gui.Resource; import me.towdium.jecalculation.client.gui.drawables.*; import me.towdium.jecalculation.data.label.ILabel; -import me.towdium.jecalculation.data.label.labels.LabelFluidStack; -import me.towdium.jecalculation.data.label.labels.LabelOreDict; +import me.towdium.jecalculation.data.label.labels.LFluidStack; +import me.towdium.jecalculation.data.label.labels.LOreDict; import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.oredict.OreDictionary; @@ -39,14 +39,14 @@ public class PickerSimple extends IWPicker.Simple { public static class FluidStack extends PickerSimple { public FluidStack() { super(FluidRegistry.getRegisteredFluids().entrySet().stream() - .map(e -> new LabelFluidStack(e.getValue(), 1000)).collect(Collectors.toList()), + .map(e -> new LFluidStack(e.getValue(), 1000)).collect(Collectors.toList()), "picker_fluid_stack"); } } public static class OreDict extends PickerSimple { public OreDict() { - super(Arrays.stream(OreDictionary.getOreNames()).map(LabelOreDict::new).collect(Collectors.toList()), + super(Arrays.stream(OreDictionary.getOreNames()).map(LOreDict::new).collect(Collectors.toList()), "picker_ore_dict"); } } diff --git a/src/main/java/me/towdium/jecalculation/client/gui/guis/pickers/PickerUniversal.java b/src/main/java/me/towdium/jecalculation/client/gui/guis/pickers/PickerUniversal.java index f5f091f..b53690f 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/guis/pickers/PickerUniversal.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/guis/pickers/PickerUniversal.java @@ -6,7 +6,7 @@ import me.towdium.jecalculation.client.gui.IWPicker; import me.towdium.jecalculation.client.gui.JecGui; import me.towdium.jecalculation.client.gui.Resource; import me.towdium.jecalculation.client.gui.drawables.*; -import me.towdium.jecalculation.data.label.labels.LabelUniversal; +import me.towdium.jecalculation.data.label.labels.LString; import javax.annotation.ParametersAreNonnullByDefault; import java.util.ArrayList; @@ -30,7 +30,7 @@ public class PickerUniversal extends IWPicker.Simple { add(new WIcon(7, 7, 20, 20, Resource.ICN_TEXT_N, Resource.ICN_TEXT_F, "picker_universal.text_create")); add(new WLine(36)); add(new WButtonIcon(95, 7, 20, 20, Resource.BTN_YES_N, Resource.BTN_YES_F).setListenerLeft(() -> { - if (!create.getText().equals("")) callback.value.accept(new LabelUniversal(create.getText(), 1)); + if (!create.getText().equals("")) callback.value.accept(new LString(create.getText(), 1)); })); add(create); diff --git a/src/main/java/me/towdium/jecalculation/command/Commands.java b/src/main/java/me/towdium/jecalculation/command/Commands.java index 7f906f3..0f85782 100644 --- a/src/main/java/me/towdium/jecalculation/command/Commands.java +++ b/src/main/java/me/towdium/jecalculation/command/Commands.java @@ -14,7 +14,7 @@ import java.util.HashMap; */ @ParametersAreNonnullByDefault public class Commands { - public static final HashMap commands; + public static final HashMap commands; public static final CommandHelp commandHelp = new CommandHelp(); public static final CommandState commandState = new CommandState(); @@ -30,7 +30,7 @@ public class Commands { add(commandUuid); } - static void add(SubCommand c) { + static void add(ISubCommand c) { commands.put(c.getName(), c); } } diff --git a/src/main/java/me/towdium/jecalculation/command/SubCommand.java b/src/main/java/me/towdium/jecalculation/command/ISubCommand.java similarity index 95% rename from src/main/java/me/towdium/jecalculation/command/SubCommand.java rename to src/main/java/me/towdium/jecalculation/command/ISubCommand.java index 69dda15..f552730 100644 --- a/src/main/java/me/towdium/jecalculation/command/SubCommand.java +++ b/src/main/java/me/towdium/jecalculation/command/ISubCommand.java @@ -12,7 +12,7 @@ import java.util.List; * Date: 8/10/17. */ @ParametersAreNonnullByDefault -public interface SubCommand { +public interface ISubCommand { String getName(); String getUsage(ICommandSender sender); diff --git a/src/main/java/me/towdium/jecalculation/command/JecaCommand.java b/src/main/java/me/towdium/jecalculation/command/JecaCommand.java index 762a2b2..a92f54c 100644 --- a/src/main/java/me/towdium/jecalculation/command/JecaCommand.java +++ b/src/main/java/me/towdium/jecalculation/command/JecaCommand.java @@ -32,7 +32,7 @@ public class JecaCommand extends CommandBase { if (args.length == 0 || args[0].toLowerCase().equals("help")) { Commands.commandHelp.execute(sender, cut(args)); } else { - SubCommand cmd = Commands.commands.get(args[0].toLowerCase()); + ISubCommand cmd = Commands.commands.get(args[0].toLowerCase()); if (cmd != null) { cmd.execute(sender, cut(args)); } else { @@ -64,10 +64,10 @@ public class JecaCommand extends CommandBase { if (args.length == 1) { return getListOfStringsFromIterableMatchingLastWord(args, Commands.commands.keySet()); } else { - Single sub = new Single<>(null); + Single sub = new Single<>(null); Commands.commands.values().stream().filter(c -> c.getName().equals(cmd)) .findFirst().ifPresent(sub::push); - return sub.value == null ? sub.value.getTabCompletions(sender, cut(args)) + return sub.value != null ? sub.value.getTabCompletions(sender, cut(args)) : Collections.emptyList(); } } diff --git a/src/main/java/me/towdium/jecalculation/command/commands/CommandHelp.java b/src/main/java/me/towdium/jecalculation/command/commands/CommandHelp.java index 791416f..cf62f7a 100644 --- a/src/main/java/me/towdium/jecalculation/command/commands/CommandHelp.java +++ b/src/main/java/me/towdium/jecalculation/command/commands/CommandHelp.java @@ -1,7 +1,7 @@ package me.towdium.jecalculation.command.commands; import me.towdium.jecalculation.command.Commands; -import me.towdium.jecalculation.command.SubCommand; +import me.towdium.jecalculation.command.ISubCommand; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.util.ChatComponentText; @@ -15,7 +15,7 @@ import java.util.Comparator; * Date: 8/10/17. */ @ParametersAreNonnullByDefault -public class CommandHelp implements SubCommand { +public class CommandHelp implements ISubCommand { @Override public String getName() { return "help"; @@ -27,14 +27,14 @@ public class CommandHelp implements SubCommand { } @Override - public void execute(ICommandSender sender, String[] args) throws CommandException { + public void execute(ICommandSender sender, String[] args) { if (args.length != 0) { sender.addChatMessage(new ChatComponentTranslation("command.common.unexpected_arg", String.join(" ", args))); return; } sender.addChatMessage(new ChatComponentTranslation("command.help.list")); - Commands.commands.values().stream().sorted(Comparator.comparing(SubCommand::getName)) + Commands.commands.values().stream().sorted(Comparator.comparing(ISubCommand::getName)) .forEachOrdered(c -> sender.addChatMessage(new ChatComponentText(c.getUsage(sender)))); } } diff --git a/src/main/java/me/towdium/jecalculation/command/commands/CommandOreDict.java b/src/main/java/me/towdium/jecalculation/command/commands/CommandOreDict.java index 74c9ca7..b0c44a0 100644 --- a/src/main/java/me/towdium/jecalculation/command/commands/CommandOreDict.java +++ b/src/main/java/me/towdium/jecalculation/command/commands/CommandOreDict.java @@ -1,6 +1,6 @@ package me.towdium.jecalculation.command.commands; -import me.towdium.jecalculation.command.SubCommand; +import me.towdium.jecalculation.command.ISubCommand; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.util.ChatComponentText; @@ -14,7 +14,7 @@ import java.util.Arrays; * Date: 17-9-11. */ @ParametersAreNonnullByDefault -public class CommandOreDict implements SubCommand { +public class CommandOreDict implements ISubCommand { @Override public String getName() { return "ore"; @@ -26,7 +26,7 @@ public class CommandOreDict implements SubCommand { } @Override - public void execute(ICommandSender sender, String[] args) throws CommandException { + public void execute(ICommandSender sender, String[] args) { if (args.length == 0) { Arrays.stream(OreDictionary.getOreNames()).forEach(s -> sender.addChatMessage(new ChatComponentText(s))); } // TODO error handling diff --git a/src/main/java/me/towdium/jecalculation/command/commands/CommandState.java b/src/main/java/me/towdium/jecalculation/command/commands/CommandState.java index 63d52a3..27f6cb2 100644 --- a/src/main/java/me/towdium/jecalculation/command/commands/CommandState.java +++ b/src/main/java/me/towdium/jecalculation/command/commands/CommandState.java @@ -1,8 +1,7 @@ package me.towdium.jecalculation.command.commands; import me.towdium.jecalculation.JustEnoughCalculation; -import me.towdium.jecalculation.command.SubCommand; -import me.towdium.jecalculation.utils.Utilities; +import me.towdium.jecalculation.command.ISubCommand; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; import net.minecraft.util.ChatComponentTranslation; @@ -14,7 +13,7 @@ import javax.annotation.ParametersAreNonnullByDefault; * Date: 8/10/17. */ @ParametersAreNonnullByDefault -public class CommandState implements SubCommand { +public class CommandState implements ISubCommand { @Override public String getName() { return "state"; @@ -26,7 +25,7 @@ public class CommandState implements SubCommand { } @Override - public void execute(ICommandSender sender, String[] args) throws CommandException { + public void execute(ICommandSender sender, String[] args) { sender.addChatMessage(new ChatComponentTranslation("command.state.desc", JustEnoughCalculation.side.toString())); } } diff --git a/src/main/java/me/towdium/jecalculation/command/commands/CommandUuid.java b/src/main/java/me/towdium/jecalculation/command/commands/CommandUuid.java index a1d268e..b34da81 100644 --- a/src/main/java/me/towdium/jecalculation/command/commands/CommandUuid.java +++ b/src/main/java/me/towdium/jecalculation/command/commands/CommandUuid.java @@ -1,10 +1,8 @@ package me.towdium.jecalculation.command.commands; -import me.towdium.jecalculation.command.SubCommand; +import me.towdium.jecalculation.command.ISubCommand; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; -import net.minecraft.entity.Entity; -import net.minecraft.server.MinecraftServer; import net.minecraft.util.ChatComponentText; import javax.annotation.ParametersAreNonnullByDefault; @@ -14,7 +12,7 @@ import javax.annotation.ParametersAreNonnullByDefault; * Date: 17-10-15. */ @ParametersAreNonnullByDefault -public class CommandUuid implements SubCommand { +public class CommandUuid implements ISubCommand { @Override public String getName() { return "uuid"; @@ -27,7 +25,7 @@ public class CommandUuid implements SubCommand { @Override - public void execute(ICommandSender sender, String[] args) throws CommandException { + public void execute(ICommandSender sender, String[] args) { sender.addChatMessage(new ChatComponentText(sender.getCommandSenderName())); } } diff --git a/src/main/java/me/towdium/jecalculation/data/label/ILabel.java b/src/main/java/me/towdium/jecalculation/data/label/ILabel.java index a4033c5..4e7d4b9 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/ILabel.java +++ b/src/main/java/me/towdium/jecalculation/data/label/ILabel.java @@ -1,16 +1,15 @@ package me.towdium.jecalculation.data.label; -import com.google.common.base.CaseFormat; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import me.towdium.jecalculation.client.gui.IWPicker; import me.towdium.jecalculation.client.gui.JecGui; import me.towdium.jecalculation.client.gui.guis.pickers.PickerSimple; import me.towdium.jecalculation.client.gui.guis.pickers.PickerUniversal; -import me.towdium.jecalculation.data.label.labels.LabelFluidStack; -import me.towdium.jecalculation.data.label.labels.LabelItemStack; -import me.towdium.jecalculation.data.label.labels.LabelOreDict; -import me.towdium.jecalculation.data.label.labels.LabelUniversal; +import me.towdium.jecalculation.data.label.labels.LFluidStack; +import me.towdium.jecalculation.data.label.labels.LItemStack; +import me.towdium.jecalculation.data.label.labels.LOreDict; +import me.towdium.jecalculation.data.label.labels.LString; import me.towdium.jecalculation.polyfill.mc.client.renderer.GlStateManager; import me.towdium.jecalculation.utils.ItemStackHelper; import me.towdium.jecalculation.utils.Utilities.Relation; @@ -31,12 +30,12 @@ import java.util.stream.Collectors; */ @ParametersAreNonnullByDefault public interface ILabel { - RegistryMerger MERGER = RegistryMerger.INSTANCE; - RegistryDeserializer DESERIALIZER = RegistryDeserializer.INSTANCE; - RegistryConverterItem CONVERTER_ITEM = RegistryConverterItem.INSTANCE; - RegistryConverterFluid CONVERTER_FLUID = RegistryConverterFluid.INSTANCE; - RegistryEditor EDITOR = RegistryEditor.INSTANCE; - ILabel EMPTY = new LabelItemStack(ItemStackHelper.EMPTY_ITEM_STACK, 0); + RegistryMerger MERGER = new RegistryMerger(); + RegistrySerializer DESERIALIZER = new RegistrySerializer(); + RegistryConverterItem CONVERTER_ITEM = new RegistryConverterItem(); + RegistryConverterFluid CONVERTER_FLUID = new RegistryConverterFluid(); + RegistryEditor EDITOR = new RegistryEditor(); + ILabel EMPTY = new LItemStack(ItemStackHelper.EMPTY_ITEM_STACK, 0); String FORMAT_BLUE = "\u00A79"; String FORMAT_GREY = "\u00A78"; @@ -55,14 +54,11 @@ public interface ILabel { @SideOnly(Side.CLIENT) String getDisplayName(); - static String getIdentifier(ILabel c) { - return getIdentifier(c.getClass()); - } - - static String getIdentifier(Class c) { - String s = c.getSimpleName(); - if (s.startsWith("Label")) s = s.substring(5); - return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, s); + static void initServer() { + DESERIALIZER.register(LFluidStack.IDENTIFIER, LFluidStack::new); + DESERIALIZER.register(LItemStack.IDENTIFIER, LItemStack::new); + DESERIALIZER.register(LOreDict.IDENTIFIER, LOreDict::new); + DESERIALIZER.register(LString.IDENTIFIER, LString::new); } List getToolTip(List existing, boolean detailed); @@ -71,6 +67,17 @@ public interface ILabel { NBTTagCompound toNBTTagCompound(); + static void initClient() { + CONVERTER_ITEM.register(LOreDict::guess); + EDITOR.register(PickerSimple.FluidStack::new, "fluid_stack", new LFluidStack(FluidRegistry.WATER, 1000)); + EDITOR.register(PickerSimple.OreDict::new, "ore_dict", new LOreDict("ingotIron")); + EDITOR.register(PickerUniversal::new, "string", new LString("example", 1)); + MERGER.register("itemStack", "itemStack", ILabel.RegistryMerger::mergeItemStackNItemStack); + MERGER.register("oreDict", "oreDict", ILabel.RegistryMerger::mergeOreDictNOreDict); + } + + String getIdentifier(); + @SideOnly(Side.CLIENT) default void drawLabel(JecGui gui, int xPos, int yPos, boolean center) { GlStateManager.pushMatrix(); @@ -87,19 +94,9 @@ public interface ILabel { * {@link ILabel label(s)}. * It uses singleton mode. First registerGuess merge functions, then use * {@link #merge(ILabel, ILabel, boolean)} to operate the {@link ILabel}. - * For registering, see {@link RegistryDeserializer}. + * For registering, see {@link RegistrySerializer}. */ class RegistryMerger { - public static final RegistryMerger INSTANCE; - - static { - INSTANCE = new RegistryMerger(); - // registerGuess functions here - INSTANCE.register("itemStack", "itemStack", RegistryMerger::mergeItemStackNItemStack); - INSTANCE.register("oreDict", "oreDict", RegistryMerger::mergeOreDictNOreDict); - } - - private Relation functions = new Relation<>(); @@ -111,15 +108,15 @@ public interface ILabel { } static Optional mergeItemStackNItemStack(ILabel a, ILabel b, boolean add) { - if (a instanceof LabelItemStack && b instanceof LabelItemStack) { - LabelItemStack lisA = (LabelItemStack) a; - LabelItemStack lisB = (LabelItemStack) b; + if (a instanceof LItemStack && b instanceof LItemStack) { + LItemStack lisA = (LItemStack) a; + LItemStack lisB = (LItemStack) b; ItemStack isA = lisA.getItemStack(); ItemStack isB = lisB.getItemStack(); if (isA.getItem() == isB.getItem() && isA.getItemDamage() == isB.getItemDamage() && (isA.getTagCompound() == null ? !isB.hasTagCompound() : isA.getTagCompound().equals(isB.getTagCompound()))) { - LabelItemStack ret = new LabelItemStack(lisA.getItemStack(), + LItemStack ret = new LItemStack(lisA.getItemStack(), add ? lisA.getAmount() + lisB.getAmount() : lisA.getAmount() - lisB.getAmount()); return Optional.of(ret); } @@ -128,18 +125,18 @@ public interface ILabel { } static Optional mergeOreDictNOreDict(ILabel a, ILabel b, boolean add) { - if (a instanceof LabelOreDict && b instanceof LabelOreDict) { - LabelOreDict lodA = (LabelOreDict) a; - LabelOreDict lodB = (LabelOreDict) b; + if (a instanceof LOreDict && b instanceof LOreDict) { + LOreDict lodA = (LOreDict) a; + LOreDict lodB = (LOreDict) b; if (lodA.getName().equals(lodB.getName())) - return Optional.of(new LabelOreDict(lodA.getName(), + return Optional.of(new LOreDict(lodA.getName(), add ? lodA.getAmount() + lodB.getAmount() : lodA.getAmount() - lodB.getAmount())); } return Optional.empty(); } public Optional merge(ILabel a, ILabel b, boolean add) { - Optional ret = functions.get(ILabel.getIdentifier(a), ILabel.getIdentifier(b)) + Optional ret = functions.get(a.getIdentifier(), b.getIdentifier()) .orElse((x, y, f) -> Optional.empty()).merge(a, b, add); if (ret.isPresent() && (ret.get() == a || ret.get() == b)) throw new RuntimeException("Merger should not modify the given label."); @@ -164,22 +161,13 @@ public interface ILabel { * Here you can find the identifier and deserializer of one type. * For {@link ILabel} operations, see {@link RegistryMerger} */ - class RegistryDeserializer { + class RegistrySerializer { public static final String KEY_IDENTIFIER = "identifier"; public static final String KEY_CONTENT = "content"; - public static final RegistryDeserializer INSTANCE; - - static { - INSTANCE = new RegistryDeserializer(); - - INSTANCE.register(ILabel.getIdentifier(LabelOreDict.class), LabelOreDict::new); - INSTANCE.register(ILabel.getIdentifier(LabelItemStack.class), LabelItemStack::new); - INSTANCE.register(ILabel.getIdentifier(LabelFluidStack.class), LabelFluidStack::new); - } private HashMap> idToData = new HashMap<>(); - private RegistryDeserializer() { + private RegistrySerializer() { } public void register(String identifier, Function deserializer) { @@ -208,7 +196,7 @@ public interface ILabel { public NBTTagCompound serialize(ILabel label) { NBTTagCompound ret = new NBTTagCompound(); - ret.setString(KEY_CONTENT, ILabel.getIdentifier(label)); + ret.setString(KEY_IDENTIFIER, label.getIdentifier()); ret.setTag(KEY_CONTENT, label.toNBTTagCompound()); return ret; } @@ -235,59 +223,33 @@ public interface ILabel { .collect(Collectors.toList()); } - void register(Function, List> handler) { + public void register(Function, List> handler) { handlers.add(handler); } } class RegistryConverterItem extends RegistryConverter { - public static final RegistryConverterItem INSTANCE; - - static { - INSTANCE = new RegistryConverterItem(); - - INSTANCE.register(LabelOreDict::guess); - } - private RegistryConverterItem() { } @Override public ILabel toLabel(ItemStack ingredient) { - return new LabelItemStack(ingredient); + return new LItemStack(ingredient); } } class RegistryConverterFluid extends RegistryConverter { - public static final RegistryConverterFluid INSTANCE; - - static { - INSTANCE = new RegistryConverterFluid(); - } private RegistryConverterFluid() { } @Override public ILabel toLabel(net.minecraftforge.fluids.FluidStack ingredient) { - return new LabelFluidStack(new net.minecraftforge.fluids.FluidStack(ingredient.getFluid(), 1000), ingredient.amount); + return new LFluidStack(new net.minecraftforge.fluids.FluidStack(ingredient.getFluid(), 1000), ingredient.amount); } } class RegistryEditor { - public static final RegistryEditor INSTANCE; - - static { - INSTANCE = new RegistryEditor(); - - INSTANCE.register(PickerSimple.OreDict::new, "common.label.ore_dict", - new LabelOreDict("ingotIron")); - INSTANCE.register(PickerSimple.FluidStack::new, "common.label.fluid_stack", - new LabelFluidStack(FluidRegistry.WATER, 1000)); - INSTANCE.register(PickerUniversal::new, "common.label.universal", - new LabelUniversal("example", 1)); - } - private ArrayList records = new ArrayList<>(); @@ -295,7 +257,7 @@ public interface ILabel { } public void register(Supplier editor, String unlocalizedName, ILabel representation) { - records.add(new Record(editor, unlocalizedName, representation)); + records.add(new Record(editor, "common.label." + unlocalizedName, representation)); } diff --git a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelFluidStack.java b/src/main/java/me/towdium/jecalculation/data/label/labels/LFluidStack.java similarity index 80% rename from src/main/java/me/towdium/jecalculation/data/label/labels/LabelFluidStack.java rename to src/main/java/me/towdium/jecalculation/data/label/labels/LFluidStack.java index dca3081..53f03a6 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelFluidStack.java +++ b/src/main/java/me/towdium/jecalculation/data/label/labels/LFluidStack.java @@ -18,23 +18,24 @@ import java.util.List; * Date: 17-9-27. */ @ParametersAreNonnullByDefault -public class LabelFluidStack implements ILabel { +public class LFluidStack implements ILabel { + public static final String IDENTIFIER = "fluidStack"; public static final String KEY_FLUID = "name"; public static final String KEY_AMOUNT = "amount"; FluidStack fluid; int amount; - public LabelFluidStack(FluidStack fluid, int amount) { + public LFluidStack(FluidStack fluid, int amount) { this.fluid = fluid; this.amount = amount; } - public LabelFluidStack(Fluid fluid, int amount) { + public LFluidStack(Fluid fluid, int amount) { this(new FluidStack(fluid, 1000), amount); } - public LabelFluidStack(NBTTagCompound nbt) { + public LFluidStack(NBTTagCompound nbt) { fluid = FluidStack.loadFluidStackFromNBT(nbt.getCompoundTag(KEY_FLUID)); amount = nbt.getInteger(KEY_AMOUNT); } @@ -70,15 +71,20 @@ public class LabelFluidStack implements ILabel { return fluid.getLocalizedName(); } + @Override + public String getIdentifier() { + return IDENTIFIER; + } + @Override public ILabel copy() { - return new LabelFluidStack(fluid, amount); + return new LFluidStack(fluid, amount); } @Override public NBTTagCompound toNBTTagCompound() { NBTTagCompound ret = new NBTTagCompound(); - ret.setTag(KEY_AMOUNT, fluid.writeToNBT(new NBTTagCompound())); + ret.setTag(KEY_FLUID, fluid.writeToNBT(new NBTTagCompound())); // TODO check ret.setInteger(KEY_AMOUNT, amount); return ret; } @@ -99,8 +105,8 @@ public class LabelFluidStack implements ILabel { @Override public boolean equals(Object obj) { - return obj instanceof LabelFluidStack - && fluid.equals(((LabelFluidStack) obj).fluid) && amount == ((LabelFluidStack) obj).amount; + return obj instanceof LFluidStack + && fluid.equals(((LFluidStack) obj).fluid) && amount == ((LFluidStack) obj).amount; } @Override diff --git a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelItemStack.java b/src/main/java/me/towdium/jecalculation/data/label/labels/LItemStack.java similarity index 78% rename from src/main/java/me/towdium/jecalculation/data/label/labels/LabelItemStack.java rename to src/main/java/me/towdium/jecalculation/data/label/labels/LItemStack.java index a3d344e..2c19d5a 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelItemStack.java +++ b/src/main/java/me/towdium/jecalculation/data/label/labels/LItemStack.java @@ -16,28 +16,28 @@ import java.util.List; * Date: 8/11/17. */ @ParametersAreNonnullByDefault -public class LabelItemStack extends LabelSimpleAmount { - public static final String IDENTIFIER = "oreDict"; +public class LItemStack extends LabelSimpleAmount { + public static final String IDENTIFIER = "itemStack"; public static final String KEY_STACK = "stack"; ItemStack itemStack; - public LabelItemStack(ItemStack is) { + public LItemStack(ItemStack is) { this(is, is.stackSize); } // I will copy it! - public LabelItemStack(ItemStack is, int amount) { + public LItemStack(ItemStack is, int amount) { super(amount); itemStack = is.copy(); } - public LabelItemStack(NBTTagCompound nbt) { + public LItemStack(NBTTagCompound nbt) { super(nbt); itemStack = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag(KEY_STACK)); } - private LabelItemStack(LabelItemStack lis) { + private LItemStack(LItemStack lis) { super(lis); itemStack = lis.itemStack; } @@ -56,9 +56,14 @@ public class LabelItemStack extends LabelSimpleAmount { return itemStack.getItem() == null ? "" : itemStack.getDisplayName(); } + @Override + public String getIdentifier() { + return IDENTIFIER; + } + @Override public ILabel copy() { - return new LabelItemStack(this); + return new LItemStack(this); } @Override @@ -77,10 +82,12 @@ public class LabelItemStack extends LabelSimpleAmount { @Override public boolean equals(Object obj) { - return obj instanceof LabelItemStack - && itemStack.equals(((LabelItemStack) obj).itemStack) && amount == ((LabelItemStack) obj).amount; + return obj instanceof LItemStack + && ItemStack.areItemStacksEqual(itemStack, ((LItemStack) obj).itemStack) + && amount == ((LItemStack) obj).amount; } + @Override public int hashCode() { return itemStack.getItemDamage() ^ itemStack.getItem().getUnlocalizedName().hashCode() ^ amount diff --git a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelOreDict.java b/src/main/java/me/towdium/jecalculation/data/label/labels/LOreDict.java similarity index 84% rename from src/main/java/me/towdium/jecalculation/data/label/labels/LabelOreDict.java rename to src/main/java/me/towdium/jecalculation/data/label/labels/LOreDict.java index 77c5899..1df54f4 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelOreDict.java +++ b/src/main/java/me/towdium/jecalculation/data/label/labels/LOreDict.java @@ -24,28 +24,32 @@ import java.util.stream.Collectors; * Date: 17-9-10. */ @ParametersAreNonnullByDefault -public class LabelOreDict extends LabelSimpleAmount { +public class LOreDict extends LabelSimpleAmount { public static final String IDENTIFIER = "oreDict"; public static final String KEY_NAME = "name"; public static final String KEY_AMOUNT = "amount"; + static { + + } + protected String name; - public LabelOreDict(String name) { + public LOreDict(String name) { this(name, 1); } - public LabelOreDict(String name, int amount) { + public LOreDict(String name, int amount) { super(amount); this.name = name; } - public LabelOreDict(LabelOreDict lod) { + public LOreDict(LOreDict lod) { super(lod); this.name = lod.name; } - public LabelOreDict(NBTTagCompound nbt) { + public LOreDict(NBTTagCompound nbt) { super(nbt); name = nbt.getString(KEY_NAME); } @@ -63,12 +67,17 @@ public class LabelOreDict extends LabelSimpleAmount { for (int i : OreDictionary.getOreIDs(is)) if (!ids.contains(i)) ids.remove(i); }); - return ids.stream().map(i -> new LabelOreDict(OreDictionary.getOreName(i))).collect(Collectors.toList()); + return ids.stream().map(i -> new LOreDict(OreDictionary.getOreName(i))).collect(Collectors.toList()); } @Override - public LabelOreDict copy() { - return new LabelOreDict(this); + public String getIdentifier() { + return IDENTIFIER; + } + + @Override + public LOreDict copy() { + return new LOreDict(this); } @Override @@ -113,8 +122,8 @@ public class LabelOreDict extends LabelSimpleAmount { @Override public boolean equals(Object obj) { - return obj instanceof LabelOreDict - && amount == ((LabelOreDict) obj).amount && name.equals(((LabelOreDict) obj).name); + return obj instanceof LOreDict + && amount == ((LOreDict) obj).amount && name.equals(((LOreDict) obj).name); } public String getName() { diff --git a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelUniversal.java b/src/main/java/me/towdium/jecalculation/data/label/labels/LString.java similarity index 73% rename from src/main/java/me/towdium/jecalculation/data/label/labels/LabelUniversal.java rename to src/main/java/me/towdium/jecalculation/data/label/labels/LString.java index 4884c7c..e889c7d 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelUniversal.java +++ b/src/main/java/me/towdium/jecalculation/data/label/labels/LString.java @@ -15,17 +15,23 @@ import javax.annotation.ParametersAreNonnullByDefault; * Date: 17-9-28. */ @ParametersAreNonnullByDefault -public class LabelUniversal extends LabelSimpleAmount { +public class LString extends LabelSimpleAmount { public static final String KEY_NAME = "name"; + public static final String IDENTIFIER = "string"; String name; - public LabelUniversal(String name, int amount) { + public LString(NBTTagCompound tag) { + super(tag); + this.name = tag.getString(KEY_NAME); + } + + public LString(String name, int amount) { super(amount); this.name = name; } - public LabelUniversal(LabelUniversal lu) { + public LString(LString lu) { super(lu); name = lu.name; } @@ -36,9 +42,14 @@ public class LabelUniversal extends LabelSimpleAmount { return Utilities.L18n.format("label.universal.name", name); } + @Override + public String getIdentifier() { + return "string"; + } + @Override public ILabel copy() { - return new LabelUniversal(this); + return new LString(this); } @Override @@ -57,8 +68,8 @@ public class LabelUniversal extends LabelSimpleAmount { @Override public boolean equals(Object obj) { - return obj instanceof LabelUniversal - && name.equals(((LabelUniversal) obj).name) && amount == ((LabelUniversal) obj).amount; + return obj instanceof LString + && name.equals(((LString) obj).name) && amount == ((LString) obj).amount; } @Override diff --git a/src/main/java/me/towdium/jecalculation/data/structure/Recipe.java b/src/main/java/me/towdium/jecalculation/data/structure/Recipe.java index 765f1e3..8be11ee 100644 --- a/src/main/java/me/towdium/jecalculation/data/structure/Recipe.java +++ b/src/main/java/me/towdium/jecalculation/data/structure/Recipe.java @@ -1,7 +1,7 @@ package me.towdium.jecalculation.data.structure; import me.towdium.jecalculation.data.label.ILabel; -import me.towdium.jecalculation.polyfill.Polyfill; +import me.towdium.jecalculation.polyfill.NBTHelper; import me.towdium.jecalculation.utils.IllegalPositionException; import me.towdium.jecalculation.utils.wrappers.Single; import net.minecraft.nbt.NBTTagCompound; @@ -61,7 +61,7 @@ public class Recipe { } static private List readNbtList(NBTTagList list) { - return StreamSupport.stream(Polyfill.spliterator(list), false) + return StreamSupport.stream(NBTHelper.spliterator(list), false) .filter(n -> n instanceof NBTTagCompound) .map(n -> ILabel.DESERIALIZER.deserialize((NBTTagCompound) n)) .collect(Collectors.toList()); diff --git a/src/main/java/me/towdium/jecalculation/data/structure/User.java b/src/main/java/me/towdium/jecalculation/data/structure/User.java index 70d992b..44b1cfe 100644 --- a/src/main/java/me/towdium/jecalculation/data/structure/User.java +++ b/src/main/java/me/towdium/jecalculation/data/structure/User.java @@ -1,14 +1,15 @@ package me.towdium.jecalculation.data.structure; import me.towdium.jecalculation.data.label.ILabel; -import me.towdium.jecalculation.data.structure.Recipe; -import me.towdium.jecalculation.polyfill.Polyfill; +import me.towdium.jecalculation.polyfill.NBTHelper; import me.towdium.jecalculation.utils.Utilities; -import me.towdium.jecalculation.utils.wrappers.Pair; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; -import java.util.*; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; import java.util.function.BiConsumer; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -23,33 +24,25 @@ public class User { public static final String KEY_RECENT = "recent"; public Recipes recipes; - public Recent recents; + public Recent recent; // client mode only public User() { recipes = new Recipes(); - recents = new Recent(); + recent = new Recent(); } public User(NBTTagCompound nbt) { recipes = new Recipes(nbt.getTagList(KEY_RECIPE, 10)); - recents = new Recent(nbt.getTagList(KEY_RECENT, 10)); + recent = new Recent(nbt.getTagList(KEY_RECENT, 10)); } public NBTTagCompound serialize() { NBTTagCompound ret = new NBTTagCompound(); ret.setTag(KEY_RECIPE, recipes.serialize()); - ret.setTag(KEY_RECENT, recents.serialize()); + ret.setTag(KEY_RECENT, recent.serialize()); return ret; } - public List search(String group, ILabel label, Recipe.enumIoType type) { - Utilities.INDListBuilder builder = new Utilities.INDListBuilder<>(); - IntStream.range(0, type.getSize()).forEach(i -> recipes.get(group).ifPresent(rs -> rs.forEach(r -> { - ILabel.MERGER.merge(label, r.getLabel(type)[i], true).ifPresent(l -> builder.add(r)); - }))); - return builder.build(); - } - public static class Recipes { public static final String KEY_NAME = "name"; public static final String KEY_CONTENT = "content"; @@ -60,13 +53,15 @@ public class User { } public Recipes(NBTTagList nbt) { - StreamSupport.stream(Polyfill.spliterator(nbt), false).filter(g -> g instanceof NBTTagCompound).forEach(g -> { - NBTTagCompound group = (NBTTagCompound) g; - String name = group.getString(KEY_NAME); - NBTTagList recipes = group.getTagList(KEY_CONTENT, 10); - StreamSupport.stream(Polyfill.spliterator(recipes), false).filter(r -> r instanceof NBTTagCompound) - .map(r -> (NBTTagCompound) r).forEach(r -> add(name, new Recipe(r))); - }); + StreamSupport.stream(NBTHelper.spliterator(nbt), false).filter(g -> g instanceof NBTTagCompound) + .forEach(g -> { + NBTTagCompound group = (NBTTagCompound) g; + String name = group.getString(KEY_NAME); + NBTTagList recipes = group.getTagList(KEY_CONTENT, 10); + StreamSupport.stream(NBTHelper.spliterator(recipes), false) + .filter(r -> r instanceof NBTTagCompound).map(r -> (NBTTagCompound) r) + .forEach(r -> add(name, new Recipe(r))); + }); } public void add(String group, Recipe recipe) { @@ -80,15 +75,21 @@ public class User { public void set(String group, int index, Recipe recipe) { records.get(group).orElseGet(() -> { throw new RuntimeException("Group not found: " + group + "."); - }) - .set(index, recipe); + }).set(index, recipe); } public void remove(String group, int index) { records.get(group).orElseGet(() -> { throw new RuntimeException("Group not found: " + group + "."); - }) - .remove(index); + }).remove(index); + } + + public List search(String group, ILabel label, Recipe.enumIoType type) { + HashSet set = new HashSet<>(); + IntStream.range(0, type.getSize()).forEach(i -> get(group).ifPresent(rs -> rs.forEach(r -> { + ILabel.MERGER.merge(label, r.getLabel(type)[i], true).ifPresent(l -> set.add(r)); + }))); + return new ArrayList<>(set); } public void forEach(BiConsumer> consumer) { @@ -115,9 +116,10 @@ public class User { public static class Recent { Utilities.Recent record = new Utilities.Recent<>(9); + public static final String IDENTIFIER = "recent"; public Recent(NBTTagList nbt) { - List ls = StreamSupport.stream(Polyfill.spliterator(nbt), false) + List ls = StreamSupport.stream(NBTHelper.spliterator(nbt), false) .filter(n -> n instanceof NBTTagCompound) .map(n -> ILabel.DESERIALIZER.deserialize((NBTTagCompound) n)) .collect(Collectors.toList()); @@ -136,8 +138,7 @@ public class User { } public List getRecords() { - List labels = record.toList(); - return labels.subList(1, labels.size()); + return record.toList(); } public NBTTagList serialize() { diff --git a/src/main/java/me/towdium/jecalculation/event/handlers/ControllerClient.java b/src/main/java/me/towdium/jecalculation/event/handlers/ControllerClient.java index eff93b6..09426db 100644 --- a/src/main/java/me/towdium/jecalculation/event/handlers/ControllerClient.java +++ b/src/main/java/me/towdium/jecalculation/event/handlers/ControllerClient.java @@ -27,14 +27,17 @@ public class ControllerClient { public static void addRecipe(String group, Recipe recipe) { getRecord().recipes.add(group, recipe); + // TODO sync } public static void setRecipe(String group, int index, Recipe recipe) { getRecord().recipes.set(group, index, recipe); + // TODO sync } public static void removeRecipe(String group, int index) { getRecord().recipes.remove(group, index); + // TODO sync } /** @@ -48,11 +51,19 @@ public class ControllerClient { } @SubscribeEvent - public static void onLogOut(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) { + public void onLogOut(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) { recordWorld = null; } - public List search(String group, ILabel label, Recipe.enumIoType type) { - return getRecord().search(group, label, type); + public static List search(String group, ILabel label, Recipe.enumIoType type) { + return getRecord().recipes.search(group, label, type); + } + + public static User.Recent getRecent() { + return getRecord().recent; + } + + public static void setRecent(ILabel label) { + getRecord().recent.push(label); // no need to sync because client only } } diff --git a/src/main/java/me/towdium/jecalculation/event/handlers/InputEventHandler.java b/src/main/java/me/towdium/jecalculation/event/handlers/InputEventHandler.java index f1f9592..98a7aa7 100644 --- a/src/main/java/me/towdium/jecalculation/event/handlers/InputEventHandler.java +++ b/src/main/java/me/towdium/jecalculation/event/handlers/InputEventHandler.java @@ -19,7 +19,7 @@ import net.minecraftforge.client.event.GuiScreenEvent; */ public class InputEventHandler { @SideOnly(Side.CLIENT) - @SubscribeEvent + @SubscribeEvent(receiveCanceled = true) public void onKey(InputEvent.KeyInputEvent event) { if (ProxyClient.keyOpenGui.isPressed()) { if (JustEnoughCalculation.side == JustEnoughCalculation.enumSide.CLIENT) JecGui.displayGui(new GuiCalculator()); @@ -27,7 +27,7 @@ public class InputEventHandler { } } - @SubscribeEvent(priority = EventPriority.HIGH) + @SubscribeEvent(priority = EventPriority.HIGHEST) public void onMouseClick(GuiScreenEvent.ActionPerformedEvent.Pre event) { if (event.gui instanceof JecGui) { JustEnoughCalculation.logger.info("on mouse click in gui screen event"); diff --git a/src/main/java/me/towdium/jecalculation/nei/NEIPlugin.java b/src/main/java/me/towdium/jecalculation/nei/NEIPlugin.java index 1d9c3c3..1b7eaa4 100644 --- a/src/main/java/me/towdium/jecalculation/nei/NEIPlugin.java +++ b/src/main/java/me/towdium/jecalculation/nei/NEIPlugin.java @@ -5,8 +5,6 @@ import me.towdium.jecalculation.data.label.ILabel; import net.minecraft.item.ItemStack; public class NEIPlugin { - public static final ILabel.RegistryConverterItem registryItem = ILabel.RegistryConverterItem.INSTANCE; - public static final ILabel.RegistryConverterFluid registryFluid = ILabel.RegistryConverterFluid.INSTANCE; public static void init() { GuiContainerManager.addTooltipHandler(new JecaTooltipHandler()); @@ -16,7 +14,8 @@ public class NEIPlugin { public static ILabel getLabelUnderMouse() { if (NEIPlugin.currentItemStack == null) return ILabel.EMPTY; - else return registryItem.toLabel(NEIPlugin.currentItemStack); + else return ILabel.CONVERTER_ITEM.toLabel(NEIPlugin.currentItemStack); + // else if (o instanceof FluidStack) return ILabel.CONVERTER_FLUID.toLabel(((FluidStack) o)); } public static void setLabelUnderMouse(ItemStack itemStack) { diff --git a/src/main/java/me/towdium/jecalculation/network/ProxyClient.java b/src/main/java/me/towdium/jecalculation/network/ProxyClient.java index 945a828..4a9153e 100644 --- a/src/main/java/me/towdium/jecalculation/network/ProxyClient.java +++ b/src/main/java/me/towdium/jecalculation/network/ProxyClient.java @@ -7,34 +7,39 @@ import me.towdium.jecalculation.JustEnoughCalculation; import me.towdium.jecalculation.client.gui.JecGui; import me.towdium.jecalculation.client.gui.guis.GuiCalculator; import me.towdium.jecalculation.command.JecaCommand; +import me.towdium.jecalculation.data.label.ILabel; import me.towdium.jecalculation.event.Handlers; import me.towdium.jecalculation.item.ItemCalculator; import net.minecraft.client.settings.KeyBinding; import net.minecraftforge.client.ClientCommandHandler; import org.lwjgl.input.Keyboard; -public class ProxyClient implements IProxy { +public class ProxyClient extends ProxyServer { public static final KeyBinding keyOpenGui = new KeyBinding("key.open_gui", Keyboard.KEY_NONE, "key.category"); + @Override + public void initPre() { + super.initPre(); + } @Override public void initPost() { - GameRegistry.registerItem(ItemCalculator.INSTANCE, ItemCalculator.INSTANCE.getUnlocalizedName()); - Handlers.handlers.forEach(FMLCommonHandler.instance().bus()::register); + super.initPost(); ClientCommandHandler.instance.registerCommand(new JecaCommand()); - new JecGui(null, new GuiCalculator()); + ILabel.initClient(); } @Override public void init() { - JustEnoughCalculation.logger.info("client proxy init"); + super.init(); ClientRegistry.registerKeyBinding(keyOpenGui); } @Override public void displayCalculator() { + super.displayCalculator(); JecGui.displayGui(new GuiCalculator()); } } diff --git a/src/main/java/me/towdium/jecalculation/network/ProxyServer.java b/src/main/java/me/towdium/jecalculation/network/ProxyServer.java index d8f7f99..6757163 100644 --- a/src/main/java/me/towdium/jecalculation/network/ProxyServer.java +++ b/src/main/java/me/towdium/jecalculation/network/ProxyServer.java @@ -2,13 +2,22 @@ package me.towdium.jecalculation.network; import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.registry.GameRegistry; -import me.towdium.jecalculation.JustEnoughCalculation; +import me.towdium.jecalculation.client.gui.JecGui; +import me.towdium.jecalculation.data.label.ILabel; import me.towdium.jecalculation.event.Handlers; import me.towdium.jecalculation.item.ItemCalculator; +import net.minecraftforge.common.MinecraftForge; public class ProxyServer implements IProxy { + @Override public void initPre() { GameRegistry.registerItem(ItemCalculator.INSTANCE, ItemCalculator.INSTANCE.getUnlocalizedName()); Handlers.handlers.forEach(FMLCommonHandler.instance().bus()::register); + MinecraftForge.EVENT_BUS.register(JecGui.class); + } + + @Override + public void initPost() { + ILabel.initServer(); } } diff --git a/src/main/java/me/towdium/jecalculation/polyfill/Polyfill.java b/src/main/java/me/towdium/jecalculation/polyfill/NBTHelper.java similarity index 74% rename from src/main/java/me/towdium/jecalculation/polyfill/Polyfill.java rename to src/main/java/me/towdium/jecalculation/polyfill/NBTHelper.java index 99dbb73..26101ca 100644 --- a/src/main/java/me/towdium/jecalculation/polyfill/Polyfill.java +++ b/src/main/java/me/towdium/jecalculation/polyfill/NBTHelper.java @@ -12,7 +12,7 @@ import java.util.List; import java.util.Spliterator; import java.util.Spliterators; -public class Polyfill { +public class NBTHelper { /** * Get an NBTTagCompound from this stack's NBT data. * @@ -38,4 +38,14 @@ public class Polyfill { return Spliterators.spliteratorUnknownSize(((List)tagList).iterator(), 0); } + public static NBTTagCompound getOrCreateSubCompound(ItemStack itemStack, String key) { + if (itemStack.stackTagCompound != null && itemStack.stackTagCompound.hasKey(key, 10)) { + return itemStack.stackTagCompound.getCompoundTag(key); + } else { + NBTTagCompound nbtTagCompound = new NBTTagCompound(); + itemStack.setTagInfo(key, nbtTagCompound); + return nbtTagCompound; + } + } + } diff --git a/src/main/java/me/towdium/jecalculation/utils/ItemStackHelper.java b/src/main/java/me/towdium/jecalculation/utils/ItemStackHelper.java index c0ff3ba..408fe8e 100644 --- a/src/main/java/me/towdium/jecalculation/utils/ItemStackHelper.java +++ b/src/main/java/me/towdium/jecalculation/utils/ItemStackHelper.java @@ -1,7 +1,7 @@ package me.towdium.jecalculation.utils; import me.towdium.jecalculation.JustEnoughCalculation; -import me.towdium.jecalculation.polyfill.Polyfill; +import me.towdium.jecalculation.polyfill.NBTHelper; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -253,7 +253,7 @@ public class ItemStackHelper { static NBTTagCompound getTag(@Nonnull ItemStack itemStack, boolean isolate) { if (isolate) { - return Polyfill.getSubCompound(itemStack, JustEnoughCalculation.Reference.MODID, true); + return NBTHelper.getSubCompound(itemStack, JustEnoughCalculation.Reference.MODID, true); } else { if (!itemStack.hasTagCompound()) { itemStack.setTagCompound(new NBTTagCompound()); diff --git a/src/main/java/me/towdium/jecalculation/utils/Utilities.java b/src/main/java/me/towdium/jecalculation/utils/Utilities.java index 8e26f33..fe79505 100644 --- a/src/main/java/me/towdium/jecalculation/utils/Utilities.java +++ b/src/main/java/me/towdium/jecalculation/utils/Utilities.java @@ -6,10 +6,13 @@ import cpw.mods.fml.common.ModContainer; import cpw.mods.fml.common.registry.GameData; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; +import me.towdium.jecalculation.JustEnoughCalculation; +import me.towdium.jecalculation.polyfill.NBTHelper; import me.towdium.jecalculation.utils.wrappers.Pair; import me.towdium.jecalculation.utils.wrappers.Single; import net.minecraft.client.resources.I18n; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.fluids.FluidStack; import org.apache.commons.lang3.StringEscapeUtils; @@ -18,6 +21,7 @@ import javax.annotation.ParametersAreNonnullByDefault; import java.util.*; import java.util.function.BiConsumer; import java.util.function.BiFunction; +import java.util.function.BiPredicate; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -87,6 +91,10 @@ public class Utilities { return dictionary.get(stack.getFluid().getStillIcon().getIconName().split(":")[0]); } + public static NBTTagCompound getTag(ItemStack is) { + return NBTHelper.getOrCreateSubCompound(is, JustEnoughCalculation.Reference.MODID); + } + public static boolean contains(String s1, String s2) { return s1.contains(s2); } @@ -223,14 +231,20 @@ public class Utilities { public static class Recent { LinkedList data = new LinkedList<>(); + BiPredicate tester; int limit; + public Recent(BiPredicate tester, int limit) { + this.tester = tester; + this.limit = limit; + } + public Recent(int limit) { this.limit = limit; } public void push(T obj) { - data.removeIf(t -> t.equals(obj)); + data.removeIf(t -> tester != null ? tester.test(t, obj) : t.equals(obj)); data.push(obj); if (data.size() > limit) data.pop(); @@ -241,23 +255,7 @@ public class Utilities { return (List) data.clone(); } } - - /** - * Immutable non-duplicated list builder - */ - public static class INDListBuilder { - public ImmutableList.Builder builder = ImmutableList.builder(); - public HashSet set = new HashSet<>(); - - public void add(T obj) { - if (set.add(obj)) builder.add(obj); - } - - public ImmutableList build() { - return builder.build(); - } - } - + public static class OrderedHashMap { public HashMap index = new HashMap<>(); public List> value = new ArrayList<>(); diff --git a/src/main/resources/assets/jecalculation/lang/en_US.lang b/src/main/resources/assets/jecalculation/lang/en_US.lang index 45073dd..4c3d18e 100644 --- a/src/main/resources/assets/jecalculation/lang/en_US.lang +++ b/src/main/resources/assets/jecalculation/lang/en_US.lang @@ -10,10 +10,10 @@ chat.server_mode=The mod is not working in client mode. Key binding will not wor gui.gui_calculator.new_recipe.name=New recipe gui.common.label.ore_dict=OreDict gui.common.label.fluid_stack=Fluid -gui.common.label.universal=Universal +gui.common.label.string=String config.force_client=Force Client label.ore_dict.name=Ore: %s -label.universal.name=Label: %s +label.string.name=Label: %s label.common.tooltip.amount=Amount: %s