diff --git a/src/com/untamedears/PrisonPearl/FeedLog.java b/src/com/untamedears/PrisonPearl/FeedLog.java new file mode 100644 index 0000000..bf4f7d0 --- /dev/null +++ b/src/com/untamedears/PrisonPearl/FeedLog.java @@ -0,0 +1,61 @@ +package com.untamedears.PrisonPearl; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Set; + +import org.bukkit.Bukkit; + +class FeedLog { + private HashMap feedLogHash; + private ArrayList flogEvents; + private boolean initialised = false; + + public FeedLog() { + } + + public String[] load(File file) { + if(!initialised){ + try { + loadFeedLog(file); + initialised = true; + String [] flogString = flogEvents.toArray(new String[flogEvents.size()]); + return flogString; + } catch (IOException e) { + e.printStackTrace(); + Bukkit.getLogger().info("Failed to load file!"); + initialised = false; + return new String[0]; + } + }else{ + String [] flogString = flogEvents.toArray(new String[flogEvents.size()]); + return flogString; + } + } + + private void loadFeedLog(File file) throws IOException { + feedLogHash = new HashMap(); + FileInputStream fis; + fis = new FileInputStream(file); + BufferedReader br = new BufferedReader(new InputStreamReader(fis)); + String line; + flogEvents = new ArrayList(); + while ((line = br.readLine()) != null) { + if (line.length() > 1) { + flogEvents.add(line); + } + } + fis.close(); + } + + private boolean saveLog(String log){ + + return true; + } +} diff --git a/src/com/untamedears/PrisonPearl/PrisonPearlStorage.java b/src/com/untamedears/PrisonPearl/PrisonPearlStorage.java index dea50e6..a53c867 100644 --- a/src/com/untamedears/PrisonPearl/PrisonPearlStorage.java +++ b/src/com/untamedears/PrisonPearl/PrisonPearlStorage.java @@ -8,7 +8,10 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.util.ArrayList; import java.util.HashMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.List; import java.util.Map; import org.bukkit.Bukkit; @@ -16,10 +19,19 @@ import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.BrewingStand; +import org.bukkit.block.Chest; +import org.bukkit.block.Dispenser; +import org.bukkit.block.Furnace; +import org.bukkit.inventory.DoubleChestInventory; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; public class PrisonPearlStorage implements SaveLoad { - private Map pearls_byid; - private Map pearls_byimprisoned; + private final Map pearls_byid; + private final Map pearls_byimprisoned; private short nextid; private boolean dirty; @@ -50,7 +62,14 @@ public void load(File file) throws IOException { short id = Short.parseShort(parts[0]); String imprisoned = parts[1]; Location loc = new Location(Bukkit.getWorld(parts[2]), Integer.parseInt(parts[3]), Integer.parseInt(parts[4]), Integer.parseInt(parts[5])); - PrisonPearl pp = PrisonPearl.makeFromLocation(id, imprisoned, loc); + PrisonPearl pp = PrisonPearl.makeFromLocation(id, imprisoned, loc); + if (parts.length != 6) { + String motd = ""; + for (int i = 6; i < parts.length; i++) { + motd = motd.concat(parts[i] + " "); + } + pp.setMotd(motd); + } if (pp == null) { System.err.println("PrisonPearl for " + imprisoned + " didn't validate, so is now set free. Chunks and/or prisonpearls.txt are corrupt"); continue; @@ -75,8 +94,21 @@ public void save(File file) throws IOException { continue; Location loc = pp.getLocation(); - br.append(pp.getID() + " " + pp.getImprisonedName() + " " + loc.getWorld().getName() + " " + loc.getBlockX() + " " + loc.getBlockY() + " " + loc.getBlockZ() + "\n"); - } + br.append(String.valueOf(pp.getID())); + br.append(" "); + br.append(pp.getImprisonedName()); + br.append(" "); + br.append(loc.getWorld().getName()); + br.append(" "); + br.append(String.valueOf(loc.getBlockX())); + br.append(" "); + br.append(String.valueOf(loc.getBlockY())); + br.append(" "); + br.append(String.valueOf(loc.getBlockZ())); + br.append(" "); + br.append(pp.getMotd()); + br.append("\n"); + } br.flush(); fos.close(); @@ -129,6 +161,10 @@ public PrisonPearl getByImprisoned(Player player) { return pearls_byimprisoned.get(player.getName()); } + public Integer getPearlCount(){ + return pearls_byimprisoned.size(); + } + boolean isImprisoned(String name) { return pearls_byimprisoned.containsKey(name); } @@ -136,4 +172,126 @@ boolean isImprisoned(String name) { boolean isImprisoned(Player player) { return pearls_byimprisoned.containsKey(player.getName()); } + + public Integer getImprisonedCount(String[] names) { + Integer count = 0; + for (String name : names) { + if (pearls_byimprisoned.containsKey(name)) { + count++; + } + } + return count; + } + + public String[] getImprisonedNames(String[] names) { + List iNames = new ArrayList(); + for (String name : names) { + if (pearls_byimprisoned.containsKey(name)) { + iNames.add(name); + } + } + int count = iNames.size(); + String[] results = new String[count]; + for (int i = 0; i < count; i++) { + results[i] = iNames.get(i); + } + return results; + } + + public String feedPearls(PrisonPearlManager pearlman){ + String message = ""; + String log = ""; + ConcurrentHashMap map = new ConcurrentHashMap(pearls_byid); + + int pearlsfed = 0; + int coalfed = 0; + int freedpearls = 0; + for (PrisonPearl pp : map.values()) { + + BlockState inherentViolence = pp.getHolderBlockState(); + Material mat = inherentViolence.getType(); + + Inventory inv[] = new Inventory[2]; + inv[0] = inv[1] = null; + if (inherentViolence == null) + { + continue; + } + else + { + switch(mat) + { + case FURNACE: + inv[0] = ((Furnace)inherentViolence).getInventory(); + break; + case DISPENSER: + inv[0] = ((Dispenser)inherentViolence).getInventory(); + break; + case BREWING_STAND: + inv[0] = ((BrewingStand)inherentViolence).getInventory(); + break; + default: + if (mat == Material.CHEST || mat == Material.LOCKED_CHEST){ + Chest c = ((Chest)inherentViolence); + DoubleChestInventory dblInv = null; + try{ + dblInv = (DoubleChestInventory)c.getInventory(); + inv[0] = dblInv.getLeftSide(); + inv[1] = dblInv.getRightSide(); + } + catch(Exception e){ + inv[0] = (Inventory)c.getInventory(); + } + }else{ + pearlman.freePearl(pp); + log+="\n freed:"+pp.getImprisonedName()+",reason:"+"badcontainer"; + freedpearls++; + } + break; + } + } + + message = message + "Pearl #" + pp.getID() + ",Name: " + pp.getImprisonedName() + " in a " + pp.getHolderBlockState().getType(); + int requirementSize = 8; + ItemStack requirement = new ItemStack(Material.COAL, requirementSize); + if(inv[0].containsAtLeast(requirement,requirementSize)) + { + message = message + "\n Chest contains enough purestrain coal."; + inv[0].removeItem(requirement); + pearlsfed++; + coalfed += requirementSize; + log+="\n fed:" + pp.getImprisonedName() + ",location:"+ pp.describeLocation(); + } + else if(inv[1] != null && inv[1].containsAtLeast(requirement,requirementSize)){ + message = message + "\n Chest contains enough purestrain coal."; + inv[1].removeItem(requirement); + pearlsfed++; + coalfed += requirementSize; + log+="\n fed:" + pp.getImprisonedName() + ",location:"+ pp.describeLocation(); + } + else { + message = message + "\n Chest does not contain enough purestrain coal."; + pearlman.freePearl(pp); + log+="\n freed:"+pp.getImprisonedName()+",reason:"+"nocoal"+",location:"+pp.describeLocation(); + freedpearls++; + } + } + message = message + "\n Feeding Complete. " + pearlsfed + " were fed " + coalfed + " coal. " + freedpearls + " players were freed."; + return message; + } + + public String restorePearls(PrisonPearlManager pearlman, String config){ + //Read pearl config + + //For each entry + + //Create pearl for player + + //Place in chest + + //Check imprisonment status + + //Report restoration + return ""; + } }