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

325 lines
8.6 KiB
Java
Raw Normal View History

2013-04-28 23:57:41 +02:00
package com.dre.brewery;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
2013-05-28 16:25:06 +02:00
import java.util.List;
2013-04-28 23:57:41 +02:00
2013-04-30 21:41:16 +02:00
import org.bukkit.configuration.ConfigurationSection;
2013-04-28 23:57:41 +02:00
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.inventory.BrewerInventory;
import com.dre.brewery.BIngredients;
public class Brew {
public static Map<Integer, Brew> potions = new HashMap<Integer, Brew>();
2013-04-28 23:57:41 +02:00
// represents the liquid in the brewed Potions
2013-04-28 23:57:41 +02:00
private BIngredients ingredients;
private int quality;
private int distillRuns;
private float ageTime;
2013-05-28 16:25:06 +02:00
private BRecipe currentRecipe;
2013-04-28 23:57:41 +02:00
public Brew(int uid, BIngredients ingredients) {
2013-04-28 23:57:41 +02:00
this.ingredients = ingredients;
potions.put(uid, this);
2013-04-28 23:57:41 +02:00
}
// quality already set
2013-05-28 16:25:06 +02:00
public Brew(int uid, int quality, BRecipe recipe, BIngredients ingredients) {
2013-04-28 23:57:41 +02:00
this.ingredients = ingredients;
this.quality = quality;
2013-05-28 16:25:06 +02:00
this.currentRecipe = recipe;
potions.put(uid, this);
2013-04-28 23:57:41 +02:00
}
// loading from file
2013-05-28 16:25:06 +02:00
public Brew(int uid, BIngredients ingredients, int quality, int distillRuns, float ageTime, String recipe) {
2013-04-30 21:41:16 +02:00
this.ingredients = ingredients;
this.quality = quality;
this.distillRuns = distillRuns;
this.ageTime = ageTime;
2013-05-28 16:25:06 +02:00
this.currentRecipe = BIngredients.getRecipeByName(recipe);
potions.put(uid, this);
2013-04-30 21:41:16 +02:00
}
// returns a Brew by its UID
public static Brew get(int uid) {
if (uid < -1) {
if (!potions.containsKey(uid)) {
P.p.errorLog("Database failure! unable to find UID " + uid + " of a custom Potion!");
return null;// throw some exception?
2013-04-28 23:57:41 +02:00
}
} else {
return null;
}
return potions.get(uid);
}
// returns a Brew by PotionMeta
public static Brew get(PotionMeta meta) {
2013-05-01 21:47:41 +02:00
return get(getUID(meta));
2013-04-28 23:57:41 +02:00
}
// returns a Brew by ItemStack
/*
* public static Brew get(ItemStack item){ if(item.getTypeId() == 373){
* PotionMeta potionMeta = (PotionMeta) item.getItemMeta(); return
* get(potionMeta); } return null; }
*/
2013-04-28 23:57:41 +02:00
// returns UID of custom Potion item
public static int getUID(ItemStack item) {
2013-05-01 21:47:41 +02:00
return getUID((PotionMeta) item.getItemMeta());
}
// returns UID of custom Potion meta
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) {
2013-05-01 21:47:41 +02:00
return effect.getDuration();
}
}
}
}
return 0;
}
2013-05-28 16:25:06 +02:00
// remove potion from file (drinking, despawning, should be more!)
public static void remove(ItemStack item) {
2013-05-01 21:47:41 +02:00
potions.remove(getUID(item));
}
// generate an UID
public static int generateUID() {
2013-05-01 21:47:41 +02:00
int uid = -2;
while (potions.containsKey(uid)) {
2013-05-01 21:47:41 +02:00
uid -= 1;
}
return uid;
}
// calculate alcohol from recipe
2013-05-28 16:25:06 +02:00
public int calcAlcohol() {
if (currentRecipe != null) {
int alc = currentRecipe.getAlcohol();
alc *= ((float) quality / 10.0);
2013-07-03 18:06:13 +02:00
if (currentRecipe.needsDistilling()) {
// distillable Potions should have full alc after 6 distills
float factor = 1.4F / (distillRuns + 1);
factor += 0.8;
alc /= factor;
2013-05-28 16:25:06 +02:00
}
return alc;
2013-05-10 00:01:28 +02:00
}
2013-05-28 16:25:06 +02:00
return 0;
2013-04-30 01:23:29 +02:00
}
// calculating quality
2013-07-03 18:06:13 +02:00
public int calcQuality(BRecipe recipe, byte wood, boolean distilled) {
// calculate quality from all of the factors
float quality = (
2013-04-30 01:23:29 +02:00
2013-05-10 00:01:28 +02:00
ingredients.getIngredientQuality(recipe) +
2013-07-03 18:06:13 +02:00
ingredients.getCookingQuality(recipe, distilled) +
2013-05-10 00:01:28 +02:00
ingredients.getWoodQuality(recipe, wood) +
ingredients.getAgeQuality(recipe, ageTime));
2013-04-30 01:23:29 +02:00
quality /= 4;
return (int) Math.round(quality);
2013-04-30 01:23:29 +02:00
}
public int getQuality() {
2013-04-30 01:23:29 +02:00
return quality;
}
2013-07-03 18:06:13 +02:00
public boolean canDistill() {
if (distillRuns >= 6) {
return false;
} else {
if (currentRecipe != null) {
return currentRecipe.needsDistilling();
}
}
return true;
}
2013-05-28 16:25:06 +02:00
// return special effect
public String getEffect() {
if (currentRecipe != null) {
return currentRecipe.getEffect();
}
return null;
}
public int getEffectDur() {
if (currentRecipe != null) {
return currentRecipe.getEffectDur();
}
return 0;
2013-04-30 01:23:29 +02:00
}
// Distilling section ---------------
2013-04-28 23:57:41 +02:00
// distill all custom potions in the brewer
2013-07-03 18:06:13 +02:00
public static void distillAll(BrewerInventory inv, Boolean[] contents) {
2013-04-28 23:57:41 +02:00
int slot = 0;
while (slot < 3) {
2013-07-03 18:06:13 +02:00
if (contents[slot]) {
distillSlot(inv, slot);
2013-04-28 23:57:41 +02:00
}
slot++;
}
}
// distill custom potion in given slot
public static void distillSlot(BrewerInventory inv, int slot) {
2013-04-28 23:57:41 +02:00
ItemStack slotItem = inv.getItem(slot);
PotionMeta potionMeta = (PotionMeta) slotItem.getItemMeta();
Brew brew = get(potionMeta);
BRecipe recipe = brew.ingredients.getdistillRecipe();
if (recipe != null) {
2013-07-03 18:06:13 +02:00
brew.quality = brew.calcQuality(recipe, (byte) 0, true);
2013-04-28 23:57:41 +02:00
brew.distillRuns += 1;
2013-05-28 16:25:06 +02:00
// distillRuns will have an effect on the amount of alcohol, not the quality
if (brew.distillRuns > 1) {
2013-05-28 16:25:06 +02:00
List<String> lore = new ArrayList<String>();
lore.add(brew.distillRuns + " fach Destilliert");
2013-04-28 23:57:41 +02:00
potionMeta.setLore(lore);
}
2013-05-28 16:25:06 +02:00
brew.currentRecipe = recipe;
2013-07-03 18:06:13 +02:00
P.p.log("destilled " + recipe.getName(5) + " has Quality: " + brew.quality + ", alc: " + brew.calcAlcohol());
2013-04-28 23:57:41 +02:00
2013-07-04 02:59:21 +02:00
potionMeta.setDisplayName(P.p.white() + recipe.getName(brew.quality));
2013-04-28 23:57:41 +02:00
// if the potion should be further distillable
2013-05-28 16:25:06 +02:00
if (recipe.getDistillRuns() > 1 && brew.distillRuns <= 5) {
2013-04-28 23:57:41 +02:00
slotItem.setDurability(PotionColor.valueOf(recipe.getColor()).getColorId(true));
} else {
slotItem.setDurability(PotionColor.valueOf(recipe.getColor()).getColorId(false));
}
} else {
2013-07-04 02:59:21 +02:00
potionMeta.setDisplayName(P.p.white() + "Undefinierbares Destillat");
2013-07-03 18:06:13 +02:00
slotItem.setDurability(PotionColor.GREY.getColorId(brew.distillRuns <= 5));
2013-04-28 23:57:41 +02:00
}
slotItem.setItemMeta(potionMeta);
}
// Ageing Section ------------------
2013-04-28 23:57:41 +02:00
public static void age(ItemStack item, float time, byte wood) {
2013-04-28 23:57:41 +02:00
PotionMeta potionMeta = (PotionMeta) item.getItemMeta();
Brew brew = get(potionMeta);
if (brew != null) {
2013-04-28 23:57:41 +02:00
brew.ageTime += time;
// if younger than half a day, it shouldnt get aged form
if (brew.ageTime > 0.5) {
2013-07-03 18:06:13 +02:00
BRecipe recipe = brew.ingredients.getAgeRecipe(wood, brew.ageTime, brew.distillRuns > 0);
if (recipe != null) {
2013-07-03 18:06:13 +02:00
//if (!recipe.needsDistilling() || brew.distillRuns > 0) {
2013-04-28 23:57:41 +02:00
2013-07-03 18:06:13 +02:00
brew.quality = brew.calcQuality(recipe, wood, brew.distillRuns > 0);
2013-05-28 16:25:06 +02:00
brew.currentRecipe = recipe;
P.p.log("Final " + recipe.getName(5) + " has Quality: " + brew.quality);
2013-04-28 23:57:41 +02:00
2013-05-28 16:25:06 +02:00
if (brew.ageTime >= 2) {
List<String> lore;
String newLore;
int index = 0;
if (brew.ageTime < 201) {
newLore = (int) Math.floor(brew.ageTime) + " Jahre Fassgereift";
} else {
newLore = "Hunderte Jahre Fassgereift";
}
if (potionMeta.hasLore()) {
lore = potionMeta.getLore();
while (index < lore.size()) {
String existingLore = lore.get(index);
if (existingLore.contains("Jahre Fassgereift")) {
lore.set(index, newLore);
break;
}
index++;
}
} else {
lore = new ArrayList<String>();
lore.add(newLore);
}
potionMeta.setLore(lore);
}
2013-07-04 02:59:21 +02:00
potionMeta.setDisplayName(P.p.white() + recipe.getName(brew.quality));
2013-04-30 01:23:29 +02:00
item.setDurability(PotionColor.valueOf(recipe.getColor()).getColorId(false));
item.setItemMeta(potionMeta);
2013-07-03 18:06:13 +02:00
//}
2013-04-28 23:57:41 +02:00
}
}
}
}
// Saves all data
public static void save(ConfigurationSection config) {
for (int uid : potions.keySet()) {
ConfigurationSection idConfig = config.createSection("" + uid);
2013-04-30 21:41:16 +02:00
Brew brew = potions.get(uid);
// not saving unneccessary data
if (brew.quality != 0) {
2013-04-30 21:41:16 +02:00
idConfig.set("quality", brew.quality);
}
if (brew.distillRuns != 0) {
2013-04-30 21:41:16 +02:00
idConfig.set("distillRuns", brew.distillRuns);
}
if (brew.ageTime != 0) {
2013-04-30 21:41:16 +02:00
idConfig.set("ageTime", brew.ageTime);
}
2013-05-28 16:25:06 +02:00
if (brew.currentRecipe != null) {
idConfig.set("recipe", brew.currentRecipe.getName(5));
2013-04-30 21:41:16 +02:00
}
// save the ingredients
2013-04-30 21:41:16 +02:00
brew.ingredients.save(idConfig.createSection("ingredients"));
}
}
public static enum PotionColor {
2013-05-28 16:25:06 +02:00
PINK(1),
CYAN(2),
ORANGE(3),
GREEN(4),
BRIGHT_RED(5),
BLUE(6),
BLACK(8),
RED(9),
GREY(10),
WATER(11),
DARK_RED(12),
BRIGHT_GREY(14);
2013-04-28 23:57:41 +02:00
private final int colorId;
private PotionColor(int colorId) {
this.colorId = colorId;
}
// 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) {
2013-04-28 23:57:41 +02:00
return (short) (colorId + 64);
}
return (short) (colorId + 32);
}
}
}