From 9cb1981fe9db1bae4b54cfa31aef5c3f9a687d69 Mon Sep 17 00:00:00 2001 From: Sn0wStorm Date: Fri, 19 Apr 2013 16:35:08 +0200 Subject: [PATCH] Added BCauldron, Config, Custom Recipes --- config.yml | 70 +++++++++ src/com/dre/brewery/BCauldron.java | 113 +++++++++++++++ src/com/dre/brewery/BIngredients.java | 135 ++++++++++++++++++ src/com/dre/brewery/BRecipe.java | 66 +++++++++ src/com/dre/brewery/P.java | 114 ++++++++++++++- .../dre/brewery/listeners/PlayerListener.java | 124 ++++++++++++++++ 6 files changed, 617 insertions(+), 5 deletions(-) create mode 100644 config.yml create mode 100644 src/com/dre/brewery/BCauldron.java create mode 100644 src/com/dre/brewery/BIngredients.java create mode 100644 src/com/dre/brewery/BRecipe.java create mode 100644 src/com/dre/brewery/listeners/PlayerListener.java diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..1c64afe --- /dev/null +++ b/config.yml @@ -0,0 +1,70 @@ +# name: versch. Namen für schlecht/mittel/gut +# ingredients: material/anzahl +# cookingtime: Zeit in minuten die die zutaten kochen müssen (o=kein kochen) +# destillruns: wie oft destilliert werden muss (0=ohne Destillieren) +# filter: filter in der destille (leer wenn egal) +# destillpotion: trank, der in der destille beigemischt wird (leer für wasser) +# wood: Holz des Fasses 0=ohne fass 1=Birch 2=Oak 3=Jungle 4=Pine +# difficulty: 1-10 genauigkeit der Einhaltung der Vorgaben +# alcohol: Alkoholgehalt 0-100 in Vol.% bei perfektem Getränk + +#cooked: Auflistung ALLER möglichen Zutaten und die daraus entstehenden Tranknahmen (leer für undef.) + + +recipes: + 1: + name: Ranziges Weißbier/Weißbier/Feines Weißbier + ingredients: + - WHEAT/6 + cookingtime: 8 + wood: 1 + difficulty: 1 + alcohol: 5 + 2: + name: Ranziges Bier/Bier/Feines Bier + ingredients: + - WHEAT/6 + cookingtime: 8 + wood: 2 + difficulty: 1 + alcohol: 5 + 3: + name: Ranziges Dunkelbier/Dunkelbier/Feines Dunkelbier + ingredients: + - WHEAT/6 + cookingtime: 8 + wood: 4 + difficulty: 2 + alcohol: 7 + 4: + name: Scheußlicher Met/Met/Goldener Met + ingredients: + - SUGAR_CANE/6 + cookingtime: 3 + wood: 2 + difficulty: 2 + alcohol: 8 + 5: + name: Apfelmet/Süßer Apfelmet/Goldensüßer Apfelmet + ingredients: + - SUGAR_CANE/6 + - APPLE/2 + cookingtime: 4 + destillruns: 0 + wood: 2 + difficulty: 4 + alcohol: 10 + 6: + name: Rum/Goldener Rum/Perfekter Rum + ingredients: + - SUGAR_CANE/14 + cookingtime: 5 + destillruns: 2 + wood: 2 + difficulty: 6 + alcohol: 60 +cooked: + WHEAT: Getreidemaische + SUGAR_CANE: Zuckersud + APPLE: Apfelmost + POTATO_ITEM: Kartoffelmaische \ No newline at end of file diff --git a/src/com/dre/brewery/BCauldron.java b/src/com/dre/brewery/BCauldron.java new file mode 100644 index 0000000..3642780 --- /dev/null +++ b/src/com/dre/brewery/BCauldron.java @@ -0,0 +1,113 @@ +package com.dre.brewery; + +import java.util.concurrent.CopyOnWriteArrayList; + +//import java.util.List; +//import java.util.ArrayList; + +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.entity.Player; +import org.bukkit.entity.HumanEntity; +import org.bukkit.Material; +import org.bukkit.material.Cauldron; +import org.bukkit.block.Block; +import org.bukkit.inventory.ItemStack; +//import org.bukkit.inventory.meta.ItemMeta; +//import org.bukkit.inventory.meta.PotionMeta; +//import org.bukkit.potion.Potion; +//import org.bukkit.potion.PotionEffect; +//import org.bukkit.potion.PotionEffectType; +import org.bukkit.World; +import org.bukkit.Effect; + +//import org.bukkit.block.BrewingStand; + +import com.dre.brewery.BIngredients; + +import com.dre.brewery.P; + +public class BCauldron { + public static CopyOnWriteArrayList bcauldrons=new CopyOnWriteArrayList(); + + private BIngredients ingredients; + private Block block; + private int state; + + public BCauldron(Block block,Material ingredient){ + P.p.log("aaand we got a fresh cauldron"); + this.block = block; + this.state = 1; + this.ingredients = new BIngredients(); + add(ingredient); + bcauldrons.add(this); + } + + + public void onUpdate(){ + state++;//wie lange es schon kocht + } + + + public void add(Material ingredient){ + ingredients.add(ingredient); + block.getWorld().playEffect(block.getLocation(),Effect.EXTINGUISH,0); + if(state > 1){ + state--; + } + } + + public static boolean ingredientAdd(Block block,Material ingredient){ + if(block.getData() != 0){ + for(BCauldron bcauldron:bcauldrons){ + if(bcauldron.block.equals(block)){ + P.p.log("is existing Cauldron"); + bcauldron.add(ingredient); + return true; + } + } + new BCauldron(block,ingredient); + return true; + } + return false; + } + + public static boolean fill(Player player,Block block){ + for(BCauldron bcauldron:bcauldrons){ + if(bcauldron.block.equals(block)){ + ItemStack potion = bcauldron.ingredients.cook(bcauldron.state); + if(potion != null){ + giveItem(player,potion);//Bukkit Bug, but could also just use deprecated updateInventory() + //player.getInventory().addItem(potion); + //player.getInventory().updateInventory(); + if(block.getData() > 3){ + block.setData((byte)3); + } + block.setData((byte)(block.getData() - 1)); + + if(block.getData() == 0){ + bcauldrons.remove(bcauldron); + } + return true; + } + } + } + return false; + } + + public static void remove(Block block){ + for(BCauldron bcauldron:bcauldrons){ + if(bcauldron.block.equals(block)){ + bcauldrons.remove(bcauldron);//reset to normal cauldron (when refilling it) + } + } + } + + public static void giveItem(final Player player,final ItemStack item){ + P.p.getServer().getScheduler().runTaskLater(P.p, new Runnable() { + public void run() { + player.getInventory().addItem(item); + } + },1L); + } + +} \ No newline at end of file diff --git a/src/com/dre/brewery/BIngredients.java b/src/com/dre/brewery/BIngredients.java new file mode 100644 index 0000000..893d673 --- /dev/null +++ b/src/com/dre/brewery/BIngredients.java @@ -0,0 +1,135 @@ +package com.dre.brewery; + +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; + +import org.bukkit.inventory.ItemStack; +import org.bukkit.Material; +import org.bukkit.potion.Potion; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionType; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.inventory.meta.PotionMeta; + +//import java.util.List; + +import com.dre.brewery.P; +import com.dre.brewery.BRecipe; + + + +public class BIngredients { + public static ArrayList possibleIngredients=new ArrayList(); + public static ArrayList recipes=new ArrayList(); + public static Map cookedNames=new HashMap(); + + private Map ingredients=new HashMap(); + //private int quality = 10; + private String cookedName; + private int cookedTime; + private PotionEffect cookedEffect = (PotionEffectType.CONFUSION).createEffect(200,0);//vorrübergehend + + public BIngredients(){ + } + + public void add(Material ingredient){ + if(ingredients.containsKey(ingredient)){ + int newAmount = ingredients.get(ingredient) + 1; + ingredients.put(ingredient,newAmount); + P.p.log("Now "+newAmount+" of this in here"); + } else { + this.ingredients.put(ingredient,1); + } + } + + public ItemStack cook(int state){ + + for(BRecipe recipe:recipes){ + if(recipe.isCookingOnly()){ + //Nur Kochen + } + } + + if(state == 0){//TESTING sonst 1 + cookedName = "Schlammiger Sud";//not cooked long enough + } else { + for(Material ingredient:ingredients.keySet()){ + if(cookedNames.containsKey(ingredient)){ + P.p.log("Trank aus "+getIngredientsAmount()+" Zutaten"); + if(ingredients.get(ingredient) > (getIngredientsAmount() / 2)){//if more than half of the ingredients is of one kind + cookedName = cookedNames.get(ingredient); + } + } + } + } + if(cookedName == null){ + cookedName = "Undefinierbarer Sud"; + } + + cookedTime = state * 2; + + ItemStack potion = new ItemStack(Material.POTION); + //new Potion(PotionType.getByEffect(PotionEffectType.SLOW)).toItemStack(1); + + PotionMeta potionMeta = (PotionMeta) potion.getItemMeta(); + potionMeta.setDisplayName(cookedName); + //List lore = new ArrayList(); + //lore.add("gesöff vom feinsten"); + //potionMeta.setLore(lore); + potionMeta.clearCustomEffects(); + potionMeta.addCustomEffect(cookedEffect,true);//Add our own effects, overriding existing ones + potion.setItemMeta(potionMeta); + + + return potion; + + + + /* while(i < possibleIngredients.size()){ + //Plain drinks + if(getAmountOf(possibleIngredients.get(i)) > (ingredients.size() / 2)){ + cookedPotion = new Potion(PotionType.getByEffect(PotionEffectType.SLOW)); + P.p.log("making a plain potion"); + return cookedPotion; + } + + //Special Recipies + //Shroom Wodka + if((getAmountOf(possibleIngredients.get(2)) * 3) >= (getAmountOf(badIngredients.get(0)) * 2)){ + if((getAmountOf(possibleIngredients.get(2)) * 2) <= (getAmountOf(badIngredients.get(0)) * 3)){ + //Type Shroom wodka + } + } + + + + i++; + }*/ + } + + private int getIngredientsAmount(){ + int amount = 0; + for(int value:ingredients.values()){ + amount += value; + } + return amount; + } + + + + + + /*private int getAmountOf(Material ingredient){ + int amount = 0; + for(Material bingredient:ingredients){ + if(bingredient == ingredient){ + amount++; + } + } + P.p.log("Amount="+amount); + return amount; + }*/ + +} \ No newline at end of file diff --git a/src/com/dre/brewery/BRecipe.java b/src/com/dre/brewery/BRecipe.java new file mode 100644 index 0000000..fe9743e --- /dev/null +++ b/src/com/dre/brewery/BRecipe.java @@ -0,0 +1,66 @@ +package com.dre.brewery; + +//import java.util.concurrent.CopyOnWriteArrayList; +import java.util.List; +import java.util.Map; +import java.util.HashMap; + +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.Material; +//import org.bukkit.potion.Potion; +//import org.bukkit.potion.PotionType; +//import org.bukkit.potion.PotionEffectType; + +import com.dre.brewery.P; +import com.dre.brewery.BIngredients; + +public class BRecipe { + + private String[] name;// = new String[3]; + private Map ingredients = new HashMap();//material and amount + private int cookingTime;//time to cook in cauldron + private int destillruns;//runs through the brewer + private Material filter;//filter on top of the brewer + private Material destillPotion;//potion to add to custom ones in brewer + private int wood;//type of wood the barrel has to consist of + private int difficulty;//difficulty to brew the potion, how exact the instruction has to be followed + private int alcohol;//Vol% of alcohol in perfect potion + + public BRecipe(ConfigurationSection configSectionRecipes,String recipeId){ + String[] name = configSectionRecipes.getString(recipeId+".name").split("/"); + if(name.length > 2){ + this.name = name; + } else { + this.name = new String[1]; + this.name[0] = name[0]; + } + P.p.log("nameLength="+name.length); + List ingredientsList = configSectionRecipes.getStringList(recipeId+".ingredients"); + for(String item:ingredientsList){ + String[] ingredParts = item.split("/"); + ingredParts[0] = ingredParts[0].toUpperCase(); + this.ingredients.put(Material.getMaterial(ingredParts[0]),P.p.parseInt(ingredParts[1])); + } + this.cookingTime = configSectionRecipes.getInt(recipeId+".cookingtime"); + this.destillruns = configSectionRecipes.getInt(recipeId+".destillruns"); + this.filter = Material.getMaterial(configSectionRecipes.getString(recipeId+".filter")); + this.destillPotion = Material.getMaterial(configSectionRecipes.getString(recipeId+".destillpotion")); + if(destillPotion == null){ + destillPotion = Material.POTION; + } + this.wood = configSectionRecipes.getInt(recipeId+".wood"); + this.difficulty = configSectionRecipes.getInt(recipeId+".difficulty"); + this.alcohol = configSectionRecipes.getInt(recipeId+".alcohol"); + } + + + + public boolean isCookingOnly(){ + if(wood == 0 && destillruns == 0){ + return true; + } + return false; + } + + +} \ No newline at end of file diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index 63fa831..5c2c9c1 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -1,29 +1,133 @@ package com.dre.brewery; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.configuration.ConfigurationSection; +import java.io.File; +import org.apache.commons.lang.math.NumberUtils; import com.dre.brewery.listeners.BlockListener; +import com.dre.brewery.listeners.PlayerListener; +import org.bukkit.event.HandlerList; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import java.io.IOException; + +import org.bukkit.inventory.ItemStack; + +import com.dre.brewery.BIngredients; +import com.dre.brewery.BRecipe; public class P extends JavaPlugin{ public static P p; //Listeners public BlockListener blockListener; + public PlayerListener playerListener; @Override public void onEnable(){ - p = this; + p = this; + + readConfig(); - //Listeners - blockListener = new BlockListener(); + //Listeners + blockListener = new BlockListener(); + playerListener = new PlayerListener(); - p.getServer().getPluginManager().registerEvents(blockListener, p); - + p.getServer().getPluginManager().registerEvents(blockListener, p); + p.getServer().getPluginManager().registerEvents(playerListener, p); + p.getServer().getScheduler().runTaskTimer(p, new BreweryRunnable(), 2400, 2400); + + + this.log(this.getDescription().getName()+" enabled!"); } @Override public void onDisable(){ + + + //Disable listeners + HandlerList.unregisterAll(p); + + //Stop shedulers + p.getServer().getScheduler().cancelTasks(this); + + + File datafile = new File(p.getDataFolder(), "data.yml"); + FileConfiguration configFile = new YamlConfiguration(); + + ItemStack test = new ItemStack(2);//speichert später die custom potions (nicht als itemstack) + configFile.set("ItemStack.Stack", test); + + try { + configFile.save(datafile); + } catch (IOException e) { + e.printStackTrace(); + } + + this.log(this.getDescription().getName()+" disabled!"); } + + public void msg(CommandSender sender,String msg){ + sender.sendMessage(ChatColor.DARK_GREEN+"[Brewery] "+ChatColor.WHITE+msg); + } + + public void log(String msg){ + this.msg(Bukkit.getConsoleSender(), msg); + } + + + public void readConfig(){ + + File file=new File(p.getDataFolder(), "config.yml"); + if(!file.exists()){ + saveDefaultConfig(); + } + FileConfiguration config = getConfig(); + + ConfigurationSection configSection = config.getConfigurationSection("recipes"); + if(configSection != null){ + for(String recipeId:configSection.getKeys(false)){ + BIngredients.recipes.add(new BRecipe(configSection,recipeId)); + } + } + + configSection = config.getConfigurationSection("cooked"); + if(configSection != null){ + for(String ingredient:configSection.getKeys(false)){ + BIngredients.cookedNames.put(Material.getMaterial(ingredient.toUpperCase()),(configSection.getString(ingredient))); + BIngredients.possibleIngredients.add(Material.getMaterial(ingredient.toUpperCase())); + } + } + + } + + public int parseInt(String string){ + return NumberUtils.toInt(string, 0); + } + + + + public class BreweryRunnable implements Runnable { + + public BreweryRunnable() { + } + + @Override + public void run() { + p.log("Update"); + for(BCauldron cauldron:BCauldron.bcauldrons){ + cauldron.onUpdate();//runs every 2 min to update cooking time + } + } + + } + } diff --git a/src/com/dre/brewery/listeners/PlayerListener.java b/src/com/dre/brewery/listeners/PlayerListener.java new file mode 100644 index 0000000..0ecbfa3 --- /dev/null +++ b/src/com/dre/brewery/listeners/PlayerListener.java @@ -0,0 +1,124 @@ +package com.dre.brewery.listeners; + +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerItemConsumeEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.BrewEvent; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.inventory.InventoryType.SlotType; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.BrewingStand; + +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +import com.dre.brewery.BCauldron; +import com.dre.brewery.BIngredients; + +import com.dre.brewery.P; + +public class PlayerListener implements Listener{ + @EventHandler(priority = EventPriority.HIGH) + public void onPlayerInteract(PlayerInteractEvent event){ + Block clickedBlock = event.getClickedBlock(); + + if(clickedBlock!=null){ + if(event.getAction() == Action.RIGHT_CLICK_BLOCK){ + if(clickedBlock.getType() == Material.CAULDRON){ + if(clickedBlock.getRelative(BlockFace.DOWN).getType() == Material.FIRE || + clickedBlock.getRelative(BlockFace.DOWN).getType() == Material.STATIONARY_LAVA){ + P.p.log("heat underneath cauldron"); + Material materialInHand = event.getMaterial(); + Player player = event.getPlayer(); + ItemStack item = event.getItem(); + + if(BIngredients.possibleIngredients.contains(materialInHand)){//add ingredient to cauldron that meet the previous contitions + if(BCauldron.ingredientAdd(clickedBlock,materialInHand)){ + if(item.getAmount() > 1){ + item.setAmount(item.getAmount() -1); + } else { + player.setItemInHand(new ItemStack(0)); + } + } + } else if(materialInHand == Material.GLASS_BOTTLE){//fill a glass bottle with potion + if(BCauldron.fill(player,clickedBlock)){ + P.p.log("custom potion done"); + event.setCancelled(true); + if(item.getAmount() > 1){ + item.setAmount(item.getAmount() -1); + } else { + player.setItemInHand(new ItemStack(0)); + } + } + } else if(materialInHand == Material.WATER_BUCKET){//reset cauldron when refilling to prevent unlimited source of potions + if(clickedBlock.getData() != 0){ + if(clickedBlock.getData() < 3){ + BCauldron.remove(clickedBlock); + } + } + } + } + } + } + } + + } + + +//TESTING! + + @EventHandler(priority = EventPriority.HIGH) + public void onBrew(BrewEvent event){ + P.p.log("Brewing"); + int slot = 0; + while(slot < 3){ + ItemStack item = event.getContents().getItem(slot); + if(item != null){ + if(item.getType() == Material.POTION){ + if(item.hasItemMeta()){ + if(item.getItemMeta().getDisplayName().equals("Alkohol")){//TESTING only! neew a way to store get UID + P.p.log("is Alcohol"); + event.getContents().setItem(slot,new ItemStack(2)); + if(!event.isCancelled()){ + event.setCancelled(true); + } + } else { + event.getContents().setItem(slot,new ItemStack(Material.GLASS_BOTTLE)); + } + } else { + event.getContents().setItem(slot,new ItemStack(Material.GLASS_BOTTLE)); + } + } + } + slot++; + } + + } + + @EventHandler(priority = EventPriority.HIGH) + public void onPlayerItemConsume(PlayerItemConsumeEvent event){ + if(event.getItem() != null){ + if(event.getItem().getType() == Material.POTION){ + P.p.log("consuming a potion"); + if(event.getItem().hasItemMeta()){ + PotionMeta potionMeta = ((PotionMeta) event.getItem().getItemMeta()); + if(potionMeta.hasCustomEffect(PotionEffectType.CONFUSION)){ + P.p.log("hasConfusion"); + } + } + } + } + + + } + +} \ No newline at end of file