diff --git a/README.md b/README.md index 6de629a..bd9d90d 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,10 @@ Done * Config value ``jailing.release.backToPreviousPosition`` is now used * Config value ``jailing.release.restorePreviousGameMode`` is now used * Config value ``jailing.jail.gameMode`` is now used +* Config value ``jailing.during.ignoreSleeping`` is now used in one part +* Config value ``jailing.jail.commands`` is now used +* Config value ``jailing.during.maxFoodLevel`` is now used in one part +* Config value ``jailing.during.minFoodLevel`` is now used in one part [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 a69ca5e..19d03e0 100644 --- a/src/main/java/com/graywolf336/jail/JailIO.java +++ b/src/main/java/com/graywolf336/jail/JailIO.java @@ -218,6 +218,8 @@ public class JailIO { 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.inventory", p.getInventory()); + flat.set(cNode + "prisoner.armor", p.getArmor()); if(p.getPreviousLocationString() != null) flat.set(cNode + "prisoner.previousLocation", p.getPreviousLocationString()); if(p.getPreviousGameMode() != null) @@ -231,6 +233,8 @@ public class JailIO { flat.set(pNode + "time", p.getRemainingTime()); flat.set(pNode + "offlinePending", p.isOfflinePending()); flat.set(pNode + "reason", p.getReason()); + flat.set(pNode + "inventory", p.getInventory()); + flat.set(pNode + "armor", p.getArmor()); if(p.getPreviousLocationString() != null) flat.set(pNode + "previousLocation", p.getPreviousLocationString()); if(p.getPreviousGameMode() != null) @@ -315,6 +319,8 @@ public class JailIO { p.setOfflinePending(flat.getBoolean(cellNode + "prisoner.offlinePending")); p.setPreviousPosition(flat.getString(cellNode + "prisoner.previousLocation")); p.setPreviousGameMode(flat.getString(cellNode + "prisoner.previousGameMode")); + p.setInventory(flat.getString(cellNode + "prisoner.inventory", "")); + p.setInventory(flat.getString(cellNode + "prisoner.armor", "")); c.setPrisoner(p); } @@ -332,6 +338,8 @@ public class JailIO { pris.setOfflinePending(flat.getBoolean(pNode + "offlinePending")); pris.setPreviousPosition(flat.getString(pNode + "previousLocation")); pris.setPreviousGameMode(flat.getString(pNode + "previousGameMode")); + pris.setInventory(flat.getString(pNode + "inventory", "")); + pris.setInventory(flat.getString(pNode + "armor", "")); j.addPrisoner(pris); } } diff --git a/src/main/java/com/graywolf336/jail/JailMain.java b/src/main/java/com/graywolf336/jail/JailMain.java index 049cfc0..ceb18ec 100644 --- a/src/main/java/com/graywolf336/jail/JailMain.java +++ b/src/main/java/com/graywolf336/jail/JailMain.java @@ -10,7 +10,6 @@ import com.graywolf336.jail.command.CommandHandler; import com.graywolf336.jail.listeners.BlockListener; import com.graywolf336.jail.listeners.EntityListener; import com.graywolf336.jail.listeners.PlayerListener; -import com.graywolf336.jail.listeners.PlayerPreventionsListener; public class JailMain extends JavaPlugin { private CommandHandler cmdHand; @@ -34,7 +33,6 @@ public class JailMain extends JavaPlugin { plm.registerEvents(new BlockListener(), this); plm.registerEvents(new EntityListener(), this); plm.registerEvents(new PlayerListener(this), this); - plm.registerEvents(new PlayerPreventionsListener(this), this); //For the time, we will use: //http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html#convert(long, java.util.concurrent.TimeUnit) diff --git a/src/main/java/com/graywolf336/jail/PrisonerManager.java b/src/main/java/com/graywolf336/jail/PrisonerManager.java index b000601..4568f9a 100644 --- a/src/main/java/com/graywolf336/jail/PrisonerManager.java +++ b/src/main/java/com/graywolf336/jail/PrisonerManager.java @@ -2,6 +2,8 @@ package com.graywolf336.jail; import org.bukkit.GameMode; import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; import com.graywolf336.jail.beans.Cell; import com.graywolf336.jail.beans.Jail; @@ -155,11 +157,96 @@ public class PrisonerManager { player.eject(); } + //If we are ignoring the sleeping state of prisoners, + //then let's set that + if(pl.getConfig().getBoolean(Settings.IGNORESLEEPINGSTATE.getPath(), true)) { + player.setSleepingIgnored(true); + } + + //Get the max and min food level in the config + int maxFood = pl.getConfig().getInt(Settings.MAXFOODLEVEL.getPath(), 20); + int minFood = pl.getConfig().getInt(Settings.MINFOODLEVEL.getPath(), 10); + + //If their food level is less than the min food level, set it to the min + //but if it is higher than the max, set it to the max + if (player.getFoodLevel() < minFood) { + player.setFoodLevel(minFood); + } else if (player.getFoodLevel() > maxFood) { + player.setFoodLevel(maxFood); + } + //If the cell doesn't equal null, then let's put them in the jail if(cell != null) { + //check if we store the inventory + if(pl.getConfig().getBoolean(Settings.JAILEDSTOREINVENTORY.getPath(), true)) { + //Check if there is a chest to store our items to and if it is a double chest, if not we will then serialize it + if(cell.hasChest()) { + //Get the chest's inventory and then clear it + Inventory chest = cell.getChest().getInventory(); + chest.clear(); + + //Get the separate inventory, so we can iterate of them + ItemStack[] inventory = player.getInventory().getContents(); + ItemStack[] armor = player.getInventory().getArmorContents(); + + for(ItemStack item : inventory) { + int i = chest.firstEmpty(); + if(i != -1) {//Check that we have got a free spot, should never happen but just in case + chest.setItem(i, item); + } + } + + for(ItemStack item : armor) { + int i = chest.firstEmpty(); + if(i != -1) {//Check that we have got a free spot, should never happen but just in case + chest.setItem(i, item); + } + } + + player.getInventory().setArmorContents(null); + player.getInventory().clear(); + + //Here so we don't forget about it later as this method isn't finished, but + //Updates the cell's signs + cell.update(); + }else { + String[] inv = Util.playerInventoryToBase64(player.getInventory()); + prisoner.setInventory(inv[0]); + prisoner.setArmor(inv[1]); + } + } + //Teleport them to the cell's teleport location + //they will now be placed in jail. + player.teleport(cell.getTeleport()); }else { //There is no cell we're jailing them to, so stick them in the jail + if(pl.getConfig().getBoolean(Settings.JAILEDSTOREINVENTORY.getPath(), true)) { + String[] inv = Util.playerInventoryToBase64(player.getInventory()); + prisoner.setInventory(inv[0]); + prisoner.setArmor(inv[1]); + + player.getInventory().setArmorContents(null); + player.getInventory().clear(); + } + + //Teleport them to the jail's teleport in location + //They will now be placed in jail. + player.teleport(jail.getTeleportIn()); } + + //Set them to not allowing teleporting, as we are not going to be moving them anymore + //this way the move checkers will start checking this player. + prisoner.setTeleporting(false); + + //Get the commands to execute after they are jailed + //replace all of the %p% so that the commands can have a player name in them + for(String command : pl.getConfig().getStringList(Settings.COMMANDSONJAIL.getPath())) { + command = command.replaceAll("%p%", player.getName()); + pl.getServer().dispatchCommand(pl.getServer().getConsoleSender(), command); + } + + //Save the data, as we have changed it + pl.getJailIO().saveJail(jail); } } diff --git a/src/main/java/com/graywolf336/jail/Util.java b/src/main/java/com/graywolf336/jail/Util.java index d809176..20e1fca 100644 --- a/src/main/java/com/graywolf336/jail/Util.java +++ b/src/main/java/com/graywolf336/jail/Util.java @@ -13,11 +13,11 @@ import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.util.Vector; import org.bukkit.util.io.BukkitObjectInputStream; import org.bukkit.util.io.BukkitObjectOutputStream; - import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; public class Util { @@ -108,6 +108,54 @@ public class Util { return Long.valueOf(t); } + /** + * Converts the player inventory to a String array of Base64 strings. First string is the content and second string is the armor. + * + * @param playerInventory to turn into an array of strings. + * @return Array of strings: [ main content, armor content ] + * @throws IllegalStateException + */ + public static String[] playerInventoryToBase64(PlayerInventory playerInventory) throws IllegalStateException { + //get the main content part, this doesn't return the armor + String content = toBase64(playerInventory); + String armor = itemStackArrayToBase64(playerInventory.getArmorContents()); + + return new String[] { content, armor }; + } + + /** + * + * A method to serialize an {@link ItemStack} array to Base64 String. + * + *

+ * + * Based off of {@link #toBase64(Inventory)}. + * + * @param items to turn into a Base64 String. + * @return Base64 string of the items. + * @throws IllegalStateException + */ + public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException { + try { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream); + + // Write the size of the inventory + dataOutput.writeInt(items.length); + + // Save every element in the list + for (int i = 0; i < items.length; i++) { + dataOutput.writeObject(items[i]); + } + + // Serialize that array + dataOutput.close(); + return Base64Coder.encodeLines(outputStream.toByteArray()); + } catch (Exception e) { + throw new IllegalStateException("Unable to save item stacks.", e); + } + } + /** * A method to serialize an inventory to Base64 string. * @@ -140,7 +188,7 @@ public class Util { return Base64Coder.encodeLines(outputStream.toByteArray()); } catch (Exception e) { throw new IllegalStateException("Unable to save item stacks.", e); - } + } } /** @@ -175,4 +223,33 @@ public class Util { throw new IOException("Unable to decode class type.", e); } } + + /** + * Gets an array of ItemStacks from Base64 string. + * + *

+ * + * Base off of {@link #fromBase64(String)}. + * + * @param data Base64 string to convert to ItemStack array. + * @return ItemStack array created from the Base64 string. + * @throws IOException + */ + public static ItemStack[] itemStackArrayFromBase64(String data) throws IOException { + try { + ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data)); + BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream); + ItemStack[] items = new ItemStack[dataInput.readInt()]; + + // Read the serialized inventory + for (int i = 0; i < items.length; i++) { + items[i] = (ItemStack) dataInput.readObject(); + } + + dataInput.close(); + return items; + } catch (ClassNotFoundException e) { + throw new IOException("Unable to decode class type.", e); + } + } } diff --git a/src/main/java/com/graywolf336/jail/beans/Cell.java b/src/main/java/com/graywolf336/jail/beans/Cell.java index 65882a3..e13d821 100644 --- a/src/main/java/com/graywolf336/jail/beans/Cell.java +++ b/src/main/java/com/graywolf336/jail/beans/Cell.java @@ -2,6 +2,7 @@ package com.graywolf336.jail.beans; import java.util.HashSet; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Chest; @@ -32,6 +33,11 @@ public class Cell { return this.name; } + /** Updates the signs of the cell, with the player name and time and such. TODO */ + public void update() { + //TODO: Update the signs + } + /** Sets the prisoner in this cell. */ public void setPrisoner(Prisoner prisoner) { this.p = prisoner; @@ -102,4 +108,20 @@ public class Cell { return (Chest) this.chest.getLocation().getBlock().getState(); } + + /** + * Checks if the chest location doesn't equal null and if it is a double chest. + * + * @return true if there is a chest, false if there isn't. + */ + public boolean hasChest() { + if(getChest() != null) { + if(getChest().getInventory().getSize() == 40) return true; + else { + Bukkit.getLogger().severe("The cell " + this.name + " has chest that isn't a double chest, please fix."); + return false; + } + }else + return false; + } } diff --git a/src/main/java/com/graywolf336/jail/beans/Prisoner.java b/src/main/java/com/graywolf336/jail/beans/Prisoner.java index 23061df..dceb69f 100644 --- a/src/main/java/com/graywolf336/jail/beans/Prisoner.java +++ b/src/main/java/com/graywolf336/jail/beans/Prisoner.java @@ -14,7 +14,7 @@ import org.bukkit.Location; * @version 2.0.1 */ public class Prisoner { - private String name, reason; + private String name, reason, inventory, armor; private boolean muted, offlinePending, teleporting; private long time; private Location previousPosition; @@ -36,6 +36,8 @@ public class Prisoner { this.teleporting = false; this.previousPosition = null; this.previousGameMode = null; + this.inventory = ""; + this.armor = ""; } /** Gets the name of this player. */ @@ -153,4 +155,24 @@ public class Prisoner { else if(previous.isEmpty()) return; else this.previousGameMode = GameMode.valueOf(previous); } + + /** Gets the inventory string for this player, it is encoded in Base64 string. */ + public String getInventory() { + return this.inventory; + } + + /** Sets the inventory Base64 string. */ + public void setInventory(String inventory) { + this.inventory = inventory; + } + + /** Gets the armor content, encoded in Base64 string. */ + public String getArmor() { + return this.armor; + } + + /** Sets the armor inventory Base64 string. */ + public void setArmor(String armor) { + this.armor = armor; + } } 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 f9a545f..755caca 100644 --- a/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java +++ b/src/main/java/com/graywolf336/jail/command/commands/JailCommand.java @@ -18,7 +18,7 @@ import com.graywolf336.jail.command.CommandInfo; import com.graywolf336.jail.command.parameters.JailParameters; import com.graywolf336.jail.enums.LangString; import com.graywolf336.jail.enums.Settings; -import com.graywolf336.jail.events.PrisonerJailedEvent; +import com.graywolf336.jail.events.PrePrisonerJailedEvent; @CommandInfo( maxArgs = -1, @@ -128,7 +128,7 @@ public class JailCommand implements Command { 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()); + PrePrisonerJailedEvent event = new PrePrisonerJailedEvent(j, c, pris, p, p == null, sender.getName()); jm.getPlugin().getServer().getPluginManager().callEvent(event); //check if the event is cancelled diff --git a/src/main/java/com/graywolf336/jail/enums/Settings.java b/src/main/java/com/graywolf336/jail/enums/Settings.java index 9c66f4f..b8a5c9d 100644 --- a/src/main/java/com/graywolf336/jail/enums/Settings.java +++ b/src/main/java/com/graywolf336/jail/enums/Settings.java @@ -2,12 +2,17 @@ package com.graywolf336.jail.enums; public enum Settings { BROADCASTJAILING("jailing.jail.broadcastJailing"), + COMMANDSONJAIL("jailing.jail.commands"), DEBUG("system.debug"), DEFAULTJAIL("jailing.jail.defaultJail"), DELETEINVENTORY("jailing.jail.deleteInventory"), + IGNORESLEEPINGSTATE("jailing.during.ignoreSleeping"), JAILDEFAULTTIME("jailing.jail.defaultTime"), JAILEDGAMEMODE("jailing.jail.gameMode"), + JAILEDSTOREINVENTORY("jailing.jail.storeInventory"), LOGJAILING("jailing.jail.logToConsole"), + MAXFOODLEVEL("jailing.during.maxFoodLevel"), + MINFOODLEVEL("jailing.during.minFoodLevel"), RELEASETOPREVIOUSPOSITION("jailing.release.backToPreviousPosition"), RESTOREPREVIOUSGAMEMODE("jailing.release.restorePreviousGameMode"), UPDATENOTIFICATIONS("system.updateNotifications"); diff --git a/src/main/java/com/graywolf336/jail/events/PrisonerJailedEvent.java b/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedEvent.java similarity index 88% rename from src/main/java/com/graywolf336/jail/events/PrisonerJailedEvent.java rename to src/main/java/com/graywolf336/jail/events/PrePrisonerJailedEvent.java index 07b93e3..d6a5325 100644 --- a/src/main/java/com/graywolf336/jail/events/PrisonerJailedEvent.java +++ b/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedEvent.java @@ -18,7 +18,7 @@ import com.graywolf336.jail.beans.Prisoner; * @since 3.0.0 * @version 1.0.0 */ -public class PrisonerJailedEvent extends Event implements Cancellable { +public class PrePrisonerJailedEvent extends Event implements Cancellable { private static final HandlerList handlers = new HandlerList(); private boolean cancelled; private boolean online; @@ -29,7 +29,7 @@ public class PrisonerJailedEvent extends Event implements Cancellable { private String jailer, cancelMsg; /** - * Creates a new {@link PrisonerJailedEvent prisoner jailed event} for the given player. + * Creates a new {@link PrePrisonerJailedEvent prisoner jailed event} for the given player. * * @param jail The jail the prisoner will be jailed at. * @param cell The cell we're going to be sending the prisoner to, can be null. @@ -38,7 +38,7 @@ public class PrisonerJailedEvent extends Event implements Cancellable { * @param online Whether the player is online or not. * @param jailer The name of what jailed this prisoner. */ - public PrisonerJailedEvent(Jail jail, Cell cell, Prisoner prisoner, Player player, boolean online, String jailer) { + public PrePrisonerJailedEvent(Jail jail, Cell cell, Prisoner prisoner, Player player, boolean online, String jailer) { this.jail = jail; this.cell = cell; this.prisoner = prisoner; diff --git a/src/main/java/com/graywolf336/jail/listeners/PlayerPreventionsListener.java b/src/main/java/com/graywolf336/jail/listeners/PlayerPreventionsListener.java deleted file mode 100644 index 0534144..0000000 --- a/src/main/java/com/graywolf336/jail/listeners/PlayerPreventionsListener.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.graywolf336.jail.listeners; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.AsyncPlayerChatEvent; - -import com.graywolf336.jail.JailMain; -import com.graywolf336.jail.beans.Jail; - -public class PlayerPreventionsListener implements Listener { - private JailMain pl; - - public PlayerPreventionsListener(JailMain plugin) { - this.pl = plugin; - } - - @EventHandler - public void preventChat(AsyncPlayerChatEvent event) { - if(event.isCancelled()) return; - - Jail j = pl.getJailManager().getJailPlayerIsIn(event.getPlayer().getName()); - - if(j != null) { - event.setCancelled(j.getPrisoner(event.getPlayer().getName()).isMuted()); - } - } -} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1f541d4..600a59f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -14,6 +14,8 @@ jailing: during: countDownTimeWhileOffline: false ignoreSleeping: true + maxFoodLevel: 20 + minFoodLevel: 10 openChest: true jail: automaticMute: true