Lots of progress was made towards jailing the player.

We now handle the jailing of the players and even store their inventory.
Unjailing hasn't been tested at all yet, so that's still to do.
This commit is contained in:
graywolf336 2013-12-27 18:19:47 -06:00
parent d9f88b8eef
commit e4f74e5e91
12 changed files with 235 additions and 37 deletions

View File

@ -23,6 +23,10 @@ Done
* Config value ``jailing.release.backToPreviousPosition`` is now used * Config value ``jailing.release.backToPreviousPosition`` is now used
* Config value ``jailing.release.restorePreviousGameMode`` is now used * Config value ``jailing.release.restorePreviousGameMode`` is now used
* Config value ``jailing.jail.gameMode`` 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) [Jail 3.0 JavaDoc](http://ci.graywolf336.com/job/Jail/javadoc)
==== ====

View File

@ -218,6 +218,8 @@ public class JailIO {
flat.set(cNode + "prisoner.time", p.getRemainingTime()); flat.set(cNode + "prisoner.time", p.getRemainingTime());
flat.set(cNode + "prisoner.offlinePending", p.isOfflinePending()); flat.set(cNode + "prisoner.offlinePending", p.isOfflinePending());
flat.set(cNode + "prisoner.reason", p.getReason()); 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) if(p.getPreviousLocationString() != null)
flat.set(cNode + "prisoner.previousLocation", p.getPreviousLocationString()); flat.set(cNode + "prisoner.previousLocation", p.getPreviousLocationString());
if(p.getPreviousGameMode() != null) if(p.getPreviousGameMode() != null)
@ -231,6 +233,8 @@ public class JailIO {
flat.set(pNode + "time", p.getRemainingTime()); flat.set(pNode + "time", p.getRemainingTime());
flat.set(pNode + "offlinePending", p.isOfflinePending()); flat.set(pNode + "offlinePending", p.isOfflinePending());
flat.set(pNode + "reason", p.getReason()); flat.set(pNode + "reason", p.getReason());
flat.set(pNode + "inventory", p.getInventory());
flat.set(pNode + "armor", p.getArmor());
if(p.getPreviousLocationString() != null) if(p.getPreviousLocationString() != null)
flat.set(pNode + "previousLocation", p.getPreviousLocationString()); flat.set(pNode + "previousLocation", p.getPreviousLocationString());
if(p.getPreviousGameMode() != null) if(p.getPreviousGameMode() != null)
@ -315,6 +319,8 @@ public class JailIO {
p.setOfflinePending(flat.getBoolean(cellNode + "prisoner.offlinePending")); p.setOfflinePending(flat.getBoolean(cellNode + "prisoner.offlinePending"));
p.setPreviousPosition(flat.getString(cellNode + "prisoner.previousLocation")); p.setPreviousPosition(flat.getString(cellNode + "prisoner.previousLocation"));
p.setPreviousGameMode(flat.getString(cellNode + "prisoner.previousGameMode")); p.setPreviousGameMode(flat.getString(cellNode + "prisoner.previousGameMode"));
p.setInventory(flat.getString(cellNode + "prisoner.inventory", ""));
p.setInventory(flat.getString(cellNode + "prisoner.armor", ""));
c.setPrisoner(p); c.setPrisoner(p);
} }
@ -332,6 +338,8 @@ public class JailIO {
pris.setOfflinePending(flat.getBoolean(pNode + "offlinePending")); pris.setOfflinePending(flat.getBoolean(pNode + "offlinePending"));
pris.setPreviousPosition(flat.getString(pNode + "previousLocation")); pris.setPreviousPosition(flat.getString(pNode + "previousLocation"));
pris.setPreviousGameMode(flat.getString(pNode + "previousGameMode")); pris.setPreviousGameMode(flat.getString(pNode + "previousGameMode"));
pris.setInventory(flat.getString(pNode + "inventory", ""));
pris.setInventory(flat.getString(pNode + "armor", ""));
j.addPrisoner(pris); j.addPrisoner(pris);
} }
} }

