Let's start work on saving of the cells.

This commit is contained in:
graywolf336 2013-12-09 14:28:38 -06:00
parent 0272a3a241
commit d0273828b8
4 changed files with 293 additions and 175 deletions

View File

@ -4,10 +4,13 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Set; import java.util.Set;
import org.bukkit.Location;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import com.graywolf336.jail.beans.Cell;
import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.beans.SimpleLocation; import com.graywolf336.jail.beans.SimpleLocation;
public class JailIO { public class JailIO {
@ -103,6 +106,33 @@ public class JailIO {
flat.set(node + "tps.free.yaw", j.getTeleportFree().getYaw()); flat.set(node + "tps.free.yaw", j.getTeleportFree().getYaw());
flat.set(node + "tps.free.pitch", j.getTeleportFree().getPitch()); flat.set(node + "tps.free.pitch", j.getTeleportFree().getPitch());
for(Cell c : j.getCells()) {
String cNode = node + ".cells." + c.getName() + ".";
if(c.getTeleport() != null) {
flat.set(cNode + "tp.x", c.getTeleport().getX());
flat.set(cNode + "tp.y", c.getTeleport().getY());
flat.set(cNode + "tp.z", c.getTeleport().getZ());
flat.set(cNode + "tp.yaw", c.getTeleport().getYaw());
flat.set(cNode + "tp.pitch", c.getTeleport().getPitch());
}
if(c.getChestLocation() != null) {
flat.set(cNode + "chest.x", c.getChestLocation().getBlockX());
flat.set(cNode + "chest.y", c.getChestLocation().getBlockY());
flat.set(cNode + "chest.z", c.getChestLocation().getBlockZ());
}
flat.set(cNode + "signs", c.getSigns().toArray(new String[c.getSigns().size()]));
if(c.getPrisoner() != null) {
Prisoner p = c.getPrisoner();
flat.set(cNode + "prisoner.name", p.getName());
flat.set(cNode + "prisoner.muted", p.isMuted());
flat.set(cNode + "prisoner.time", p.getRemainingTime());
}
}
try { try {
flat.save(new File(pl.getDataFolder(), "data.yml")); flat.save(new File(pl.getDataFolder(), "data.yml"));
} catch (IOException e) { } catch (IOException e) {
@ -122,6 +152,7 @@ public class JailIO {
break; break;
default: default:
String node = "jails." + name + "."; String node = "jails." + name + ".";
String cNode = node + "cells.";
Jail j = new Jail(pl, name); Jail j = new Jail(pl, name);
j.setWorld(node + "world"); j.setWorld(node + "world");
@ -143,6 +174,47 @@ public class JailIO {
(float) flat.getDouble(node + "tps.free.yaw"), (float) flat.getDouble(node + "tps.free.yaw"),
(float) flat.getDouble(node + "tps.free.pitch"))); (float) flat.getDouble(node + "tps.free.pitch")));
if(flat.isConfigurationSection(node + "cells")) {
Set<String> cells = flat.getConfigurationSection(node + "cells").getKeys(false);
if(!cells.isEmpty()) {
pl.getLogger().info("Cell configuration section for " + name + " exists and there are " + cells.size() + " cells.");
for(String cell : cells) {
Cell c = new Cell(cell);
String cellNode = cNode + "cell.";
c.setTeleport(new SimpleLocation(j.getTeleportIn().getWorld().getName(),
flat.getDouble(cellNode + "tp.x"),
flat.getDouble(cellNode + "tp.y"),
flat.getDouble(cellNode + "tp.z"),
(float) flat.getDouble(cellNode + "tp.yaw"),
(float) flat.getDouble(cellNode + "tp.pitch")));
c.setChestLocation(new Location(j.getTeleportIn().getWorld(),
flat.getInt(cellNode + "chest.x"),
flat.getInt(cellNode + "chest.y"),
flat.getInt(cellNode + "cheset.z")));
for(String sign : flat.getStringList(cellNode + "signs")) {
String[] arr = sign.split(",");
c.addSign(new SimpleLocation(arr[0],
Double.valueOf(arr[1]),
Double.valueOf(arr[2]),
Double.valueOf(arr[3]),
Float.valueOf(arr[4]),
Float.valueOf(arr[5])));
}
Prisoner p = null;
if(flat.contains(cellNode + "prisoner")) {
p = new Prisoner(flat.getString(cellNode + "prisoner.name"), flat.getBoolean(cellNode + "prisoner.muted"), flat.getLong(cellNode + "prisoner.time"));
c.setPrisoner(p);
}
}
}else {
pl.getLogger().warning("Cell configuration section for " + name + " exists but no cells are there.");
}
}
pl.getJailManager().addJail(j, false); pl.getJailManager().addJail(j, false);
break; break;
} }

View File

@ -1,95 +1,104 @@
package com.graywolf336.jail.beans; package com.graywolf336.jail.beans;
import java.util.HashSet; import java.util.HashSet;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
/** Represents a Cell inside of a {@link Jail}. /** Represents a Cell inside of a {@link Jail}.
* *
* @author graywolf336 * @author graywolf336
* @since 3.0.0 * @since 3.0.0
* @version 1.1.1 * @version 1.1.2
*/ */
public class Cell { public class Cell {
private String name; private String name;
private Prisoner p; private Prisoner p;
private HashSet<SimpleLocation> signs; private HashSet<SimpleLocation> signs;
private SimpleLocation teleport, chest; private SimpleLocation teleport, chest;
public Cell(String name) { /** Creates a new Cell with the given name
this.name = name; *
this.signs = new HashSet<SimpleLocation>(); * @param name The name of the cell.
} */
public Cell(String name) {
/** Gets the name of the cell. */ this.name = name;
public String getName() { this.signs = new HashSet<SimpleLocation>();
return this.name; }
}
/** Gets the name of the cell. */
/** Sets the prisoner in this cell. */ public String getName() {
public void setPrisoner(Prisoner prisoner) { return this.name;
this.p = prisoner; }
}
/** Sets the prisoner in this cell. */
/** Gets the prisoner being held in this cell. */ public void setPrisoner(Prisoner prisoner) {
public Prisoner getPrisoner() { this.p = prisoner;
return this.p; }
}
/** Gets the prisoner being held in this cell. */
public void removePrisoner() { public Prisoner getPrisoner() {
this.p = null; return this.p;
} }
/** Returns true if there is currently a prisoner in this cell. */ public void removePrisoner() {
public boolean hasPrisoner() { this.p = null;
return this.p != null; //Return true if prison is not null, as when it isn't null we have a prisoner in this cell }
}
/** Returns true if there is currently a prisoner in this cell. */
/** Adds all the given signs to the cell. */ public boolean hasPrisoner() {
public void addAllSigns(HashSet<SimpleLocation> signs) { return this.p != null; //Return true if prison is not null, as when it isn't null we have a prisoner in this cell
this.signs.addAll(signs); }
}
/** Adds all the given signs to the cell. */
/** Adds a sign to the cell. */ public void addAllSigns(HashSet<SimpleLocation> signs) {
public void addSign(SimpleLocation sign) { this.signs.addAll(signs);
this.signs.add(sign); }
}
/** Adds a sign to the cell. */
/** Sets the location of where the prisoner will be teleported at when jailed here. */ public void addSign(SimpleLocation sign) {
public void setTeleport(SimpleLocation location) { this.signs.add(sign);
this.teleport = location; }
}
/** Returns all the signs for this cell. */
/** Gets the teleport location where the prisoner will be teleported at when jailed here. */ public HashSet<SimpleLocation> getSigns() {
public Location getTeleport() { return this.signs;
return this.teleport.getLocation(); }
}
/** Sets the location of where the prisoner will be teleported at when jailed here. */
/** Sets the location of the chest. */ public void setTeleport(SimpleLocation location) {
public void setChestLocation(Location location) { this.teleport = location;
this.chest = new SimpleLocation(location); }
}
/** Gets the teleport location where the prisoner will be teleported at when jailed here. */
/** public Location getTeleport() {
* Gets the location of the chest, returns null if no chest is stored at this cell. return this.teleport.getLocation();
* }
* @return The location of the chest, null if none.
*/ /** Sets the location of the chest. */
public Location getChestLocation() { public void setChestLocation(Location location) {
return this.chest.getLocation(); this.chest = new SimpleLocation(location);
} }
/** /**
* Gets the chest for this cell, returns null if there is no chest or the location we have is not a chest. * Gets the location of the chest, returns null if no chest is stored at this cell.
* *
* @return The chest and its state. * @return The location of the chest, null if none.
*/ */
public Chest getChest() { public Location getChestLocation() {
if(this.chest == null) return null; return this.chest.getLocation();
if((this.chest.getLocation().getBlock() == null) || (this.chest.getLocation().getBlock().getType() != Material.CHEST)) return null; }
return (Chest) this.chest.getLocation().getBlock().getState(); /**
} * Gets the chest for this cell, returns null if there is no chest or the location we have is not a chest.
} *
* @return The chest and its state.
*/
public Chest getChest() {
if(this.chest == null) return null;
if((this.chest.getLocation().getBlock() == null) || (this.chest.getLocation().getBlock().getType() != Material.CHEST)) return null;
return (Chest) this.chest.getLocation().getBlock().getState();
}
}

View File

@ -1,32 +1,65 @@
package com.graywolf336.jail.beans; package com.graywolf336.jail.beans;
/** /**
* Represents a Prisoner, player who is jailed, and contains all the information about him/her. * Represents a Prisoner, player who is jailed, and contains all the information about him/her.
* *
* @author graywolf336 * @author graywolf336
* @since 2.x.x * @since 2.x.x
* @version 2.0.0 * @version 2.0.1
*/ */
public class Prisoner { public class Prisoner {
private String name; private String name;
private boolean muted; private boolean muted;
private long time;
/**
* Creates a new prisoner with a name and whether they are muted or not. /**
* * Creates a new prisoner with a name and whether they are muted or not.
* @param name *
* @param muted * @param name The name of the prisoner
*/ * @param muted Whether the prisoner is muted or not
public Prisoner(String name, boolean muted) { * @param time The amount of remaining time the prisoner has
this.name = name; */
this.muted = muted; public Prisoner(String name, boolean muted, long time) {
} this.name = name;
this.muted = muted;
public String getName() { this.time = time;
return this.name; }
}
/** Gets the name of this player. */
public boolean isMuted() { public String getName() {
return this.muted; return this.name;
} }
}
/** Gets whether the prisoner is muted or not. */
public boolean isMuted() {
return this.muted;
}
/** Sets whether the prisoner is muted or not. */
public void setMuted(boolean muted) {
this.muted = muted;
}
/** Gets the remaining time the prisoner has.
*
* <p />
*
* <strong>WARNING:</strong> The time system hasn't been implemented so this is likely to change.
*/
public long getRemainingTime() {
return this.time;
}
/**
* Sets the remaining time the prisoner has left.
*
* <p />
*
* <strong>WARNING:</strong> The time system hasn't been implemented so this is likely to change.
*
* @param time The amount of time left, in milliseconds.
*/
public void setRemainingTime(long time) {
this.time = time;
}
}

View File

@ -1,48 +1,52 @@
package com.graywolf336.jail.beans; package com.graywolf336.jail.beans;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
/** /**
* Simple location class which doesn't store any instances of {@link World worlds} or {@link org.bukkit.block.Block blocks}, just uses strings, floats, and doubles. * Simple location class which doesn't store any instances of {@link World worlds} or {@link org.bukkit.block.Block blocks}, just uses strings, floats, and doubles.
* *
* @author graywolf336 * @author graywolf336
* @since 3.0.0 * @since 3.0.0
* @version 1.1.0 * @version 1.1.1
*/ */
public class SimpleLocation { public class SimpleLocation {
private String world; private String world;
private double x, y, z; private double x, y, z;
private float yaw, pitch; private float yaw, pitch;
public SimpleLocation(String world, double x, double y, double z, float yaw, float pitch) { public SimpleLocation(String world, double x, double y, double z, float yaw, float pitch) {
this.world = world; this.world = world;
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
this.yaw = yaw; this.yaw = yaw;
this.pitch = pitch; this.pitch = pitch;
} }
public SimpleLocation(Location location) { public SimpleLocation(Location location) {
this.world = location.getWorld().getName(); this.world = location.getWorld().getName();
this.x = location.getX(); this.x = location.getX();
this.y = location.getY(); this.y = location.getY();
this.z = location.getZ(); this.z = location.getZ();
this.yaw = location.getYaw(); this.yaw = location.getYaw();
this.pitch = location.getPitch(); this.pitch = location.getPitch();
} }
public World getWorld() { public World getWorld() {
return Bukkit.getWorld(world); return Bukkit.getWorld(world);
} }
public String getWorldName() { public String getWorldName() {
return this.world; return this.world;
} }
public Location getLocation() { public Location getLocation() {
return new Location(Bukkit.getWorld(world), x, y, z, yaw, pitch); return new Location(Bukkit.getWorld(world), x, y, z, yaw, pitch);
} }
}
public String toString() {
return world + "," + x + "," + y + "," + z + "," + yaw + "," + pitch;
}
}