Fix #91, fix #109, and fix #100.

This should work around a lot of the plugins out there which handle
inventories per world. I might have missed a few things here and there,
but that's what testing by others is for. :)
This commit is contained in:
graywolf336 2016-03-22 12:21:20 -05:00
parent bd6b9adefb
commit 92e6f5e641
2 changed files with 180 additions and 163 deletions

View File

@ -172,7 +172,7 @@ public class PrisonerManager {
* @param player who is the prisoner * @param player who is the prisoner
* @param prisoner data containing everything pertaining to them * @param prisoner data containing everything pertaining to them
*/ */
protected void jailPrisoner(Jail jail, ICell cell, Player player, Prisoner prisoner) { protected void jailPrisoner(final Jail jail, ICell cell, final Player player, final Prisoner prisoner) {
if(cell instanceof NoCell) if(cell instanceof NoCell)
cell = null; cell = null;
else if(cell instanceof AnyCell) else if(cell instanceof AnyCell)
@ -256,94 +256,103 @@ public class PrisonerManager {
//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)) {
List<String> blacklist = pl.getConfig().getStringList(Settings.JAILEDINVENTORYBLACKLIST.getPath());
//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) {
if(item != null) {
if(!Util.isStringInsideList(item.getType().toString(), blacklist)) {
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) {
if(item != null) {
if(!Util.isStringInsideList(item.getType().toString(), blacklist)) {
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();
}else {
for(ItemStack item : player.getInventory().getContents())
if(item != null)
if(Util.isStringInsideList(item.getType().toString(), blacklist))
player.getInventory().remove(item);
for(ItemStack item : player.getInventory().getArmorContents())
if(item != null)
if(Util.isStringInsideList(item.getType().toString(), blacklist))
player.getInventory().remove(item);
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 cell's teleport location //Teleport them to the cell's teleport location
//they will now be placed in jail. //they will now be placed in jail.
pl.debug("Teleporting " + player.getName() + " to " + jail.getName() + " in the cell " + cell.getName() + "'s in: " + jail.getTeleportIn().toString()); pl.debug("Teleporting " + player.getName() + " to " + jail.getName() + " in the cell " + cell.getName() + "'s in: " + jail.getTeleportIn().toString());
player.teleport(cell.getTeleport()); player.teleport(cell.getTeleport());
}else {
//There is no cell we're jailing them to, so stick them in the jail //check if we store the inventory
if(pl.getConfig().getBoolean(Settings.JAILEDSTOREINVENTORY.getPath(), true)) { if(pl.getConfig().getBoolean(Settings.JAILEDSTOREINVENTORY.getPath(), true)) {
List<String> blacklist = pl.getConfig().getStringList(Settings.JAILEDINVENTORYBLACKLIST.getPath()); final ICell theCell = cell;
pl.getServer().getScheduler().runTaskLater(pl, new Runnable() {
public void run() {
List<String> blacklist = pl.getConfig().getStringList(Settings.JAILEDINVENTORYBLACKLIST.getPath());
//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(theCell.hasChest()) {
//Get the chest's inventory and then clear it
Inventory chest = theCell.getChest().getInventory();
chest.clear();
for(ItemStack item : player.getInventory().getContents()) //Get the separate inventory, so we can iterate of them
if(item != null) ItemStack[] inventory = player.getInventory().getContents();
if(Util.isStringInsideList(item.getType().toString(), blacklist)) ItemStack[] armor = player.getInventory().getArmorContents();
player.getInventory().remove(item);
for(ItemStack item : player.getInventory().getArmorContents()) for(ItemStack item : inventory) {
if(item != null) if(item != null) {
if(Util.isStringInsideList(item.getType().toString(), blacklist)) if(!Util.isStringInsideList(item.getType().toString(), blacklist)) {
player.getInventory().remove(item); 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);
}
}
}
}
String[] inv = Util.playerInventoryToBase64(player.getInventory()); for(ItemStack item : armor) {
prisoner.setInventory(inv[0]); if(item != null) {
prisoner.setArmor(inv[1]); if(!Util.isStringInsideList(item.getType().toString(), blacklist)) {
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().setArmorContents(null);
player.getInventory().clear(); player.getInventory().clear();
}else {
for(ItemStack item : player.getInventory().getContents())
if(item != null)
if(Util.isStringInsideList(item.getType().toString(), blacklist))
player.getInventory().remove(item);
for(ItemStack item : player.getInventory().getArmorContents())
if(item != null)
if(Util.isStringInsideList(item.getType().toString(), blacklist))
player.getInventory().remove(item);
String[] inv = Util.playerInventoryToBase64(player.getInventory());
prisoner.setInventory(inv[0]);
prisoner.setArmor(inv[1]);
player.getInventory().setArmorContents(null);
player.getInventory().clear();
}
};
}, 10);
} }
}else {
//Teleport them to the jail's teleport in location //Teleport them to the jail's teleport in location
//They will now be placed in jail. //They will now be placed in jail.
pl.debug("Teleporting " + player.getName() + " to " + jail.getName() + "'s in: " + jail.getTeleportIn().toString()); pl.debug("Teleporting " + player.getName() + " to " + jail.getName() + "'s in: " + jail.getTeleportIn().toString());
player.teleport(jail.getTeleportIn()); player.teleport(jail.getTeleportIn());
//There is no cell we're jailing them to, so stick them in the jail
if(pl.getConfig().getBoolean(Settings.JAILEDSTOREINVENTORY.getPath(), true)) {
pl.getServer().getScheduler().runTaskLater(pl, new Runnable() {
public void run() {
List<String> blacklist = pl.getConfig().getStringList(Settings.JAILEDINVENTORYBLACKLIST.getPath());
for(ItemStack item : player.getInventory().getContents())
if(item != null)
if(Util.isStringInsideList(item.getType().toString(), blacklist))
player.getInventory().remove(item);
for(ItemStack item : player.getInventory().getArmorContents())
if(item != null)
if(Util.isStringInsideList(item.getType().toString(), blacklist))
player.getInventory().remove(item);
String[] inv = Util.playerInventoryToBase64(player.getInventory());
prisoner.setInventory(inv[0]);
prisoner.setArmor(inv[1]);
player.getInventory().setArmorContents(null);
player.getInventory().clear();
};
}, 10);
}
} }
//Set them to not allowing teleporting, as we are not going to be moving them anymore //Set them to not allowing teleporting, as we are not going to be moving them anymore
@ -437,7 +446,7 @@ public class PrisonerManager {
* @throws PrisonerRequiredException when the provided prisoner data is null. * @throws PrisonerRequiredException when the provided prisoner data is null.
* *
*/ */
public void unJail(Jail jail, ICell cell, Player player, Prisoner prisoner, CommandSender sender) throws AsyncUnJailingNotSupportedException, JailRequiredException, PrisonerRequiredException { public void unJail(final Jail jail, ICell cell, final Player player, final Prisoner prisoner, final CommandSender sender) throws AsyncUnJailingNotSupportedException, JailRequiredException, PrisonerRequiredException {
if(!pl.getServer().isPrimaryThread()) throw new AsyncUnJailingNotSupportedException(); if(!pl.getServer().isPrimaryThread()) throw new AsyncUnJailingNotSupportedException();
//Do some checks of whether the passed params are null. //Do some checks of whether the passed params are null.
@ -468,30 +477,6 @@ public class PrisonerManager {
player.eject(); player.eject();
} }
//In case we had set their sleeping state to be ignored
//let's enable their sleeping state taking place again
player.setSleepingIgnored(false);
//If the config has us teleporting them back to their
//previous position then let's do that
boolean tpd = false;
if(pl.getConfig().getBoolean(Settings.RELEASETOPREVIOUSPOSITION.getPath(), false)) {
if(prisoner.getPreviousLocation() != null)
tpd = player.teleport(prisoner.getPreviousLocation());
}
//If they haven't already been teleported and the config has us to teleport on release,
//then we teleport players to the jail's free spot
if(!tpd && pl.getConfig().getBoolean(Settings.TELEPORTONRELEASE.getPath(), true)) {
player.teleport(jail.getTeleportFree());
}
//If we are to restore their previous gamemode and we have it stored,
//then by all means let's restore it
if(pl.getConfig().getBoolean(Settings.RESTOREPREVIOUSGAMEMODE.getPath(), false)) {
player.setGameMode(prisoner.getPreviousGameMode());
}
//Now, let's restore their inventory if we can store it but //Now, let's restore their inventory if we can store it but
//first up is clearing their inventory...if we can store it //first up is clearing their inventory...if we can store it
boolean store = pl.getConfig().getBoolean(Settings.JAILEDSTOREINVENTORY.getPath(), true); boolean store = pl.getConfig().getBoolean(Settings.JAILEDSTOREINVENTORY.getPath(), true);
@ -545,6 +530,34 @@ public class PrisonerManager {
jail.removePrisoner(prisoner); jail.removePrisoner(prisoner);
} }
//In case we had set their sleeping state to be ignored
//let's enable their sleeping state taking place again
player.setSleepingIgnored(false);
pl.getServer().getScheduler().runTaskLater(pl, new Runnable() {
public void run() {
//If the config has us teleporting them back to their
//previous position then let's do that
boolean tpd = false;
if(pl.getConfig().getBoolean(Settings.RELEASETOPREVIOUSPOSITION.getPath(), false)) {
if(prisoner.getPreviousLocation() != null)
tpd = player.teleport(prisoner.getPreviousLocation());
}
//If they haven't already been teleported and the config has us to teleport on release,
//then we teleport players to the jail's free spot
if(!tpd && pl.getConfig().getBoolean(Settings.TELEPORTONRELEASE.getPath(), true)) {
player.teleport(jail.getTeleportFree());
}
//If we are to restore their previous gamemode and we have it stored,
//then by all means let's restore it
if(pl.getConfig().getBoolean(Settings.RESTOREPREVIOUSGAMEMODE.getPath(), false)) {
player.setGameMode(prisoner.getPreviousGameMode());
}
};
}, 5);
//Get the commands to execute prisoners are unjailed //Get the commands to execute prisoners are unjailed
//replace all of the %p% so that the commands can have a player name in them //replace all of the %p% so that the commands can have a player name in them
for(String command : pl.getConfig().getStringList(Settings.COMMANDSONRELEASE.getPath())) { for(String command : pl.getConfig().getStringList(Settings.COMMANDSONRELEASE.getPath())) {

View File

@ -48,71 +48,75 @@ public class JailingListener implements Listener {
} }
@EventHandler @EventHandler
public void setInmatesClothing(PrisonerJailedEvent event) { public void setInmatesClothing(final PrisonerJailedEvent event) {
if(pl.getConfig().getBoolean(Settings.CLOTHINGENABLED.getPath())) { if(pl.getConfig().getBoolean(Settings.CLOTHINGENABLED.getPath())) {
String[] helmet = pl.getConfig().getString(Settings.CLOTHINGHELMET.getPath()).toUpperCase().split("~"); pl.getServer().getScheduler().runTaskLater(pl, new Runnable() {
switch(helmet.length) { public void run() {
case 1: String[] helmet = pl.getConfig().getString(Settings.CLOTHINGHELMET.getPath()).toUpperCase().split("~");
event.getPlayer().getInventory().setHelmet(new ItemStack(Material.valueOf(helmet[0]))); switch(helmet.length) {
break; case 1:
case 2: event.getPlayer().getInventory().setHelmet(new ItemStack(Material.valueOf(helmet[0])));
ItemStack item = new ItemStack(Material.valueOf(helmet[0])); break;
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta(); case 2:
String[] colors = helmet[1].split(","); ItemStack item = new ItemStack(Material.valueOf(helmet[0]));
meta.setColor(Color.fromBGR(Integer.parseInt(colors[2]), Integer.parseInt(colors[1]), Integer.parseInt(colors[0]))); LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
item.setItemMeta(meta); String[] colors = helmet[1].split(",");
event.getPlayer().getInventory().setHelmet(item); meta.setColor(Color.fromBGR(Integer.parseInt(colors[2]), Integer.parseInt(colors[1]), Integer.parseInt(colors[0])));
default: item.setItemMeta(meta);
break; event.getPlayer().getInventory().setHelmet(item);
} default:
break;
}
String[] chest = pl.getConfig().getString(Settings.CLOTHINGCHEST.getPath()).toUpperCase().split("~"); String[] chest = pl.getConfig().getString(Settings.CLOTHINGCHEST.getPath()).toUpperCase().split("~");
switch(chest.length) { switch(chest.length) {
case 1: case 1:
event.getPlayer().getInventory().setChestplate(new ItemStack(Material.valueOf(chest[0]))); event.getPlayer().getInventory().setChestplate(new ItemStack(Material.valueOf(chest[0])));
break; break;
case 2: case 2:
ItemStack item = new ItemStack(Material.valueOf(chest[0])); ItemStack item = new ItemStack(Material.valueOf(chest[0]));
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta(); LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
String[] colors = chest[1].split(","); String[] colors = chest[1].split(",");
meta.setColor(Color.fromBGR(Integer.parseInt(colors[2]), Integer.parseInt(colors[1]), Integer.parseInt(colors[0]))); meta.setColor(Color.fromBGR(Integer.parseInt(colors[2]), Integer.parseInt(colors[1]), Integer.parseInt(colors[0])));
item.setItemMeta(meta); item.setItemMeta(meta);
event.getPlayer().getInventory().setChestplate(item); event.getPlayer().getInventory().setChestplate(item);
default: default:
break; break;
} }
String[] legs = pl.getConfig().getString(Settings.CLOTHINGLEGS.getPath()).toUpperCase().split("~"); String[] legs = pl.getConfig().getString(Settings.CLOTHINGLEGS.getPath()).toUpperCase().split("~");
switch(legs.length) { switch(legs.length) {
case 1: case 1:
event.getPlayer().getInventory().setLeggings(new ItemStack(Material.valueOf(legs[0]))); event.getPlayer().getInventory().setLeggings(new ItemStack(Material.valueOf(legs[0])));
break; break;
case 2: case 2:
ItemStack item = new ItemStack(Material.valueOf(legs[0])); ItemStack item = new ItemStack(Material.valueOf(legs[0]));
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta(); LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
String[] colors = legs[1].split(","); String[] colors = legs[1].split(",");
meta.setColor(Color.fromBGR(Integer.parseInt(colors[2]), Integer.parseInt(colors[1]), Integer.parseInt(colors[0]))); meta.setColor(Color.fromBGR(Integer.parseInt(colors[2]), Integer.parseInt(colors[1]), Integer.parseInt(colors[0])));
item.setItemMeta(meta); item.setItemMeta(meta);
event.getPlayer().getInventory().setLeggings(item); event.getPlayer().getInventory().setLeggings(item);
default: default:
break; break;
} }
String[] boots = pl.getConfig().getString(Settings.CLOTHINGBOOTS.getPath()).toUpperCase().split("~"); String[] boots = pl.getConfig().getString(Settings.CLOTHINGBOOTS.getPath()).toUpperCase().split("~");
switch(boots.length) { switch(boots.length) {
case 1: case 1:
event.getPlayer().getInventory().setBoots(new ItemStack(Material.valueOf(boots[0]))); event.getPlayer().getInventory().setBoots(new ItemStack(Material.valueOf(boots[0])));
break; break;
case 2: case 2:
ItemStack item = new ItemStack(Material.valueOf(boots[0])); ItemStack item = new ItemStack(Material.valueOf(boots[0]));
LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta(); LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
String[] colors = boots[1].split(","); String[] colors = boots[1].split(",");
meta.setColor(Color.fromBGR(Integer.parseInt(colors[2]), Integer.parseInt(colors[1]), Integer.parseInt(colors[0]))); meta.setColor(Color.fromBGR(Integer.parseInt(colors[2]), Integer.parseInt(colors[1]), Integer.parseInt(colors[0])));
item.setItemMeta(meta); item.setItemMeta(meta);
event.getPlayer().getInventory().setBoots(item); event.getPlayer().getInventory().setBoots(item);
default: default:
break; break;
} }
};
}, 20);
} }
} }
} }