Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/com/untamedears/PrisonPearl/FeedLog.java
Original file line number Diff line number Diff line change
@@ -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<String, String[]> feedLogHash;
private ArrayList<String> 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<String, String[]>();
FileInputStream fis;
fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line;
flogEvents = new ArrayList<String>();
while ((line = br.readLine()) != null) {
if (line.length() > 1) {
flogEvents.add(line);
}
}
fis.close();
}

private boolean saveLog(String log){

return true;
}
}
168 changes: 163 additions & 5 deletions src/com/untamedears/PrisonPearl/PrisonPearlStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@
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;
import org.bukkit.Location;
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<Short, PrisonPearl> pearls_byid;
private Map<String, PrisonPearl> pearls_byimprisoned;
private final Map<Short, PrisonPearl> pearls_byid;
private final Map<String, PrisonPearl> pearls_byimprisoned;
private short nextid;

private boolean dirty;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -129,11 +161,137 @@ 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);
}

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<String> iNames = new ArrayList<String>();
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<Short,PrisonPearl> map = new ConcurrentHashMap<Short,PrisonPearl>(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 "";
}
}