Changed Lore Handling

This commit is contained in:
Sn0wStorm 2016-07-01 22:01:35 +02:00
parent eeb0075f2e
commit a1897d085c
7 changed files with 334 additions and 218 deletions

View File

@ -1,5 +1,6 @@
package com.dre.brewery;
import com.dre.brewery.lore.BrewLore;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
@ -89,7 +90,8 @@ public class BIngredients {
int quality = (int) Math.round((getIngredientQuality(cookRecipe) + getCookingQuality(cookRecipe, false)) / 2.0);
P.p.debugLog("cooked potion has Quality: " + quality);
brew = new Brew(quality, cookRecipe, this);
Brew.addOrReplaceEffects(potionMeta, brew.getEffects(), brew.getQuality());
BrewLore lore = new BrewLore(brew, potionMeta);
lore.addOrReplaceEffects(brew.getEffects(), brew.getQuality());
cookedName = cookRecipe.getName(quality);
Brew.PotionColor.valueOf(cookRecipe.getColor()).colorBrew(potionMeta, potion, false);

View File

@ -1,5 +1,6 @@
package com.dre.brewery;
import com.dre.brewery.lore.BrewLore;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
@ -260,8 +261,10 @@ public class BRecipe {
// This effect stores the UID in its Duration
//potionMeta.addCustomEffect((PotionEffectType.REGENERATION).createEffect((uid * 4), 0), true);
brew.convertLore(potionMeta, false);
Brew.addOrReplaceEffects(potionMeta, effects, quality);
BrewLore lore = new BrewLore(brew, potionMeta);
lore.convertLore(false);
lore.addOrReplaceEffects(effects, quality);
lore.write();
brew.touch();
brew.save(potionMeta);

View File

@ -0,0 +1,28 @@
package com.dre.brewery;
import java.util.List;
public class BUtil {
// Returns the Index of a String from the list that contains this substring
public static int indexOfSubstring(List<String> list, String substring) {
if (list.isEmpty()) return -1;
for (int index = 0, size = list.size(); index < size; index++) {
String string = list.get(index);
if (string.contains(substring)) {
return index;
}
}
return -1;
}
// Returns the index of a String from the list that starts with 'lineStart', returns -1 if not found;
public static int indexOfStart(List<String> list, String lineStart) {
for (int i = 0, size = list.size(); i < size; i++) {
if (list.get(i).startsWith(lineStart)) {
return i;
}
}
return -1;
}
}

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.Map;
import com.dre.brewery.lore.BrewLore;
import org.bukkit.Material;
import org.bukkit.TreeSpecies;
import org.bukkit.entity.HumanEntity;
@ -473,8 +474,10 @@ public class Barrel implements InventoryHolder {
// Brew before throwing
brew.age(item, time, getWood());
PotionMeta meta = (PotionMeta) item.getItemMeta();
if (Brew.hasColorLore(meta)) {
brew.convertLore(meta, false);
if (BrewLore.hasColorLore(meta)) {
BrewLore lore = new BrewLore(brew, meta);
lore.convertLore(false);
lore.write();
item.setItemMeta(meta);
}
}

View File

@ -20,7 +20,6 @@ import java.security.InvalidKeyException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Brew {
@ -228,7 +227,7 @@ public class Brew {
persistent == brew.persistent &&
immutable == brew.immutable &&
ingredients.equals(brew.ingredients) &&
currentRecipe != null ? currentRecipe.equals(brew.currentRecipe) : brew.currentRecipe == null;
(currentRecipe != null ? currentRecipe.equals(brew.currentRecipe) : brew.currentRecipe == null);
}
// Clones this instance
@ -345,17 +344,19 @@ public class Brew {
// Set unlabeled to true to hide the numbers in Lore
public void unLabel(ItemStack item) {
PotionMeta meta = (PotionMeta) item.getItemMeta();
if (meta.hasLore()) {
unlabeled = true;
ItemMeta meta = item.getItemMeta();
if (meta instanceof PotionMeta && meta.hasLore()) {
BrewLore lore = new BrewLore(this, ((PotionMeta) meta));
if (distillRuns > 0) {
addOrReplaceLore(meta, P.p.color("&7"), P.p.languageReader.get("Brew_Distilled"));
lore.updateDistillLore(false);
}
if (ageTime >= 1) {
addOrReplaceLore(meta, P.p.color("&7"), P.p.languageReader.get("Brew_BarrelRiped"));
lore.updateAgeLore(false);
}
lore.write();
item.setItemMeta(meta);
}
unlabeled = true;
}
// Do some regular updates
@ -372,6 +373,18 @@ public class Brew {
return ageTime;
}
public float getWood() {
return wood;
}
public BIngredients getIngredients() {
return ingredients;
}
public boolean hasRecipe() {
return currentRecipe != null;
}
public BRecipe getCurrentRecipe() {
return currentRecipe;
}
@ -401,6 +414,14 @@ public class Brew {
return immutable;
}
public boolean isImmutable() {
return immutable;
}
public boolean isUnlabeled() {
return unlabeled;
}
// Set the Static flag, so potion is unchangeable
public void setStatic(boolean immutable, ItemStack potion) {
this.immutable = immutable;
@ -435,34 +456,32 @@ public class Brew {
if (immutable) return;
distillRuns += 1;
BrewLore lore = new BrewLore(this, potionMeta);
BRecipe recipe = ingredients.getdistillRecipe(wood, ageTime);
if (recipe != null) {
// distillRuns will have an effect on the amount of alcohol, not the quality
currentRecipe = recipe;
quality = calcQuality();
addOrReplaceEffects(potionMeta, getEffects(), quality);
lore.addOrReplaceEffects(getEffects(), quality);
potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality)));
PotionColor.valueOf(recipe.getColor()).colorBrew(potionMeta, slotItem, canDistill());
} else {
quality = 0;
removeEffects(potionMeta);
lore.removeEffects();
potionMeta.setDisplayName(P.p.color("&f" + P.p.languageReader.get("Brew_DistillUndefined")));
PotionColor.GREY.colorBrew(potionMeta, slotItem, canDistill());
}
// Distill Lore
if (currentRecipe != null) {
if (colorInBrewer != hasColorLore(potionMeta)) {
convertLore(potionMeta, colorInBrewer);
if (colorInBrewer != BrewLore.hasColorLore(potionMeta)) {
lore.convertLore(colorInBrewer);
}
}
String prefix = P.p.color("&7");
if (colorInBrewer && currentRecipe != null) {
prefix = getQualityColor(ingredients.getDistillQuality(recipe, distillRuns));
}
updateDistillLore(prefix, potionMeta);
lore.updateDistillLore(colorInBrewer);
lore.write();
touch();
save(potionMeta);
@ -491,6 +510,7 @@ public class Brew {
if (immutable) return;
PotionMeta potionMeta = (PotionMeta) item.getItemMeta();
BrewLore lore = new BrewLore(this, potionMeta);
ageTime += time;
// if younger than half a day, it shouldnt get aged form
@ -505,12 +525,12 @@ public class Brew {
currentRecipe = recipe;
quality = calcQuality();
addOrReplaceEffects(potionMeta, getEffects(), quality);
lore.addOrReplaceEffects(getEffects(), quality);
potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality)));
PotionColor.valueOf(recipe.getColor()).colorBrew(potionMeta, item, canDistill());
} else {
quality = 0;
removeEffects(potionMeta);
lore.removeEffects();
potionMeta.setDisplayName(P.p.color("&f" + P.p.languageReader.get("Brew_BadPotion")));
PotionColor.GREY.colorBrew(potionMeta, item, canDistill());
}
@ -518,22 +538,19 @@ public class Brew {
// Lore
if (currentRecipe != null) {
if (colorInBarrels != hasColorLore(potionMeta)) {
convertLore(potionMeta, colorInBarrels);
if (colorInBarrels != BrewLore.hasColorLore(potionMeta)) {
lore.convertLore(colorInBarrels);
}
}
if (ageTime >= 1) {
String prefix = P.p.color("&7");
if (colorInBarrels && currentRecipe != null) {
prefix = getQualityColor(ingredients.getAgeQuality(currentRecipe, ageTime));
}
updateAgeLore(prefix, potionMeta);
lore.updateAgeLore(colorInBarrels);
}
if (ageTime > 0.5) {
if (colorInBarrels && !unlabeled && currentRecipe != null) {
updateWoodLore(potionMeta);
lore.updateWoodLore(true);
}
}
lore.write();
touch();
save(potionMeta);
item.setItemMeta(potionMeta);
@ -541,12 +558,12 @@ public class Brew {
// Slowly shift the wood of the Brew to the new Type
public void woodShift(float time, byte to) {
byte factor = 1;
float factor = 1;
if (ageTime > 5) {
factor = 2;
} else if (ageTime > 10) {
factor = 2;
factor += Math.round(ageTime / 10);
factor += (float) ageTime / 10F;
}
if (wood > to) {
wood -= time / factor;
@ -561,179 +578,6 @@ public class Brew {
}
}
// Lore -----------
// Converts to/from qualitycolored Lore
public void convertLore(PotionMeta meta, Boolean toQuality) {
if (currentRecipe == null) {
return;
}
meta.setLore(null);
int quality;
String prefix = P.p.color("&7");
String lore;
// Ingredients
if (toQuality && !unlabeled) {
quality = ingredients.getIngredientQuality(currentRecipe);
prefix = getQualityColor(quality);
lore = P.p.languageReader.get("Brew_Ingredients");
addOrReplaceLore(meta, prefix, lore);
}
// Cooking
if (toQuality && !unlabeled) {
if (distillRuns > 0 == currentRecipe.needsDistilling()) {
quality = ingredients.getCookingQuality(currentRecipe, distillRuns > 0);
prefix = getQualityColor(quality) + ingredients.getCookedTime() + " " + P.p.languageReader.get("Brew_minute");
if (ingredients.getCookedTime() > 1) {
prefix = prefix + P.p.languageReader.get("Brew_MinutePluralPostfix");
}
lore = " " + P.p.languageReader.get("Brew_fermented");
addOrReplaceLore(meta, prefix, lore);
}
}
// Distilling
if (distillRuns > 0) {
if (toQuality) {
quality = ingredients.getDistillQuality(currentRecipe, distillRuns);
prefix = getQualityColor(quality);
}
updateDistillLore(prefix, meta);
}
// Ageing
if (ageTime >= 1) {
if (toQuality) {
quality = ingredients.getAgeQuality(currentRecipe, ageTime);
prefix = getQualityColor(quality);
}
updateAgeLore(prefix, meta);
}
// WoodType
if (toQuality && !unlabeled) {
if (ageTime > 0.5) {
updateWoodLore(meta);
}
}
}
// sets the DistillLore. Prefix is the color to be used
public void updateDistillLore(String prefix, PotionMeta meta) {
if (!unlabeled) {
if (distillRuns > 1) {
prefix = prefix + distillRuns + P.p.languageReader.get("Brew_-times") + " ";
}
}
addOrReplaceLore(meta, prefix, P.p.languageReader.get("Brew_Distilled"));
}
// sets the AgeLore. Prefix is the color to be used
public void updateAgeLore(String prefix, PotionMeta meta) {
if (!unlabeled) {
if (ageTime >= 1 && ageTime < 2) {
prefix = prefix + P.p.languageReader.get("Brew_OneYear") + " ";
} else if (ageTime < 201) {
prefix = prefix + (int) Math.floor(ageTime) + " " + P.p.languageReader.get("Brew_Years") + " ";
} else {
prefix = prefix + P.p.languageReader.get("Brew_HundredsOfYears") + " ";
}
}
addOrReplaceLore(meta, prefix, P.p.languageReader.get("Brew_BarrelRiped"));
}
// updates/sets the color on WoodLore
public void updateWoodLore(PotionMeta meta) {
if (currentRecipe.getWood() > 0) {
int quality = ingredients.getWoodQuality(currentRecipe, wood);
addOrReplaceLore(meta, getQualityColor(quality), P.p.languageReader.get("Brew_Woodtype"));
} else if (meta.hasLore()) {
List<String> existingLore = meta.getLore();
int index = indexOfSubstring(existingLore, P.p.languageReader.get("Brew_Woodtype"));
if (index > -1) {
existingLore.remove(index);
meta.setLore(existingLore);
}
}
}
// Adds or replaces a line of Lore. Searches for Substring lore and replaces it
public static void addOrReplaceLore(PotionMeta meta, String prefix, String lore) {
if (meta.hasLore()) {
List<String> existingLore = meta.getLore();
int index = indexOfSubstring(existingLore, lore);
if (index > -1) {
existingLore.set(index, prefix + lore);
} else {
existingLore.add(prefix + lore);
}
meta.setLore(existingLore);
return;
}
List<String> newLore = new ArrayList<>();
newLore.add("");
newLore.add(prefix + lore);
meta.setLore(newLore);
}
// Adds the Effect names to the Items description
public static void addOrReplaceEffects(PotionMeta meta, ArrayList<BEffect> effects, int quality) {
if (!P.use1_9 && effects != null) {
for (BEffect effect : effects) {
if (!effect.isHidden()) {
effect.writeInto(meta, quality);
}
}
}
}
// Removes all effects
public static void removeEffects(PotionMeta meta) {
if (meta.hasCustomEffects()) {
for (PotionEffect effect : meta.getCustomEffects()) {
PotionEffectType type = effect.getType();
//if (!type.equals(PotionEffectType.REGENERATION)) {
meta.removeCustomEffect(type);
//}
}
}
}
// Returns the Index of a String from the list that contains this substring
public static int indexOfSubstring(List<String> list, String substring) {
for (int index = 0; index < list.size(); index++) {
String string = list.get(index);
if (string.contains(substring)) {
return index;
}
}
return -1;
}
// True if the PotionMeta has colored Lore
public static Boolean hasColorLore(PotionMeta meta) {
return meta.hasLore() && !meta.getLore().get(1).startsWith(P.p.color("&7"));
}
// gets the Color that represents a quality in Lore
public static String getQualityColor(int quality) {
String color;
if (quality > 8) {
color = "&a";
} else if (quality > 6) {
color = "&e";
} else if (quality > 4) {
color = "&6";
} else if (quality > 2) {
color = "&c";
} else {
color = "&4";
}
return P.p.color(color);
}
private static Brew load(ItemMeta meta) {
LoreLoadStream loreStream;
try {
@ -890,6 +734,20 @@ public class Brew {
}
}
public void convertLegacy(ItemStack item) {
removeLegacy(item);
PotionMeta potionMeta = ((PotionMeta) item.getItemMeta());
if (hasRecipe()) {
BrewLore lore = new BrewLore(this, potionMeta);
lore.removeEffects();
PotionColor.valueOf(currentRecipe.getColor()).colorBrew(potionMeta, item, canDistill());
} else {
PotionColor.GREY.colorBrew(potionMeta, item, canDistill());
}
save(potionMeta);
item.setItemMeta(potionMeta);
}
// Saves all data
// Legacy method to save to data file
@Deprecated

View File

@ -2,6 +2,7 @@ package com.dre.brewery.listeners;
import com.dre.brewery.*;
import com.dre.brewery.integration.LogBlockBarrel;
import com.dre.brewery.lore.BrewLore;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -260,17 +261,15 @@ public class InventoryListener implements Listener {
ItemStack item = event.getCurrentItem();
if (item.hasItemMeta()) {
PotionMeta potion = ((PotionMeta) item.getItemMeta());
Brew brew = Brew.get(potion);
if (brew != null) {
// convert potions from 1.8 to 1.9 for color and to remove effect descriptions
if (P.use1_9 && !potion.hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)) {
BRecipe recipe = brew.getCurrentRecipe();
if (recipe != null) {
Brew.removeEffects(potion);
Brew.PotionColor.valueOf(recipe.getColor()).colorBrew(potion, item, brew.canDistill());
item.setItemMeta(potion);
}
// convert potions from 1.8 to 1.9 for color and to remove effect descriptions
if (P.use1_9 && !potion.hasItemFlag(ItemFlag.HIDE_POTION_EFFECTS)) {
Brew brew = Brew.get(potion);
if (brew != null) {
brew.convertLegacy(item);
}
}
Brew brew = Brew.get(item);
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));
@ -361,8 +360,10 @@ public class InventoryListener implements Listener {
PotionMeta meta = (PotionMeta) item.getItemMeta();
Brew brew = Brew.get(meta);
if (brew != null) {
if (Brew.hasColorLore(meta)) {
brew.convertLore(meta, false);
if (BrewLore.hasColorLore(meta)) {
BrewLore lore = new BrewLore(brew, meta);
lore.convertLore(false);
lore.write();
item.setItemMeta(meta);
}
}

View File

@ -0,0 +1,221 @@
package com.dre.brewery.lore;
import com.dre.brewery.*;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList;
import java.util.List;
public class BrewLore {
private static final String INGR = "§v";
private static final String COOK = "§w";
private static final String DISTILL = "§x";
private static final String AGE = "§y";
private static final String WOOD = "§z";
private Brew brew;
private PotionMeta meta;
private List<String> lore;
public BrewLore(Brew brew, PotionMeta meta) {
this.brew = brew;
this.meta = meta;
if (meta.hasLore()) {
lore = meta.getLore();
} else {
lore = new ArrayList<>();
}
}
// Write the new lore into the Meta
public PotionMeta write() {
meta.setLore(lore);
return meta;
}
public void updateIngredientLore(boolean qualityColor) {
if (qualityColor && brew.hasRecipe()) {
String prefix = getQualityColor(brew.getIngredients().getIngredientQuality(brew.getCurrentRecipe()));
addOrReplaceLore(INGR, prefix, P.p.languageReader.get("Brew_Ingredients"));
} else {
removeLore(INGR, P.p.languageReader.get("Brew_Ingredients"));
}
}
public void updateCookLore(boolean qualityColor) {
if (qualityColor && brew.hasRecipe() && brew.getDistillRuns() > 0 == brew.getCurrentRecipe().needsDistilling()) {
BIngredients ingredients = brew.getIngredients();
int quality = ingredients.getCookingQuality(brew.getCurrentRecipe(), brew.getDistillRuns() > 0);
String prefix = getQualityColor(quality) + ingredients.getCookedTime() + " " + P.p.languageReader.get("Brew_minute");
if (ingredients.getCookedTime() > 1) {
prefix = prefix + P.p.languageReader.get("Brew_MinutePluralPostfix");
}
addOrReplaceLore(COOK, prefix, " " + P.p.languageReader.get("Brew_fermented"));
} else {
removeLore(COOK, P.p.languageReader.get("Brew_fermented"));
}
}
// sets the DistillLore. Prefix is the color to be used
public void updateDistillLore(boolean qualityColor) {
if (brew.getDistillRuns() <= 0) return;
String prefix;
byte distillRuns = brew.getDistillRuns();
if (qualityColor && brew.hasRecipe()) {
prefix = getQualityColor(brew.getIngredients().getDistillQuality(brew.getCurrentRecipe(), distillRuns));
} else {
prefix = "§7";
}
if (!brew.isUnlabeled()) {
if (distillRuns > 1) {
prefix = prefix + distillRuns + P.p.languageReader.get("Brew_-times") + " ";
}
}
addOrReplaceLore(DISTILL, prefix, P.p.languageReader.get("Brew_Distilled"));
}
// sets the AgeLore. Prefix is the color to be used
public void updateAgeLore(boolean qualityColor) {
String prefix;
float age = brew.getAgeTime();
if (qualityColor && brew.hasRecipe()) {
prefix = getQualityColor(brew.getIngredients().getAgeQuality(brew.getCurrentRecipe(), age));
} else {
prefix = "§7";
}
if (!brew.isUnlabeled()) {
if (age >= 1 && age < 2) {
prefix = prefix + P.p.languageReader.get("Brew_OneYear") + " ";
} else if (age < 201) {
prefix = prefix + (int) Math.floor(age) + " " + P.p.languageReader.get("Brew_Years") + " ";
} else {
prefix = prefix + P.p.languageReader.get("Brew_HundredsOfYears") + " ";
}
}
addOrReplaceLore(AGE, prefix, P.p.languageReader.get("Brew_BarrelRiped"));
}
// updates/sets the color on WoodLore
public void updateWoodLore(boolean qualityColor) {
if (qualityColor && brew.hasRecipe()) {
int quality = brew.getIngredients().getWoodQuality(brew.getCurrentRecipe(), brew.getWood());
addOrReplaceLore(WOOD, getQualityColor(quality), P.p.languageReader.get("Brew_Woodtype"));
} else {
removeLore(WOOD, P.p.languageReader.get("Brew_Woodtype"));
}
}
// Converts to/from qualitycolored Lore
public void convertLore(boolean toQuality) {
if (!brew.hasRecipe()) {
return;
}
if (!brew.isUnlabeled()) {
// Ingredients
updateIngredientLore(toQuality);
// Cooking
updateCookLore(toQuality);
}
// Distilling
updateDistillLore(toQuality);
// Ageing
if (brew.getAgeTime() >= 1) {
updateAgeLore(toQuality);
}
// WoodType
if (!brew.isUnlabeled()) {
if (brew.getAgeTime() > 0.5) {
updateWoodLore(toQuality);
}
}
}
// Adds or replaces a line of Lore.
// Searches for type and if not found for Substring lore and replaces it
public void addOrReplaceLore(String type, String prefix, String line) {
int index = BUtil.indexOfStart(lore, type);
if (index == -1) {
index = BUtil.indexOfSubstring(lore, line);
}
if (index > -1) {
lore.set(index, type + prefix + line);
} else {
lore.add(type + prefix + line);
}
}
// Adds or replaces a line of Lore.
// Searches for type and if not found for Substring lore and replaces it
public void removeLore(String type, String line) {
int index = BUtil.indexOfStart(lore, type);
if (index == -1) {
index = BUtil.indexOfSubstring(lore, line);
}
if (index > -1) {
lore.remove(index);
}
}
// Adds the Effect names to the Items description
public void addOrReplaceEffects(ArrayList<BEffect> effects, int quality) {
if (!P.use1_9 && effects != null) {
for (BEffect effect : effects) {
if (!effect.isHidden()) {
effect.writeInto(meta, quality);
}
}
}
}
// Removes all effects
public void removeEffects() {
if (meta.hasCustomEffects()) {
for (PotionEffect effect : meta.getCustomEffects()) {
PotionEffectType type = effect.getType();
//if (!type.equals(PotionEffectType.REGENERATION)) {
meta.removeCustomEffect(type);
//}
}
}
}
// True if the PotionMeta has Lore in quality color
public static boolean hasColorLore(PotionMeta meta) {
if (!meta.hasLore()) return false;
List<String> lore = meta.getLore();
if (lore.size() < 2) {
return false;
}
if (BUtil.indexOfStart(lore, INGR) != -1) {
// Ingredient lore present, must be quality colored
return true;
}
return false;
//!meta.getLore().get(1).startsWith("§7");
}
// gets the Color that represents a quality in Lore
public static String getQualityColor(int quality) {
String color;
if (quality > 8) {
color = "&a";
} else if (quality > 6) {
color = "&e";
} else if (quality > 4) {
color = "&6";
} else if (quality > 2) {
color = "&c";
} else {
color = "&4";
}
return P.p.color(color);
}
}