From 11d6c296d7315ac7ab8a188cb8e9571bda7e95ce Mon Sep 17 00:00:00 2001 From: Discreater Date: Mon, 1 Mar 2021 16:21:43 +0800 Subject: [PATCH] Chore: rename itemCalculator to item_calculator --- build.gradle | 1 + .../me/towdium/jecalculation/JecaItem.java | 7 +- .../jecalculation/JustEnoughCalculation.java | 10 +- .../{plugin => nei}/MyOverlayHandler.java | 2 +- .../{plugin => nei}/NEICalculatorConfig.java | 2 +- .../jecalculation/utils/Utilities.java | 901 ++++++++++-------- .../jecalculation/utils/polyfill/Event.java | 30 + .../jecalculation/utils/polyfill/Loader.java | 43 + .../assets/jecalculation/lang/en_US.lang | 2 +- .../assets/jecalculation/lang/zh_CN.lang | 2 +- ...emCalculator.json => item_calculator.json} | 2 +- ...itemCalculator.png => item_calculator.png} | Bin 12 files changed, 567 insertions(+), 435 deletions(-) rename src/main/java/me/towdium/jecalculation/{plugin => nei}/MyOverlayHandler.java (97%) rename src/main/java/me/towdium/jecalculation/{plugin => nei}/NEICalculatorConfig.java (96%) create mode 100644 src/main/java/me/towdium/jecalculation/utils/polyfill/Event.java create mode 100644 src/main/java/me/towdium/jecalculation/utils/polyfill/Loader.java rename src/main/resources/assets/jecalculation/models/item/{itemCalculator.json => item_calculator.json} (52%) rename src/main/resources/assets/jecalculation/textures/items/{itemCalculator.png => item_calculator.png} (100%) diff --git a/build.gradle b/build.gradle index 6906e5f..313e94d 100644 --- a/build.gradle +++ b/build.gradle @@ -65,6 +65,7 @@ dependencies { compile "codechicken:NotEnoughItems:${mc_version}-${nei_version}:dev" compile 'codechicken:CodeChickenLib:1.7.10-1.1.3.138:dev' compile 'codechicken:CodeChickenCore:1.7.10-1.0.7.+:dev' + compile 'org.reflections:reflections:0.9.12' } processResources { diff --git a/src/main/java/me/towdium/jecalculation/JecaItem.java b/src/main/java/me/towdium/jecalculation/JecaItem.java index f73a7e1..1d10f99 100644 --- a/src/main/java/me/towdium/jecalculation/JecaItem.java +++ b/src/main/java/me/towdium/jecalculation/JecaItem.java @@ -1,7 +1,5 @@ package me.towdium.jecalculation; -import cpw.mods.fml.common.eventhandler.SubscribeEvent; -import me.towdium.jecalculation.JustEnoughCalculation; import me.towdium.jecalculation.gui.GuiHandler; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.player.EntityPlayer; @@ -16,11 +14,14 @@ import net.minecraft.world.World; public class JecaItem extends Item { public static JecaItem INSTANCE = new JecaItem(); + public static final String NAME = "item_calculator"; + public JecaItem() { setHasSubtypes(true); setMaxStackSize(1); + setUnlocalizedName(NAME); + setTextureName(JustEnoughCalculation.Reference.MODID + ":" + NAME); setCreativeTab(CreativeTabs.tabTools); - setUnlocalizedName("item_calculator"); } @Override diff --git a/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java b/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java index 747a899..902a9fa 100644 --- a/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java +++ b/src/main/java/me/towdium/jecalculation/JustEnoughCalculation.java @@ -7,6 +7,7 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent; 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.common.registry.GameData; import cpw.mods.fml.common.registry.GameRegistry; import cpw.mods.fml.relauncher.Side; import me.towdium.jecalculation.network.IProxy; @@ -43,9 +44,6 @@ public class JustEnoughCalculation { public static SimpleNetworkWrapper network; public static Logger logger = LogManager.getLogger(Reference.MODID); - public static Item itemCalculator = new JecaItem().setUnlocalizedName("itemCalculator") - .setTextureName(Reference.MODID + ":" + "itemCalculator"); - @NetworkCheckHandler public static boolean networkCheck(Map mods, Side s) { return s == Side.SERVER || mods.containsKey(Reference.MODID); @@ -54,7 +52,7 @@ public class JustEnoughCalculation { @Mod.EventHandler public static void initPre(FMLPreInitializationEvent event) { JecaConfig.preInit(event); - GameRegistry.registerItem(itemCalculator, itemCalculator.getUnlocalizedName().substring(5)); + GameRegistry.registerItem(JecaItem.INSTANCE, JecaItem.NAME); network = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MODID); network.registerMessage(PacketCalculatorUpdate.class, PacketCalculatorUpdate.class, 1, Side.SERVER); network.registerMessage(PacketRecipeUpdate.class, PacketRecipeUpdate.class, 2, Side.SERVER); @@ -68,8 +66,8 @@ public class JustEnoughCalculation { Minecraft.getMinecraft().getRenderItem().getItemModelMesher(). register(itemCalculator, 0, new ModelResourceLocation(Reference.MODID + ":" + itemCalculator.getUnlocalizedName().substring(5), "inventory")); }*/ - ShapedOreRecipe recipe = new ShapedOreRecipe(new ItemStack(itemCalculator), "SIS", "SRS", "SOS", 'S', "stone", - 'I', "dyeBlack", 'R', "dustRedstone", 'O', "ingotIron") { + ShapedOreRecipe recipe = new ShapedOreRecipe(new ItemStack(JecaItem.INSTANCE), "SIS", "SRS", "SOS", 'S', + "stone", 'I', "dyeBlack", 'R', "dustRedstone", 'O', "ingotIron") { { try { Field f = ShapedOreRecipe.class.getDeclaredField("input"); diff --git a/src/main/java/me/towdium/jecalculation/plugin/MyOverlayHandler.java b/src/main/java/me/towdium/jecalculation/nei/MyOverlayHandler.java similarity index 97% rename from src/main/java/me/towdium/jecalculation/plugin/MyOverlayHandler.java rename to src/main/java/me/towdium/jecalculation/nei/MyOverlayHandler.java index 00de970..f83d38b 100644 --- a/src/main/java/me/towdium/jecalculation/plugin/MyOverlayHandler.java +++ b/src/main/java/me/towdium/jecalculation/nei/MyOverlayHandler.java @@ -1,4 +1,4 @@ -package me.towdium.jecalculation.plugin; +package me.towdium.jecalculation.nei; import codechicken.nei.PositionedStack; import codechicken.nei.api.IOverlayHandler; diff --git a/src/main/java/me/towdium/jecalculation/plugin/NEICalculatorConfig.java b/src/main/java/me/towdium/jecalculation/nei/NEICalculatorConfig.java similarity index 96% rename from src/main/java/me/towdium/jecalculation/plugin/NEICalculatorConfig.java rename to src/main/java/me/towdium/jecalculation/nei/NEICalculatorConfig.java index 29caa87..8451196 100644 --- a/src/main/java/me/towdium/jecalculation/plugin/NEICalculatorConfig.java +++ b/src/main/java/me/towdium/jecalculation/nei/NEICalculatorConfig.java @@ -1,4 +1,4 @@ -package me.towdium.jecalculation.plugin; +package me.towdium.jecalculation.nei; import codechicken.nei.api.API; import codechicken.nei.api.IConfigureNEI; diff --git a/src/main/java/me/towdium/jecalculation/utils/Utilities.java b/src/main/java/me/towdium/jecalculation/utils/Utilities.java index 6e3c50e..56e700d 100644 --- a/src/main/java/me/towdium/jecalculation/utils/Utilities.java +++ b/src/main/java/me/towdium/jecalculation/utils/Utilities.java @@ -1,421 +1,480 @@ -//package me.towdium.jecalculation.utils; -// -//import me.towdium.jecalculation.JustEnoughCalculation; -//import me.towdium.jecalculation.utils.wrappers.Pair; -//import net.minecraft.client.Minecraft; -//import net.minecraft.client.gui.FontRenderer; -//import net.minecraft.item.Item; -//import net.minecraft.item.ItemStack; -//import net.minecraft.nbt.*; -//import net.minecraft.util.ResourceLocation; -//import net.minecraftforge.client.MinecraftForgeClient; -//import net.minecraftforge.fluids.Fluid; -//import net.minecraftforge.fml.common.Loader; -//import net.minecraftforge.fml.relauncher.Side; -//import net.minecraftforge.fml.relauncher.SideOnly; -//import org.apache.commons.io.FileUtils; -//import org.apache.commons.lang3.StringEscapeUtils; -// -//import javax.annotation.Nullable; -//import javax.annotation.ParametersAreNonnullByDefault; -//import java.io.File; -//import java.io.FileOutputStream; -//import java.io.IOException; -//import java.nio.charset.Charset; -//import java.text.BreakIterator; -//import java.text.DecimalFormat; -//import java.util.*; -//import java.util.function.BiFunction; -//import java.util.function.BiPredicate; -//import java.util.function.Function; -//import java.util.function.Supplier; -//import java.util.stream.Stream; -//import java.util.stream.StreamSupport; -// -//import static net.minecraft.client.resources.I18n.format; -// -///** -// * Author: Towdium -// * Date: 2016/6/25. -// */ -//@SuppressWarnings("UnusedReturnValue") -//@ParametersAreNonnullByDefault -//public class Utilities { -// // FLOAT FORMATTING -// public static char[] suffix = new char[]{'K', 'M', 'B', 'G', 'T', 'P'}; -// public static DecimalFormat[] format = new DecimalFormat[]{ -// new DecimalFormat("#."), new DecimalFormat("#.#"), new DecimalFormat("#.##"), -// new DecimalFormat("#.###"), new DecimalFormat("#.####") -// }; -// -// public static String cutNumber(float f, int size) { -// BiFunction form = (fl, len) -> format[len - 1 - (int) Math.log10(fl)].format(fl); -// int scale = (int) Math.log10(f) / 3; -// if (scale == 0) return form.apply(f, size); -// else return form.apply(f / (float) Math.pow(1000, scale), size - 1) + suffix[scale - 1]; -// } -// -// @SuppressWarnings("OptionalUsedAsFieldOrParameterType") -// public static Stream stream(Optional o) { -// return o.map(Stream::of).orElse(Stream.empty()); -// } -// -// public static Supplier fake(Runnable r) { -// return () -> { -// r.run(); -// return null; -// }; -// } -// -// public static String repeat(String s, int n) { -// StringBuilder sb = new StringBuilder(); -// for (int i = 0; i < n; i++) sb.append(s); -// return sb.toString(); -// } -// -// // MOD NAME -// @Nullable -// public static String getModName(Item item) { -// ResourceLocation tmp = item.getRegistryName(); -// if (tmp == null) return null; -// String id = tmp.getNamespace(); -// return id.equals("minecraft") ? "Minecraft" : Loader.instance().getIndexedModList().get(id).getName(); -// } -// -// public static String getModName(Fluid fluid) { -// String name = fluid.getName(); -// if (name.equals("lava") || name.equals("water")) return "Minecraft"; -// else return Loader.instance().getIndexedModList().get(fluid.getStill().getNamespace()).getName(); -// } -// -// public static NBTTagCompound getTag(ItemStack is) { -// NBTTagCompound tagCompound = is.getTagCompound(); -// if (tagCompound == null) { -// tagCompound = new NBTTagCompound(); -// is.setTagCompound(tagCompound); -// } -// return tagCompound; -// // TODO check return is.getOrCreateSubCompound(JustEnoughCalculation.Reference.MODID); -// } -// -// public static class Relation { -// public HashMap, V> data = new HashMap<>(); -// -// public void put(K a, K b, V v) { -// Pair pair = new Pair<>(a, b); -// V tmp = data.get(pair); -// data.put(tmp == null ? new Pair<>(b, a) : pair, v); -// } -// -// @Nullable -// public V get(K a, K b) { -// V ret = data.get(new Pair<>(a, b)); -// if (ret == null) ret = data.get(new Pair<>(b, a)); -// return ret; -// } -// } -// -// public static class Timer { -// long time = System.currentTimeMillis(); -// boolean running = false; -// -// public void setState(boolean b) { -// if (!b && running) running = false; -// if (b && !running) { -// running = true; -// time = System.currentTimeMillis(); -// } -// } -// -// public long getTime() { -// return running ? System.currentTimeMillis() - time : 0; -// } -// } -// -// @SuppressWarnings("UnusedReturnValue") -// public static class Circulator { -// int total, current; -// -// public Circulator(int total) { -// this(total, 0); -// } -// -// public Circulator(int total, int current) { -// this.total = total; -// this.current = current; -// } -// -// public int next() { -// return current + 1 == total ? 0 : current + 1; -// } -// -// public int prev() { -// return current == 0 ? total - 1 : current - 1; -// } -// -// public Circulator move(int steps) { -// current += steps; -// if (current < 0) current += (-current) / total * total + total; -// else current = current % total; -// return this; -// } -// -// public int current() { -// return current; -// } -// -// public Circulator set(int index) { -// if (index >= 0 && index < total) current = index; -// else throw new RuntimeException(String.format("Expected: [0, %d), given: %d.", total, index)); -// return this; -// } -// -// public Circulator copy() { -// return new Circulator(total).set(current); -// } -// } -// -// public static class ReversedIterator implements Iterator { -// ListIterator i; -// -// public ReversedIterator(List l) { -// i = l.listIterator(l.size()); -// } -// -// @Override -// public boolean hasNext() { -// return i.hasPrevious(); -// } -// -// @Override -// public T next() { -// return i.previous(); -// } -// -// public Stream stream() { -// return StreamSupport.stream(Spliterators.spliteratorUnknownSize(this, Spliterator.ORDERED), false); -// } -// } -// -// @SideOnly(Side.CLIENT) -// public static class I18n { -// public static boolean contains(String s1, String s2) { -// return s1.contains(s2); -// } -// -// public static Pair search(String translateKey, Object... parameters) { -// Pair ret = new Pair<>(null, null); -// translateKey = "jecalculation." + translateKey; -// String buffer = format(translateKey, parameters); -// ret.two = !buffer.equals(translateKey); -// buffer = StringEscapeUtils.unescapeJava(buffer); -// ret.one = buffer.replace("\t", " "); -// return ret; -// } -// -// public static String get(String translateKey, Object... parameters) { -// return search(translateKey, parameters).one; -// } -// -// public static List wrap(String s, int width) { -// return new TextWrapper().wrap(s, MinecraftForgeClient.getLocale(), -// i -> TextWrapper.renderer.getCharWidth(i), width); -// } -// -// static class TextWrapper { -// static FontRenderer renderer = Minecraft.getMinecraft().fontRenderer; -// -// String str; -// BreakIterator it; -// List temp = new ArrayList<>(); -// Function func; -// int start, end, section, space, cursor, width; -// -// private void cut() { -// char c = str.charAt(cursor); -// if (c == '\f') cursor++; -// temp.add(str.substring(start, cursor)); -// if (c == ' ' || c == ' ' || c == '\n') cursor++; -// start = cursor; -// end = cursor; -// space = width; -// section = func.apply(str.charAt(cursor)); -// } -// -// private void move() { -// temp.add(str.substring(start, end)); -// start = end; -// space = width; -// } -// -// private List wrap(String s, Locale l, Function func, int width) { -// temp.clear(); -// start = 0; -// end = 0; -// cursor = 0; -// space = width; -// str = s; -// it = BreakIterator.getLineInstance(l); -// it.setText(s); -// this.width = width; -// this.func = func; -// for (int i = it.next(); i != BreakIterator.DONE; i = it.next()) { -// for (cursor = end; cursor < i; cursor++) { -// char ch = str.charAt(cursor); -// section += func.apply(str.charAt(cursor)); -// if (ch == '\n' || ch == '\f') cut(); -// else if (section > space) { -// if (start == end) cut(); -// else move(); -// } -// } -// space -= section; -// section = 0; -// end = cursor; -// } -// move(); -// return temp; -// } -// } -// } -// -// 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 boolean push(T obj, boolean replace) { -// if (replace) data.pop(); -// boolean ret = data.removeIf(t -> tester != null ? tester.test(t, obj) : t.equals(obj)); -// data.addFirst(obj); -// if (data.size() > limit) data.removeLast(); -// return ret; -// } -// -// public List toList() { -// //noinspection unchecked -// return (List) data.clone(); -// } -// -// public void clear() { -// data.clear(); -// } -// -// public int size() { -// return data.size(); -// } -// } -// -// public static class Json { -// @Nullable -// public static NBTTagCompound read(File f) { -// try { -// String s = FileUtils.readFileToString(f, "UTF-8"); -// return read(s); -// } catch (IOException e) { -// return null; -// } -// } -// -// @Nullable -// public static NBTTagCompound read(String s) { -// try { -// return JsonToNBT.getTagFromJson(s); -// } catch (NBTException e) { -// e.printStackTrace(); -// return null; -// } -// } -// -// public static void write(NBTTagCompound nbt, File f) { -// FileOutputStream fos = null; -// try { -// fos = new FileOutputStream(f); -// fos.write(write(nbt).getBytes(Charset.forName("UTF-8"))); -// } catch (IOException e) { -// e.printStackTrace(); -// } finally { -// if (fos != null) { -// try { -// fos.close(); -// } catch (IOException e) { -// e.printStackTrace(); -// } -// } -// } -// } -// -// public static String write(NBTTagCompound nbt) { -// Writer w = new Writer(); -// write(nbt, w); -// w.enter(); -// return w.build(); -// } -// -// private static void write(NBTBase nbt, Writer w) { -// if (nbt instanceof NBTTagCompound) { -// NBTTagCompound tags = (NBTTagCompound) nbt; -// boolean wrap = tags.getKeySet().size() > 1; -// boolean first = true; -// w.sb.append('{'); -// if (wrap) w.indent++; -// for (String i : tags.getKeySet()) { -// if (first) first = false; -// else w.sb.append(','); -// if (wrap) w.enter(); -// w.sb.append('"'); -// w.sb.append(i); -// w.sb.append("\": "); -// write(tags.getTag(i), w); -// } -// if (wrap) { -// w.indent--; -// w.enter(); -// } -// w.sb.append('}'); -// } else if (nbt instanceof NBTTagList) { -// NBTTagList tags = (NBTTagList) nbt; -// boolean wrap = tags.tagCount() > 1; -// boolean first = true; -// w.sb.append('['); -// if (wrap) w.indent++; -// for (NBTBase i : tags) { -// if (first) first = false; -// else w.sb.append(','); -// if (wrap) w.enter(); -// write(i, w); -// } -// if (wrap) { -// w.indent--; -// w.enter(); -// } -// w.sb.append(']'); -// } else w.sb.append(nbt.toString()); -// } -// -// private static class Writer { -// public int indent = 0; -// StringBuilder sb = new StringBuilder(); -// -// public void enter() { -// sb.append('\n'); -// char[] tmp = new char[4 * indent]; -// Arrays.fill(tmp, ' '); -// sb.append(tmp); -// } -// -// public String build() { -// return sb.toString(); -// } -// } -// } -//} +package me.towdium.jecalculation.utils; + + +import cpw.mods.fml.common.registry.GameData; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import me.towdium.jecalculation.JecaItem; +import me.towdium.jecalculation.JustEnoughCalculation; +import me.towdium.jecalculation.utils.wrappers.Pair; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraft.client.resources.Language; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.*; +import net.minecraftforge.fluids.Fluid; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang3.StringEscapeUtils; + +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.charset.Charset; +import java.text.BreakIterator; +import java.text.DecimalFormat; +import java.util.*; +import java.util.function.BiFunction; +import java.util.function.BiPredicate; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import static net.minecraft.client.resources.I18n.format; + +/** + * Author: Towdium + * Date: 2016/6/25. + */ +@SuppressWarnings("UnusedReturnValue") +@ParametersAreNonnullByDefault +public class Utilities { + // FLOAT FORMATTING + public static char[] suffix = new char[]{'K', 'M', 'B', 'G', 'T', 'P'}; + public static DecimalFormat[] format = new DecimalFormat[]{new DecimalFormat("#."), + new DecimalFormat("#.#"), + new DecimalFormat("#.##"), + new DecimalFormat("#.###"), + new DecimalFormat("#.####")}; + + public static String cutNumber(float f, int size) { + BiFunction form = (fl, len) -> format[len - 1 - (int) Math.log10(fl)].format(fl); + int scale = (int) Math.log10(f) / 3; + if (scale == 0) + return form.apply(f, size); + else + return form.apply(f / (float) Math.pow(1000, scale), size - 1) + suffix[scale - 1]; + } + + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") + public static Stream stream(Optional o) { + return o.map(Stream::of).orElse(Stream.empty()); + } + + public static Supplier fake(Runnable r) { + return () -> { + r.run(); + return null; + }; + } + + public static String repeat(String s, int n) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < n; i++) + sb.append(s); + return sb.toString(); + } + + // MOD NAME + @Nullable + + + + public static String getModName(Item item) { + String name = GameData.getItemRegistry().getNameForObject(item); + JustEnoughCalculation.logger.warn(String.format("item name: %s", name)); + // TODO check + return name; + // ResourceLocation tmp = item.getRegistryName(); + // if (tmp == null) return null; + // String id = tmp.getNamespace(); + // return id.equals("minecraft") ? "Minecraft" : Loader.instance().getIndexedModList().get(id).getName(); + } + + public static String getModName(Fluid fluid) { + String name = GameData.getItemRegistry().getNameForObject(fluid); + JustEnoughCalculation.logger.warn(String.format("fluid name: %s", fluid)); + // TODO check + return name; + } + + public static NBTTagCompound getTag(ItemStack is) { + NBTTagCompound tagCompound = is.getTagCompound(); + if (tagCompound == null) { + tagCompound = new NBTTagCompound(); + is.setTagCompound(tagCompound); + } + return tagCompound; + // TODO check return is.getOrCreateSubCompound(JustEnoughCalculation.Reference.MODID); + } + + public static String translate(String unlocalized, Object... args) { + String localized = getLocalizedString(unlocalized); + return String.format(localized, args); + } + + private static String getLocalizedString(String unlocalized) { + // TODO Implement + return unlocalized; + } + + + public static class Relation { + public HashMap, V> data = new HashMap<>(); + + public void put(K a, K b, V v) { + Pair pair = new Pair<>(a, b); + V tmp = data.get(pair); + data.put(tmp == null ? new Pair<>(b, a) : pair, v); + } + + @Nullable + public V get(K a, K b) { + V ret = data.get(new Pair<>(a, b)); + if (ret == null) + ret = data.get(new Pair<>(b, a)); + return ret; + } + } + + public static class Timer { + long time = System.currentTimeMillis(); + boolean running = false; + + public void setState(boolean b) { + if (!b && running) + running = false; + if (b && !running) { + running = true; + time = System.currentTimeMillis(); + } + } + + public long getTime() { + return running ? System.currentTimeMillis() - time : 0; + } + } + + @SuppressWarnings("UnusedReturnValue") + public static class Circulator { + int total, current; + + public Circulator(int total) { + this(total, 0); + } + + public Circulator(int total, int current) { + this.total = total; + this.current = current; + } + + public int next() { + return current + 1 == total ? 0 : current + 1; + } + + public int prev() { + return current == 0 ? total - 1 : current - 1; + } + + public Circulator move(int steps) { + current += steps; + if (current < 0) + current += (-current) / total * total + total; + else + current = current % total; + return this; + } + + public int current() { + return current; + } + + public Circulator set(int index) { + if (index >= 0 && index < total) + current = index; + else + throw new RuntimeException(String.format("Expected: [0, %d), given: %d.", total, index)); + return this; + } + + public Circulator copy() { + return new Circulator(total).set(current); + } + } + + public static class ReversedIterator implements Iterator { + ListIterator i; + + public ReversedIterator(List l) { + i = l.listIterator(l.size()); + } + + @Override + public boolean hasNext() { + return i.hasPrevious(); + } + + @Override + public T next() { + return i.previous(); + } + + public Stream stream() { + return StreamSupport.stream(Spliterators.spliteratorUnknownSize(this, Spliterator.ORDERED), false); + } + } + + @SideOnly(Side.CLIENT) + public static class I18n { + public static boolean contains(String s1, String s2) { + return s1.contains(s2); + } + + public static Pair search(String translateKey, Object... parameters) { + Pair ret = new Pair<>(null, null); + translateKey = "jecalculation." + translateKey; + String buffer = format(translateKey, parameters); + ret.two = !buffer.equals(translateKey); + buffer = StringEscapeUtils.unescapeJava(buffer); + ret.one = buffer.replace("\t", " "); + return ret; + } + + public static String get(String translateKey, Object... parameters) { + return search(translateKey, parameters).one; + } + + public static List wrap(String s, int width) { + return new TextWrapper().wrap(s, getLocale(), i -> TextWrapper.renderer.getCharWidth(i), width); + } + + /** + * Copy from `net.minecraft.client.resources.Language()` 1.12.2 + * + * @return Locale + */ + public static Locale getLocale() { + Language language = Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage(); + String languageCode = language.getLanguageCode(); + String[] splitLangCode = languageCode.split("_", 2); + if (splitLangCode.length == 1) { // Vanilla has some languages without underscores + return new Locale(languageCode); + } else { + return new Locale(splitLangCode[0], splitLangCode[1]); + } + } + + static class TextWrapper { + static FontRenderer renderer = Minecraft.getMinecraft().fontRenderer; + + String str; + BreakIterator it; + List temp = new ArrayList<>(); + Function func; + int start, end, section, space, cursor, width; + + private void cut() { + char c = str.charAt(cursor); + if (c == '\f') + cursor++; + temp.add(str.substring(start, cursor)); + if (c == ' ' || c == ' ' || c == '\n') + cursor++; + start = cursor; + end = cursor; + space = width; + section = func.apply(str.charAt(cursor)); + } + + private void move() { + temp.add(str.substring(start, end)); + start = end; + space = width; + } + + private List wrap(String s, Locale l, Function func, int width) { + temp.clear(); + start = 0; + end = 0; + cursor = 0; + space = width; + str = s; + it = BreakIterator.getLineInstance(l); + it.setText(s); + this.width = width; + this.func = func; + for (int i = it.next(); i != BreakIterator.DONE; i = it.next()) { + for (cursor = end; cursor < i; cursor++) { + char ch = str.charAt(cursor); + section += func.apply(str.charAt(cursor)); + if (ch == '\n' || ch == '\f') + cut(); + else if (section > space) { + if (start == end) + cut(); + else + move(); + } + } + space -= section; + section = 0; + end = cursor; + } + move(); + return temp; + } + } + } + + 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 boolean push(T obj, boolean replace) { + if (replace) + data.pop(); + boolean ret = data.removeIf(t -> tester != null ? tester.test(t, obj) : t.equals(obj)); + data.addFirst(obj); + if (data.size() > limit) + data.removeLast(); + return ret; + } + + public List toList() { + //noinspection unchecked + return (List) data.clone(); + } + + public void clear() { + data.clear(); + } + + public int size() { + return data.size(); + } + } + + public static class Json { + @Nullable + public static NBTTagCompound read(File f) { + try { + String s = FileUtils.readFileToString(f, "UTF-8"); + return read(s); + } catch (IOException e) { + return null; + } + } + + @Nullable + public static NBTTagCompound read(String s) { + try { + return (NBTTagCompound) JsonToNBT.func_150315_a(s); + } catch (NBTException e) { + e.printStackTrace(); + return null; + } + } + + public static void write(NBTTagCompound nbt, File f) { + FileOutputStream fos = null; + try { + fos = new FileOutputStream(f); + fos.write(write(nbt).getBytes(Charset.forName("UTF-8"))); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + public static String write(NBTTagCompound nbt) { + Writer w = new Writer(); + write(nbt, w); + w.enter(); + return w.build(); + } + + private static void write(NBTBase nbt, Writer w) { + if (nbt instanceof NBTTagCompound) { + NBTTagCompound tags = (NBTTagCompound) nbt; + boolean wrap = tags.func_150296_c().size() > 1; + boolean first = true; + w.sb.append('{'); + if (wrap) + w.indent++; + for (String i : (Set) tags.func_150296_c()) { + if (first) + first = false; + else + w.sb.append(','); + if (wrap) + w.enter(); + w.sb.append('"'); + w.sb.append(i); + w.sb.append("\": "); + write(tags.getTag(i), w); + } + if (wrap) { + w.indent--; + w.enter(); + } + w.sb.append('}'); + } else if (nbt instanceof NBTTagList) { + JustEnoughCalculation.logger.error("nbt is TagList"); + // TODO + // NBTTagList tags = (NBTTagList) nbt; + // boolean wrap = tags.tagCount() > 1; + // boolean first = true; + // w.sb.append('['); + // if (wrap) w.indent++; + // for (NBTBase i : tags) { + // if (first) first = false; + // else w.sb.append(','); + // if (wrap) w.enter(); + // write(i, w); + // } + // if (wrap) { + // w.indent--; + // w.enter(); + // } + // w.sb.append(']'); + } else + w.sb.append(nbt.toString()); + } + + private static class Writer { + public int indent = 0; + StringBuilder sb = new StringBuilder(); + + public void enter() { + sb.append('\n'); + char[] tmp = new char[4 * indent]; + Arrays.fill(tmp, ' '); + sb.append(tmp); + } + + public String build() { + return sb.toString(); + } + } + } +} diff --git a/src/main/java/me/towdium/jecalculation/utils/polyfill/Event.java b/src/main/java/me/towdium/jecalculation/utils/polyfill/Event.java new file mode 100644 index 0000000..bbdc6eb --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/utils/polyfill/Event.java @@ -0,0 +1,30 @@ +package me.towdium.jecalculation.utils.polyfill; + +import cpw.mods.fml.relauncher.Side; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface Event { + + /** + * A class which will be subscribed to {@link net.minecraftforge.common.MinecraftForge.EVENT_BUS} at mod construction time. + */ + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + @interface EventBusSubscriber { + Side[] value() default {Side.CLIENT, Side.SERVER}; + + /** + * Optional value, only nessasary if tis annotation is not on the same class that has a @Mod annotation. + * Needed to prevent early classloading of classes not owned by your mod. + * + * @return mod id + */ + String modid() default ""; + } +} diff --git a/src/main/java/me/towdium/jecalculation/utils/polyfill/Loader.java b/src/main/java/me/towdium/jecalculation/utils/polyfill/Loader.java new file mode 100644 index 0000000..db6d2fd --- /dev/null +++ b/src/main/java/me/towdium/jecalculation/utils/polyfill/Loader.java @@ -0,0 +1,43 @@ +package me.towdium.jecalculation.utils.polyfill; + + +import cpw.mods.fml.common.eventhandler.EventBus; +import me.towdium.jecalculation.JustEnoughCalculation; +import net.minecraftforge.common.MinecraftForge; +import org.reflections.Reflections; +import org.reflections.scanners.ResourcesScanner; +import org.reflections.scanners.SubTypesScanner; +import org.reflections.util.ClasspathHelper; +import org.reflections.util.ConfigurationBuilder; +import org.reflections.util.FilterBuilder; + +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +public class Loader { + + public static void subscribeToEventBus() { + Set> classes = getAllClasses("me.towdium.jecalculation"); + classes.stream().filter((cls) -> cls.isAnnotationPresent(Event.EventBusSubscriber.class)).forEach((cls) -> { + JustEnoughCalculation.logger.info("loading class" + cls.getName()); + try { + MinecraftForge.EVENT_BUS.register(cls.newInstance()); + } catch (InstantiationException | IllegalAccessException e) { + e.printStackTrace(); + } + }); + } + + private static Set> getAllClasses(String packageName) { + List classLoadersList = new LinkedList(); + classLoadersList.add(ClasspathHelper.contextClassLoader()); + classLoadersList.add(ClasspathHelper.staticClassLoader()); + + Reflections reflections = new Reflections(new ConfigurationBuilder().setScanners( + new SubTypesScanner(false /* don't exclude Object.class */), new ResourcesScanner()).setUrls( + ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0]))).filterInputsBy( + new FilterBuilder().include(FilterBuilder.prefix(packageName)))); + return reflections.getSubTypesOf(Object.class); + } +} diff --git a/src/main/resources/assets/jecalculation/lang/en_US.lang b/src/main/resources/assets/jecalculation/lang/en_US.lang index 07fc541..b93d472 100644 --- a/src/main/resources/assets/jecalculation/lang/en_US.lang +++ b/src/main/resources/assets/jecalculation/lang/en_US.lang @@ -16,4 +16,4 @@ gui.recipeEditor.save=Save gui.recipeEditor.clear=Clear gui.recipeViewer.edit=Edit gui.recipeViewer.delete=Delete -item.itemCalculator.name=Calculator +item.item_calculator.name=Calculator diff --git a/src/main/resources/assets/jecalculation/lang/zh_CN.lang b/src/main/resources/assets/jecalculation/lang/zh_CN.lang index a1cd753..3b4e396 100644 --- a/src/main/resources/assets/jecalculation/lang/zh_CN.lang +++ b/src/main/resources/assets/jecalculation/lang/zh_CN.lang @@ -16,4 +16,4 @@ gui.recipeEditor.save=储存 gui.recipeEditor.clear=清除 gui.recipeViewer.edit=编辑 gui.recipeViewer.delete=删除 -item.itemCalculator.name=合成计算器 +item.item_calculator.name=合成计算器 diff --git a/src/main/resources/assets/jecalculation/models/item/itemCalculator.json b/src/main/resources/assets/jecalculation/models/item/item_calculator.json similarity index 52% rename from src/main/resources/assets/jecalculation/models/item/itemCalculator.json rename to src/main/resources/assets/jecalculation/models/item/item_calculator.json index 63059a6..49b1708 100644 --- a/src/main/resources/assets/jecalculation/models/item/itemCalculator.json +++ b/src/main/resources/assets/jecalculation/models/item/item_calculator.json @@ -1,6 +1,6 @@ { "parent": "builtin/generated", "textures": { - "layer0": "jecalculation:items/itemCalculator" + "layer0": "jecalculation:items/item_calculator" } } diff --git a/src/main/resources/assets/jecalculation/textures/items/itemCalculator.png b/src/main/resources/assets/jecalculation/textures/items/item_calculator.png similarity index 100% rename from src/main/resources/assets/jecalculation/textures/items/itemCalculator.png rename to src/main/resources/assets/jecalculation/textures/items/item_calculator.png