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

470 lines
12 KiB
Java
Raw Normal View History

package com.dre.brewery;
2013-04-28 23:57:41 +02:00
import java.util.concurrent.CopyOnWriteArrayList;
2013-04-30 21:41:16 +02:00
import java.util.Map;
2013-04-28 23:57:41 +02:00
import org.bukkit.entity.Player;
import org.bukkit.block.Block;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
2013-04-30 21:41:16 +02:00
import org.bukkit.configuration.ConfigurationSection;
2013-04-28 23:57:41 +02:00
public class Barrel {
public static CopyOnWriteArrayList<Barrel> barrels = new CopyOnWriteArrayList<Barrel>();
2013-04-28 23:57:41 +02:00
// private CopyOnWriteArrayList<Brew> brews = new
// CopyOnWriteArrayList<Brew>();
private Block spigot;
private Inventory inventory;
private float time;
2013-04-28 23:57:41 +02:00
public Barrel(Block spigot) {
2013-04-28 23:57:41 +02:00
this.spigot = spigot;
}
// load from file
public Barrel(Block spigot, Map<String, Object> items, float time) {
2013-04-30 21:41:16 +02:00
this.spigot = spigot;
if (isLarge()) {
2013-04-30 21:41:16 +02:00
this.inventory = org.bukkit.Bukkit.createInventory(null, 27, "Fass");
} else {
this.inventory = org.bukkit.Bukkit.createInventory(null, 9, "Fass");
}
for (String slot : items.keySet()) {
if (items.get(slot) instanceof ItemStack) {
this.inventory.setItem(P.p.parseInt(slot), (ItemStack) items.get(slot));
2013-04-30 21:41:16 +02:00
}
}
this.time = time;
barrels.add(this);
}
// load from file (without inventory)
public Barrel(Block spigot, float time) {
2013-04-30 21:41:16 +02:00
this.spigot = spigot;
if (isLarge()) {
2013-04-30 21:41:16 +02:00
this.inventory = org.bukkit.Bukkit.createInventory(null, 27, "Fass");
} else {
this.inventory = org.bukkit.Bukkit.createInventory(null, 9, "Fass");
}
this.time = time;
barrels.add(this);
}
public static void onUpdate() {
2013-04-28 23:57:41 +02:00
Block broken;
for (Barrel barrel : barrels) {
2013-04-28 23:57:41 +02:00
broken = getBrokenBlock(barrel.spigot);
// remove the barrel if it was destroyed
if (broken != null) {
2013-04-28 23:57:41 +02:00
barrel.remove(broken);
} else {
// Minecraft day is 20 min, so add 1/20 to the time every minute
barrel.time += 1.0 / 20.0;
2013-04-28 23:57:41 +02:00
}
}
}
// player opens the barrel
public void open(Player player) {
if (inventory == null) {
if (isLarge()) {
2013-04-29 09:50:19 +02:00
inventory = org.bukkit.Bukkit.createInventory(null, 27, "Fass");
} else {
inventory = org.bukkit.Bukkit.createInventory(null, 9, "Fass");
}
2013-04-28 23:57:41 +02:00
} else {
// if nobody has the inventory opened
if (inventory.getViewers().isEmpty()) {
// if inventory contains potions
if (inventory.contains(373)) {
for (ItemStack item : inventory.getContents()) {
if (item != null) {
if (item.getTypeId() == 373) {
if (item.hasItemMeta()) {
Brew.age(item, time, getWood());
2013-04-28 23:57:41 +02:00
}
}
}
}
}
}
}
// reset barreltime, potions have new age
2013-04-28 23:57:41 +02:00
time = 0;
player.openInventory(inventory);
}
public static Barrel get(Block spigot) {
// convert spigot if neccessary
2013-04-29 09:50:19 +02:00
spigot = getSpigotOfSign(spigot);
for (Barrel barrel : barrels) {
if (barrel.spigot.equals(spigot)) {
2013-04-28 23:57:41 +02:00
return barrel;
}
}
return null;
}
// creates a new Barrel out of a sign
public static boolean create(Block spigot) {
2013-04-29 09:50:19 +02:00
spigot = getSpigotOfSign(spigot);
if (getBrokenBlock(spigot) == null) {
if (get(spigot) == null) {
2013-04-28 23:57:41 +02:00
barrels.add(new Barrel(spigot));
return true;
}
}
return false;
}
// removes a barrel, throwing included potions to the ground
public void remove(Block broken) {
if (inventory != null) {
2013-04-28 23:57:41 +02:00
ItemStack[] items = inventory.getContents();
for (ItemStack item : items) {
if (item != null) {
if (item.getTypeId() == 373) {
// Brew before throwing
Brew.age(item, time, getWood());
2013-04-28 23:57:41 +02:00
}
// "broken" is the block that destroyed, throw them there!
if (broken != null) {
2013-05-09 21:47:58 +02:00
broken.getWorld().dropItem(broken.getLocation(), item);
2013-04-28 23:57:41 +02:00
} else {
2013-05-09 21:47:58 +02:00
spigot.getWorld().dropItem(spigot.getLocation(), item);
2013-04-28 23:57:41 +02:00
}
}
}
}
barrels.remove(this);
}
2013-05-09 21:47:58 +02:00
//unloads barrels that are in a unloading world
public static void onUnload(String name) {
for (Barrel barrel : barrels) {
if (barrel.spigot.getWorld().getName().equals(name)) {
barrels.remove(barrel);
}
}
}
// Saves all data
2013-05-10 00:01:28 +02:00
public static void save(ConfigurationSection config, ConfigurationSection oldData) {
2013-05-28 16:25:06 +02:00
if (!barrels.isEmpty()) {
int id = 0;
for (Barrel barrel : barrels) {
2013-06-05 19:44:30 +02:00
String worldName = barrel.spigot.getWorld().getName();
String prefix = null;
if (worldName.startsWith("DXL_")) {
prefix = P.p.getDxlName(worldName) + "." + id;
} else {
prefix = barrel.spigot.getWorld().getUID().toString() + "." + id;
}
2013-05-28 16:25:06 +02:00
// block: x/y/z
config.set(prefix + ".spigot", barrel.spigot.getX() + "/" + barrel.spigot.getY() + "/" + barrel.spigot.getZ());
if (barrel.inventory != null) {
int slot = 0;
ItemStack item = null;
ConfigurationSection invConfig = null;
while (slot < barrel.inventory.getSize()) {
item = barrel.inventory.getItem(slot);
if (item != null) {
if (invConfig == null) {
if (barrel.time != 0) {
config.set(prefix + ".time", barrel.time);
}
invConfig = config.createSection(prefix + ".inv");
2013-05-09 21:47:58 +02:00
}
2013-05-28 16:25:06 +02:00
// ItemStacks are configurationSerializeable, makes them
// really easy to save
invConfig.set(slot + "", item);
2013-04-30 21:41:16 +02:00
}
2013-05-28 16:25:06 +02:00
slot++;
}
2013-04-30 21:41:16 +02:00
}
2013-05-28 16:25:06 +02:00
id++;
}
2013-04-30 21:41:16 +02:00
}
2013-05-09 21:47:58 +02:00
// also save barrels that are not loaded
2013-05-10 00:01:28 +02:00
if (oldData != null){
for (String uuid : oldData.getKeys(false)) {
if (!config.contains(uuid)) {
config.set(uuid, oldData.get(uuid));
}
2013-05-09 21:47:58 +02:00
}
}
2013-04-30 21:41:16 +02:00
}
// direction of the barrel from the spigot
public static int getDirection(Block spigot) {
int direction = 0;// 1=x+ 2=x- 3=z+ 4=z-
int typeId = spigot.getRelative(0, 0, 1).getTypeId();
if (typeId == 5 || typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) {
2013-04-28 23:57:41 +02:00
direction = 3;
}
typeId = spigot.getRelative(0, 0, -1).getTypeId();
if (typeId == 5 || typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) {
if (direction == 0) {
2013-04-28 23:57:41 +02:00
direction = 4;
} else {
return 0;
}
}
typeId = spigot.getRelative(1, 0, 0).getTypeId();
if (typeId == 5 || typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) {
if (direction == 0) {
2013-04-28 23:57:41 +02:00
direction = 1;
} else {
return 0;
}
}
typeId = spigot.getRelative(-1, 0, 0).getTypeId();
if (typeId == 5 || typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) {
if (direction == 0) {
2013-04-28 23:57:41 +02:00
direction = 2;
} else {
return 0;
}
}
return direction;
}
// is this a Large barrel?
public boolean isLarge() {
if (spigot.getTypeId() == 63 || spigot.getTypeId() == 68) {
2013-04-29 09:50:19 +02:00
return false;
}
return true;
}
// true for small barrels
public static boolean isSign(Block spigot) {
if (spigot.getTypeId() == 63 || spigot.getTypeId() == 68) {
2013-04-29 09:50:19 +02:00
return true;
}
return false;
}
// woodtype of the block the spigot is attached to
public byte getWood() {
int direction = getDirection(this.spigot);// 1=x+ 2=x- 3=z+ 4=z-
2013-04-28 23:57:41 +02:00
Block wood = null;
if (direction == 0) {
2013-04-28 23:57:41 +02:00
return 0;
} else if (direction == 1) {
wood = this.spigot.getRelative(1, 0, 0);
} else if (direction == 2) {
wood = this.spigot.getRelative(-1, 0, 0);
} else if (direction == 3) {
wood = this.spigot.getRelative(0, 0, 1);
2013-04-28 23:57:41 +02:00
} else {
wood = this.spigot.getRelative(0, 0, -1);
2013-04-28 23:57:41 +02:00
}
if (wood.getTypeId() == 5) {
2013-04-28 23:57:41 +02:00
return wood.getData();
}
if (wood.getTypeId() == 53) {
2013-04-29 09:50:19 +02:00
return 0x0;
}
if (wood.getTypeId() == 134) {
2013-04-29 09:50:19 +02:00
return 0x1;
}
if (wood.getTypeId() == 135) {
2013-04-29 09:50:19 +02:00
return 0x2;
}
if (wood.getTypeId() == 136) {
2013-04-29 09:50:19 +02:00
return 0x3;
}
2013-04-28 23:57:41 +02:00
return 0;
}
// returns the fence above/below a block, itself if there is none
public static Block getSpigotOfSign(Block block) {
2013-04-30 21:41:16 +02:00
int y = -2;
while (y <= 1) {
// Fence and Netherfence
if (block.getRelative(0, y, 0).getTypeId() == 85 || block.getRelative(0, y, 0).getTypeId() == 113) {
return (block.getRelative(0, y, 0));
2013-04-30 21:41:16 +02:00
}
y++;
}
return block;
}
// returns null if Barrel is correctly placed; the block that is missing
// when not
// the barrel needs to be formed correctly
public static Block getBrokenBlock(Block spigot) {
2013-06-30 23:41:37 +02:00
if (spigot.getChunk().isLoaded()) {
spigot = getSpigotOfSign(spigot);
if (isSign(spigot)) {
2013-04-29 09:50:19 +02:00
return checkSBarrel(spigot);
2013-06-30 23:41:37 +02:00
} else {
2013-04-29 09:50:19 +02:00
return checkLBarrel(spigot);
2013-06-30 23:41:37 +02:00
}
2013-04-29 09:50:19 +02:00
}
2013-06-30 23:41:37 +02:00
return null;
2013-04-29 09:50:19 +02:00
}
public static Block checkSBarrel(Block spigot) {
int direction = getDirection(spigot);// 1=x+ 2=x- 3=z+ 4=z-
if (direction == 0) {
2013-04-28 23:57:41 +02:00
return spigot;
}
2013-04-29 09:50:19 +02:00
int startX = 0;
int startZ = 0;
int endX;
int endZ;
if (direction == 1) {
2013-04-29 09:50:19 +02:00
startX = 1;
endX = startX + 1;
startZ = -1;
endZ = 0;
} else if (direction == 2) {
2013-04-29 09:50:19 +02:00
startX = -2;
endX = startX + 1;
startZ = 0;
endZ = 1;
} else if (direction == 3) {
2013-04-29 09:50:19 +02:00
startX = 0;
endX = 1;
startZ = 1;
endZ = startZ + 1;
} else {
startX = -1;
endX = 0;
startZ = -2;
endZ = startZ + 1;
}
int typeId;
int x = startX;
int y = 0;
int z = startZ;
// P.p.log("startX="+startX+" startZ="+startZ+" endX="+endX+" endZ="+endZ+" direction="+direction);
while (y <= 1) {
while (x <= endX) {
while (z <= endZ) {
typeId = spigot.getRelative(x, y, z).getTypeId();
if (typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) {
if (y == 0) {
// stairs have to be upside down
if (spigot.getRelative(x, y, z).getData() < 4) {
return spigot.getRelative(x, y, z);
2013-04-29 09:50:19 +02:00
}
}
z++;
continue;
} else {
return spigot.getRelative(x, y, z);
2013-04-29 09:50:19 +02:00
}
}
z = startZ;
x++;
}
z = startZ;
x = startX;
y++;
}
return null;
}
public static Block checkLBarrel(Block spigot) {
int direction = getDirection(spigot);// 1=x+ 2=x- 3=z+ 4=z-
if (direction == 0) {
2013-04-28 23:57:41 +02:00
return spigot;
}
int startX = 0;
int startZ = 0;
int endX;
int endZ;
if (direction == 1) {
2013-04-28 23:57:41 +02:00
startX = 1;
endX = startX + 3;
startZ = -1;
endZ = 1;
} else if (direction == 2) {
2013-04-28 23:57:41 +02:00
startX = -4;
endX = startX + 3;
startZ = -1;
endZ = 1;
} else if (direction == 3) {
2013-04-28 23:57:41 +02:00
startX = -1;
endX = 1;
startZ = 1;
endZ = startZ + 3;
} else {
startX = -1;
endX = 1;
startZ = -4;
endZ = startZ + 3;
}
int typeId;
int x = startX;
int y = 0;
int z = startZ;
// P.p.log("startX="+startX+" startZ="+startZ+" endX="+endX+" endZ="+endZ+" direction="+direction);
while (y <= 2) {
while (x <= endX) {
while (z <= endZ) {
typeId = spigot.getRelative(x, y, z).getTypeId();
// spigot.getRelative(x,y,z).setTypeId(1);
if (direction == 1 || direction == 2) {
if (y == 1 && z == 0) {
if (x == -2 || x == -3 || x == 2 || x == 3) {
2013-04-28 23:57:41 +02:00
z++;
continue;
} else if (x == -1 || x == -4 || x == 1 || x == 4) {
if (typeId != 0) {
2013-04-28 23:57:41 +02:00
z++;
continue;
}
}
}
} else {
if (y == 1 && x == 0) {
if (z == -2 || z == -3 || z == 2 || z == 3) {
2013-04-28 23:57:41 +02:00
z++;
continue;
} else if (z == -1 || z == -4 || z == 1 || z == 4) {
if (typeId != 0) {
2013-04-28 23:57:41 +02:00
z++;
continue;
}
}
}
}
if (typeId == 5 || typeId == 53 || typeId == 134 || typeId == 135 || typeId == 136) {
2013-04-28 23:57:41 +02:00
z++;
continue;
} else {
return spigot.getRelative(x, y, z);
2013-04-28 23:57:41 +02:00
}
}
z = startZ;
x++;
}
z = startZ;
x = startX;
y++;
}
return null;
}
}