feat: more data storage and side separation

This commit is contained in:
Discreater committed 2021-04-29 23:44:46 +08:00
1 parent 90f768ed21
commit ae0d3869cb
24 files changed
+502 -244

No files matched your search

@@ -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;
@@ -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<String, String> 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.
*/
@@ -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<String> keys;
protected Circulator index;
@Nullable
protected String temp;
public Consumer<Integer> 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<Integer> 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;
}
}
@@ -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) {
@@ -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];
@@ -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()));
}
}
@@ -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()));
}
}
@@ -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<UUID, RecordSP> 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<Pair<String, ArrayList<Recipe>>> record = new ArrayList<>();
HashMap<String, Integer> 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<String, List<Recipe>> 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<ILabel> record = new Utilities.Recent<>(9);
public RecordRecent(NBTTagList nbt) {
List<ILabel> 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<ILabel> getRecords() {
List<ILabel> 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;
}
}
}
@@ -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;
}
}
@@ -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() {
@@ -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;
}
@@ -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;
}
}
@@ -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<Integer> hash = new Single<>(0);
Consumer<ILabel[]> 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<ILabel> 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<ILabel[], NBTTagList> 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;
}
}
}
@@ -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<Recipe> search(String group, ILabel label, Recipe.enumIoType type) {
Utilities.INDListBuilder<Recipe> 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<String, List<Recipe>> 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<Recipe> 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<String, List<Recipe>> consumer) {
records.forEach(consumer);
}
public Optional<List<Recipe>> 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<ILabel> record = new Utilities.Recent<>(9);
public Recent(NBTTagList nbt) {
List<ILabel> 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<ILabel> getRecords() {
List<ILabel> 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;
}
}
}
@@ -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());
}
}
@@ -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<Recipe> search(String group, ILabel label, Recipe.enumIoType type) {
return getRecord().search(group, label, type);
}
}
@@ -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<UUID, Pair<Boolean, User>> 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));
}
}
@@ -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());
}
}
}
@@ -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);
}
@@ -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() {
}
}
@@ -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);
}
@@ -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() {
}
}
@@ -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<T>) data.clone();
}
}
/**
* Immutable non-duplicated list builder
*/
public static class INDListBuilder<T> {
public ImmutableList.Builder<T> builder = ImmutableList.builder();
public HashSet<T> set = new HashSet<>();
public void add(T obj) {
if (set.add(obj)) builder.add(obj);
}
public ImmutableList<T> build() {
return builder.build();
}
}
public static class OrderedHashMap<K, V> {
public HashMap<K, Integer> index = new HashMap<>();
public List<Pair<K, V>> 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<V> get(int index) {
Pair<K, V> p = value.get(index);
return Optional.ofNullable(p == null ? null : p.two);
}
public Optional<V> 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<K> getKey(int index) {
Pair<K, V> ret = value.get(index);
return Optional.ofNullable(ret == null ? null : ret.one);
}
public void forEach(BiConsumer<K, V> func) {
value.forEach(p -> func.accept(p.one, p.two));
}
}
}