From 7976dec870118a87f0969319b948dc9eb28d1ee2 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Fri, 6 Dec 2013 21:50:22 -0600 Subject: [PATCH] Save the Jail when we add it. --- .../com/graywolf336/jail/JailManager.java | 544 +++++++++--------- .../jail/steps/JailCreationSteps.java | 2 +- 2 files changed, 276 insertions(+), 270 deletions(-) diff --git a/src/main/java/com/graywolf336/jail/JailManager.java b/src/main/java/com/graywolf336/jail/JailManager.java index c9d8359..05d4674 100644 --- a/src/main/java/com/graywolf336/jail/JailManager.java +++ b/src/main/java/com/graywolf336/jail/JailManager.java @@ -1,269 +1,275 @@ -package com.graywolf336.jail; - -import java.util.HashMap; -import java.util.HashSet; - -import com.graywolf336.jail.beans.CreationPlayer; -import com.graywolf336.jail.beans.Jail; -import com.graywolf336.jail.beans.Prisoner; -import com.graywolf336.jail.steps.CellCreationSteps; -import com.graywolf336.jail.steps.JailCreationSteps; - -/** - * Handles all things related to jails. - * - *

- * - * Stores the following: - *

- * - * @author graywolf336 - * @since 3.0.0 - * @version 1.1.0 - */ -public class JailManager { - private JailMain plugin; - private HashMap jails; - private HashMap jailCreators; - private HashMap cellCreators; - private JailCreationSteps jcs; - private CellCreationSteps ccs; - - public JailManager(JailMain plugin) { - this.plugin = plugin; - this.jails = new HashMap(); - this.jailCreators = new HashMap(); - this.cellCreators = new HashMap(); - this.jcs = new JailCreationSteps(); - this.ccs = new CellCreationSteps(); - } - - /** Returns the instance of the plugin main class. */ - public JailMain getPlugin() { - return this.plugin; - } - - /** Returns a HashSet of all the jails. */ - public HashSet getJails() { - return new HashSet(jails.values()); - } - - /** Returns an array of all the names of the jails. */ - public String[] getJailNames() { - return this.jails.keySet().toArray(new String[jails.size()]); - } - - /** Adds a jail to the collection of them. */ - public void addJail(Jail jail) { - this.jails.put(jail.getName(), jail); - } - - /** - * Gets a jail by the given name. - * - * @param name The name of the jail to get. - * @return The {@link Jail} with the given name, if no jail found this will return null. - */ - public Jail getJail(String name) { - return this.jails.get(name); - } - - /** - * Checks to see if the given name for a {@link Jail} is valid, returns true if it is a valid jail. - * - * @param name The name of the jail to check. - * @return True if a valid jail was found, false if no jail was found. - */ - public boolean isValidJail(String name) { - return this.jails.get(name) != null; - } - - /** - * Gets the {@link Jail jail} the given player is in. - * - * @param name The name of the player whos jail we are getting. - * @return The jail the player is in, CAN BE NULL. - */ - public Jail getJailPlayerIsIn(String name) { - Jail re = null; - - for(Jail j : jails.values()) { - if(j.isPlayerAPrisoner(name)) { - re = j; - break; - } - } - - return re; - } - - /** - * Gets if the given player is jailed or not, in all the jails and cells. - * - * @param name The name of the player to check. - * @return true if they are jailed, false if not. - */ - public boolean isPlayerJailed(String name) { - boolean r = false; - - for(Jail j : jails.values()) { - if(j.isPlayerAPrisoner(name)) { - r = true; - break; - } - } - - return r; - } - - /** - * Gets the {@link Prisoner} data from for this user, if they are jailed. - * - * @param name The name of prisoner who's data to get - * @return {@link Prisoner prisoner} data. - */ - public Prisoner getPrisoner(String name) { - Jail j = getJailPlayerIsIn(name); - - if(j != null) { - return j.getPrisoner(name); - }else { - return null; - } - } - - /** - * Returns whether or not the player is creating a jail or a cell. - * - *

- * - * If you want to check to see if they're just creating a jail then use {@link #isCreatingAJail(String) isCreatingAJail} or if you want to see if they're creating a cell then use {@link #isCreatingACell(String) isCreatingACell}. - * - * @param name The name of the player, in any case as we convert it to lowercase. - * @return True if the player is creating a jail or cell, false if they're not creating anything. - */ - public boolean isCreatingSomething(String name) { - return this.jailCreators.containsKey(name.toLowerCase()) || this.cellCreators.containsKey(name.toLowerCase()); - } - - /** Returns a message used for telling them what they're creating and what step they're on. */ - public String getStepMessage(String player) { - String message = ""; - - if(isCreatingACell(player)) {//Check whether it is a jail cell - CreationPlayer cp = this.getCellCreationPlayer(player); - message = "You're already creating a Cell with the name '" + cp.getCellName() + "' and you still need to "; - - switch(cp.getStep()) { - case 1: - message += "set the teleport in location."; - break; - case 2: - message += "select all the signs."; - break; - case 3: - message += "set the double chest location."; - break; - } - - }else if(isCreatingAJail(player)) {//If not a cell, then check if a jail. - CreationPlayer cp = this.getJailCreationPlayer(player); - message = "You're already creating a Jail with the name '" + cp.getJailName() + "' and you still need to "; - - switch(cp.getStep()) { - case 1: - message += "select the first point."; - break; - case 2: - message += "select the second point."; - break; - case 3: - message += "set the teleport in location."; - break; - case 4: - message += "set the release location."; - break; - } - } - - return message; - } - - /** Returns whether or not someone is creating a Jail. */ - public boolean isCreatingAJail(String name) { - return this.jailCreators.containsKey(name.toLowerCase()); - } - - /** - * Method for setting a player to be creating a Jail, returns whether or not they were added successfully. - * - * @param player The player who is creating a jail. - * @param jailName The name of the jail we are creating. - * @return True if they were added successfully, false if they are already creating a Jail. - */ - public boolean addCreatingJail(String player, String jailName) { - if(isCreatingAJail(player)) { - return false; - }else { - this.jailCreators.put(player.toLowerCase(), new CreationPlayer(jailName)); - return true; - } - } - - /** Returns the instance of the CreationPlayer for this player, null if there was none found. */ - public CreationPlayer getJailCreationPlayer(String name) { - return this.jailCreators.get(name.toLowerCase()); - } - - /** Removes a CreationPlayer with the given name from the jail creators. */ - public void removeJailCreationPlayer(String name) { - this.jailCreators.remove(name.toLowerCase()); - } - - /** Returns whether or not someone is creating a Cell. */ - public boolean isCreatingACell(String name) { - return this.cellCreators.containsKey(name.toLowerCase()); - } - - /** - * Method for setting a player to be creating a Cell, returns whether or not they were added successfully. - * - * @param player The player who is creating a jail. - * @param jailName The name of the jail this cell is going. - * @param cellName The name of the cell we are creating. - * @return True if they were added successfully, false if they are already creating a Jail. - */ - public boolean addCreatingCell(String player, String jailName, String cellName) { - if(isCreatingACell(player)) { - return false; - }else { - this.cellCreators.put(player.toLowerCase(), new CreationPlayer(cellName)); - return true; - } - } - - /** Returns the instance of the CreationPlayer for this player, null if there was none found. */ - public CreationPlayer getCellCreationPlayer(String name) { - return this.cellCreators.get(name.toLowerCase()); - } - - /** Removes a CreationPlayer with the given name from the cell creators. */ - public void removeCellCreationPlayer(String name) { - this.cellCreators.remove(name.toLowerCase()); - } - - /** Gets the instance of the {@link JailCreationSteps}. */ - public JailCreationSteps getJailCreationSteps() { - return this.jcs; - } - - /** Gets the instance of the {@link CellCreationSteps}. */ - public CellCreationSteps getCellCreationSteps() { - return this.ccs; - } -} +package com.graywolf336.jail; + +import java.util.HashMap; +import java.util.HashSet; + +import com.graywolf336.jail.beans.CreationPlayer; +import com.graywolf336.jail.beans.Jail; +import com.graywolf336.jail.beans.Prisoner; +import com.graywolf336.jail.steps.CellCreationSteps; +import com.graywolf336.jail.steps.JailCreationSteps; + +/** + * Handles all things related to jails. + * + *

+ * + * Stores the following: + *

    + *
  • The {@link Jail jails}, which contains the prisoners and cells.
  • + *
  • Players creating jails, see {@link CreationPlayer}.
  • + *
  • Players creating jail cells, see {@link CreationPlayer}.
  • + *
  • An instance of {@link JailCreationSteps} for stepping players through the Jail creation process.
  • + *
+ * + * @author graywolf336 + * @since 3.0.0 + * @version 1.1.0 + */ +public class JailManager { + private JailMain plugin; + private HashMap jails; + private HashMap jailCreators; + private HashMap cellCreators; + private JailCreationSteps jcs; + private CellCreationSteps ccs; + + public JailManager(JailMain plugin) { + this.plugin = plugin; + this.jails = new HashMap(); + this.jailCreators = new HashMap(); + this.cellCreators = new HashMap(); + this.jcs = new JailCreationSteps(); + this.ccs = new CellCreationSteps(); + } + + /** Returns the instance of the plugin main class. */ + public JailMain getPlugin() { + return this.plugin; + } + + /** Returns a HashSet of all the jails. */ + public HashSet getJails() { + return new HashSet(jails.values()); + } + + /** Returns an array of all the names of the jails. */ + public String[] getJailNames() { + return this.jails.keySet().toArray(new String[jails.size()]); + } + + /** + * Adds a jail to the collection of them. + * + * @param jail The jail to add + * @param n True if this is a new jail, false if it isn't. + */ + public void addJail(Jail jail, boolean n) { + this.jails.put(jail.getName(), jail); + if(n) plugin.getJailIO().saveJail(jail); + } + + /** + * Gets a jail by the given name. + * + * @param name The name of the jail to get. + * @return The {@link Jail} with the given name, if no jail found this will return null. + */ + public Jail getJail(String name) { + return this.jails.get(name); + } + + /** + * Checks to see if the given name for a {@link Jail} is valid, returns true if it is a valid jail. + * + * @param name The name of the jail to check. + * @return True if a valid jail was found, false if no jail was found. + */ + public boolean isValidJail(String name) { + return this.jails.get(name) != null; + } + + /** + * Gets the {@link Jail jail} the given player is in. + * + * @param name The name of the player whos jail we are getting. + * @return The jail the player is in, CAN BE NULL. + */ + public Jail getJailPlayerIsIn(String name) { + Jail re = null; + + for(Jail j : jails.values()) { + if(j.isPlayerAPrisoner(name)) { + re = j; + break; + } + } + + return re; + } + + /** + * Gets if the given player is jailed or not, in all the jails and cells. + * + * @param name The name of the player to check. + * @return true if they are jailed, false if not. + */ + public boolean isPlayerJailed(String name) { + boolean r = false; + + for(Jail j : jails.values()) { + if(j.isPlayerAPrisoner(name)) { + r = true; + break; + } + } + + return r; + } + + /** + * Gets the {@link Prisoner} data from for this user, if they are jailed. + * + * @param name The name of prisoner who's data to get + * @return {@link Prisoner prisoner} data. + */ + public Prisoner getPrisoner(String name) { + Jail j = getJailPlayerIsIn(name); + + if(j != null) { + return j.getPrisoner(name); + }else { + return null; + } + } + + /** + * Returns whether or not the player is creating a jail or a cell. + * + *

+ * + * If you want to check to see if they're just creating a jail then use {@link #isCreatingAJail(String) isCreatingAJail} or if you want to see if they're creating a cell then use {@link #isCreatingACell(String) isCreatingACell}. + * + * @param name The name of the player, in any case as we convert it to lowercase. + * @return True if the player is creating a jail or cell, false if they're not creating anything. + */ + public boolean isCreatingSomething(String name) { + return this.jailCreators.containsKey(name.toLowerCase()) || this.cellCreators.containsKey(name.toLowerCase()); + } + + /** Returns a message used for telling them what they're creating and what step they're on. */ + public String getStepMessage(String player) { + String message = ""; + + if(isCreatingACell(player)) {//Check whether it is a jail cell + CreationPlayer cp = this.getCellCreationPlayer(player); + message = "You're already creating a Cell with the name '" + cp.getCellName() + "' and you still need to "; + + switch(cp.getStep()) { + case 1: + message += "set the teleport in location."; + break; + case 2: + message += "select all the signs."; + break; + case 3: + message += "set the double chest location."; + break; + } + + }else if(isCreatingAJail(player)) {//If not a cell, then check if a jail. + CreationPlayer cp = this.getJailCreationPlayer(player); + message = "You're already creating a Jail with the name '" + cp.getJailName() + "' and you still need to "; + + switch(cp.getStep()) { + case 1: + message += "select the first point."; + break; + case 2: + message += "select the second point."; + break; + case 3: + message += "set the teleport in location."; + break; + case 4: + message += "set the release location."; + break; + } + } + + return message; + } + + /** Returns whether or not someone is creating a Jail. */ + public boolean isCreatingAJail(String name) { + return this.jailCreators.containsKey(name.toLowerCase()); + } + + /** + * Method for setting a player to be creating a Jail, returns whether or not they were added successfully. + * + * @param player The player who is creating a jail. + * @param jailName The name of the jail we are creating. + * @return True if they were added successfully, false if they are already creating a Jail. + */ + public boolean addCreatingJail(String player, String jailName) { + if(isCreatingAJail(player)) { + return false; + }else { + this.jailCreators.put(player.toLowerCase(), new CreationPlayer(jailName)); + return true; + } + } + + /** Returns the instance of the CreationPlayer for this player, null if there was none found. */ + public CreationPlayer getJailCreationPlayer(String name) { + return this.jailCreators.get(name.toLowerCase()); + } + + /** Removes a CreationPlayer with the given name from the jail creators. */ + public void removeJailCreationPlayer(String name) { + this.jailCreators.remove(name.toLowerCase()); + } + + /** Returns whether or not someone is creating a Cell. */ + public boolean isCreatingACell(String name) { + return this.cellCreators.containsKey(name.toLowerCase()); + } + + /** + * Method for setting a player to be creating a Cell, returns whether or not they were added successfully. + * + * @param player The player who is creating a jail. + * @param jailName The name of the jail this cell is going. + * @param cellName The name of the cell we are creating. + * @return True if they were added successfully, false if they are already creating a Jail. + */ + public boolean addCreatingCell(String player, String jailName, String cellName) { + if(isCreatingACell(player)) { + return false; + }else { + this.cellCreators.put(player.toLowerCase(), new CreationPlayer(cellName)); + return true; + } + } + + /** Returns the instance of the CreationPlayer for this player, null if there was none found. */ + public CreationPlayer getCellCreationPlayer(String name) { + return this.cellCreators.get(name.toLowerCase()); + } + + /** Removes a CreationPlayer with the given name from the cell creators. */ + public void removeCellCreationPlayer(String name) { + this.cellCreators.remove(name.toLowerCase()); + } + + /** Gets the instance of the {@link JailCreationSteps}. */ + public JailCreationSteps getJailCreationSteps() { + return this.jcs; + } + + /** Gets the instance of the {@link CellCreationSteps}. */ + public CellCreationSteps getCellCreationSteps() { + return this.ccs; + } +} diff --git a/src/main/java/com/graywolf336/jail/steps/JailCreationSteps.java b/src/main/java/com/graywolf336/jail/steps/JailCreationSteps.java index 24cd192..bb8893b 100644 --- a/src/main/java/com/graywolf336/jail/steps/JailCreationSteps.java +++ b/src/main/java/com/graywolf336/jail/steps/JailCreationSteps.java @@ -135,7 +135,7 @@ public class JailCreationSteps { jail.setTeleportIn(cp.getTeleportInSL()); jail.setTeleportFree(cp.getTeleportFreeSL()); - jm.addJail(jail); + jm.addJail(jail, true); p.sendMessage(ChatColor.GREEN + "Jail (" + jail.getName() + ") created successfully!");