Brewery/src/com/dre/brewery/Brew.java

898 lines
25 KiB
Java
Raw Normal View History

2013-04-28 23:57:41 +02:00
package com.dre.brewery;
import org.bukkit.Color;
2016-07-09 00:01:31 +02:00
import com.dre.brewery.api.events.brew.BrewModifyEvent;
import com.dre.brewery.lore.*;
2014-05-19 15:17:55 +02:00
import org.bukkit.Material;
2013-04-30 21:41:16 +02:00
import org.bukkit.configuration.ConfigurationSection;
2016-03-21 20:35:12 +01:00
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.ItemFlag;
2013-04-28 23:57:41 +02:00
import org.bukkit.inventory.ItemStack;
2016-06-16 22:13:21 +02:00
import org.bukkit.inventory.meta.ItemMeta;
2013-04-28 23:57:41 +02:00
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
2013-04-28 23:57:41 +02:00
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
2016-06-29 23:24:39 +02:00
import java.security.InvalidKeyException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
2013-04-28 23:57:41 +02:00
public class Brew {
// represents the liquid in the brewed Potions
private static long saveSeed;
2016-06-16 22:13:21 +02:00
public static Map<Integer, Brew> legacyPotions = new HashMap<>();
2016-05-18 22:31:32 +02:00
public static long installTime = System.currentTimeMillis(); // plugin install time in millis after epoch
public static Boolean colorInBarrels; // color the Lore while in Barrels
public static Boolean colorInBrewer; // color the Lore while in Brewer
private BIngredients ingredients;
private int quality;
2016-06-16 22:13:21 +02:00
private byte distillRuns;
private float ageTime;
private float wood;
private BRecipe currentRecipe;
private boolean unlabeled;
2016-06-17 00:04:41 +02:00
private boolean persistent; // Only for legacy
private boolean immutable; // static/immutable potions should not be changed
2016-06-16 22:13:21 +02:00
//private int lastUpdate; // last update in hours after install time
2016-06-16 22:13:21 +02:00
public Brew(BIngredients ingredients) {
this.ingredients = ingredients;
}
// quality already set
2016-06-16 22:13:21 +02:00
public Brew(int quality, BRecipe recipe, BIngredients ingredients) {
this.ingredients = ingredients;
this.quality = quality;
this.currentRecipe = recipe;
}
2016-06-16 22:13:21 +02:00
// loading with all values set
2016-06-17 00:04:41 +02:00
public Brew(BIngredients ingredients, int quality, byte distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean immutable) {
this.ingredients = ingredients;
this.quality = quality;
this.distillRuns = distillRuns;
this.ageTime = ageTime;
this.wood = wood;
this.unlabeled = unlabeled;
2016-06-17 00:04:41 +02:00
this.immutable = immutable;
setRecipeFromString(recipe);
}
2016-06-16 22:13:21 +02:00
// Loading from InputStream
private Brew() {
}
2016-06-16 22:13:21 +02:00
// returns a Brew by ItemMeta
public static Brew get(ItemMeta meta) {
if (meta.hasLore()) {
if (meta instanceof PotionMeta && ((PotionMeta) meta).hasCustomEffect(PotionEffectType.REGENERATION)) {
Brew brew = load(meta);
if (brew != null) {
brew = getFromPotionEffect(((PotionMeta) meta), false);
}
return brew;
} else {
return load(meta);
}
}
return null;
}
// returns a Brew by ItemStack
public static Brew get(ItemStack item) {
if (item.getType() == Material.POTION) {
if (item.hasItemMeta()) {
2016-06-16 22:13:21 +02:00
ItemMeta meta = item.getItemMeta();
if (meta.hasLore()) {
if (meta instanceof PotionMeta && ((PotionMeta) meta).hasCustomEffect(PotionEffectType.REGENERATION)) {
Brew brew = load(meta);
if (brew != null) {
((PotionMeta) meta).removeCustomEffect(PotionEffectType.REGENERATION);
} else {
brew = getFromPotionEffect(((PotionMeta) meta), true);
if (brew == null) return null;
brew.save(meta);
}
item.setItemMeta(meta);
return brew;
} else {
return load(meta);
}
}
}
}
return null;
}
2016-06-16 22:13:21 +02:00
private static Brew getFromPotionEffect(PotionMeta potionMeta, boolean remove) {
for (PotionEffect effect : potionMeta.getCustomEffects()) {
if (effect.getType().equals(PotionEffectType.REGENERATION)) {
if (effect.getDuration() < -1) {
if (remove) {
return legacyPotions.remove(effect.getDuration());
} else {
return legacyPotions.get(effect.getDuration());
}
}
}
}
return null;
}
// returns a Brew by its UID
// Does not work anymore with new save system
@Deprecated
public static Brew get(int uid) {
if (uid < -1) {
if (!legacyPotions.containsKey(uid)) {
P.p.errorLog("Database failure! unable to find UID " + uid + " of a custom Potion!");
return null;// throw some exception?
}
} else {
return null;
}
return legacyPotions.get(uid);
}
// returns UID of custom Potion item
2016-06-16 22:13:21 +02:00
// Does not work anymore with new save system
@Deprecated
public static int getUID(ItemStack item) {
return getUID((PotionMeta) item.getItemMeta());
}
// returns UID of custom Potion meta
2016-06-16 22:13:21 +02:00
// Does not work anymore with new save system
@Deprecated
public static int getUID(PotionMeta potionMeta) {
if (potionMeta.hasCustomEffect(PotionEffectType.REGENERATION)) {
for (PotionEffect effect : potionMeta.getCustomEffects()) {
if (effect.getType().equals(PotionEffectType.REGENERATION)) {
if (effect.getDuration() < -1) {
return effect.getDuration();
}
}
}
}
return 0;
}
// generate an UID
2016-06-16 22:13:21 +02:00
/*public static int generateUID() {
int uid = -2;
while (potions.containsKey(uid)) {
uid -= 1;
}
return uid;
2016-06-16 22:13:21 +02:00
}*/
//returns the recipe with the given name, recalculates if not found
public boolean setRecipeFromString(String name) {
currentRecipe = null;
if (name != null && !name.equals("")) {
for (BRecipe recipe : BIngredients.recipes) {
if (recipe.getName(5).equalsIgnoreCase(name)) {
currentRecipe = recipe;
return true;
}
}
if (quality > 0) {
currentRecipe = ingredients.getBestRecipe(wood, ageTime, distillRuns > 0);
if (currentRecipe != null) {
2016-06-17 00:04:41 +02:00
if (!immutable) {
this.quality = calcQuality();
}
P.p.log("Brew was made from Recipe: '" + name + "' which could not be found. '" + currentRecipe.getName(5) + "' used instead!");
return true;
} else {
P.p.errorLog("Brew was made from Recipe: '" + name + "' which could not be found!");
}
}
}
return false;
}
public boolean reloadRecipe() {
2016-05-27 19:31:05 +02:00
return currentRecipe == null || setRecipeFromString(currentRecipe.getName(5));
}
// Copy a Brew with a new unique ID and return its item
2016-06-16 22:13:21 +02:00
// Not needed anymore
/*public ItemStack copy(ItemStack item) {
ItemStack copy = item.clone();
int uid = generateUID();
clone(uid);
PotionMeta meta = (PotionMeta) copy.getItemMeta();
if (!P.use1_14) {
// This is due to the Duration Modifier, that is removed in 1.14
uid *= 4;
}
meta.addCustomEffect((PotionEffectType.REGENERATION).createEffect(uid, 0), true);
copy.setItemMeta(meta);
return copy;
2016-06-16 22:13:21 +02:00
}*/
public boolean isSimilar(Brew brew) {
if (brew == null) return false;
if (equals(brew)) return true;
return quality == brew.quality &&
distillRuns == brew.distillRuns &&
Float.compare(brew.ageTime, ageTime) == 0 &&
Float.compare(brew.wood, wood) == 0 &&
unlabeled == brew.unlabeled &&
persistent == brew.persistent &&
immutable == brew.immutable &&
ingredients.equals(brew.ingredients) &&
2016-07-01 22:01:35 +02:00
(currentRecipe != null ? currentRecipe.equals(brew.currentRecipe) : brew.currentRecipe == null);
}
2016-06-16 22:13:21 +02:00
// Clones this instance
@Override
public Brew clone() throws CloneNotSupportedException {
super.clone();
Brew brew = new Brew(quality, currentRecipe, ingredients);
brew.distillRuns = distillRuns;
brew.ageTime = ageTime;
brew.unlabeled = unlabeled;
2016-06-17 00:04:41 +02:00
brew.persistent = persistent;
brew.immutable = immutable;
return brew;
}
2016-06-16 22:13:21 +02:00
@Override
public String toString() {
return "Brew{" +
ingredients + " ingredients" +
", quality=" + quality +
", distillRuns=" + distillRuns +
", ageTime=" + ageTime +
", wood=" + wood +
", currentRecipe=" + currentRecipe +
", unlabeled=" + unlabeled +
2016-06-17 00:04:41 +02:00
", immutable=" + immutable +
2016-06-16 22:13:21 +02:00
'}';
}
// remove potion from file (drinking, despawning, combusting, cmdDeleting, should be more!)
2016-06-16 22:13:21 +02:00
// Not needed anymore
/*public void remove(ItemStack item) {
if (!persistent) {
potions.remove(getUID(item));
}
2016-06-16 22:13:21 +02:00
}*/
// calculate alcohol from recipe
public int calcAlcohol() {
if (quality == 0) {
// Give bad potions some alc
int badAlc = 0;
if (distillRuns > 1) {
badAlc = distillRuns;
}
if (ageTime > 10) {
badAlc += 5;
} else if (ageTime > 2) {
badAlc += 3;
}
if (currentRecipe != null) {
return badAlc;
} else {
return badAlc / 2;
}
}
if (currentRecipe != null) {
int alc = currentRecipe.getAlcohol();
if (currentRecipe.needsDistilling()) {
if (distillRuns == 0) {
return 0;
}
// bad quality can decrease alc by up to 40%
alc *= 1 - ((float) (10 - quality) * 0.04);
// distillable Potions should have half alc after one and full alc after all needed distills
alc /= 2;
alc *= 1.0F + ((float) distillRuns / currentRecipe.getDistillRuns());
} else {
// quality decides 10% - 100%
alc *= ((float) quality / 10.0);
}
if (alc > 0) {
return alc;
}
}
return 0;
}
// calculating quality
public int calcQuality() {
// calculate quality from all of the factors
float quality = ingredients.getIngredientQuality(currentRecipe) + ingredients.getCookingQuality(currentRecipe, distillRuns > 0);
if (currentRecipe.needsToAge() || ageTime > 0.5) {
quality += ingredients.getWoodQuality(currentRecipe, wood) + ingredients.getAgeQuality(currentRecipe, ageTime);
quality /= 4;
} else {
quality /= 2;
}
return Math.round(quality);
}
public int getQuality() {
return quality;
}
public boolean canDistill() {
2016-06-17 00:04:41 +02:00
if (immutable) return false;
if (currentRecipe != null) {
return currentRecipe.getDistillRuns() > distillRuns;
2018-10-30 15:59:56 +01:00
} else {
return distillRuns < 6;
}
}
// return special effect
public ArrayList<BEffect> getEffects() {
if (currentRecipe != null && quality > 0) {
return currentRecipe.getEffects();
}
return null;
}
// Set unlabeled to true to hide the numbers in Lore
public void unLabel(ItemStack item) {
2016-07-01 22:01:35 +02:00
unlabeled = true;
ItemMeta meta = item.getItemMeta();
if (meta instanceof PotionMeta && meta.hasLore()) {
BrewLore lore = new BrewLore(this, ((PotionMeta) meta));
if (distillRuns > 0) {
2016-07-01 22:01:35 +02:00
lore.updateDistillLore(false);
}
if (ageTime >= 1) {
2016-07-01 22:01:35 +02:00
lore.updateAgeLore(false);
}
2016-07-01 22:01:35 +02:00
lore.write();
item.setItemMeta(meta);
}
}
2016-05-18 22:31:32 +02:00
// Do some regular updates
2016-06-16 22:13:21 +02:00
// Currently does nothing, but may be used to update something on this brew
2016-05-18 22:31:32 +02:00
public void touch() {
2016-06-16 22:13:21 +02:00
//lastUpdate = (int) ((double) (System.currentTimeMillis() - installTime) / 3600000D);
2016-05-18 22:31:32 +02:00
}
2016-06-16 22:13:21 +02:00
public byte getDistillRuns() {
return distillRuns;
}
public float getAgeTime() {
return ageTime;
}
2016-07-01 22:01:35 +02:00
public float getWood() {
return wood;
}
public BIngredients getIngredients() {
return ingredients;
}
public boolean hasRecipe() {
return currentRecipe != null;
}
public BRecipe getCurrentRecipe() {
return currentRecipe;
}
2016-06-16 22:13:21 +02:00
// Not needed anymore
// TODO remove
@Deprecated
public boolean isPersistent() {
return persistent;
}
// Make a potion persistent to not delete it when drinking it
2016-06-16 22:13:21 +02:00
// Not needed anymore
@Deprecated
public void makePersistent() {
persistent = true;
}
// Remove the Persistence Flag from a brew, so it will be normally deleted when drinking it
2016-06-16 22:13:21 +02:00
// Not needed anymore
@Deprecated
public void removePersistence() {
persistent = false;
}
public boolean isStatic() {
2016-06-17 00:04:41 +02:00
return immutable;
}
2016-07-01 22:01:35 +02:00
public boolean isImmutable() {
return immutable;
}
public boolean isUnlabeled() {
return unlabeled;
}
// Set the Static flag, so potion is unchangeable
2016-06-17 00:04:41 +02:00
public void setStatic(boolean immutable, ItemStack potion) {
this.immutable = immutable;
if (currentRecipe != null && canDistill()) {
2016-06-17 00:04:41 +02:00
if (immutable) {
2019-03-05 20:55:52 +01:00
PotionColor.fromString(currentRecipe.getColor()).colorBrew(((PotionMeta) potion.getItemMeta()), potion, false);
} else {
2019-03-05 20:55:52 +01:00
PotionColor.fromString(currentRecipe.getColor()).colorBrew(((PotionMeta) potion.getItemMeta()), potion, true);
}
}
}
2016-06-16 22:13:21 +02:00
/*public int getLastUpdate() {
2016-05-18 22:31:32 +02:00
return lastUpdate;
2016-06-16 22:13:21 +02:00
}*/
2016-05-18 22:31:32 +02:00
// Distilling section ---------------
2016-04-23 17:45:01 +02:00
// distill all custom potions in the brewer
public static void distillAll(BrewerInventory inv, Brew[] contents) {
for (int slot = 0; slot < 3; slot++) {
if (contents[slot] != null) {
ItemStack slotItem = inv.getItem(slot);
PotionMeta potionMeta = (PotionMeta) slotItem.getItemMeta();
contents[slot].distillSlot(slotItem, potionMeta);
}
}
}
// distill custom potion in given slot
public void distillSlot(ItemStack slotItem, PotionMeta potionMeta) {
2016-06-17 00:04:41 +02:00
if (immutable) return;
2016-07-09 00:01:31 +02:00
BrewModifyEvent modifyEvent = new BrewModifyEvent(this, BrewModifyEvent.Type.DISTILL);
P.p.getServer().getPluginManager().callEvent(modifyEvent);
if (modifyEvent.isCancelled()) return;
distillRuns += 1;
2016-07-01 22:01:35 +02:00
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();
2016-07-01 22:01:35 +02:00
lore.addOrReplaceEffects(getEffects(), quality);
potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality)));
2019-03-05 20:55:52 +01:00
PotionColor.fromString(recipe.getColor()).colorBrew(potionMeta, slotItem, canDistill());
} else {
quality = 0;
2016-07-01 22:01:35 +02:00
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) {
2016-07-01 22:01:35 +02:00
if (colorInBrewer != BrewLore.hasColorLore(potionMeta)) {
lore.convertLore(colorInBrewer);
}
}
2016-07-01 22:01:35 +02:00
lore.updateDistillLore(colorInBrewer);
lore.write();
2016-05-18 22:31:32 +02:00
touch();
2016-06-16 22:13:21 +02:00
save(potionMeta);
slotItem.setItemMeta(potionMeta);
}
public int getDistillTimeNextRun() {
if (!canDistill()) {
return -1;
}
if (currentRecipe != null) {
return currentRecipe.getDistillTime();
}
BRecipe recipe = ingredients.getdistillRecipe(wood, ageTime);
if (recipe != null) {
return recipe.getDistillTime();
}
return 0;
}
// Ageing Section ------------------
2016-04-23 17:45:01 +02:00
public void age(ItemStack item, float time, byte woodType) {
2016-06-17 00:04:41 +02:00
if (immutable) return;
2016-07-09 00:01:31 +02:00
BrewModifyEvent modifyEvent = new BrewModifyEvent(this, BrewModifyEvent.Type.AGE);
P.p.getServer().getPluginManager().callEvent(modifyEvent);
if (modifyEvent.isCancelled()) return;
PotionMeta potionMeta = (PotionMeta) item.getItemMeta();
2016-07-01 22:01:35 +02:00
BrewLore lore = new BrewLore(this, potionMeta);
ageTime += time;
// if younger than half a day, it shouldnt get aged form
if (ageTime > 0.5) {
if (wood == 0) {
wood = woodType;
} else if (wood != woodType) {
woodShift(time, woodType);
}
BRecipe recipe = ingredients.getAgeRecipe(wood, ageTime, distillRuns > 0);
if (recipe != null) {
currentRecipe = recipe;
quality = calcQuality();
2016-07-01 22:01:35 +02:00
lore.addOrReplaceEffects(getEffects(), quality);
potionMeta.setDisplayName(P.p.color("&f" + recipe.getName(quality)));
2019-03-05 20:55:52 +01:00
PotionColor.fromString(recipe.getColor()).colorBrew(potionMeta, item, canDistill());
} else {
quality = 0;
2016-07-01 22:01:35 +02:00
lore.removeEffects();
potionMeta.setDisplayName(P.p.color("&f" + P.p.languageReader.get("Brew_BadPotion")));
PotionColor.GREY.colorBrew(potionMeta, item, canDistill());
}
}
// Lore
if (currentRecipe != null) {
2016-07-01 22:01:35 +02:00
if (colorInBarrels != BrewLore.hasColorLore(potionMeta)) {
lore.convertLore(colorInBarrels);
}
}
if (ageTime >= 1) {
2016-07-01 22:01:35 +02:00
lore.updateAgeLore(colorInBarrels);
}
if (ageTime > 0.5) {
if (colorInBarrels && !unlabeled && currentRecipe != null) {
2016-07-01 22:01:35 +02:00
lore.updateWoodLore(true);
}
}
2016-07-01 22:01:35 +02:00
lore.write();
2016-05-18 22:31:32 +02:00
touch();
2016-06-16 22:13:21 +02:00
save(potionMeta);
item.setItemMeta(potionMeta);
}
// Slowly shift the wood of the Brew to the new Type
public void woodShift(float time, byte to) {
2016-07-01 22:01:35 +02:00
float factor = 1;
if (ageTime > 5) {
factor = 2;
} else if (ageTime > 10) {
factor = 2;
2016-07-01 22:01:35 +02:00
factor += (float) ageTime / 10F;
}
if (wood > to) {
wood -= time / factor;
if (wood < to) {
wood = to;
}
} else {
wood += time / factor;
if (wood > to) {
wood = to;
}
}
}
2016-06-16 22:13:21 +02:00
private static Brew load(ItemMeta meta) {
LoreLoadStream loreStream;
try {
loreStream = new LoreLoadStream(meta, 0);
} catch (IllegalArgumentException ignored) {
return null;
}
XORUnscrambleStream unscrambler = new XORUnscrambleStream(new Base91DecoderStream(loreStream), saveSeed);
try (DataInputStream in = new DataInputStream(unscrambler)) {
boolean parityFailed = false;
2016-06-16 22:13:21 +02:00
if (in.readByte() != 86) {
P.p.errorLog("Parity check failed on Brew while loading, trying to load anyways!");
parityFailed = true;
2016-06-16 22:13:21 +02:00
}
Brew brew = new Brew();
byte ver = in.readByte();
switch (ver) {
case 1:
unscrambler.start();
2016-06-16 22:13:21 +02:00
brew.loadFromStream(in);
break;
default:
if (parityFailed) {
P.p.errorLog("Failed to load Brew. Maybe something corrupted the Lore of the Item?");
} else {
P.p.errorLog("Brew has data stored in v" + ver + " this Plugin version supports up to v1");
}
2016-06-16 22:13:21 +02:00
return null;
}
return brew;
} catch (IOException e) {
P.p.errorLog("IO Error while loading Brew");
e.printStackTrace();
2016-06-29 23:24:39 +02:00
} catch (InvalidKeyException e) {
P.p.errorLog("Failed to load Brew, has the data key 'BrewDataSeed' in the data.yml been changed?");
e.printStackTrace();
}
2016-06-16 22:13:21 +02:00
return null;
}
2016-06-16 22:13:21 +02:00
private void loadFromStream(DataInputStream in) throws IOException {
quality = in.readByte();
int bools = in.readUnsignedByte();
if ((bools & 1) != 0) {
2016-06-16 22:13:21 +02:00
distillRuns = in.readByte();
}
if ((bools & 2) != 0) {
2016-06-16 22:13:21 +02:00
ageTime = in.readFloat();
}
if ((bools & 4) != 0) {
2016-06-16 22:13:21 +02:00
wood = in.readFloat();
}
if ((bools & 8) != 0) {
2016-06-16 22:13:21 +02:00
setRecipeFromString(in.readUTF());
} else {
setRecipeFromString(null);
}
2016-06-16 22:13:21 +02:00
unlabeled = (bools & 16) != 0;
2016-06-17 00:04:41 +02:00
//persistent = (bools & 32) != 0;
immutable = (bools & 32) != 0;
ingredients = BIngredients.load(in);
2016-06-16 22:13:21 +02:00
}
// Save brew data into meta/lore
public void save(ItemMeta meta) {
XORScrambleStream scrambler = new XORScrambleStream(new Base91EncoderStream(new LoreSaveStream(meta, 0)), saveSeed);
try (DataOutputStream out = new DataOutputStream(scrambler)) {
out.writeByte(86); // Parity/sanity
out.writeByte(1); // Version
scrambler.start();
2016-06-17 00:04:41 +02:00
saveToStream(out);
} catch (IOException e) {
P.p.errorLog("IO Error while saving Brew");
e.printStackTrace();
}
2016-06-16 22:13:21 +02:00
}
// Save brew data into the meta/lore of the specified item
// The meta on the item changes, so to make further changes to the meta, item.getItemMeta() has to be called again after this
public void save(ItemStack item) {
ItemMeta meta;
if (!item.hasItemMeta()) {
meta = P.p.getServer().getItemFactory().getItemMeta(item.getType());
} else {
meta = item.getItemMeta();
}
save(meta);
item.setItemMeta(meta);
}
2016-06-17 00:04:41 +02:00
public void saveToStream(DataOutputStream out) throws IOException {
if (quality > 10) {
quality = 10;
}
out.writeByte((byte) quality);
int bools = 0;
bools |= ((distillRuns != 0) ? 1 : 0);
bools |= (ageTime > 0 ? 2 : 0);
bools |= (wood != -1 ? 4 : 0);
bools |= (currentRecipe != null ? 8 : 0);
bools |= (unlabeled ? 16 : 0);
//bools |= (persistent ? 32 : 0);
bools |= (immutable ? 32 : 0);
out.writeByte(bools);
if (distillRuns != 0) {
out.writeByte(distillRuns);
}
if (ageTime > 0) {
out.writeFloat(ageTime);
}
if (wood != -1) {
out.writeFloat(wood);
}
if (currentRecipe != null) {
out.writeUTF(currentRecipe.getName(5));
}
2016-06-17 00:04:41 +02:00
ingredients.save(out);
2016-06-16 22:13:21 +02:00
}
public static void writeSeed(ConfigurationSection section) {
2016-06-29 23:24:39 +02:00
section.set("BrewDataSeed", saveSeed);
}
public static void loadSeed(ConfigurationSection section) {
2016-06-29 23:24:39 +02:00
if (section.contains("BrewDataSeed")) {
saveSeed = section.getLong("BrewDataSeed");
} else {
while (saveSeed == 0) {
saveSeed = new SecureRandom().nextLong();
}
}
}
2016-06-16 22:13:21 +02:00
// Load potion data from data file for backwards compatibility
public static void loadLegacy(BIngredients ingredients, int uid, int quality, byte distillRuns, float ageTime, float wood, String recipe, boolean unlabeled, boolean persistent, boolean stat) {
2016-06-17 00:04:41 +02:00
Brew brew = new Brew(ingredients, quality, distillRuns, ageTime, wood, recipe, unlabeled, stat);
brew.persistent = persistent;
2016-06-16 22:13:21 +02:00
legacyPotions.put(uid, brew);
}
// remove legacy potiondata from item
public static void removeLegacy(ItemStack item) {
if (legacyPotions.isEmpty()) return;
if (!item.hasItemMeta()) return;
ItemMeta meta = item.getItemMeta();
if (!(meta instanceof PotionMeta)) return;
for (PotionEffect effect : ((PotionMeta) meta).getCustomEffects()) {
if (effect.getType().equals(PotionEffectType.REGENERATION)) {
if (effect.getDuration() < -1) {
legacyPotions.remove(effect.getDuration());
return;
}
}
}
}
2016-07-01 22:01:35 +02:00
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
2016-06-16 22:13:21 +02:00
// Legacy method to save to data file
@Deprecated
public static void save(ConfigurationSection config) {
2016-06-16 22:13:21 +02:00
for (Map.Entry<Integer, Brew> entry : legacyPotions.entrySet()) {
int uid = entry.getKey();
Brew brew = entry.getValue();
ConfigurationSection idConfig = config.createSection("" + uid);
// not saving unneccessary data
if (brew.quality != 0) {
idConfig.set("quality", brew.quality);
}
if (brew.distillRuns != 0) {
idConfig.set("distillRuns", brew.distillRuns);
}
if (brew.ageTime != 0) {
idConfig.set("ageTime", brew.ageTime);
}
if (brew.wood != -1) {
idConfig.set("wood", brew.wood);
}
if (brew.currentRecipe != null) {
idConfig.set("recipe", brew.currentRecipe.getName(5));
}
if (brew.unlabeled) {
idConfig.set("unlabeled", true);
}
if (brew.persistent) {
idConfig.set("persist", true);
}
2016-06-17 00:04:41 +02:00
if (brew.immutable) {
idConfig.set("stat", true);
}
2016-06-16 22:13:21 +02:00
/*if (brew.lastUpdate > 0) {
2016-05-18 22:31:32 +02:00
idConfig.set("lastUpdate", brew.lastUpdate);
2016-06-16 22:13:21 +02:00
}*/
// save the ingredients
idConfig.set("ingId", brew.ingredients.save(config.getParent()));
}
}
2019-03-05 20:55:52 +01:00
public static class PotionColor {
public static final PotionColor PINK = new PotionColor(1, PotionType.REGEN, Color.FUCHSIA);
public static final PotionColor CYAN = new PotionColor(2, PotionType.SPEED, Color.AQUA);
public static final PotionColor ORANGE = new PotionColor(3, PotionType.FIRE_RESISTANCE, Color.ORANGE);
public static final PotionColor GREEN = new PotionColor(4, PotionType.POISON, Color.GREEN);
public static final PotionColor BRIGHT_RED = new PotionColor(5, PotionType.INSTANT_HEAL, Color.fromRGB(255,0,0));
public static final PotionColor BLUE = new PotionColor(6, PotionType.NIGHT_VISION, Color.NAVY);
public static final PotionColor BLACK = new PotionColor(8, PotionType.WEAKNESS, Color.BLACK);
public static final PotionColor RED = new PotionColor(9, PotionType.STRENGTH, Color.fromRGB(196,0,0));
public static final PotionColor GREY = new PotionColor(10, PotionType.SLOWNESS, Color.GRAY);
public static final PotionColor WATER = new PotionColor(11, P.use1_9 ? PotionType.WATER_BREATHING : null, Color.BLUE);
public static final PotionColor DARK_RED = new PotionColor(12, PotionType.INSTANT_DAMAGE, Color.fromRGB(128,0,0));
public static final PotionColor BRIGHT_GREY = new PotionColor(14, PotionType.INVISIBILITY, Color.SILVER);
private final int colorId;
private final PotionType type;
private final Color color;
PotionColor(int colorId, PotionType type, Color color) {
this.colorId = colorId;
this.type = type;
this.color = color;
}
2019-03-05 20:55:52 +01:00
public PotionColor(Color color) {
colorId = -1;
type = WATER.getType();
2019-03-05 20:55:52 +01:00
this.color = color;
}
// gets the Damage Value, that sets a color on the potion
// offset +32 is not accepted by brewer, so not further destillable
public short getColorId(boolean destillable) {
if (destillable) {
return (short) (colorId + 64);
}
return (short) (colorId + 32);
}
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);
2018-10-31 22:25:32 +01:00
if (P.use1_11) {
// BasePotionData was only used for the Color, so starting with 1.12 we can use setColor instead
meta.setColor(getColor());
} else {
meta.setBasePotionData(new PotionData(getType()));
}
} else {
potion.setDurability(getColorId(destillable));
}
}
2019-03-05 20:55:52 +01:00
public static PotionColor fromString(String string) {
switch (string) {
case "PINK": return PINK;
case "CYAN": return CYAN;
case "ORANGE": return ORANGE;
case "GREEN": return GREEN;
case "BRIGHT_RED": return BRIGHT_RED;
case "BLUE": return BLUE;
case "BLACK": return BLACK;
case "RED": return RED;
case "GREY": return GREY;
case "WATER": return WATER;
case "DARK_RED": return DARK_RED;
case "BRIGHT_GREY": return BRIGHT_GREY;
default:
try{
2019-08-17 22:42:58 +02:00
if (string.length() >= 7) {
string = string.substring(1);
}
2019-03-05 20:55:52 +01:00
return new PotionColor(Color.fromRGB(
2019-08-17 22:42:58 +02:00
Integer.parseInt(string.substring( 0, 2 ), 16 ),
Integer.parseInt(string.substring( 2, 4 ), 16 ),
Integer.parseInt(string.substring( 4, 6 ), 16 )
2019-03-05 20:55:52 +01:00
));
} catch (Exception e) {
return WATER;
}
}
}
}
2016-03-21 20:35:12 +01:00
}