From a72f6c273bfcaf601788a29d7e856604b5a59a64 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Wed, 25 Dec 2013 21:56:01 -0600 Subject: [PATCH] Work on the actual jailing of the player. Lots of work done here, untested. --- README.md | 2 + .../java/com/graywolf336/jail/JailIO.java | 18 +++-- .../com/graywolf336/jail/PrisonerManager.java | 49 +++++++++++++- .../java/com/graywolf336/jail/beans/Jail.java | 29 ++++++++ .../com/graywolf336/jail/beans/Prisoner.java | 66 ++++++++++++++++++- .../jail/command/commands/JailCommand.java | 19 +++--- .../command/parameters/JailParameters.java | 9 +++ .../graywolf336/jail/enums/LangString.java | 2 + .../com/graywolf336/jail/enums/Settings.java | 2 + .../jail/listeners/PlayerListener.java | 10 +++ src/main/resources/en.yml | 3 +- 11 files changed, 187 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 7e99edc..a3ff32c 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ Done * The time passed can be represented by time shorthand, aka "3hours" or "15minutes" or etc (defaults to minutes) * Config value ``jailing.jail.broadcastJailing`` is now used * Config value ``jailing.jail.logToConsole`` is now used +* Config value ``jailing.jail.deleteInventory`` is now used +* Config value ``jailing.release.backToPreviousPosition`` is now used [Jail 3.0 JavaDoc](http://ci.graywolf336.com/job/Jail/javadoc) ==== \ No newline at end of file diff --git a/src/main/java/com/graywolf336/jail/JailIO.java b/src/main/java/com/graywolf336/jail/JailIO.java index 747e2be..a18be1c 100644 --- a/src/main/java/com/graywolf336/jail/JailIO.java +++ b/src/main/java/com/graywolf336/jail/JailIO.java @@ -215,6 +215,8 @@ public class JailIO { flat.set(cNode + "prisoner.muted", p.isMuted()); flat.set(cNode + "prisoner.time", p.getRemainingTime()); flat.set(cNode + "prisoner.offlinePending", p.isOfflinePending()); + flat.set(cNode + "prisoner.reason", p.getReason()); + flat.set(cNode + "prisoner.previousLocation", p.getPreviousLocationString()); } } @@ -223,6 +225,8 @@ public class JailIO { flat.set(pNode + "muted", p.isMuted()); flat.set(pNode + "time", p.getRemainingTime()); flat.set(pNode + "offlinePending", p.isOfflinePending()); + flat.set(pNode + "reason", p.getReason()); + flat.set(pNode + "previousLocation", p.getPreviousLocationString()); } try { @@ -296,30 +300,30 @@ public class JailIO { } if(flat.contains(cellNode + "prisoner")) { - Prisoner p = new Prisoner(flat.getString(cellNode + "prisoner.name"), flat.getBoolean(cellNode + "prisoner.muted"), flat.getLong(cellNode + "prisoner.time")); + Prisoner p = new Prisoner(flat.getString(cellNode + "prisoner.name"), + flat.getBoolean(cellNode + "prisoner.muted"), + flat.getLong(cellNode + "prisoner.time"), + flat.getString(cellNode + "prisoner.reason")); p.setOfflinePending(flat.getBoolean(cellNode + "prisoner.offlinePending")); + p.setPreviousPosition(flat.getString(cellNode + "prisoner.previousLocation")); c.setPrisoner(p); } j.addCell(c, false); } - }else { - pl.getLogger().warning("Cell configuration section for " + name + " exists but no cells are there."); } } if(flat.isConfigurationSection(node + "prisoners")) { Set prisoners = flat.getConfigurationSection(node + "prisoners").getKeys(false); if(!prisoners.isEmpty()) { - pl.getLogger().info("Prisoner configuration section for " + j.getName() + " exists and there are " + prisoners.size() + " prisoners."); for(String prisoner : prisoners) { String pNode = node + "prisoners." + prisoner + "."; - Prisoner pris = new Prisoner(prisoner, flat.getBoolean(pNode + "muted"), flat.getLong(pNode + "time")); + Prisoner pris = new Prisoner(prisoner, flat.getBoolean(pNode + "muted"), flat.getLong(pNode + "time"), flat.getString(pNode + "reason")); pris.setOfflinePending(flat.getBoolean(pNode + "offlinePending")); + pris.setPreviousPosition(flat.getString(pNode + "previousLocation")); j.addPrisoner(pris); } - }else { - pl.getLogger().warning("Prisoner configuration section for " + j.getName() + "exists but there are no prisoners."); } } diff --git a/src/main/java/com/graywolf336/jail/PrisonerManager.java b/src/main/java/com/graywolf336/jail/PrisonerManager.java index b47a854..0baf692 100644 --- a/src/main/java/com/graywolf336/jail/PrisonerManager.java +++ b/src/main/java/com/graywolf336/jail/PrisonerManager.java @@ -47,7 +47,7 @@ public class PrisonerManager { //If they are NOT offline, jail them if(!prisoner.isOfflinePending()) { - + jailPrisoner(jail, cell, player, prisoner); } //Get a message ready for broadcasting or logging. @@ -70,4 +70,51 @@ public class PrisonerManager { pl.getServer().getConsoleSender().sendMessage(msg); } } + + /** + * Jails the prisoner with the given name. + * + * @param name of the prisoner to jail. + */ + public void jailPrisoner(String name) { + Jail j = pl.getJailManager().getJailPlayerIsIn(name); + + jailPrisoner(j, j.getCellPrisonerIsIn(name), pl.getServer().getPlayerExact(name), j.getPrisoner(name)); + } + + /** + * Jails the prisoner with the proper information given. + * + * @param jail where they are going + * @param cell where they are being placed in, can be null + * @param player who is the prisoner + * @param prisoner data containing everything pertaining to them + */ + public void jailPrisoner(Jail jail, Cell cell, Player player, Prisoner prisoner) { + //They are no longer offline, so set that. + prisoner.setOfflinePending(false); + + //We are getting ready to teleport them, so set it to true so that + //the *future* move checkers won't be cancelling our moving. + prisoner.setTeleporting(true); + + //If their reason is empty send proper message, else send other proper message + if(prisoner.getReason().isEmpty()) { + player.sendMessage(pl.getJailIO().getLanguageString(LangString.JAILED)); + }else { + player.sendMessage(pl.getJailIO().getLanguageString(LangString.JAILEDWITHREASON, new String[] { prisoner.getReason() })); + } + + //If the config has inventory deletion, then let's delete it + if(pl.getConfig().getBoolean(Settings.DELETEINVENTORY.getPath(), false)) { + player.getInventory().setArmorContents(null); + player.getInventory().clear(); + } + + //If the config has releasing them back to their previous position, + //then let's set it in the prisoner data. + if(pl.getConfig().getBoolean(Settings.RELEASETOPREVIOUSPOSITION.getPath(), false)) { + prisoner.setPreviousPosition(player.getLocation()); + } + } } diff --git a/src/main/java/com/graywolf336/jail/beans/Jail.java b/src/main/java/com/graywolf336/jail/beans/Jail.java index ae1feb5..6061419 100644 --- a/src/main/java/com/graywolf336/jail/beans/Jail.java +++ b/src/main/java/com/graywolf336/jail/beans/Jail.java @@ -145,6 +145,16 @@ public class Jail { return this.cells.get(name); } + /** Returns the cell which the given player name is jailed in, null if not. */ + public Cell getCellPrisonerIsIn(String name) { + for(Cell c : cells.values()) + if(c.getPrisoner() != null) + if(c.getPrisoner().getName().equalsIgnoreCase(name)) + return c; + + return null; + } + /** Returns the first empty cell, returns null if there aren't any cells or any free cells. */ public Cell getFirstEmptyCell() { for(Cell c : getCells()) @@ -232,6 +242,25 @@ public class Jail { return is; } + /** + * Checks if the given name is a prisoner in a cell. + * + * @param name of the prisoner to check. + * @return true if is jailed in a cell, false if not. + */ + public boolean isJailedInACell(String name) { + for(Prisoner p : nocellPrisoners) + if(p.getName().equalsIgnoreCase(name)) + return false; + + for(Cell c : cells.values()) + if(c.getPrisoner() != null) + if(c.getPrisoner().getName().equalsIgnoreCase(name)) + return true; + + return false; + } + /** * Gets the {@link Prisoner prisoner} instance for the given name. * diff --git a/src/main/java/com/graywolf336/jail/beans/Prisoner.java b/src/main/java/com/graywolf336/jail/beans/Prisoner.java index 3e84492..b637244 100644 --- a/src/main/java/com/graywolf336/jail/beans/Prisoner.java +++ b/src/main/java/com/graywolf336/jail/beans/Prisoner.java @@ -2,6 +2,9 @@ package com.graywolf336.jail.beans; import java.util.concurrent.TimeUnit; +import org.bukkit.Bukkit; +import org.bukkit.Location; + /** * Represents a Prisoner, player who is jailed, and contains all the information about him/her. * @@ -10,9 +13,10 @@ import java.util.concurrent.TimeUnit; * @version 2.0.1 */ public class Prisoner { - private String name; - private boolean muted, offlinePending; + private String name, reason; + private boolean muted, offlinePending, teleporting; private long time; + private Location previousPosition; /** * Creates a new prisoner with a name and whether they are muted or not. @@ -21,10 +25,14 @@ public class Prisoner { * @param muted Whether the prisoner is muted or not * @param time The amount of remaining time the prisoner has */ - public Prisoner(String name, boolean muted, long time) { + public Prisoner(String name, boolean muted, long time, String reason) { this.name = name; this.muted = muted; this.time = time; + this.reason = reason; + this.offlinePending = false; + this.teleporting = false; + this.previousPosition = null; } /** Gets the name of this player. */ @@ -32,6 +40,16 @@ public class Prisoner { return this.name; } + /** Gets the reason this player was jailed for. */ + public String getReason() { + return this.reason; + } + + /** Sets the reason this player was jailed for. */ + public void setReason(String reason) { + this.reason = reason; + } + /** Gets whether the prisoner is muted or not. */ public boolean isMuted() { return this.muted; @@ -70,4 +88,46 @@ public class Prisoner { public void setOfflinePending(boolean offline) { this.offlinePending = offline; } + + /** Gets whether the player is being teleported or not. */ + public boolean isTeleporting() { + return this.teleporting; + } + + /** Sets whether the player is being teleported or not. */ + public void setTeleporting(boolean teleport) { + this.teleporting = teleport; + } + + /** Gets the previous location of this player, can be null. */ + public Location getPreviousLocation() { + return this.previousPosition; + } + + /** Gets the previous location of this player, separated by a comma. */ + public String getPreviousLocationString() { + if(previousPosition == null) return ""; + else return previousPosition.getWorld().getName() + "," + + previousPosition.getX() + "," + + previousPosition.getY() + "," + + previousPosition.getZ() + "," + + previousPosition.getYaw() + "," + + previousPosition.getPitch(); + } + + /** Sets the previous location of this player. */ + public void setPreviousPosition(Location location) { + this.previousPosition = location; + } + + /** Sets the previous location of this player from a comma separated string. */ + public void setPreviousPosition(String location) { + String[] s = location.split(","); + this.previousPosition = new Location(Bukkit.getWorld(s[0]), + Double.valueOf(s[1]), + Double.valueOf(s[2]), + Double.valueOf(s[3]), + Float.valueOf(s[4]), + Float.valueOf(s[5])); + } } diff --git a/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java b/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java index b80b1ac..b31de48 100644 --- a/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java +++ b/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java @@ -98,10 +98,10 @@ public class JailCommand implements Command { } } - //Get the jail instance from the name of jail in the params. - Jail j = jm.getJail(params.jail()); - Cell c = j.getCell(params.cell()); - Prisoner pris = new Prisoner(params.player(), params.muted(), time); + if(params.reason().isEmpty()) { + params.setReason(jm.getPlugin().getJailIO().getLanguageString(LangString.DEFAULTJAILEDREASON)); + } + Player p = jm.getPlugin().getServer().getPlayer(params.player()); //If the player instance is not null and the player has the permission @@ -111,6 +111,11 @@ public class JailCommand implements Command { return true; } + //Get the jail instance from the name of jail in the params. + Jail j = jm.getJail(params.jail()); + Cell c = j.getCell(params.cell()); + Prisoner pris = new Prisoner(params.player(), params.muted(), time, params.reason()); + //call the event PrisonerJailedEvent event = new PrisonerJailedEvent(j, c, pris, p, p == null, sender.getName()); jm.getPlugin().getServer().getPluginManager().callEvent(event); @@ -147,12 +152,6 @@ public class JailCommand implements Command { //Player *is* online sender.sendMessage(pris.getName() + " is online and will be jailed for " + pris.getRemainingTime() + " milliseconds in the jail " + params.jail() + " in the cell " + params.cell() + " and will be muted: " + pris.isMuted() + "."); sender.sendMessage(pris.getName() + " will be jailed for " + TimeUnit.MINUTES.convert(pris.getRemainingTime(), TimeUnit.MILLISECONDS) + " minutes by " + jailer + "."); - - if(params.reason().isEmpty()) { - p.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.JAILED)); - }else { - p.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.JAILEDWITHREASON, new String[] { params.reason() })); - } } return true; diff --git a/src/main/java/com/graywolf336/jail/command/parameters/JailParameters.java b/src/main/java/com/graywolf336/jail/command/parameters/JailParameters.java index 1796507..138c023 100644 --- a/src/main/java/com/graywolf336/jail/command/parameters/JailParameters.java +++ b/src/main/java/com/graywolf336/jail/command/parameters/JailParameters.java @@ -93,4 +93,13 @@ public class JailParameters { return r; } + + /** Sets the reason. */ + public void setReason(String r) { + String[] rs = r.split(" "); + + for(String s : rs) { + reason.add(s); + } + } } diff --git a/src/main/java/com/graywolf336/jail/enums/LangString.java b/src/main/java/com/graywolf336/jail/enums/LangString.java index 8368ad3..99079e7 100644 --- a/src/main/java/com/graywolf336/jail/enums/LangString.java +++ b/src/main/java/com/graywolf336/jail/enums/LangString.java @@ -7,6 +7,8 @@ public enum LangString { BROADCASTMESSAGEFORMINUTES, /** The message sent when trying to jail someone who can't be jailed by permission. */ CANTBEJAILED, + /** The message sent when someone is jailed without a reason. */ + DEFAULTJAILEDREASON, /** The message sent when players are jailed without a reason. */ JAILED, /** The message sent when players are jailed with a reason. */ diff --git a/src/main/java/com/graywolf336/jail/enums/Settings.java b/src/main/java/com/graywolf336/jail/enums/Settings.java index 1194abc..efbf3a1 100644 --- a/src/main/java/com/graywolf336/jail/enums/Settings.java +++ b/src/main/java/com/graywolf336/jail/enums/Settings.java @@ -4,8 +4,10 @@ public enum Settings { BROADCASTJAILING("jailing.jail.broadcastJailing"), DEBUG("system.debug"), DEFAULTJAIL("jailing.jail.defaultJail"), + DELETEINVENTORY("jailing.jail.deleteInventory"), JAILDEFAULTTIME("jailing.jail.defaultTime"), LOGJAILING("jailing.jail.logToConsole"), + RELEASETOPREVIOUSPOSITION("jailing.release.backToPreviousPosition"), UPDATENOTIFICATIONS("system.updateNotifications"); private String path; diff --git a/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java b/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java index 21da347..86d3a45 100644 --- a/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java +++ b/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java @@ -8,6 +8,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerJoinEvent; import com.graywolf336.jail.JailMain; import com.graywolf336.jail.JailManager; @@ -50,4 +51,13 @@ public class PlayerListener implements Listener { } } } + + @EventHandler + public void checkForOfflineJail(PlayerJoinEvent event) { + if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) { + if(pl.getJailManager().getJailPlayerIsIn(event.getPlayer().getName()).getPrisoner(event.getPlayer().getName()).isOfflinePending()) { + pl.getPrisonerManager().jailPrisoner(event.getPlayer().getName()); + } + } + } } diff --git a/src/main/resources/en.yml b/src/main/resources/en.yml index 64250bd..36b1326 100644 --- a/src/main/resources/en.yml +++ b/src/main/resources/en.yml @@ -4,4 +4,5 @@ language: unjailed: "&2You have been released! Please respect the server rules." broadcastmessageforever: "&9%0% has been jailed forever." broadcastmessageforminutes: "&9%0% has been jailed for %1% minutes." - cantbejailed: "&cThat player can't be jailed, due to permissions." \ No newline at end of file + cantbejailed: "&cThat player can't be jailed, due to permissions." + defaultjailedreason: "Breaking the rules." \ No newline at end of file