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

110 lines
2.8 KiB
Java
Raw Normal View History

package com.dre.brewery;
import java.util.concurrent.CopyOnWriteArrayList;
import org.bukkit.entity.Player;
import org.bukkit.Material;
import org.bukkit.block.Block;
2013-04-28 23:57:41 +02:00
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack;
import org.bukkit.Effect;
import com.dre.brewery.BIngredients;
public class BCauldron {
public static CopyOnWriteArrayList<BCauldron> bcauldrons=new CopyOnWriteArrayList<BCauldron>();
private BIngredients ingredients;
private Block block;
private int state;
public BCauldron(Block block,Material ingredient){
this.block = block;
this.state = 1;
this.ingredients = new BIngredients();
add(ingredient);
bcauldrons.add(this);
}
public void onUpdate(){
2013-04-28 23:57:41 +02:00
//Check if fire still alive
if(block.getRelative(BlockFace.DOWN).getType() == Material.FIRE ||
block.getRelative(BlockFace.DOWN).getType() == Material.STATIONARY_LAVA ||
block.getRelative(BlockFace.DOWN).getType() == Material.LAVA){
//add a minute to cooking time
state++;
}
}
2013-04-28 23:57:41 +02:00
//add an ingredient to the cauldron
public void add(Material ingredient){
ingredients.add(ingredient);
block.getWorld().playEffect(block.getLocation(),Effect.EXTINGUISH,0);
if(state > 1){
state--;
}
}
2013-04-28 23:57:41 +02:00
//get cauldron from block and add given ingredient
public static boolean ingredientAdd(Block block,Material ingredient){
2013-04-28 23:57:41 +02:00
//if not empty
if(block.getData() != 0){
for(BCauldron bcauldron:bcauldrons){
if(bcauldron.block.equals(block)){
bcauldron.add(ingredient);
return true;
}
}
new BCauldron(block,ingredient);
return true;
}
return false;
}
2013-04-28 23:57:41 +02:00
//fills players bottle with cooked brew
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){
2013-04-28 23:57:41 +02:00
//Bukkit Bug, inventory not updating while in event so this will delay the give
//but could also just use deprecated updateInventory()
giveItem(player,potion);
//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;
}
2013-04-28 23:57:41 +02:00
//reset to normal cauldron
public static void remove(Block block){
for(BCauldron bcauldron:bcauldrons){
if(bcauldron.block.equals(block)){
2013-04-28 23:57:41 +02:00
bcauldrons.remove(bcauldron);
}
}
}
2013-04-28 23:57:41 +02:00
//bukkit bug not updating the inventory while executing event, have to schedule the give
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);
}
}