added rename method to the gate class

This commit is contained in:
Tobias Ottenweller 2012-02-28 13:40:39 +01:00
parent 12295ac7b8
commit 0fe721b8c0

View File

@ -16,12 +16,12 @@ import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.mcteam.ancientgates.gson.reflect.TypeToken; import org.mcteam.ancientgates.gson.reflect.TypeToken;
import org.mcteam.ancientgates.util.DiscUtil; import org.mcteam.ancientgates.util.DiscUtil;
import org.mcteam.ancientgates.util.FloodUtil; import org.mcteam.ancientgates.util.FloodUtil;
public class Gate { public class Gate
{
private static transient TreeMap<String, Gate> instances = new TreeMap<String, Gate>(String.CASE_INSENSITIVE_ORDER); private static transient TreeMap<String, Gate> instances = new TreeMap<String, Gate>(String.CASE_INSENSITIVE_ORDER);
private static transient File file = new File(Plugin.instance.getDataFolder(), "gates.json"); private static transient File file = new File(Plugin.instance.getDataFolder(), "gates.json");
@ -31,51 +31,69 @@ public class Gate {
private Integer[][] gateBlocks; private Integer[][] gateBlocks;
public Gate() {
public Gate()
{
} }
// -------------------------------------------- // // -------------------------------------------- //
// Getters And Setters // Getters And Setters
// -------------------------------------------- // // -------------------------------------------- //
public void setId(String id) { public void setId(String id)
{
this.id = id; this.id = id;
} }
public String getId() {
public String getId()
{
return id; return id;
} }
public void setFrom(Location from) {
public void setFrom(Location from)
{
this.from = from; this.from = from;
} }
public Location getFrom() {
public Location getFrom()
{
return from; return from;
} }
public void setTo(Location to) { public void setTo(Location to)
{
this.to = to; this.to = to;
} }
public Location getTo() {
public Location getTo()
{
return to; return to;
} }
public Integer[][] getGateBlocks() {
public Integer[][] getGateBlocks()
{
return gateBlocks; return gateBlocks;
} }
public void setGateBlocks(Set<Block> gateBlocks) {
public void setGateBlocks(Set<Block> gateBlocks)
{
if (gateBlocks == null) if (gateBlocks == null)
return; return;
this.gateBlocks = new Integer[gateBlocks.size()][3]; this.gateBlocks = new Integer[gateBlocks.size()][3];
int blockcount = 0; int blockcount = 0;
for (Block b: gateBlocks) { for (Block b : gateBlocks)
if (b != null) { {
if (b != null)
{
this.gateBlocks[blockcount][0] = b.getX(); this.gateBlocks[blockcount][0] = b.getX();
this.gateBlocks[blockcount][1] = b.getY(); this.gateBlocks[blockcount][1] = b.getY();
this.gateBlocks[blockcount][2] = b.getZ(); this.gateBlocks[blockcount][2] = b.getZ();
@ -84,32 +102,32 @@ public class Gate {
} }
} }
//----------------------------------------------// //----------------------------------------------//
// The Open And Close Methods // The Open And Close Methods
//----------------------------------------------// //----------------------------------------------//
public boolean open() { public boolean open()
{
Set<Block> blocks = FloodUtil.getGateFrameBlocks(from.getBlock()); Set<Block> blocks = FloodUtil.getGateFrameBlocks(from.getBlock());
if (blocks == null) { if (blocks == null)
return false; return false;
}
// Uncomment lines below to have the old Portal open functionality back. // Uncomment lines below to have the old Portal open functionality back.
// This is not to do an effect // This is not to do an effect
// It is to stop portalblocks from destroyingthemself as they cant rely on non created blocks :P // It is to stop portal blocks from destroying themself as they cant rely on non created blocks :P
for (Block block : blocks) { for (Block block : blocks)
block.setType(Material.GLOWSTONE); block.setType(Material.GLOWSTONE);
}
for (Block block : blocks) { for (Block block : blocks)
block.setType(Material.PORTAL); block.setType(Material.PORTAL);
}
return true; return true;
} }
public void close() public void close()
{ {
if (from != null) if (from != null)
@ -121,40 +139,64 @@ public class Gate {
} }
} }
//----------------------------------------------// //----------------------------------------------//
// Persistance and entity management // Persistance and entity management
//----------------------------------------------// //----------------------------------------------//
public static Gate get(String id) { public static Gate get(String id)
{
return instances.get(id); return instances.get(id);
} }
public static boolean exists(String id) {
public static boolean exists(String id)
{
return instances.containsKey(id); return instances.containsKey(id);
} }
public static Gate create(String id) {
public static Gate create(String id)
{
Gate gate = new Gate(); Gate gate = new Gate();
gate.id = id; gate.id = id;
instances.put(gate.id, gate); instances.put(gate.id, gate);
Plugin.log("created new gate "+gate.id); Plugin.log("created new gate " + gate.id);
//faction.save(); //faction.save();
return gate; return gate;
} }
public static void delete(String id) {
public static void rename(Gate gate, String newId)
{
delete(gate.id);
gate.setId(newId);
instances.put(gate.id, gate);
}
public static void delete(String id)
{
// Remove the faction // Remove the faction
instances.remove(id); instances.remove(id);
} }
public static boolean save() {
try { public static boolean save()
{
try
{
DiscUtil.write(file, Plugin.gson.toJson(instances)); DiscUtil.write(file, Plugin.gson.toJson(instances));
} catch (IOException e) { }
catch (IOException e)
{
Plugin.log("Failed to save the gates to disk due to I/O exception."); Plugin.log("Failed to save the gates to disk due to I/O exception.");
e.printStackTrace(); e.printStackTrace();
return false; return false;
} catch (NullPointerException e) { }
catch (NullPointerException e)
{
Plugin.log("Failed to save the gates to disk due to NPE."); Plugin.log("Failed to save the gates to disk due to NPE.");
e.printStackTrace(); e.printStackTrace();
return false; return false;
@ -163,7 +205,9 @@ public class Gate {
return true; return true;
} }
public static boolean load() {
public static boolean load()
{
Plugin.log("Loading gates from disk"); Plugin.log("Loading gates from disk");
if ( ! file.exists()) { if ( ! file.exists()) {
Plugin.log("No gates to load from disk. Creating new file."); Plugin.log("No gates to load from disk. Creating new file.");
@ -171,12 +215,15 @@ public class Gate {
return true; return true;
} }
try { try
{
Type type = new TypeToken<Map<String, Gate>>(){}.getType(); Type type = new TypeToken<Map<String, Gate>>(){}.getType();
Map<String, Gate> instancesFromFile = Plugin.gson.fromJson(DiscUtil.read(file), type); Map<String, Gate> instancesFromFile = Plugin.gson.fromJson(DiscUtil.read(file), type);
instances.clear(); instances.clear();
instances.putAll(instancesFromFile); instances.putAll(instancesFromFile);
} catch (IOException e) { }
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
return false; return false;
} }
@ -186,13 +233,16 @@ public class Gate {
return true; return true;
} }
public static Collection<Gate> getAll() {
public static Collection<Gate> getAll()
{
return instances.values(); return instances.values();
} }
public static void fillIds() {
for(Entry<String, Gate> entry : instances.entrySet()) { public static void fillIds()
{
for(Entry<String, Gate> entry : instances.entrySet())
entry.getValue().setId(entry.getKey()); entry.getValue().setId(entry.getKey());
} }
}
} }