From 67ab8e7de3979375f81a7d8fc88974505df7c979 Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Sat, 17 Aug 2019 18:53:06 +0200 Subject: [PATCH] Support 1.14 Minecraft Barrels for Aging --- resources/config/v13/de/config.yml | 17 +- resources/config/v13/en/config.yml | 17 +- resources/config/v13/fr/config.yml | 19 +- resources/config/v13/it/config.yml | 17 +- resources/languages/de.yml | 1 + resources/languages/en.yml | 1 + resources/languages/fr.yml | 1 + resources/languages/it.yml | 1 + src/com/dre/brewery/BIngredients.java | 10 +- src/com/dre/brewery/Brew.java | 7 +- src/com/dre/brewery/MCBarrel.java | 201 ++++++++++++------ src/com/dre/brewery/P.java | 8 +- .../dre/brewery/filedata/ConfigUpdater.java | 65 +++++- src/com/dre/brewery/filedata/DataSave.java | 2 + .../brewery/listeners/InventoryListener.java | 78 +++++-- 15 files changed, 324 insertions(+), 121 deletions(-) diff --git a/resources/config/v13/de/config.yml b/resources/config/v13/de/config.yml index a09de94..2444d0b 100644 --- a/resources/config/v13/de/config.yml +++ b/resources/config/v13/de/config.yml @@ -54,6 +54,9 @@ colorInBrewer: true # Ob große Fässer an jedem Block geöffnet werden können, nicht nur an Zapfhahn und Schild. Bei kleinen Fässern geht dies immer. [true] openLargeBarrelEverywhere: true +# Wie viele Brewery Getränke in die Minecraft Fässer getan werden können [6] +maxBrewsInMCBarrels: 6 + # In den Serverlog loggen was der Spieler tatsächlich geschrieben hat, bevor seine Worte verändert wurden [false] logRealChat: false @@ -121,7 +124,7 @@ recipes: - POISONX/1-0/20-0 1: name: Ranziges Weißbier/Weißbier/Feines Weißbier - ingredients: + ingredients: - Wheat/3 cookingtime: 8 distillruns: 0 @@ -132,7 +135,7 @@ recipes: alcohol: 5 2: name: Ranziges Bier/Bier/Feines Bier - ingredients: + ingredients: - Wheat/6 cookingtime: 8 distillruns: 0 @@ -143,7 +146,7 @@ recipes: alcohol: 6 3: name: Ranziges Dunkelbier/Dunkelbier/Feines Dunkelbier - ingredients: + ingredients: - Wheat/6 cookingtime: 8 distillruns: 0 @@ -154,7 +157,7 @@ recipes: alcohol: 7 4: name: Scheußlicher Met/Met/&6Goldener Met - ingredients: + ingredients: - Sugar_Cane/6 cookingtime: 3 distillruns: 0 @@ -165,7 +168,7 @@ recipes: alcohol: 9 5: name: Apfelmet/Süßer Apfelmet/&6Goldensüßer Apfelmet - ingredients: + ingredients: - Sugar_Cane/6 - Apple/2 cookingtime: 4 @@ -179,7 +182,7 @@ recipes: - WATER_BREATHINGX/1-2/150 6: name: Bitterer Rum/Würziger Rum/&6Goldener Rum - ingredients: + ingredients: - Sugar_Cane/14 cookingtime: 5 distillruns: 2 @@ -194,7 +197,7 @@ recipes: - POISONX/1-0/30-0 7: name: Abgeranzter Vodka/Vodka/Russischer Vodka - ingredients: + ingredients: - Potato/10 cookingtime: 15 distillruns: 3 diff --git a/resources/config/v13/en/config.yml b/resources/config/v13/en/config.yml index 8bddf21..c96eba4 100644 --- a/resources/config/v13/en/config.yml +++ b/resources/config/v13/en/config.yml @@ -54,6 +54,9 @@ colorInBrewer: true # If a Large Barrel can be opened by clicking on any of its blocks, not just Spigot or Sign. This is always true for Small Barrels. [true] openLargeBarrelEverywhere: true +# How many Brewery drinks can be put into the Minecraft barrels [6] +maxBrewsInMCBarrels: 6 + # Enable checking for Updates, Checks the curseforge api for updates to Brewery [true] # If an Update is found a Message is logged on Server-start and displayed to OPs joining the game updateCheck: true @@ -118,7 +121,7 @@ recipes: - POISONX/1-0/20-0 1: name: Skunky Wheatbeer/Wheatbeer/Fine Wheatbeer - ingredients: + ingredients: - Wheat/3 cookingtime: 8 distillruns: 0 @@ -129,7 +132,7 @@ recipes: alcohol: 5 2: name: Skunky Beer/Beer/Fine Beer - ingredients: + ingredients: - Wheat/6 cookingtime: 8 distillruns: 0 @@ -140,7 +143,7 @@ recipes: alcohol: 6 3: name: Skunky Darkbeer/Darkbeer/Fine Darkbeer - ingredients: + ingredients: - Wheat/6 cookingtime: 8 distillruns: 0 @@ -151,7 +154,7 @@ recipes: alcohol: 7 4: name: Awkward Mead/Mead/&6Golden Mead - ingredients: + ingredients: - Sugar_Cane/6 cookingtime: 3 distillruns: 0 @@ -162,7 +165,7 @@ recipes: alcohol: 9 5: name: Apple Mead/Sweet Apple Mead/&6Sweet Golden Apple Mead - ingredients: + ingredients: - Sugar_Cane/6 - Apple/2 cookingtime: 4 @@ -176,7 +179,7 @@ recipes: - WATER_BREATHINGX/1-2/150 6: name: Bitter Rum/Spicy Rum/&6Golden Rum - ingredients: + ingredients: - Sugar_Cane/14 cookingtime: 5 distillruns: 2 @@ -191,7 +194,7 @@ recipes: - POISONX/1-0/30-0 7: name: Lousy Vodka/Vodka/Russian Vodka - ingredients: + ingredients: - Potato/10 cookingtime: 15 distillruns: 3 diff --git a/resources/config/v13/fr/config.yml b/resources/config/v13/fr/config.yml index 6f82efb..21d1ef4 100644 --- a/resources/config/v13/fr/config.yml +++ b/resources/config/v13/fr/config.yml @@ -54,6 +54,9 @@ colorInBrewer: true # Si le grand tonneau peut être ouvert en cliquant sur n'importe quel bloc, non seulement le robinet ou le panneau. Toujours "true" pour les petits tonneaux. [true] openLargeBarrelEverywhere: true +# How many Brewery drinks can be put into the Minecraft barrels [6] +maxBrewsInMCBarrels: 6 + # Enable checking for Updates, Checks the curseforge api for updates to Brewery [true] # If an Update is found a Message is logged on Server-start and displayed to OPs joining the game updateCheck: true @@ -70,7 +73,7 @@ version: '1.7' # name: Différents noms pour la mauvaise/moyen/bonne qualité (Les codes de mise en forme sont pris en charge: comme par exemple &6 pour la couleur Or.) # ingredients: Liste des 'matériaux,data/montant' # (Les id d'objets à la place des matériaux sont obsolètes pour bukkit) -# Ex: 'Sugar_Cane' +# Ex: 'Sugar_Cane' # Une liste des matériaux peuvent-être trouvés ici: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html # Vous pouvez spécifier une data (Ex: 5,3 -> Planche de bois de jungle), si vous ne le faites pas la data ne sera pas prise en compte (Ex : 5 -> Bois en général) # If Vault is installed normal names can be used instead of material or id, so using Vault is highly recommended. @@ -123,7 +126,7 @@ recipes: - POISONX/1-0/20-0 1: name: Bière Blanche Fade/Bière Blanche/Bonne Bière Blanche - ingredients: + ingredients: - Wheat/3 cookingtime: 8 distillruns: 0 @@ -134,7 +137,7 @@ recipes: alcohol: 5 2: name: Bière Fade/Bière/Bonne Bière - ingredients: + ingredients: - Wheat/6 cookingtime: 8 distillruns: 0 @@ -145,7 +148,7 @@ recipes: alcohol: 6 3: name: Bière Brune Fade/Bière Brune/Bonne Bière Brune - ingredients: + ingredients: - Wheat/6 cookingtime: 8 distillruns: 0 @@ -156,7 +159,7 @@ recipes: alcohol: 7 4: name: Hydromel Bizarre/Hydromel/&6Hydromel Doré - ingredients: + ingredients: - Sugar_Cane/6 cookingtime: 3 distillruns: 0 @@ -167,7 +170,7 @@ recipes: alcohol: 9 5: name: Hydromel de Pommes/Doux Hydromel de Pommes/&6Doux Hydromel de Pommes Dorées - ingredients: + ingredients: - Sugar_Cane/6 - Apple/2 cookingtime: 4 @@ -181,7 +184,7 @@ recipes: - WATER_BREATHINGX/1-2/150 6: name: Rhum Amer/Rhum Epicé/&6Rhum Doré - ingredients: + ingredients: - Sugar_Cane/14 cookingtime: 5 distillruns: 2 @@ -196,7 +199,7 @@ recipes: - POISONX/1-0/30-0 7: name: Vodka Sale/Vodka/Vodka Russe - ingredients: + ingredients: - Potato/10 cookingtime: 15 distillruns: 3 diff --git a/resources/config/v13/it/config.yml b/resources/config/v13/it/config.yml index 32d32ec..0bd980c 100644 --- a/resources/config/v13/it/config.yml +++ b/resources/config/v13/it/config.yml @@ -54,6 +54,9 @@ colorInBrewer: true # Se un barile grande può essere aperto cliccandoci sopra, non solo sul cartello e sulla staccionata. Questo è sempre true per i barili piccoli. [true] openLargeBarrelEverywhere: true +# How many Brewery drinks can be put into the Minecraft barrels [6] +maxBrewsInMCBarrels: 6 + # Abilita il controllo degli aggiornamenti, controlla l'API di CurseForge per eventuali aggiornamenti di Brewery [true] # Se quando un aggiornamento viene trovato un messaggio è loggato e mostrato agli OPs quando entrano in gioco. updateCheck: true @@ -118,7 +121,7 @@ recipes: - POISONX/1-0/20-0 1: name: Birra di frumento puzzolente/Birra di frumento/Birra di frumento pregiata - ingredients: + ingredients: - Wheat/3 cookingtime: 8 distillruns: 0 @@ -129,7 +132,7 @@ recipes: alcohol: 5 2: name: Birra puzzolente/Birra/Birra pregiata - ingredients: + ingredients: - Wheat/6 cookingtime: 8 distillruns: 0 @@ -140,7 +143,7 @@ recipes: alcohol: 6 3: name: Birra scura puzzolente/Birra scura/Birra scura pregiata - ingredients: + ingredients: - Wheat/6 cookingtime: 8 distillruns: 0 @@ -151,7 +154,7 @@ recipes: alcohol: 7 4: name: Idromele scarso/Idromele/&6Idromele dorato - ingredients: + ingredients: - Sugar_Cane/6 cookingtime: 3 distillruns: 0 @@ -162,7 +165,7 @@ recipes: alcohol: 9 5: name: Idromele di mele/Idromele di mele dolci/&6Idromele di mele dolci dorato - ingredients: + ingredients: - Sugar_Cane/6 - Apple/2 cookingtime: 4 @@ -176,7 +179,7 @@ recipes: - WATER_BREATHINGX/1-2/150 6: name: Rum amaro/Rum speziato/&6Rum dorato - ingredients: + ingredients: - Sugar_Cane/14 cookingtime: 5 distillruns: 2 @@ -191,7 +194,7 @@ recipes: - POISONX/1-0/30-0 7: name: Vodka schifosa/Vodka/Vodka russa - ingredients: + ingredients: - Potato/10 cookingtime: 15 distillruns: 3 diff --git a/resources/languages/de.yml b/resources/languages/de.yml index bcd8367..ff0548a 100644 --- a/resources/languages/de.yml +++ b/resources/languages/de.yml @@ -75,6 +75,7 @@ Help_Create: '&6/brew Create [Qualität] [Spieler] &9Erstellt einen Tra # Player Player_BarrelCreated: Fass erfolgreich erstellt +Player_BarrelFull: '&cHier passen nicht mehr Getränke hinein' Player_CantDrink: Du kannst nicht mehr trinken Player_CauldronInfo1: Dieser Kessel siedet nun seit &v1 Minuten Player_CauldronInfo2: Dieser Kessel siedet seit weniger als einer Minute diff --git a/resources/languages/en.yml b/resources/languages/en.yml index a4b49bf..d0d24bc 100644 --- a/resources/languages/en.yml +++ b/resources/languages/en.yml @@ -75,6 +75,7 @@ Perms_NoSmallBarrelCreate: '&cYou don''t have permissions to create small barrel # Player Player_BarrelCreated: Barrel created +Player_BarrelFull: '&cThis barrel can''t hold any more drinks' Player_CantDrink: You can't drink any more. Player_CauldronInfo1: This cauldron has been boiling for &v1 minutes. Player_CauldronInfo2: This cauldron has just started boiling. diff --git a/resources/languages/fr.yml b/resources/languages/fr.yml index 0af27d9..1e75aa2 100644 --- a/resources/languages/fr.yml +++ b/resources/languages/fr.yml @@ -75,6 +75,7 @@ Perms_NoSmallBarrelCreate: '&cVous n´avez pas la permission de créer de petits # Joueur Player_BarrelCreated: Baril créé +Player_BarrelFull: '&cThis barrel can''t hold any more drinks' Player_CantDrink: Vous ne pouvez pas boire plus. Player_CauldronInfo1: Ce chaudron a été en ébullition durant &v1 minutes. Player_CauldronInfo2: Ce chaudron est tout juste à ébullition. diff --git a/resources/languages/it.yml b/resources/languages/it.yml index a5de775..df17341 100755 --- a/resources/languages/it.yml +++ b/resources/languages/it.yml @@ -75,6 +75,7 @@ Perms_NoSmallBarrelCreate: '&cNon hai il permesso di costruire piccoli barili!' # Giocatore Player_BarrelCreated: Barile creato +Player_BarrelFull: '&cThis barrel can''t hold any more drinks' Player_CantDrink: Non puoi bere dell''altro. Player_CauldronInfo1: Questo calderone sta bollendo da &v1 minuti. Player_CauldronInfo2: Questo calderone ha appena iniziato a bollire. diff --git a/src/com/dre/brewery/BIngredients.java b/src/com/dre/brewery/BIngredients.java index 6034d34..11ab899 100644 --- a/src/com/dre/brewery/BIngredients.java +++ b/src/com/dre/brewery/BIngredients.java @@ -302,20 +302,14 @@ public class BIngredients { } int quality = 10 - Math.round(recipe.getWoodDiff(wood) * recipe.getDifficulty()); - if (quality > 0) { - return quality; - } - return 0; + return Math.max(quality, 0); } // returns the quality regarding the ageing time conditioning given Recipe public int getAgeQuality(BRecipe recipe, float time) { int quality = 10 - Math.round(Math.abs(time - recipe.getAge()) * ((float) recipe.getDifficulty() / 2)); - if (quality > 0) { - return quality; - } - return 0; + return Math.max(quality, 0); } // Creates a copy ingredients diff --git a/src/com/dre/brewery/Brew.java b/src/com/dre/brewery/Brew.java index c26fa3c..a581838 100644 --- a/src/com/dre/brewery/Brew.java +++ b/src/com/dre/brewery/Brew.java @@ -375,7 +375,7 @@ public class Brew { } String prefix = P.p.color("&7"); if (colorInBrewer && currentRecipe != null) { - prefix = getQualityColor(ingredients.getDistillQuality(recipe, distillRuns)); + prefix = getQualityColor(ingredients.getDistillQuality(currentRecipe, distillRuns)); } updateDistillLore(prefix, potionMeta); touch(); @@ -477,7 +477,7 @@ public class Brew { } // Lore ----------- - + // Converts to/from qualitycolored Lore public void convertLore(PotionMeta meta, Boolean toQuality) { if (currentRecipe == null) { @@ -724,11 +724,12 @@ public class Brew { public PotionType getType() { return type; } - + public Color getColor() { return color; } + @SuppressWarnings("deprecation") public void colorBrew(PotionMeta meta, ItemStack potion, boolean destillable) { if (P.use1_9) { meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS); diff --git a/src/com/dre/brewery/MCBarrel.java b/src/com/dre/brewery/MCBarrel.java index 4b49ac5..28a86b3 100644 --- a/src/com/dre/brewery/MCBarrel.java +++ b/src/com/dre/brewery/MCBarrel.java @@ -1,39 +1,53 @@ package com.dre.brewery; import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.block.BlockState; -import org.bukkit.entity.Player; +import org.bukkit.NamespacedKey; +import org.bukkit.block.Barrel; +import org.bukkit.event.inventory.InventoryAction; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; import java.util.ArrayList; import java.util.List; -import java.util.ListIterator; public class MCBarrel { public static final byte OAK = 2; - public static List barrels = new ArrayList<>(); + public static final String TAG = "Btime"; + public static int maxBrews = 6; + + public static long mcBarrelTime; // Globally stored Barrel time. Difference between this and the time stored on each mc-barrel will give the barrel age time + public static List openBarrels = new ArrayList<>(); - private Block block; - private float time; private byte brews = -1; // How many Brewery Brews are in this Barrel + private final Inventory inv; - public MCBarrel(Block block, float time) { - this.block = block; - this.time = time; + public MCBarrel(Inventory inv) { + this.inv = inv; } // Now Opening this Barrel for a player - public void open(Inventory inv, Player player) { - brews = -1; - if (time > 0) { - // if nobody has the inventory opened - if (inv.getViewers().isEmpty()) { + public void open() { + // if nobody had the inventory opened + if (inv.getViewers().size() == 1 && inv.getHolder() instanceof org.bukkit.block.Barrel) { + Barrel barrel = (Barrel) inv.getHolder(); + PersistentDataContainer data = barrel.getPersistentDataContainer(); + NamespacedKey key = new NamespacedKey(P.p, TAG); + if (!data.has(key, PersistentDataType.LONG)) return; + + // Get the difference between the time that is stored on the Barrel and the current stored global mcBarrelTime + long time = mcBarrelTime - data.getOrDefault(key, PersistentDataType.LONG, mcBarrelTime); + data.remove(key); + barrel.update(); + P.p.debugLog("Barrel Time since last open: " + time); + + if (time > 0) { brews = 0; // if inventory contains potions if (inv.contains(Material.POTION)) { @@ -42,8 +56,9 @@ public class MCBarrel { if (item != null) { Brew brew = Brew.get(item); if (brew != null) { - if (brews <= 6) { - brew.age(item, time, OAK); + if (brews < maxBrews) { + // The time is in minutes, but brew.age() expects time in mc-days + brew.age(item, ((float) time) / 20f, OAK); } brews++; } @@ -55,70 +70,132 @@ public class MCBarrel { } } } - // reset barreltime, potions have new age - time = 0; } - // Closing Inventory. Check if we need to track this Barrel - // Returns true if there are Brews in the Inv - public boolean close(Inventory inv, Player player) { + // Closing Inventory. Check if we need to set a time on the Barrel + public void close() { if (inv.getViewers().size() == 1) { // This is the last viewer for (ItemStack item : inv.getContents()) { if (item != null) { Brew brew = Brew.get(item); if (brew != null) { - // We found a brew, so we keep this Barrel - return true; + // We found a brew, so set time on this Barrel + if (inv.getHolder() instanceof org.bukkit.block.Barrel) { + Barrel barrel = (Barrel) inv.getHolder(); + PersistentDataContainer data = barrel.getPersistentDataContainer(); + data.set(new NamespacedKey(P.p, TAG), PersistentDataType.LONG, mcBarrelTime); + barrel.update(); + } + return; } } } - // No Brew found, remove this Barrel - return false; + // No Brew found, ignore this Barrel } - return true; } - public Block getBlock() { - return block; - } + // Used to visually stop Players from placing more than 6 (configurable) brews in the MC Barrels. + // There are still methods to place more Brews in that would be too tedious to catch. + // This is only for direct visual Notification, the age routine above will never age more than 6 brews in any case. + public void clickInv(InventoryClickEvent event) { + boolean adding = false; + switch (event.getAction()) { + case PLACE_ALL: + case PLACE_ONE: + case PLACE_SOME: + case SWAP_WITH_CURSOR: + // Placing Brew in MC Barrel + if (event.getCursor() != null && event.getClickedInventory() != null && event.getClickedInventory().getType() == InventoryType.BARREL && event.getCursor().getType() == Material.POTION) { + Brew b = Brew.get(event.getCursor()); + if (b != null) { + if (event.getAction() == InventoryAction.SWAP_WITH_CURSOR && event.getCurrentItem() != null && event.getCurrentItem().getType() == Material.POTION) { + Brew bb = Brew.get(event.getCurrentItem()); + if (bb != null) { + // The item we are swapping with is also a brew, dont change the count and allow + break; + } + } + adding = true; + } + } + break; + case MOVE_TO_OTHER_INVENTORY: + if (event.getCurrentItem() != null && event.getCurrentItem().getType() == Material.POTION && event.getClickedInventory() != null) { + if (event.getClickedInventory().getType() == InventoryType.BARREL) { + // Moving Brew out of MC Barrel + Brew b = Brew.get(event.getCurrentItem()); + if (b != null) { + if (brews == -1) { + countBrews(); + } + brews--; + } + break; + } else if (event.getClickedInventory().getType() == InventoryType.PLAYER) { + // Moving Brew into MC Barrel + Brew b = Brew.get(event.getCurrentItem()); + if (b != null) { + adding = true; + } + } + } + break; - public float getTime() { - return time; - } - - public Inventory getInventory() { - BlockState state = block.getState(); - if (state instanceof InventoryHolder) { - return ((InventoryHolder) state).getInventory(); + case PICKUP_ALL: + case PICKUP_ONE: + case PICKUP_HALF: + case PICKUP_SOME: + case COLLECT_TO_CURSOR: + // Pickup Brew from MC Barrel + if (event.getCurrentItem() != null && event.getClickedInventory() != null && event.getClickedInventory().getType() == InventoryType.BARREL && event.getCurrentItem().getType() == Material.POTION) { + Brew b = Brew.get(event.getCurrentItem()); + if (b != null) { + if (brews == -1) { + countBrews(); + } + brews--; + } + } + break; + case HOTBAR_MOVE_AND_READD: + case HOTBAR_SWAP: + brews = -1; + break; + default: + return; + } + if (adding) { + if (brews == -1) { + countBrews(); + } + if (brews >= maxBrews) { + event.setCancelled(true); + P.p.msg(event.getWhoClicked(), P.p.languageReader.get("Player_BarrelFull")); + } else { + brews++; + } } - return null; } - public static void onUpdate() { - if (barrels.isEmpty()) return; - - // Check if stored MCBarrels still exist - // Choose a random starting point for check - int random = (int) Math.floor(Math.random() * barrels.size()); - random = Math.max(0, random - 5); - ListIterator iter = barrels.listIterator(random); - // Check at least 4 barrels, but if there are many, check about 1/64 of them all, so in about 1 hour we have checked all - for (int i = Math.max(4, barrels.size() >> 6); i <= 0; i--) { - if (!iter.hasNext()) break; - - Block block = iter.next().block; - if (Util.isChunkLoaded(block)) { - // If the chunk is loaded we can check if the block is still a MC Barrel. If not we remove the stored entry. - if (block.getType() != Material.BARREL) { - iter.remove(); + public void countBrews() { + brews = 0; + for (ItemStack item : inv.getContents()) { + if (item != null) { + Brew brew = Brew.get(item); + if (brew != null) { + brews++; } } } - - for (MCBarrel barrel : barrels) { - // Minecraft day is 20 min, so add 1/20 to the time every minute - barrel.time += (1.0 / 20.0); - } } + + public Inventory getInventory() { + return inv; + } + + public static void onUpdate() { + mcBarrelTime++; + } + } diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index 100b01e..6b75112 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -38,7 +38,7 @@ import org.bukkit.plugin.java.JavaPlugin; public class P extends JavaPlugin { public static P p; - public static final String configVersion = "1.7"; + public static final String configVersion = "1.8"; public static boolean debug; public static boolean useUUID; public static boolean use1_9; @@ -270,7 +270,7 @@ public class P extends JavaPlugin { successful = false; } } - if (!successful) { + if (!successful && sender != null) { msg(sender, p.languageReader.get("Error_Recipeload")); } reloader = null; @@ -369,6 +369,7 @@ public class P extends JavaPlugin { Brew.colorInBarrels = config.getBoolean("colorInBarrels", false); Brew.colorInBrewer = config.getBoolean("colorInBrewer", false); PlayerListener.openEverywhere = config.getBoolean("openLargeBarrelEverywhere", false); + MCBarrel.maxBrews = config.getInt("maxBrewsInMCBarrels", 6); // loading recipes ConfigurationSection configSection = config.getConfigurationSection("recipes"); @@ -458,6 +459,7 @@ public class P extends JavaPlugin { FileConfiguration data = YamlConfiguration.loadConfiguration(file); Brew.installTime = data.getLong("installTime", System.currentTimeMillis()); + MCBarrel.mcBarrelTime = data.getLong("MCBarrelTime", 0); // Check if data is the newest version String version = data.getString("Version", null); @@ -903,7 +905,7 @@ public class P extends JavaPlugin { cauldron.onUpdate();// runs every min to update cooking time } Barrel.onUpdate();// runs every min to check and update ageing time - MCBarrel.onUpdate(); + if (use1_14) MCBarrel.onUpdate(); BPlayer.onUpdate();// updates players drunkeness debugLog("Update"); diff --git a/src/com/dre/brewery/filedata/ConfigUpdater.java b/src/com/dre/brewery/filedata/ConfigUpdater.java index f925149..a1c603e 100644 --- a/src/com/dre/brewery/filedata/ConfigUpdater.java +++ b/src/com/dre/brewery/filedata/ConfigUpdater.java @@ -152,11 +152,20 @@ public class ConfigUpdater { oldMat = false; } + if (fromVersion.equals("1.7")) { + if (de) { + update17de(); + } else { + update17en(); + } + fromVersion = "1.8"; + } + if (P.use1_13 && oldMat) { updateMaterials(true); } - if (!fromVersion.equals("1.7")) { + if (!fromVersion.equals("1.8")) { P.p.log(P.p.languageReader.get("Error_ConfigUpdate", fromVersion)); return; } @@ -1108,6 +1117,60 @@ public class ConfigUpdater { } } + // Update de from 1.7 to 1.8 + private void update17de() { + updateVersion("1.8"); + + int index = indexOfStart("openLargeBarrelEverywhere"); + if (index == -1) { + index = indexOfStart("colorInBrewer"); + if (index == -1) { + index = indexOfStart("colorInBarrels"); + if (index == -1) { + index = indexOfStart("hangoverDays"); + if (index == -1) { + index = indexOfStart("language"); + } + } + } + } + String[] lines = {"", + "# Wie viele Brewery Getränke in die Minecraft Fässer getan werden können [6]", + "maxBrewsInMCBarrels: 6"}; + if (index == 0) { + appendLines(lines); + } else { + addLines(index + 1, lines); + } + } + + // Update en from 1.7 to 1.8 + private void update17en() { + updateVersion("1.8"); + + int index = indexOfStart("openLargeBarrelEverywhere"); + if (index == -1) { + index = indexOfStart("colorInBrewer"); + if (index == -1) { + index = indexOfStart("colorInBarrels"); + if (index == -1) { + index = indexOfStart("hangoverDays"); + if (index == -1) { + index = indexOfStart("language"); + } + } + } + } + String[] lines = {"", + "# How many Brewery drinks can be put into the Minecraft barrels [6]", + "maxBrewsInMCBarrels: 6"}; + if (index == 0) { + appendLines(lines); + } else { + addLines(index + 1, lines); + } + } + // Update all Materials to Minecraft 1.13 private void updateMaterials(boolean toMC113) { int index; diff --git a/src/com/dre/brewery/filedata/DataSave.java b/src/com/dre/brewery/filedata/DataSave.java index f9d212c..5280f25 100644 --- a/src/com/dre/brewery/filedata/DataSave.java +++ b/src/com/dre/brewery/filedata/DataSave.java @@ -3,6 +3,7 @@ package com.dre.brewery.filedata; import java.io.File; +import com.dre.brewery.MCBarrel; import org.bukkit.World; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; @@ -60,6 +61,7 @@ public class DataSave extends BukkitRunnable { FileConfiguration configFile = new YamlConfiguration(); configFile.set("installTime", Brew.installTime); + configFile.set("MCBarrelTime", MCBarrel.mcBarrelTime); if (!Brew.potions.isEmpty()) { Brew.save(configFile.createSection("Brew")); diff --git a/src/com/dre/brewery/listeners/InventoryListener.java b/src/com/dre/brewery/listeners/InventoryListener.java index d599784..c952fd0 100644 --- a/src/com/dre/brewery/listeners/InventoryListener.java +++ b/src/com/dre/brewery/listeners/InventoryListener.java @@ -1,20 +1,30 @@ package com.dre.brewery.listeners; -import com.dre.brewery.*; +import com.dre.brewery.BPlayer; +import com.dre.brewery.BRecipe; +import com.dre.brewery.Barrel; +import com.dre.brewery.Brew; +import com.dre.brewery.MCBarrel; +import com.dre.brewery.P; import com.dre.brewery.integration.LogBlockBarrel; import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.BrewingStand; import org.bukkit.entity.HumanEntity; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.*; -import org.bukkit.inventory.BlockInventoryHolder; +import org.bukkit.event.inventory.BrewEvent; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryAction; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.inventory.InventoryOpenEvent; +import org.bukkit.event.inventory.InventoryPickupItemEvent; +import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.BrewerInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemFlag; @@ -24,6 +34,7 @@ import org.bukkit.scheduler.BukkitRunnable; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.UUID; /** @@ -297,7 +308,7 @@ public class InventoryListener implements Listener { if (event.getSlot() > 2) { return; } - } else if (!(event.getInventory().getHolder() instanceof Barrel)) { + } else if (!(event.getInventory().getHolder() instanceof Barrel) && !(event.getInventory().getHolder() instanceof org.bukkit.block.Barrel)) { return; } @@ -318,22 +329,40 @@ public class InventoryListener implements Listener { } } - @EventHandler(ignoreCancelled = true) + // Check if the player tries to add more than the allowed amount of brews into an mc-barrel + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onInventoryClickMCBarrel(InventoryClickEvent event) { + if (!P.use1_14) return; + if (event.getInventory().getType() != InventoryType.BARREL) return; + + Inventory inv = event.getInventory(); + for (MCBarrel barrel : MCBarrel.openBarrels) { + if (barrel.getInventory().equals(inv)) { + barrel.clickInv(event); + return; + } + } + MCBarrel barrel = new MCBarrel(inv); + MCBarrel.openBarrels.add(barrel); + barrel.clickInv(event); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onInventoryOpen(InventoryOpenEvent event) { if (!P.use1_14) return; - if (!(event.getPlayer() instanceof Player)) return; + // Check for MC Barrel if (event.getInventory().getType() == InventoryType.BARREL) { Inventory inv = event.getInventory(); - if (inv.getHolder() instanceof BlockInventoryHolder) { - Location loc = ((BlockInventoryHolder) inv.getHolder()).getBlock().getLocation(); - for (MCBarrel barrel : MCBarrel.barrels) { - if (barrel.getBlock().getLocation().equals(loc)) { - barrel.open(inv, ((Player) event.getPlayer())); - return; - } + for (MCBarrel barrel : MCBarrel.openBarrels) { + if (barrel.getInventory().equals(inv)) { + barrel.open(); + return; } } + MCBarrel barrel = new MCBarrel(inv); + MCBarrel.openBarrels.add(barrel); + barrel.open(); } } @@ -358,5 +387,24 @@ public class InventoryListener implements Listener { } } } + + if (!P.use1_14) return; + + // Check for MC Barrel + if (event.getInventory().getType() == InventoryType.BARREL) { + Inventory inv = event.getInventory(); + for (Iterator iter = MCBarrel.openBarrels.iterator(); iter.hasNext(); ) { + MCBarrel barrel = iter.next(); + if (barrel.getInventory().equals(inv)) { + barrel.close(); + if (inv.getViewers().size() == 1) { + // Last viewer, remove Barrel from List of open Barrels + iter.remove(); + } + return; + } + } + new MCBarrel(inv).close(); + } } }