From c287b6350ff6687791cb950910cb8af917b45523 Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Mon, 25 Nov 2019 20:28:08 +0100 Subject: [PATCH] Moved some test code, optimized Distiller --- resources/config/v13/de/config.yml | 3 + resources/config/v13/en/config.yml | 3 + src/com/dre/brewery/BDistiller.java | 42 +- src/com/dre/brewery/P.java | 476 +++++------------- src/com/dre/brewery/api/BreweryApi.java | 7 +- src/com/dre/brewery/filedata/BData.java | 4 +- .../brewery/listeners/InventoryListener.java | 72 +-- test/com/dre/brewery/RecipeTests.java | 314 ++++++++++++ 8 files changed, 476 insertions(+), 445 deletions(-) diff --git a/resources/config/v13/de/config.yml b/resources/config/v13/de/config.yml index e8821a8..be39ba1 100644 --- a/resources/config/v13/de/config.yml +++ b/resources/config/v13/de/config.yml @@ -76,6 +76,9 @@ updateCheck: true # Autosave Intervall in Minuten [3] autosave: 3 +# Debug Nachrichten im Log anzeigen [false] +debug: false + # Config Version version: '1.8' diff --git a/resources/config/v13/en/config.yml b/resources/config/v13/en/config.yml index 4da7fce..2704767 100644 --- a/resources/config/v13/en/config.yml +++ b/resources/config/v13/en/config.yml @@ -77,6 +77,9 @@ updateCheck: true # Autosave interval in minutes [3] autosave: 3 +# Debug Nachrichten im Log anzeigen [false] +debug: false + # Config Version version: '1.8' diff --git a/src/com/dre/brewery/BDistiller.java b/src/com/dre/brewery/BDistiller.java index 0c0c757..da7e2ba 100644 --- a/src/com/dre/brewery/BDistiller.java +++ b/src/com/dre/brewery/BDistiller.java @@ -84,11 +84,23 @@ public class BDistiller { return contents; } - public static byte hasBrew(BrewerInventory brewer) { + public static void checkContents(BrewerInventory inv, Brew[] contents) { + ItemStack item; + for (int slot = 0; slot < 3; slot++) { + if (contents[slot] != null) { + item = inv.getItem(slot); + if (item == null || !Brew.isBrew(item)) { + contents[slot] = null; + } + } + } + } + + public static byte hasBrew(BrewerInventory brewer, Brew[] contents) { ItemStack item = brewer.getItem(3); // ingredient boolean glowstone = (item != null && Material.GLOWSTONE_DUST == item.getType()); // need dust in the top slot. byte customFound = 0; - for (Brew brew : getDistillContents(brewer)) { + for (Brew brew : contents) { if (brew != null) { if (!glowstone) { return 1; @@ -103,9 +115,8 @@ public class BDistiller { return customFound; } - public static boolean runDistill(BrewerInventory inv) { + public static boolean runDistill(BrewerInventory inv, Brew[] contents) { boolean custom = false; - Brew[] contents = getDistillContents(inv); for (int slot = 0; slot < 3; slot++) { if (contents[slot] == null) continue; if (contents[slot].canDistill()) { @@ -122,10 +133,9 @@ public class BDistiller { return false; } - public static int getLongestDistillTime(BrewerInventory inv) { + public static int getLongestDistillTime(Brew[] contents) { int bestTime = 0; int time; - Brew[] contents = getDistillContents(inv); for (int slot = 0; slot < 3; slot++) { if (contents[slot] == null) continue; time = contents[slot].getDistillTimeNextRun(); @@ -143,8 +153,7 @@ public class BDistiller { return 800; } - public static void showAlc(BrewerInventory inv) { - Brew[] contents = getDistillContents(inv); + public static void showAlc(BrewerInventory inv, Brew[] contents) { for (int slot = 0; slot < 3; slot++) { if (contents[slot] != null) { // Show Alc in lore @@ -159,6 +168,7 @@ public class BDistiller { } public class DistillRunnable extends BukkitRunnable { + Brew[] contents = null; @Override public void run() { @@ -166,7 +176,13 @@ public class BDistiller { if (now instanceof BrewingStand) { BrewingStand stand = (BrewingStand) now; if (brewTime == -1) { // only check at the beginning (and end) for distillables - switch (hasBrew(stand.getInventory())) { + BrewerInventory inventory = stand.getInventory(); + if (contents == null) { + contents = getDistillContents(inventory); + } else { + checkContents(inventory, contents); + } + switch (hasBrew(inventory, contents)) { case 1: // Custom potion but not for distilling. Stop any brewing and cancel this task if (stand.getBrewingTime() > 0) { @@ -187,11 +203,11 @@ public class BDistiller { // No custom potion, cancel and ignore this.cancel(); trackedDistillers.remove(standBlock); - showAlc(stand.getInventory()); + showAlc(inventory, contents); P.p.debugLog("nothing to distill"); return; default: - runTime = getLongestDistillTime(stand.getInventory()); + runTime = getLongestDistillTime(contents); brewTime = runTime; P.p.debugLog("using brewtime: " + runTime); @@ -202,10 +218,10 @@ public class BDistiller { stand.setBrewingTime((int) ((float) brewTime / ((float) runTime / (float) DISTILLTIME)) + 1); if (brewTime <= 1) { // Done! + checkContents(stand.getInventory(), contents); stand.setBrewingTime(0); stand.update(); - BrewerInventory brewer = stand.getInventory(); - if (!runDistill(brewer)) { + if (!runDistill(stand.getInventory(), contents)) { this.cancel(); trackedDistillers.remove(standBlock); P.p.debugLog("All done distilling"); diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index e48ba79..64c882e 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -72,254 +72,6 @@ public class P extends JavaPlugin { useNBT = true; } - //P.p.log("§" + (use1_9 ? "a":"c") + "1.9 " + "§" + (use1_11 ? "a":"c") + "1.11 " + "§" + (use1_13 ? "a":"c") + "1.13 " + "§" + (use1_14 ? "a":"c") + "1.14"); - - /*long master = new SecureRandom().nextLong(); - ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); - XORScrambleStream scramble = new XORScrambleStream(new Base91EncoderStream(byteStream), master); - DataOutputStream data = new DataOutputStream(scramble); - DataInputStream dataIn = null; - try { - scramble.start(); - data.writeLong(12345L); - scramble.stop(); - data.writeInt(1); - data.writeInt(1); - scramble.start(); - data.writeDouble(0.55555D); - data.writeInt(234323); - //data.writeUTF("Hallo Peter"); - data.writeLong(5419L); // Skip - data.writeDouble(0.55555D); - - data.close(); - - XORUnscrambleStream unscramble = new XORUnscrambleStream(new Base91DecoderStream(new ByteArrayInputStream(byteStream.toByteArray())), master); - dataIn = new DataInputStream(unscramble); - unscramble.start(); - P.p.log(dataIn.readLong() + ""); - unscramble.stop(); - P.p.log(dataIn.readInt() + ""); - P.p.log(dataIn.readInt() + ""); - unscramble.start(); - P.p.log(dataIn.readDouble() + ""); - dataIn.mark(1000); - P.p.log(dataIn.readInt() + ""); - //P.p.log(dataIn.readUTF()); - dataIn.skip(8); - P.p.log(dataIn.readDouble() + ""); - P.p.log("reset"); - dataIn.reset(); - P.p.log(dataIn.readInt() + ""); - //P.p.log(dataIn.readUTF()); - dataIn.skip(8); - P.p.log(dataIn.readDouble() + ""); - - dataIn.close(); - - *//*for (int i = 0; i < 10; i++) { - byteStream = new ByteArrayOutputStream(); - scramble = new XORScrambleStream(new Base91EncoderStream(byteStream)); - data = new DataOutputStream(scramble); - data.writeInt(i); - scramble.start(); - data.writeLong(12345L); - data.writeLong(12345L); - scramble.stop(); - data.writeInt(1); - data.writeInt(1); - scramble.start(); - data.writeInt(234323); - data.writeDouble(0.55555D); - - P.p.log(byteStream.toString()); - data.close(); - }*//* - - - long time = System.currentTimeMillis(); - for (int i = 0; i < 100000; i++) { - unscramble = new XORUnscrambleStream(new Base91DecoderStream(new ByteArrayInputStream(byteStream.toByteArray())), master); - dataIn = new DataInputStream(unscramble); - unscramble.start(); - dataIn.readLong(); - unscramble.stop(); - dataIn.readInt(); - dataIn.readInt(); - unscramble.start(); - dataIn.readDouble(); - dataIn.mark(1000); - dataIn.readInt(); - //dataIn.readUTF(); - dataIn.skip(8); - dataIn.readDouble(); - dataIn.reset(); - dataIn.readInt(); - //dataIn.readUTF(); - dataIn.skip(8); - dataIn.readDouble(); - - dataIn.close(); - } - long time2 = System.currentTimeMillis(); - - for (int i = 0; i < 100000; i++) { - unscramble = new XORUnscrambleStream(new ByteArrayInputStream(byteStream.toByteArray()), master); - dataIn = new DataInputStream(unscramble); - unscramble.start(); - dataIn.skip(2); - dataIn.readLong(); - unscramble.stop(); - dataIn.readInt(); - dataIn.readInt(); - unscramble.start(); - dataIn.readDouble(); - dataIn.mark(1000); - dataIn.readInt(); - //dataIn.readUTF(); - dataIn.skip(8); - dataIn.readDouble(); - dataIn.reset(); - dataIn.readInt(); - //dataIn.readUTF(); - dataIn.skip(8); - dataIn.readDouble(); - - dataIn.close(); - } - long time3 = System.currentTimeMillis(); - - P.p.log("Time with base91: " + (time2 - time)); - P.p.log("Time without base91: " + (time3 - time2)); - - } catch (IOException e) { - e.printStackTrace(); - } catch (InvalidKeyException e) { - e.printStackTrace(); - } finally { - try { - data.close(); - if (dataIn != null) { - dataIn.close(); - } - } catch (IOException e) { - e.printStackTrace(); - } - }*/ - - /*try { - ItemMeta meta = new ItemStack(Material.POTION).getItemMeta(); - DataOutputStream data = new DataOutputStream(new Base91EncoderStream(new LoreSaveStream(meta, 3))); - - data.writeInt(2); - data.writeLong(5); - - byte[] test = new byte[128]; - test[1] = 6; - test[2] = 12; - test[3] = 21; - test[127] = 99; - data.write(test); - - data.writeInt(123324); - data.writeLong(12343843); - - data.close(); - meta.getLore(); - - DataInputStream dataIn = new DataInputStream(new Base91DecoderStream(new LoreLoadStream(meta))); - - P.p.log(dataIn.readInt() + ", " + dataIn.readLong() + ", "); - - byte[] testIn = new byte[128]; - dataIn.read(testIn); - P.p.log(testIn[1] + ", " + testIn[2] + ", " + testIn[3] + ", " + testIn[127]); - - P.p.log(dataIn.readInt() + ", " + dataIn.readLong() + ", "); - - dataIn.close(); - - - - basE91 basE91 = new basE91(); - int[] input = new int[] {12, 65, 324, 5, 12, 129459, 1234567, Integer.MIN_VALUE, Integer.MAX_VALUE}; - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - DataOutputStream data = new DataOutputStream(stream); - for (int i = 0; i < input.length; i++) { - data.writeInt(input[i]); - } - data.flush(); - data.close(); - byte[] in = stream.toByteArray(); - byte[] out = new byte[4096]; - int lenght = basE91.encode(in, in.length, out); - basE91.encEnd(out); - String done = new String(out, 0, lenght); - - byte[] tin = done.getBytes(); - - byte[] tout = new byte[4096]; - lenght = basE91.decode(tin, tin.length, tout); - basE91.decEnd(tout); - - - ByteArrayInputStream tstream = new ByteArrayInputStream(tout, 0, lenght); - DataInputStream tdata = new DataInputStream(tstream); - int[] test = new int[4096]; - for (int j = 0; j < 6; j++) { - if (tstream.available() <= 0) break; - test[j] = tdata.readInt(); - - } - tdata.close(); - test = test;*/ - - - - /*basE91 basE91 = new basE91(); - int[] input = new int[] {12, 65, 324, 5, 12, 129459, 1234567, Integer.MIN_VALUE, Integer.MAX_VALUE}; - ByteArrayOutputStream stream = new ByteArrayOutputStream(); - DataOutputStream data = new DataOutputStream(stream); - for (int i = 0; i < input.length; i++) { - data.writeInt(input[i]); - } - data.flush(); - data.close(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ByteArrayInputStream in = new ByteArrayInputStream(stream.toByteArray()); - - encode(in, out, in.available()); - - in.close(); - out.flush(); - out.close(); - - String done = new String(out.toByteArray()); - - ByteArrayInputStream tin = new ByteArrayInputStream(done.getBytes()); - ByteArrayOutputStream tout = new ByteArrayOutputStream(); - - decode(tin, tout, tin.available()); - - tin.close(); - tout.flush(); - tout.close(); - - ByteArrayInputStream tstream = new ByteArrayInputStream(tout.toByteArray()); - DataInputStream tdata = new DataInputStream(tstream); - int[] test = new int[4096]; - for (int j = 0; j < 9; j++) { - if (tstream.available() <= 0) break; - test[j] = tdata.readInt(); - - } - tdata.close(); - test = test; - - } catch (IOException e) { - e.printStackTrace(); - }*/ - if (use1_14) { // Campfires are weird // Initialize once now so it doesn't lag later when we check for campfires under Cauldrons @@ -343,108 +95,7 @@ public class P extends JavaPlugin { BData.readData(); // Setup Metrics - /*try { - Metrics metrics = new Metrics(this); - metrics.addCustomChart(new Metrics.SingleLineChart("drunk_players", BPlayer::numDrunkPlayers)); - metrics.addCustomChart(new Metrics.SingleLineChart("brews_in_existence", () -> brewsCreated)); - metrics.addCustomChart(new Metrics.SingleLineChart("barrels_built", () -> Barrel.barrels.size())); - metrics.addCustomChart(new Metrics.SingleLineChart("cauldrons_boiling", () -> BCauldron.bcauldrons.size())); - metrics.addCustomChart(new Metrics.AdvancedPie("brew_quality", () -> { - Map map = new HashMap<>(8); - map.put("excellent", exc); - map.put("good", good); - map.put("normal", norm); - map.put("bad", bad); - map.put("terrible", terr); - return map; - })); - metrics.addCustomChart(new Metrics.AdvancedPie("brews_created", () -> { - Map map = new HashMap<>(4); - map.put("by command", brewsCreatedCmd); - map.put("brewing", brewsCreated - brewsCreatedCmd); - return map; - })); - - metrics.addCustomChart(new Metrics.SimplePie("number_of_recipes", () -> { - int recipes = BRecipe.getAllRecipes().size(); - if (recipes < 7) { - return "Less than 7"; - } else if (recipes < 11) { - return "7-10"; - } else if (recipes == 11) { - // There are 11 default recipes, so show this as its own slice - return "11"; - } else if (recipes <= 31) { - if (recipes % 2 == 0) { - return recipes + "-" + (recipes + 1); - } else { - return (recipes - 1) + "-" + recipes; - } - } else { - return "More than 31"; - } - - })); - metrics.addCustomChart(new Metrics.SimplePie("v2_mc_version", () -> { - String mcv = Bukkit.getBukkitVersion(); - mcv = mcv.substring(0, mcv.indexOf('.', 2)); - if (mcv.matches("^\\d\\.\\d{1,2}$")) { - // Start, digit, dot, 1-2 digits, end - return mcv; - } else { - return "undef"; - } - })); - metrics.addCustomChart(new Metrics.DrilldownPie("plugin_mc_version", () -> { - Map> map = new HashMap<>(3); - String mcv = Bukkit.getBukkitVersion(); - mcv = mcv.substring(0, mcv.indexOf('.', 2)); - if (mcv.matches("^\\d\\.\\d{1,2}$")) { - // Start, digit, dot, 1-2 digits, end - mcv = "MC " + mcv; - } else { - mcv = "undef"; - } - Map innerMap = new HashMap<>(3); - innerMap.put(mcv, 1); - map.put(getDescription().getVersion(), innerMap); - return map; - })); - metrics.addCustomChart(new Metrics.SimplePie("language", () -> language)); - metrics.addCustomChart(new Metrics.SimplePie("config_scramble", () -> BConfig.enableEncode ? "enabled" : "disabled")); - metrics.addCustomChart(new Metrics.SimplePie("config_lore_color", () -> { - if (BConfig.colorInBarrels) { - if (BConfig.colorInBrewer) { - return "both"; - } else { - return "in barrels"; - } - } else { - if (BConfig.colorInBrewer) { - return "in distiller"; - } else { - return "none"; - } - } - })); - metrics.addCustomChart(new Metrics.SimplePie("config_always_show", () -> { - if (BConfig.alwaysShowQuality) { - if (BConfig.alwaysShowAlc) { - return "both"; - } else { - return "quality stars"; - } - } else { - if (BConfig.alwaysShowAlc) { - return "alc content"; - } else { - return "none"; - } - } - })); - } catch (Throwable e) { - e.printStackTrace(); - }*/ + setupMetrics(); // Listeners blockListener = new BlockListener(); @@ -584,6 +235,111 @@ public class P extends JavaPlugin { return p; } + private void setupMetrics() { + /*try { + Metrics metrics = new Metrics(this); + metrics.addCustomChart(new Metrics.SingleLineChart("drunk_players", BPlayer::numDrunkPlayers)); + metrics.addCustomChart(new Metrics.SingleLineChart("brews_in_existence", () -> brewsCreated)); + metrics.addCustomChart(new Metrics.SingleLineChart("barrels_built", () -> Barrel.barrels.size())); + metrics.addCustomChart(new Metrics.SingleLineChart("cauldrons_boiling", () -> BCauldron.bcauldrons.size())); + metrics.addCustomChart(new Metrics.AdvancedPie("brew_quality", () -> { + Map map = new HashMap<>(8); + map.put("excellent", exc); + map.put("good", good); + map.put("normal", norm); + map.put("bad", bad); + map.put("terrible", terr); + return map; + })); + metrics.addCustomChart(new Metrics.AdvancedPie("brews_created", () -> { + Map map = new HashMap<>(4); + map.put("by command", brewsCreatedCmd); + map.put("brewing", brewsCreated - brewsCreatedCmd); + return map; + })); + + metrics.addCustomChart(new Metrics.SimplePie("number_of_recipes", () -> { + int recipes = BRecipe.getAllRecipes().size(); + if (recipes < 7) { + return "Less than 7"; + } else if (recipes < 11) { + return "7-10"; + } else if (recipes == 11) { + // There are 11 default recipes, so show this as its own slice + return "11"; + } else if (recipes <= 31) { + if (recipes % 2 == 0) { + return recipes + "-" + (recipes + 1); + } else { + return (recipes - 1) + "-" + recipes; + } + } else { + return "More than 31"; + } + + })); + metrics.addCustomChart(new Metrics.SimplePie("v2_mc_version", () -> { + String mcv = Bukkit.getBukkitVersion(); + mcv = mcv.substring(0, mcv.indexOf('.', 2)); + if (mcv.matches("^\\d\\.\\d{1,2}$")) { + // Start, digit, dot, 1-2 digits, end + return mcv; + } else { + return "undef"; + } + })); + metrics.addCustomChart(new Metrics.DrilldownPie("plugin_mc_version", () -> { + Map> map = new HashMap<>(3); + String mcv = Bukkit.getBukkitVersion(); + mcv = mcv.substring(0, mcv.indexOf('.', 2)); + if (mcv.matches("^\\d\\.\\d{1,2}$")) { + // Start, digit, dot, 1-2 digits, end + mcv = "MC " + mcv; + } else { + mcv = "undef"; + } + Map innerMap = new HashMap<>(3); + innerMap.put(mcv, 1); + map.put(getDescription().getVersion(), innerMap); + return map; + })); + metrics.addCustomChart(new Metrics.SimplePie("language", () -> language)); + metrics.addCustomChart(new Metrics.SimplePie("config_scramble", () -> BConfig.enableEncode ? "enabled" : "disabled")); + metrics.addCustomChart(new Metrics.SimplePie("config_lore_color", () -> { + if (BConfig.colorInBarrels) { + if (BConfig.colorInBrewer) { + return "both"; + } else { + return "in barrels"; + } + } else { + if (BConfig.colorInBrewer) { + return "in distiller"; + } else { + return "none"; + } + } + })); + metrics.addCustomChart(new Metrics.SimplePie("config_always_show", () -> { + if (BConfig.alwaysShowQuality) { + if (BConfig.alwaysShowAlc) { + return "both"; + } else { + return "quality stars"; + } + } else { + if (BConfig.alwaysShowAlc) { + return "alc content"; + } else { + return "none"; + } + } + })); + } catch (Throwable e) { + e.printStackTrace(); + }*/ + } + public void metricsForCreate(boolean byCmd) { if (brewsCreated == Integer.MAX_VALUE) return; brewsCreated++; @@ -653,32 +409,28 @@ public class P extends JavaPlugin { public class BreweryRunnable implements Runnable { @Override public void run() { - //long t1 = System.nanoTime(); + long t1 = System.nanoTime(); BConfig.reloader = null; for (BCauldron cauldron : BCauldron.bcauldrons.values()) { cauldron.onUpdate();// runs every min to update cooking time } - //long t2 = System.nanoTime(); + long t2 = System.nanoTime(); Barrel.onUpdate();// runs every min to check and update ageing time - //long t3 = System.nanoTime(); + long t3 = System.nanoTime(); if (use1_14) MCBarrel.onUpdate(); - //long t4 = System.nanoTime(); + long t4 = System.nanoTime(); BPlayer.onUpdate();// updates players drunkeness - //long t5 = System.nanoTime(); - debugLog("Update"); - - //long t6 = System.nanoTime(); + long t5 = System.nanoTime(); DataSave.autoSave(); - //long t7 = System.nanoTime(); + long t6 = System.nanoTime(); - /*P.p.log("BreweryRunnable: " + + debugLog("BreweryRunnable: " + "t1: " + (t2 - t1) / 1000000.0 + "ms" + " | t2: " + (t3 - t2) / 1000000.0 + "ms" + " | t3: " + (t4 - t3) / 1000000.0 + "ms" + " | t4: " + (t5 - t4) / 1000000.0 + "ms" + - " | t5: " + (t6 - t5) / 1000000.0 + "ms" + - " | t6: " + (t7 - t6) / 1000000.0 + "ms" );*/ + " | t5: " + (t6 - t5) / 1000000.0 + "ms" ); } } diff --git a/src/com/dre/brewery/api/BreweryApi.java b/src/com/dre/brewery/api/BreweryApi.java index 5b256aa..6748ce8 100644 --- a/src/com/dre/brewery/api/BreweryApi.java +++ b/src/com/dre/brewery/api/BreweryApi.java @@ -7,7 +7,6 @@ import com.dre.brewery.recipe.BCauldronRecipe; import com.dre.brewery.recipe.BRecipe; import com.dre.brewery.Barrel; import com.dre.brewery.Brew; -import com.dre.brewery.utility.Tuple; import org.apache.commons.lang.NotImplementedException; import org.bukkit.block.Block; import org.bukkit.entity.Player; @@ -242,7 +241,8 @@ public class BreweryApi { * The recipe can be changed or removed later. * * @param recipe The Recipe to add - * @param saveForever If the recipe should be saved forever, even after the Server restarts + * @param saveForever Not Implemented yet + * If the recipe should be saved forever, even after the Server restarts * If True: Recipe will be saved until removed manually * If False: Recipe will be removed when the Server restarts, existing potions using * this Recipe will become bad after continued aging, if the recipe is not added again. @@ -310,7 +310,8 @@ public class BreweryApi { * The recipe can be changed or removed later. * * @param recipe The Cauldron Recipe to add - * @param saveForever If the recipe should be saved forever, even after the Server restarts + * @param saveForever Not Implemented yet + * If the recipe should be saved forever, even after the Server restarts * If True: Recipe will be saved until removed manually * If False: Recipe will be removed when the Server restarts */ diff --git a/src/com/dre/brewery/filedata/BData.java b/src/com/dre/brewery/filedata/BData.java index 0ddb8b9..37e4263 100644 --- a/src/com/dre/brewery/filedata/BData.java +++ b/src/com/dre/brewery/filedata/BData.java @@ -38,10 +38,12 @@ public class BData { long t2 = System.currentTimeMillis(); - if (t2 - t1 > 5000) { + if (t2 - t1 > 8000) { // Spigot is very slow at loading inventories from yml. Notify Admin that loading Data took long P.p.log("Bukkit took " + (t2 - t1) / 1000.0 + "s to load the Data File,"); P.p.log("consider switching to Paper, or have less items in Barrels"); + } else { + P.p.debugLog("Loading data.yml: " + (t2 - t1) + "ms"); } Brew.installTime = data.getLong("installTime", System.currentTimeMillis()); diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index de8b5dc..b30d6e7 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -91,12 +91,12 @@ public class InventoryListener implements Listener { @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onBrew(BrewEvent event) { if (P.use1_9) { - if (BDistiller.hasBrew(event.getContents()) != 0) { + if (BDistiller.hasBrew(event.getContents(), BDistiller.getDistillContents(event.getContents())) != 0) { event.setCancelled(true); } return; } - if (BDistiller.runDistill(event.getContents())) { + if (BDistiller.runDistill(event.getContents(), BDistiller.getDistillContents(event.getContents()))) { event.setCancelled(true); } } @@ -126,75 +126,15 @@ public class InventoryListener implements Listener { } } } + //long t1 = System.nanoTime(); Brew brew = Brew.get(item); + //long t2 = System.nanoTime(); if (brew != null) { P.p.log(brew.toString()); - //P.p.log(potion.getLore().get(0).replaceAll("§", "")); - //P.p.log("similar to beispiel? " + BRecipe.get("Beispiel").createBrew(10).isSimilar(brew)); - brew.touch(); + //P.p.log("Brew.get(): " + (t2 - t1) / 1000000.0 + "ms"); - /*try { - DataInputStream in = new DataInputStream(new Base91DecoderStream(new LoreLoadStream(potion))); - - brew.testLoad(in); - - *//*if (in.readByte() == 27 && in.skip(48) > 0) { - in.mark(100); - if (in.readUTF().equals("TESTHalloª∆Ω") && in.readInt() == 34834 && in.skip(4) > 0 && in.readLong() == Long.MAX_VALUE) { - in.reset(); - if (in.readUTF().equals("TESTHalloª∆Ω")) { - P.p.log("true"); - } else { - P.p.log("false3"); - } - } else { - P.p.log("false2"); - } - } else { - P.p.log("false1"); - }*//* - - in.close(); - } catch (IllegalArgumentException argExc) { - P.p.log("No Data in Lore"); - - try { - - DataOutputStream out = new DataOutputStream(new Base91EncoderStream(new LoreSaveStream(potion, 2))); - - brew.testStore(out); - - - *//*out.writeByte(27); - out.writeLong(1111); //skip - out.writeLong(1111); //skip - out.writeLong(1111); //skip - out.writeLong(1111); //skip - out.writeLong(1111); //skip - out.writeLong(1111); //skip - out.writeUTF("TESTHalloª∆Ω"); - out.writeInt(34834); - out.writeInt(6436); //skip - out.writeLong(Long.MAX_VALUE);*//* - - out.close(); - *//*StringBuilder b = new StringBuilder(); - for (char c : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!$%&()*+,-./:;<=>?@[]^_`{|}~\"".toCharArray()) { - b.append('§').append(c); - } - List lore = potion.getLore(); - lore.add(b.toString()); - potion.setLore(lore);*//* - item.setItemMeta(potion); - - } catch (IOException h) { - h.printStackTrace(); - } - - } catch (IOException e) { - e.printStackTrace(); - }*/ + //brew.touch(); } } } diff --git a/test/com/dre/brewery/RecipeTests.java b/test/com/dre/brewery/RecipeTests.java index e7efb04..1edc82f 100644 --- a/test/com/dre/brewery/RecipeTests.java +++ b/test/com/dre/brewery/RecipeTests.java @@ -89,4 +89,318 @@ public class RecipeTests { .get(); BreweryApi.addCauldronRecipe(r, false); } + + public static void onClick() { + /*try { + DataInputStream in = new DataInputStream(new Base91DecoderStream(new LoreLoadStream(potion))); + + brew.testLoad(in); + + *//*if (in.readByte() == 27 && in.skip(48) > 0) { + in.mark(100); + if (in.readUTF().equals("TESTHalloª∆Ω") && in.readInt() == 34834 && in.skip(4) > 0 && in.readLong() == Long.MAX_VALUE) { + in.reset(); + if (in.readUTF().equals("TESTHalloª∆Ω")) { + P.p.log("true"); + } else { + P.p.log("false3"); + } + } else { + P.p.log("false2"); + } + } else { + P.p.log("false1"); + }*//* + + in.close(); + } catch (IllegalArgumentException argExc) { + P.p.log("No Data in Lore"); + + try { + + DataOutputStream out = new DataOutputStream(new Base91EncoderStream(new LoreSaveStream(potion, 2))); + + brew.testStore(out); + + + *//*out.writeByte(27); + out.writeLong(1111); //skip + out.writeLong(1111); //skip + out.writeLong(1111); //skip + out.writeLong(1111); //skip + out.writeLong(1111); //skip + out.writeLong(1111); //skip + out.writeUTF("TESTHalloª∆Ω"); + out.writeInt(34834); + out.writeInt(6436); //skip + out.writeLong(Long.MAX_VALUE);*//* + + out.close(); + *//*StringBuilder b = new StringBuilder(); + for (char c : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!$%&()*+,-./:;<=>?@[]^_`{|}~\"".toCharArray()) { + b.append('§').append(c); + } + List lore = potion.getLore(); + lore.add(b.toString()); + potion.setLore(lore);*//* + item.setItemMeta(potion); + + } catch (IOException h) { + h.printStackTrace(); + } + + } catch (IOException e) { + e.printStackTrace(); + }*/ + } + + public static void onLoad() { + //P.p.log("§" + (use1_9 ? "a":"c") + "1.9 " + "§" + (use1_11 ? "a":"c") + "1.11 " + "§" + (use1_13 ? "a":"c") + "1.13 " + "§" + (use1_14 ? "a":"c") + "1.14"); + + /*long master = new SecureRandom().nextLong(); + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + XORScrambleStream scramble = new XORScrambleStream(new Base91EncoderStream(byteStream), master); + DataOutputStream data = new DataOutputStream(scramble); + DataInputStream dataIn = null; + try { + scramble.start(); + data.writeLong(12345L); + scramble.stop(); + data.writeInt(1); + data.writeInt(1); + scramble.start(); + data.writeDouble(0.55555D); + data.writeInt(234323); + //data.writeUTF("Hallo Peter"); + data.writeLong(5419L); // Skip + data.writeDouble(0.55555D); + + data.close(); + + XORUnscrambleStream unscramble = new XORUnscrambleStream(new Base91DecoderStream(new ByteArrayInputStream(byteStream.toByteArray())), master); + dataIn = new DataInputStream(unscramble); + unscramble.start(); + P.p.log(dataIn.readLong() + ""); + unscramble.stop(); + P.p.log(dataIn.readInt() + ""); + P.p.log(dataIn.readInt() + ""); + unscramble.start(); + P.p.log(dataIn.readDouble() + ""); + dataIn.mark(1000); + P.p.log(dataIn.readInt() + ""); + //P.p.log(dataIn.readUTF()); + dataIn.skip(8); + P.p.log(dataIn.readDouble() + ""); + P.p.log("reset"); + dataIn.reset(); + P.p.log(dataIn.readInt() + ""); + //P.p.log(dataIn.readUTF()); + dataIn.skip(8); + P.p.log(dataIn.readDouble() + ""); + + dataIn.close(); + + *//*for (int i = 0; i < 10; i++) { + byteStream = new ByteArrayOutputStream(); + scramble = new XORScrambleStream(new Base91EncoderStream(byteStream)); + data = new DataOutputStream(scramble); + data.writeInt(i); + scramble.start(); + data.writeLong(12345L); + data.writeLong(12345L); + scramble.stop(); + data.writeInt(1); + data.writeInt(1); + scramble.start(); + data.writeInt(234323); + data.writeDouble(0.55555D); + + P.p.log(byteStream.toString()); + data.close(); + }*//* + + + long time = System.currentTimeMillis(); + for (int i = 0; i < 100000; i++) { + unscramble = new XORUnscrambleStream(new Base91DecoderStream(new ByteArrayInputStream(byteStream.toByteArray())), master); + dataIn = new DataInputStream(unscramble); + unscramble.start(); + dataIn.readLong(); + unscramble.stop(); + dataIn.readInt(); + dataIn.readInt(); + unscramble.start(); + dataIn.readDouble(); + dataIn.mark(1000); + dataIn.readInt(); + //dataIn.readUTF(); + dataIn.skip(8); + dataIn.readDouble(); + dataIn.reset(); + dataIn.readInt(); + //dataIn.readUTF(); + dataIn.skip(8); + dataIn.readDouble(); + + dataIn.close(); + } + long time2 = System.currentTimeMillis(); + + for (int i = 0; i < 100000; i++) { + unscramble = new XORUnscrambleStream(new ByteArrayInputStream(byteStream.toByteArray()), master); + dataIn = new DataInputStream(unscramble); + unscramble.start(); + dataIn.skip(2); + dataIn.readLong(); + unscramble.stop(); + dataIn.readInt(); + dataIn.readInt(); + unscramble.start(); + dataIn.readDouble(); + dataIn.mark(1000); + dataIn.readInt(); + //dataIn.readUTF(); + dataIn.skip(8); + dataIn.readDouble(); + dataIn.reset(); + dataIn.readInt(); + //dataIn.readUTF(); + dataIn.skip(8); + dataIn.readDouble(); + + dataIn.close(); + } + long time3 = System.currentTimeMillis(); + + P.p.log("Time with base91: " + (time2 - time)); + P.p.log("Time without base91: " + (time3 - time2)); + + } catch (IOException e) { + e.printStackTrace(); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } finally { + try { + data.close(); + if (dataIn != null) { + dataIn.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + }*/ + + /*try { + ItemMeta meta = new ItemStack(Material.POTION).getItemMeta(); + DataOutputStream data = new DataOutputStream(new Base91EncoderStream(new LoreSaveStream(meta, 3))); + + data.writeInt(2); + data.writeLong(5); + + byte[] test = new byte[128]; + test[1] = 6; + test[2] = 12; + test[3] = 21; + test[127] = 99; + data.write(test); + + data.writeInt(123324); + data.writeLong(12343843); + + data.close(); + meta.getLore(); + + DataInputStream dataIn = new DataInputStream(new Base91DecoderStream(new LoreLoadStream(meta))); + + P.p.log(dataIn.readInt() + ", " + dataIn.readLong() + ", "); + + byte[] testIn = new byte[128]; + dataIn.read(testIn); + P.p.log(testIn[1] + ", " + testIn[2] + ", " + testIn[3] + ", " + testIn[127]); + + P.p.log(dataIn.readInt() + ", " + dataIn.readLong() + ", "); + + dataIn.close(); + + + + basE91 basE91 = new basE91(); + int[] input = new int[] {12, 65, 324, 5, 12, 129459, 1234567, Integer.MIN_VALUE, Integer.MAX_VALUE}; + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + DataOutputStream data = new DataOutputStream(stream); + for (int i = 0; i < input.length; i++) { + data.writeInt(input[i]); + } + data.flush(); + data.close(); + byte[] in = stream.toByteArray(); + byte[] out = new byte[4096]; + int lenght = basE91.encode(in, in.length, out); + basE91.encEnd(out); + String done = new String(out, 0, lenght); + + byte[] tin = done.getBytes(); + + byte[] tout = new byte[4096]; + lenght = basE91.decode(tin, tin.length, tout); + basE91.decEnd(tout); + + + ByteArrayInputStream tstream = new ByteArrayInputStream(tout, 0, lenght); + DataInputStream tdata = new DataInputStream(tstream); + int[] test = new int[4096]; + for (int j = 0; j < 6; j++) { + if (tstream.available() <= 0) break; + test[j] = tdata.readInt(); + + } + tdata.close(); + test = test;*/ + + + + /*basE91 basE91 = new basE91(); + int[] input = new int[] {12, 65, 324, 5, 12, 129459, 1234567, Integer.MIN_VALUE, Integer.MAX_VALUE}; + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + DataOutputStream data = new DataOutputStream(stream); + for (int i = 0; i < input.length; i++) { + data.writeInt(input[i]); + } + data.flush(); + data.close(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayInputStream in = new ByteArrayInputStream(stream.toByteArray()); + + encode(in, out, in.available()); + + in.close(); + out.flush(); + out.close(); + + String done = new String(out.toByteArray()); + + ByteArrayInputStream tin = new ByteArrayInputStream(done.getBytes()); + ByteArrayOutputStream tout = new ByteArrayOutputStream(); + + decode(tin, tout, tin.available()); + + tin.close(); + tout.flush(); + tout.close(); + + ByteArrayInputStream tstream = new ByteArrayInputStream(tout.toByteArray()); + DataInputStream tdata = new DataInputStream(tstream); + int[] test = new int[4096]; + for (int j = 0; j < 9; j++) { + if (tstream.available() <= 0) break; + test[j] = tdata.readInt(); + + } + tdata.close(); + test = test; + + } catch (IOException e) { + e.printStackTrace(); + }*/ + } }