diff --git a/src/main/java/me/towdium/jecalculation/JecaConfig.java b/src/main/java/me/towdium/jecalculation/JecaConfig.java index bafcd8e..c2a6230 100644 --- a/src/main/java/me/towdium/jecalculation/JecaConfig.java +++ b/src/main/java/me/towdium/jecalculation/JecaConfig.java @@ -10,16 +10,16 @@ public class JecaConfig { public static boolean initialized = false; public static Configuration config; - public static boolean isForceClient() { - return EnumItems.ForceClient.getProperty().getBoolean(); + public static boolean isClintMode() { + return EnumItems.ClientMode.getProperty().getBoolean(); } public enum EnumItems { - ForceClient, EnableInventoryCheck, ListRecipeBlackList, ListRecipeCategory; + ClientMode, EnableInventoryCheck, ListRecipeBlackList, ListRecipeCategory; public String getComment() { switch (this) { - case ForceClient: + case ClientMode: return "Set to true to force client mode: no item and recipe allowed, use key bindings instead."; case EnableInventoryCheck: return "Set to false to disable auto inventory check"; @@ -34,8 +34,8 @@ public class JecaConfig { public String getName() { switch (this) { - case ForceClient: - return "ForceClient"; + case ClientMode: + return "ClientMode"; case EnableInventoryCheck: return "EnableInventoryCheck"; case ListRecipeBlackList: @@ -48,7 +48,7 @@ public class JecaConfig { public String getCategory() { switch (this) { - case ForceClient: + case ClientMode: case EnableInventoryCheck: case ListRecipeBlackList: case ListRecipeCategory: @@ -59,7 +59,7 @@ public class JecaConfig { public EnumType getType() { switch (this) { - case ForceClient: + case ClientMode: case EnableInventoryCheck: return EnumType.Boolean; case ListRecipeBlackList: @@ -71,7 +71,7 @@ public class JecaConfig { public Object getDefault() { switch (this) { - case ForceClient: + case ClientMode: return false; case EnableInventoryCheck: return true; diff --git a/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java b/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java index 822bbf6..d24bca9 100644 --- a/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java +++ b/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java @@ -10,10 +10,9 @@ 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 cpw.mods.fml.relauncher.SideOnly; import me.towdium.jecalculation.command.JecaCommand; import me.towdium.jecalculation.nei.NEIPlugin; -import me.towdium.jecalculation.network.ProxyCommon; +import me.towdium.jecalculation.network.IProxy; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -26,7 +25,6 @@ import java.util.Map; @ParametersAreNonnullByDefault @SuppressWarnings("unused") -@SideOnly(Side.CLIENT) @Mod(modid = JustEnoughCalculation.Reference.MODID, name = JustEnoughCalculation.Reference.MODNAME, version = JustEnoughCalculation.Reference.VERSION, @@ -37,8 +35,8 @@ public class JustEnoughCalculation { @SidedProxy(modId = Reference.MODID, clientSide = "me.towdium.jecalculation.network.ProxyClient", - serverSide = "me.towdium.jecalculation.network.ProxyCommon") - public static ProxyCommon proxy; + serverSide = "me.towdium.jecalculation.network.ProxyServer") + public static IProxy proxy; public static SimpleNetworkWrapper network; public static Logger logger = LogManager.getLogger(Reference.MODID); @@ -47,19 +45,13 @@ public class JustEnoughCalculation { @NetworkCheckHandler public static boolean networkCheck(Map mods, Side s) { if (s == Side.SERVER) { - if (mods.containsKey(Reference.MODID) && !JecaConfig.isForceClient()) { + if (mods.containsKey(Reference.MODID) && !JecaConfig.isClintMode()) { side = enumSide.BOTH; } else { side = enumSide.CLIENT; } - } else { - if (mods.containsKey(Reference.MODID)) { - side = enumSide.SERVER; - } else { - return false; - } - } - return true; + return true; + } else return mods.containsKey(Reference.MODID); } @Mod.EventHandler @@ -80,14 +72,6 @@ public class JustEnoughCalculation { NEIPlugin.init(); } - @Mod.EventHandler - public static void onServerStart(FMLServerStartingEvent event) { - if (side == enumSide.SERVER) { - event.registerServerCommand(new JecaCommand()); - } - } - - public static class Reference { public static final String MODID = "jecalculation"; public static final String MODNAME = "Just Enough Calculation"; @@ -99,10 +83,6 @@ public class JustEnoughCalculation { * Running at client side and server not installed. */ CLIENT, - /** - * Running at server side whether client is installed. - */ - SERVER, /** * Running at client side and both installed. */ diff --git a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WSwitcher.java b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WSwitcher.java index 71dc71a..5864c42 100644 --- a/src/main/java/me/towdium/jecalculation/client/gui/drawables/WSwitcher.java +++ b/src/main/java/me/towdium/jecalculation/client/gui/drawables/WSwitcher.java @@ -7,6 +7,7 @@ import me.towdium.jecalculation.client.gui.JecGui; import me.towdium.jecalculation.client.gui.Resource; import me.towdium.jecalculation.utils.Utilities.Circulator; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import java.util.List; import java.util.function.Consumer; @@ -20,7 +21,7 @@ import java.util.stream.Stream; */ @ParametersAreNonnullByDefault @SideOnly(Side.CLIENT) -public class WSwitcher implements IWidget { +public class WSwitcher extends WContainer { public static final int SIZE = 13; protected int xPos, xSize, yPos; @@ -29,6 +30,8 @@ public class WSwitcher implements IWidget { protected WText wText; protected List keys; protected Circulator index; + @Nullable + protected String temp; public Consumer listener; public WSwitcher(int xPos, int yPos, int xSize, int total) { @@ -42,43 +45,44 @@ public class WSwitcher implements IWidget { this.yPos = yPos; this.keys = keys; left = new WButtonIcon(xPos, yPos, SIZE, SIZE, Resource.WGT_ARR_L_N, Resource.WGT_ARR_L_F, - Resource.WGT_ARR_L_D).setListenerLeft(() -> move(true)); + Resource.WGT_ARR_L_D).setListenerLeft(() -> move(false)); right = new WButtonIcon(xPos + xSize - SIZE, yPos, SIZE, SIZE, Resource.WGT_ARR_R_N, Resource.WGT_ARR_R_F, - Resource.WGT_ARR_R_D).setListenerLeft(() -> move(false)); + Resource.WGT_ARR_R_D).setListenerLeft(() -> move(true)); wRect = new WRectangle(xPos + SIZE, yPos, xSize - 2 * SIZE, SIZE, JecGui.COLOR_GUI_GREY); wText = new WText(xPos + SIZE, yPos, xSize - 2 * SIZE, SIZE, JecGui.Font.DEFAULT_SHADOW, - () -> keys.get(index.current())); + () -> temp == null ? keys.get(index.current()) : temp); index = new Circulator(keys.size()); if (keys.size() == 1) { left.setDisabled(true); right.setDisabled(true); } + addAll(left, right, wRect, wText); } protected void move(boolean forward) { - if (forward) index.move(1); - else index.move(-1); + if (temp == null) { + if (forward) index.move(1); + else index.move(-1); + } else temp = null; - if (listener != null) listener.accept(index.current()); - } - - @Override - public void onDraw(JecGui gui, int xMouse, int yMouse) { - Stream.of(left, right, wRect, wText).forEach(w -> w.onDraw(gui, xMouse, yMouse)); - } - - @Override - public boolean onClicked(JecGui gui, int xMouse, int yMouse, int button) { - return Stream.of(left, right).anyMatch(w -> w.onClicked(gui, xMouse, yMouse, button)); - } - - @Override - public boolean onKey(JecGui gui, char ch, int code) { - return Stream.of(left, right).anyMatch(w -> w.onKey(gui, ch, code)); + if (listener != null) listener.accept(getIndex()); } public WSwitcher setListener(Consumer listener) { this.listener = listener; return this; } + + public void setTemp(@Nullable String temp) { + this.temp = temp; + if (listener != null) listener.accept(getIndex()); + } + + public int getIndex() { + return temp == null ? index.current() : -1; + } + + public String getText() { + return temp == null ? keys.get(index.current()) : temp; + } } diff --git a/src/main/java/me/towdium/jecalculation/command/Commands.java b/src/main/java/me/towdium/jecalculation/command/Commands.java index 283c795..7f906f3 100644 --- a/src/main/java/me/towdium/jecalculation/command/Commands.java +++ b/src/main/java/me/towdium/jecalculation/command/Commands.java @@ -3,6 +3,7 @@ package me.towdium.jecalculation.command; import me.towdium.jecalculation.command.commands.CommandHelp; import me.towdium.jecalculation.command.commands.CommandOreDict; import me.towdium.jecalculation.command.commands.CommandState; +import me.towdium.jecalculation.command.commands.CommandUuid; import javax.annotation.ParametersAreNonnullByDefault; import java.util.HashMap; @@ -18,6 +19,7 @@ public class Commands { public static final CommandHelp commandHelp = new CommandHelp(); public static final CommandState commandState = new CommandState(); public static final CommandOreDict commandOreDict = new CommandOreDict(); + public static final CommandUuid commandUuid = new CommandUuid(); static { commands = new HashMap<>(); @@ -25,6 +27,7 @@ public class Commands { add(commandHelp); add(commandState); add(commandOreDict); + add(commandUuid); } static void add(SubCommand c) { diff --git a/src/main/java/me/towdium/jecalculation/command/JecaCommand.java b/src/main/java/me/towdium/jecalculation/command/JecaCommand.java index 459f576..762a2b2 100644 --- a/src/main/java/me/towdium/jecalculation/command/JecaCommand.java +++ b/src/main/java/me/towdium/jecalculation/command/JecaCommand.java @@ -41,6 +41,11 @@ public class JecaCommand extends CommandBase { } } + @Override + public int getRequiredPermissionLevel() { + return 0; + } + private String[] cut(String[] args) { if (args.length == 0) return new String[0]; 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 4ca92d7..63d52a3 100644 --- a/src/main/java/me/towdium/jecalculation/command/commands/CommandState.java +++ b/src/main/java/me/towdium/jecalculation/command/commands/CommandState.java @@ -27,7 +27,6 @@ public class CommandState implements SubCommand { @Override public void execute(ICommandSender sender, String[] args) throws CommandException { - sender.addChatMessage(new ChatComponentTranslation("command.state.desc", - Utilities.L18n.format(JustEnoughCalculation.side.toString()))); + 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 new file mode 100644 index 0000000..a1d268e --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/command/commands/CommandUuid.java @@ -0,0 +1,33 @@ +package me.towdium.jecalculation.command.commands; + +import me.towdium.jecalculation.command.SubCommand; +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; + +/** + * Author: towdium + * Date: 17-10-15. + */ +@ParametersAreNonnullByDefault +public class CommandUuid implements SubCommand { + @Override + public String getName() { + return "uuid"; + } + + @Override + public String getUsage(ICommandSender sender) { + return "/jec uuid"; + } + + + @Override + public void execute(ICommandSender sender, String[] args) throws CommandException { + sender.addChatMessage(new ChatComponentText(sender.getCommandSenderName())); + } +} diff --git a/src/main/java/me/towdium/jecalculation/data/Record.java b/src/main/java/me/towdium/jecalculation/data/Record.java deleted file mode 100644 index ea0f131..0000000 --- a/src/main/java/me/towdium/jecalculation/data/Record.java +++ /dev/null @@ -1,153 +0,0 @@ -package me.towdium.jecalculation.data; - -import me.towdium.jecalculation.data.label.ILabel; -import me.towdium.jecalculation.polyfill.Polyfill; -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.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; -import java.util.function.BiConsumer; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; - -/** - * Author: towdium - * Date: 17-10-6. - */ -public abstract class Record { - - public static class RecordSP { - public static final String KEY_RECIPE = "recipe"; - public static final String KEY_RECENT = "recent"; - - public RecordRecipe recipes; - public RecordRecent recents; - - public RecordSP() { - recipes = new RecordRecipe(); - recents = new RecordRecent(); - } - - public RecordSP(NBTTagCompound nbt) { - recipes = new RecordRecipe(nbt.getTagList(KEY_RECIPE, 10)); - recents = new RecordRecent(nbt.getTagList(KEY_RECENT, 10)); - } - - public NBTTagCompound seialize() { - NBTTagCompound ret = new NBTTagCompound(); - ret.setTag(KEY_RECIPE, recipes.serialize()); - ret.setTag(KEY_RECENT, recents.setialize()); - return ret; - } - } - - public static class RecordMP { - HashMap records; - - public RecordSP get(UUID id) { - return records.get(id); - } - } - - public static class RecordRecipe { - public static final String KEY_NAME = "name"; - public static final String KEY_CONTENT = "content"; - - ArrayList>> record = new ArrayList<>(); - HashMap index = new HashMap<>(); - - public RecordRecipe() { - } - - - public RecordRecipe(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))); - }); - } - - public void add(String group, Recipe recipe) { - Integer i = index.get(group); - if (i == null) { - record.add(new Pair<>(group, new ArrayList<>())); - i = record.size() - 1; - } - record.get(i).two.add(recipe); - } - - public void set(String group, int index, Recipe recipe) { - Integer i = this.index.get(group); - if (i == null) - throw new RuntimeException("Group not found: " + group + "."); - record.get(i).two.set(index, recipe); - } - - public void remove(String group, int index) { - Integer i = this.index.get(group); - if (i == null) - throw new RuntimeException("Group not found: " + group + "."); - record.get(i).two.remove(index); - } - - public void foreach(BiConsumer> consumer) { - record.forEach(p -> consumer.accept(p.one, p.two)); - } - - public NBTTagList serialize() { - NBTTagList ret = new NBTTagList(); - foreach((n, rs) -> { - NBTTagCompound nbt = new NBTTagCompound(); - nbt.setString(KEY_NAME, n); - NBTTagList l = new NBTTagList(); - rs.forEach(r -> l.appendTag(r.serialize())); - nbt.setTag(KEY_CONTENT, l); - ret.appendTag(nbt); - }); - return ret; - } - } - - public static class RecordRecent { - Utilities.Recent record = new Utilities.Recent<>(9); - - public RecordRecent(NBTTagList nbt) { - List ls = StreamSupport.stream(Polyfill.spliterator(nbt), false).filter(n -> n instanceof NBTTagCompound) - .map(n -> ILabel.DESERIALIZER.deserialize((NBTTagCompound) n)) - .collect(Collectors.toList()); - new Utilities.ReversedIterator<>(ls).forEachRemaining(l -> record.push(l)); - } - - public RecordRecent() { - } - - public void push(ILabel label) { - record.push(label); - } - - public ILabel getLatest() { - return record.toList().get(0); - } - - public List getRecords() { - List labels = record.toList(); - return labels.subList(1, labels.size()); - } - - public NBTTagList setialize() { - NBTTagList ret = new NBTTagList(); - record.toList().forEach(l -> ret.appendTag(ILabel.DESERIALIZER.serialize(l))); - return ret; - } - } -} diff --git a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelFluidStack.java b/src/main/java/me/towdium/jecalculation/data/label/labels/LabelFluidStack.java index beaff9d..dca3081 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelFluidStack.java +++ b/src/main/java/me/towdium/jecalculation/data/label/labels/LabelFluidStack.java @@ -100,7 +100,11 @@ public class LabelFluidStack implements ILabel { @Override public boolean equals(Object obj) { return obj instanceof LabelFluidStack - && fluid.equals(((LabelFluidStack) obj).fluid) - && amount == ((LabelFluidStack) obj).amount; + && fluid.equals(((LabelFluidStack) obj).fluid) && amount == ((LabelFluidStack) obj).amount; + } + + @Override + public int hashCode() { + return fluid.hashCode() ^ amount; } } diff --git a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelItemStack.java b/src/main/java/me/towdium/jecalculation/data/label/labels/LabelItemStack.java index c534116..a3d344e 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelItemStack.java +++ b/src/main/java/me/towdium/jecalculation/data/label/labels/LabelItemStack.java @@ -78,8 +78,13 @@ public class LabelItemStack extends LabelSimpleAmount { @Override public boolean equals(Object obj) { return obj instanceof LabelItemStack - && itemStack.equals(((LabelItemStack) obj).itemStack) - && amount == ((LabelItemStack) obj).amount; + && itemStack.equals(((LabelItemStack) obj).itemStack) && amount == ((LabelItemStack) obj).amount; + } + + @Override + public int hashCode() { + return itemStack.getItemDamage() ^ itemStack.getItem().getUnlocalizedName().hashCode() ^ amount + ^ (itemStack.getTagCompound() == null ? 0 : itemStack.getTagCompound().hashCode()); } public ItemStack getItemStack() { diff --git a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelOreDict.java b/src/main/java/me/towdium/jecalculation/data/label/labels/LabelOreDict.java index 51ed156..77c5899 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelOreDict.java +++ b/src/main/java/me/towdium/jecalculation/data/label/labels/LabelOreDict.java @@ -106,6 +106,17 @@ public class LabelOreDict extends LabelSimpleAmount { return existing; } + @Override + public int hashCode() { + return name.hashCode() ^ amount; + } + + @Override + public boolean equals(Object obj) { + return obj instanceof LabelOreDict + && amount == ((LabelOreDict) obj).amount && name.equals(((LabelOreDict) obj).name); + } + public String getName() { return name; } diff --git a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelUniversal.java b/src/main/java/me/towdium/jecalculation/data/label/labels/LabelUniversal.java index 749192e..4884c7c 100644 --- a/src/main/java/me/towdium/jecalculation/data/label/labels/LabelUniversal.java +++ b/src/main/java/me/towdium/jecalculation/data/label/labels/LabelUniversal.java @@ -57,6 +57,12 @@ public class LabelUniversal extends LabelSimpleAmount { @Override public boolean equals(Object obj) { - return obj instanceof LabelUniversal && name.equals(((LabelUniversal) obj).name); + return obj instanceof LabelUniversal + && name.equals(((LabelUniversal) obj).name) && amount == ((LabelUniversal) obj).amount; + } + + @Override + public int hashCode() { + return name.hashCode() ^ amount; } } diff --git a/src/main/java/me/towdium/jecalculation/data/Recipe.java b/src/main/java/me/towdium/jecalculation/data/structure/Recipe.java similarity index 68% rename from src/main/java/me/towdium/jecalculation/data/Recipe.java rename to src/main/java/me/towdium/jecalculation/data/structure/Recipe.java index e422e05..765f1e3 100644 --- a/src/main/java/me/towdium/jecalculation/data/Recipe.java +++ b/src/main/java/me/towdium/jecalculation/data/structure/Recipe.java @@ -1,13 +1,17 @@ -package me.towdium.jecalculation.data; +package me.towdium.jecalculation.data.structure; import me.towdium.jecalculation.data.label.ILabel; import me.towdium.jecalculation.polyfill.Polyfill; +import me.towdium.jecalculation.utils.IllegalPositionException; +import me.towdium.jecalculation.utils.wrappers.Single; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.function.BiFunction; +import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.StreamSupport; @@ -24,6 +28,7 @@ public class Recipe { ILabel[] input; ILabel[] catalyst; ILabel[] output; + int hashcode; public Recipe(NBTTagCompound nbt) { this(readNbtList(nbt.getTagList(KEY_INPUT, 10)), @@ -45,6 +50,14 @@ public class Recipe { this.input = convert.apply(input, 16); this.catalyst = convert.apply(catalyst, 8); this.output = convert.apply(output, 8); + + Single hash = new Single<>(0); + Consumer hasher = (ls) -> Arrays.stream(ls) + .filter(Objects::nonNull).forEach(i -> hash.value ^= i.hashCode()); + hasher.accept(input); + hasher.accept(catalyst); + hasher.accept(output); + hashcode = hash.value; } static private List readNbtList(NBTTagList list) { @@ -54,6 +67,24 @@ public class Recipe { .collect(Collectors.toList()); } + @Override + public int hashCode() { + return hashcode; + } + + public ILabel[] getLabel(enumIoType type) { + switch (type) { + case INPUT: + return input; + case OUTPUT: + return output; + case CATALYST: + return catalyst; + default: + throw new IllegalPositionException(); + } + } + public NBTTagCompound serialize() { NBTTagCompound ret = new NBTTagCompound(); Function convert = (ls) -> { @@ -66,4 +97,12 @@ public class Recipe { ret.setTag(KEY_OUTPUT, convert.apply(output)); return ret; } + + public enum enumIoType { + INPUT, OUTPUT, CATALYST; + + public int getSize() { + return this == INPUT ? 16 : 8; + } + } } diff --git a/src/main/java/me/towdium/jecalculation/data/structure/User.java b/src/main/java/me/towdium/jecalculation/data/structure/User.java new file mode 100644 index 0000000..70d992b --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/data/structure/User.java @@ -0,0 +1,149 @@ +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.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.function.BiConsumer; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.StreamSupport; + +/** + * Author: towdium + * Date: 17-10-15. + */ +public class User { + public static final String KEY_RECIPE = "recipe"; + public static final String KEY_RECENT = "recent"; + + public Recipes recipes; + public Recent recents; + + public User() { + recipes = new Recipes(); + recents = new Recent(); + } + + public User(NBTTagCompound nbt) { + recipes = new Recipes(nbt.getTagList(KEY_RECIPE, 10)); + recents = 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()); + 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"; + + Utilities.OrderedHashMap> records = new Utilities.OrderedHashMap<>(); + + public Recipes() { + } + + 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))); + }); + } + + public void add(String group, Recipe recipe) { + records.get(group).orElseGet(() -> { + ArrayList ret = new ArrayList<>(); + records.put(group, ret); + return ret; + }).add(recipe); + } + + public void set(String group, int index, Recipe recipe) { + records.get(group).orElseGet(() -> { + throw new RuntimeException("Group not found: " + group + "."); + }) + .set(index, recipe); + } + + public void remove(String group, int index) { + records.get(group).orElseGet(() -> { + throw new RuntimeException("Group not found: " + group + "."); + }) + .remove(index); + } + + public void forEach(BiConsumer> consumer) { + records.forEach(consumer); + } + + public Optional> get(String group) { + return records.get(group); + } + + public NBTTagList serialize() { + NBTTagList ret = new NBTTagList(); + forEach((n, rs) -> { + NBTTagCompound nbt = new NBTTagCompound(); + nbt.setString(KEY_NAME, n); + NBTTagList l = new NBTTagList(); + rs.forEach(r -> l.appendTag(r.serialize())); + nbt.setTag(KEY_CONTENT, l); + ret.appendTag(nbt); + }); + return ret; + } + } + + public static class Recent { + Utilities.Recent record = new Utilities.Recent<>(9); + + public Recent(NBTTagList nbt) { + List ls = StreamSupport.stream(Polyfill.spliterator(nbt), false) + .filter(n -> n instanceof NBTTagCompound) + .map(n -> ILabel.DESERIALIZER.deserialize((NBTTagCompound) n)) + .collect(Collectors.toList()); + new Utilities.ReversedIterator<>(ls).forEachRemaining(l -> record.push(l)); + } + + public Recent() { + } + + public void push(ILabel label) { + record.push(label); + } + + public ILabel getLatest() { + return record.toList().get(0); + } + + public List getRecords() { + List labels = record.toList(); + return labels.subList(1, labels.size()); + } + + public NBTTagList serialize() { + NBTTagList ret = new NBTTagList(); + record.toList().forEach(l -> ret.appendTag(ILabel.DESERIALIZER.serialize(l))); + return ret; + } + } +} diff --git a/src/main/java/me/towdium/jecalculation/event/Handlers.java b/src/main/java/me/towdium/jecalculation/event/Handlers.java index 6b4f17c..03bb7fe 100644 --- a/src/main/java/me/towdium/jecalculation/event/Handlers.java +++ b/src/main/java/me/towdium/jecalculation/event/Handlers.java @@ -1,5 +1,7 @@ package me.towdium.jecalculation.event; +import me.towdium.jecalculation.event.handlers.ControllerClient; +import me.towdium.jecalculation.event.handlers.ControllerServer; import me.towdium.jecalculation.event.handlers.InputEventHandler; import java.util.ArrayList; @@ -11,5 +13,7 @@ public class Handlers { handlers = new ArrayList<>(); handlers.add(new InputEventHandler()); + handlers.add(new ControllerClient()); + handlers.add(new ControllerServer()); } } diff --git a/src/main/java/me/towdium/jecalculation/event/handlers/ControllerClient.java b/src/main/java/me/towdium/jecalculation/event/handlers/ControllerClient.java new file mode 100644 index 0000000..eff93b6 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/event/handlers/ControllerClient.java @@ -0,0 +1,58 @@ +package me.towdium.jecalculation.event.handlers; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.network.FMLNetworkEvent; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import me.towdium.jecalculation.data.label.ILabel; +import me.towdium.jecalculation.data.structure.Recipe; +import me.towdium.jecalculation.data.structure.User; +import net.minecraft.nbt.NBTTagCompound; + +import javax.annotation.Nullable; +import java.util.List; + +/** + * Author: towdium + * Date: 17-10-15. + */ +@SideOnly(Side.CLIENT) +public class ControllerClient { + static User recordWorld; + static User recordClient; + + static User getRecord() { + return recordWorld == null ? recordClient : recordWorld; + } + + public static void addRecipe(String group, Recipe recipe) { + getRecord().recipes.add(group, recipe); + } + + public static void setRecipe(String group, int index, Recipe recipe) { + getRecord().recipes.set(group, index, recipe); + } + + public static void removeRecipe(String group, int index) { + getRecord().recipes.remove(group, index); + } + + /** + * This is used when syncing data from the server. + * Client record will be initialized when client starts. + * + * @param nbt {@link NBTTagCompound containing the user data} + */ + public static void init(@Nullable NBTTagCompound nbt) { + recordWorld = nbt == null ? new User() : new User(nbt); + } + + @SubscribeEvent + public static void onLogOut(FMLNetworkEvent.ClientDisconnectionFromServerEvent event) { + recordWorld = null; + } + + public List search(String group, ILabel label, Recipe.enumIoType type) { + return getRecord().search(group, label, type); + } +} diff --git a/src/main/java/me/towdium/jecalculation/event/handlers/ControllerServer.java b/src/main/java/me/towdium/jecalculation/event/handlers/ControllerServer.java new file mode 100644 index 0000000..e057e1e --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/event/handlers/ControllerServer.java @@ -0,0 +1,46 @@ +package me.towdium.jecalculation.event.handlers; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.PlayerEvent; +import me.towdium.jecalculation.JustEnoughCalculation; +import me.towdium.jecalculation.data.structure.User; +import me.towdium.jecalculation.utils.wrappers.Pair; +import net.minecraftforge.event.entity.player.PlayerEvent.LoadFromFile; +import net.minecraftforge.event.entity.player.PlayerEvent.SaveToFile; + +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.HashMap; +import java.util.UUID; + +/** + * Author: towdium + * Date: 17-10-15. + */ +@ParametersAreNonnullByDefault +public class ControllerServer { + static HashMap> records; + + @SubscribeEvent + public void onJoin(PlayerEvent.PlayerLoggedInEvent e) { + JustEnoughCalculation.logger.info(String.format("Player join, name: %s, remote: %b, uuid: %s", + e.player.getDisplayName(), e.player.worldObj.isRemote, e.player.getUniqueID())); + } + + @SubscribeEvent + public void onQuit(PlayerEvent.PlayerLoggedOutEvent e) { + JustEnoughCalculation.logger.info(String.format("Player quit, name: %s, remote: %b", + e.player.getDisplayName(), e.player.worldObj.isRemote)); + } + + @SubscribeEvent + public void onLoad(LoadFromFile e) { + JustEnoughCalculation.logger.info(String.format("Player load, name: %s, remote: %b", + e.entityPlayer.getDisplayName(), e.entityPlayer.worldObj.isRemote)); + } + + @SubscribeEvent + public void onSave(SaveToFile e) { + JustEnoughCalculation.logger.info(String.format("Player save, name: %s, remote: %b", + e.entityPlayer.getDisplayName(), e.entityPlayer.worldObj.isRemote)); + } +} 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 e6d063b..f1f9592 100644 --- a/src/main/java/me/towdium/jecalculation/event/handlers/InputEventHandler.java +++ b/src/main/java/me/towdium/jecalculation/event/handlers/InputEventHandler.java @@ -10,7 +10,7 @@ import me.towdium.jecalculation.client.gui.JecGui; import me.towdium.jecalculation.client.gui.guis.GuiCalculator; import me.towdium.jecalculation.network.ProxyClient; import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.GuiScreen; +import net.minecraft.util.ChatComponentTranslation; import net.minecraftforge.client.event.GuiScreenEvent; /** @@ -21,8 +21,9 @@ public class InputEventHandler { @SideOnly(Side.CLIENT) @SubscribeEvent public void onKey(InputEvent.KeyInputEvent event) { - if (ProxyClient.keyOpenGui.isPressed() && JustEnoughCalculation.side != JustEnoughCalculation.enumSide.SERVER) { - JecGui.displayGui(new GuiCalculator()); + if (ProxyClient.keyOpenGui.isPressed()) { + if (JustEnoughCalculation.side == JustEnoughCalculation.enumSide.CLIENT) JecGui.displayGui(new GuiCalculator()); + else Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentTranslation("chat.server_mode")); } } @@ -34,5 +35,6 @@ public class InputEventHandler { event.setCanceled(gui.handleMouseEvent()); } } + } diff --git a/src/main/java/me/towdium/jecalculation/item/ItemCalculator.java b/src/main/java/me/towdium/jecalculation/item/ItemCalculator.java index 8326c51..8297754 100644 --- a/src/main/java/me/towdium/jecalculation/item/ItemCalculator.java +++ b/src/main/java/me/towdium/jecalculation/item/ItemCalculator.java @@ -95,7 +95,7 @@ public class ItemCalculator extends Item { @Override public ItemStack onItemRightClick(ItemStack itemStack, World worldIn, EntityPlayer playerIn) { - if (JustEnoughCalculation.side != JustEnoughCalculation.enumSide.SERVER) + if (JustEnoughCalculation.side != JustEnoughCalculation.enumSide.CLIENT) JustEnoughCalculation.proxy.displayCalculator(); return super.onItemRightClick(itemStack, worldIn, playerIn); } diff --git a/src/main/java/me/towdium/jecalculation/network/IProxy.java b/src/main/java/me/towdium/jecalculation/network/IProxy.java new file mode 100644 index 0000000..13a4841 --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/network/IProxy.java @@ -0,0 +1,20 @@ +package me.towdium.jecalculation.network; + +/** + * Author: towdium + * Date: 17-10-10. + */ +public interface IProxy { + default void initPre() { + + } + + default void init() { + } + + default void initPost() { + } + + default void displayCalculator() { + } +} diff --git a/src/main/java/me/towdium/jecalculation/network/ProxyClient.java b/src/main/java/me/towdium/jecalculation/network/ProxyClient.java index eb4b499..945a828 100644 --- a/src/main/java/me/towdium/jecalculation/network/ProxyClient.java +++ b/src/main/java/me/towdium/jecalculation/network/ProxyClient.java @@ -1,30 +1,33 @@ package me.towdium.jecalculation.network; import cpw.mods.fml.client.registry.ClientRegistry; +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.client.gui.guis.GuiCalculator; import me.towdium.jecalculation.command.JecaCommand; +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 extends ProxyCommon{ +public class ProxyClient implements IProxy { public static final KeyBinding keyOpenGui = - new KeyBinding("key.open_gui", Keyboard.KEY_F, "key.category"); + new KeyBinding("key.open_gui", Keyboard.KEY_NONE, "key.category"); @Override public void initPost() { - super.initPost(); - JustEnoughCalculation.logger.info("client proxy init post"); + GameRegistry.registerItem(ItemCalculator.INSTANCE, ItemCalculator.INSTANCE.getUnlocalizedName()); + Handlers.handlers.forEach(FMLCommonHandler.instance().bus()::register); ClientCommandHandler.instance.registerCommand(new JecaCommand()); new JecGui(null, new GuiCalculator()); } @Override public void init() { - super.init(); JustEnoughCalculation.logger.info("client proxy init"); ClientRegistry.registerKeyBinding(keyOpenGui); } diff --git a/src/main/java/me/towdium/jecalculation/network/ProxyCommon.java b/src/main/java/me/towdium/jecalculation/network/ProxyServer.java similarity index 60% rename from src/main/java/me/towdium/jecalculation/network/ProxyCommon.java rename to src/main/java/me/towdium/jecalculation/network/ProxyServer.java index d33d648..d8f7f99 100644 --- a/src/main/java/me/towdium/jecalculation/network/ProxyCommon.java +++ b/src/main/java/me/towdium/jecalculation/network/ProxyServer.java @@ -6,21 +6,9 @@ import me.towdium.jecalculation.JustEnoughCalculation; import me.towdium.jecalculation.event.Handlers; import me.towdium.jecalculation.item.ItemCalculator; -public class ProxyCommon { +public class ProxyServer implements IProxy { public void initPre() { - JustEnoughCalculation.logger.info("common proxy init pre"); GameRegistry.registerItem(ItemCalculator.INSTANCE, ItemCalculator.INSTANCE.getUnlocalizedName()); Handlers.handlers.forEach(FMLCommonHandler.instance().bus()::register); } - - public void init() { - JustEnoughCalculation.logger.info("common proxy init"); - } - - public void initPost() { - JustEnoughCalculation.logger.info("common proxy init post"); - } - - public void displayCalculator() { - } } diff --git a/src/main/java/me/towdium/jecalculation/utils/Utilities.java b/src/main/java/me/towdium/jecalculation/utils/Utilities.java index 336b792..8e26f33 100644 --- a/src/main/java/me/towdium/jecalculation/utils/Utilities.java +++ b/src/main/java/me/towdium/jecalculation/utils/Utilities.java @@ -1,5 +1,6 @@ package me.towdium.jecalculation.utils; +import com.google.common.collect.ImmutableList; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.ModContainer; import cpw.mods.fml.common.registry.GameData; @@ -15,6 +16,7 @@ import org.apache.commons.lang3.StringEscapeUtils; import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import java.util.*; +import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.stream.Stream; import java.util.stream.StreamSupport; @@ -239,4 +241,57 @@ 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<>(); + + public void put(K key, V value) { + if (index.containsKey(key)) this.value.set(index.get(key), new Pair<>(key, value)); + else { + index.put(key, this.value.size()); + this.value.add(new Pair<>(key, value)); + } + } + + public Optional get(int index) { + Pair p = value.get(index); + return Optional.ofNullable(p == null ? null : p.two); + } + + public Optional get(K key) { + Integer i = index.get(key); + return Optional.ofNullable(i == null ? null : value.get(i).two); + } + + public int getIndex(K key) { + Integer ret = index.get(key); + return ret == null ? -1 : ret; + } + + public Optional getKey(int index) { + Pair ret = value.get(index); + return Optional.ofNullable(ret == null ? null : ret.one); + } + + public void forEach(BiConsumer func) { + value.forEach(p -> func.accept(p.one, p.two)); + } + } } diff --git a/src/main/resources/assets/jecalculation/lang/en_US.lang b/src/main/resources/assets/jecalculation/lang/en_US.lang index 2cbee02..45073dd 100644 --- a/src/main/resources/assets/jecalculation/lang/en_US.lang +++ b/src/main/resources/assets/jecalculation/lang/en_US.lang @@ -5,10 +5,7 @@ command.common.not_found=Invalid option: %s command.common.unexpected_arg=Unexpected arg: %s command.help.list=Here is the list of all commands available: command.state.desc=The mod is working on state: %s -command.state.s_server=server -command.state.s_client=client only -command.state.s_both=client with server -command.state.s_undefined=error state +chat.server_mode=The mod is not working in client mode. Key binding will not work. gui.gui_calculator.new_recipe.name=New recipe gui.common.label.ore_dict=OreDict