View File

@ -10,7 +10,6 @@ import com.graywolf336.jail.command.CommandHandler;
import com.graywolf336.jail.listeners.BlockListener; import com.graywolf336.jail.listeners.BlockListener;
import com.graywolf336.jail.listeners.EntityListener; import com.graywolf336.jail.listeners.EntityListener;
import com.graywolf336.jail.listeners.PlayerListener; import com.graywolf336.jail.listeners.PlayerListener;
import com.graywolf336.jail.listeners.PlayerPreventionsListener;
public class JailMain extends JavaPlugin { public class JailMain extends JavaPlugin {
private CommandHandler cmdHand; private CommandHandler cmdHand;
@ -34,7 +33,6 @@ public class JailMain extends JavaPlugin {
plm.registerEvents(new BlockListener(), this); plm.registerEvents(new BlockListener(), this);
plm.registerEvents(new EntityListener(), this); plm.registerEvents(new EntityListener(), this);
plm.registerEvents(new PlayerListener(this), this); plm.registerEvents(new PlayerListener(this), this);
plm.registerEvents(new PlayerPreventionsListener(this), this);
//For the time, we will use: //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) //http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html#convert(long, java.util.concurrent.TimeUnit)

View File

@ -2,6 +2,8 @@ package com.graywolf336.jail;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.entity.Player; 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.Cell;
import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.Jail;
@ -155,11 +157,96 @@ public class PrisonerManager {
player.eject(); 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 the cell doesn't equal null, then let's put them in the jail
if(cell != null) { 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 { }else {
//There is no cell we're jailing them to, so stick them in the jail //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);
} }
} }

View File

@ -13,11 +13,11 @@ import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.bukkit.util.io.BukkitObjectInputStream; import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream; import org.bukkit.util.io.BukkitObjectOutputStream;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
public class Util { public class Util {
@ -108,6 +108,54 @@ public class Util {
return Long.valueOf(t); 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.
*
* <p />
*
* 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. * A method to serialize an inventory to Base64 string.
* *
@ -140,7 +188,7 @@ public class Util {
return Base64Coder.encodeLines(outputStream.toByteArray()); return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) { } catch (Exception e) {
throw new IllegalStateException("Unable to save item stacks.", 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); throw new IOException("Unable to decode class type.", e);
} }
} }
/**
* Gets an array of ItemStacks from Base64 string.
*
* <p />
*
* 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);
}
}
} }

View File

@ -2,6 +2,7 @@ package com.graywolf336.jail.beans;
import java.util.HashSet; import java.util.HashSet;
import org.bukkit.Bukkit;
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;
@ -32,6 +33,11 @@ public class Cell {
return this.name; 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. */ /** Sets the prisoner in this cell. */
public void setPrisoner(Prisoner prisoner) { public void setPrisoner(Prisoner prisoner) {
this.p = prisoner; this.p = prisoner;
@ -102,4 +108,20 @@ public class Cell {
return (Chest) this.chest.getLocation().getBlock().getState(); 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;
}
} }

View File

@ -14,7 +14,7 @@ import org.bukkit.Location;
* @version 2.0.1 * @version 2.0.1
*/ */
public class Prisoner { public class Prisoner {
private String name, reason; private String name, reason, inventory, armor;
private boolean muted, offlinePending, teleporting; private boolean muted, offlinePending, teleporting;
private long time; private long time;
private Location previousPosition; private Location previousPosition;
@ -36,6 +36,8 @@ public class Prisoner {
this.teleporting = false; this.teleporting = false;
this.previousPosition = null; this.previousPosition = null;
this.previousGameMode = null; this.previousGameMode = null;
this.inventory = "";
this.armor = "";
} }
/** Gets the name of this player. */ /** Gets the name of this player. */
@ -153,4 +155,24 @@ public class Prisoner {
else if(previous.isEmpty()) return; else if(previous.isEmpty()) return;
else this.previousGameMode = GameMode.valueOf(previous); 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;
}
} }

View File

@ -18,7 +18,7 @@ import com.graywolf336.jail.command.CommandInfo;
import com.graywolf336.jail.command.parameters.JailParameters; import com.graywolf336.jail.command.parameters.JailParameters;
import com.graywolf336.jail.enums.LangString; import com.graywolf336.jail.enums.LangString;
import com.graywolf336.jail.enums.Settings; import com.graywolf336.jail.enums.Settings;
import com.graywolf336.jail.events.PrisonerJailedEvent; import com.graywolf336.jail.events.PrePrisonerJailedEvent;
@CommandInfo( @CommandInfo(
maxArgs = -1, maxArgs = -1,
@ -128,7 +128,7 @@ public class JailCommand implements Command {
Prisoner pris = new Prisoner(params.player(), params.muted(), time, params.reason()); Prisoner pris = new Prisoner(params.player(), params.muted(), time, params.reason());
//call the event //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); jm.getPlugin().getServer().getPluginManager().callEvent(event);
//check if the event is cancelled //check if the event is cancelled

View File

@ -2,12 +2,17 @@ package com.graywolf336.jail.enums;
public enum Settings { public enum Settings {
BROADCASTJAILING("jailing.jail.broadcastJailing"), BROADCASTJAILING("jailing.jail.broadcastJailing"),
COMMANDSONJAIL("jailing.jail.commands"),
DEBUG("system.debug"), DEBUG("system.debug"),
DEFAULTJAIL("jailing.jail.defaultJail"), DEFAULTJAIL("jailing.jail.defaultJail"),
DELETEINVENTORY("jailing.jail.deleteInventory"), DELETEINVENTORY("jailing.jail.deleteInventory"),
IGNORESLEEPINGSTATE("jailing.during.ignoreSleeping"),
JAILDEFAULTTIME("jailing.jail.defaultTime"), JAILDEFAULTTIME("jailing.jail.defaultTime"),
JAILEDGAMEMODE("jailing.jail.gameMode"), JAILEDGAMEMODE("jailing.jail.gameMode"),
JAILEDSTOREINVENTORY("jailing.jail.storeInventory"),
LOGJAILING("jailing.jail.logToConsole"), LOGJAILING("jailing.jail.logToConsole"),
MAXFOODLEVEL("jailing.during.maxFoodLevel"),
MINFOODLEVEL("jailing.during.minFoodLevel"),
RELEASETOPREVIOUSPOSITION("jailing.release.backToPreviousPosition"), RELEASETOPREVIOUSPOSITION("jailing.release.backToPreviousPosition"),
RESTOREPREVIOUSGAMEMODE("jailing.release.restorePreviousGameMode"), RESTOREPREVIOUSGAMEMODE("jailing.release.restorePreviousGameMode"),
UPDATENOTIFICATIONS("system.updateNotifications"); UPDATENOTIFICATIONS("system.updateNotifications");

View File

@ -18,7 +18,7 @@ import com.graywolf336.jail.beans.Prisoner;
* @since 3.0.0 * @since 3.0.0
* @version 1.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 static final HandlerList handlers = new HandlerList();
private boolean cancelled; private boolean cancelled;
private boolean online; private boolean online;
@ -29,7 +29,7 @@ public class PrisonerJailedEvent extends Event implements Cancellable {
private String jailer, cancelMsg; 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 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. * @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 online Whether the player is online or not.
* @param jailer The name of what jailed this prisoner. * @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.jail = jail;
this.cell = cell; this.cell = cell;
this.prisoner = prisoner; this.prisoner = prisoner;

View File

@ -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());
}
}
}

View File

@ -14,6 +14,8 @@ jailing:
during: during:
countDownTimeWhileOffline: false countDownTimeWhileOffline: false
ignoreSleeping: true ignoreSleeping: true
maxFoodLevel: 20
minFoodLevel: 10
openChest: true openChest: true
jail: jail:
automaticMute: true automaticMute: true