Further work on #3, lots of logic work figured out. The clear command

uses the confirmation method now, just need to test it.
This commit is contained in:
graywolf336 2014-02-13 13:40:23 -06:00
parent f6686fee8d
commit 54bea41d05
6 changed files with 163 additions and 33 deletions

View File

@ -7,9 +7,12 @@ import org.bukkit.Location;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.graywolf336.jail.beans.ConfirmPlayer;
import com.graywolf336.jail.beans.CreationPlayer; import com.graywolf336.jail.beans.CreationPlayer;
import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner; import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.enums.Confirmation;
import com.graywolf336.jail.enums.LangString;
import com.graywolf336.jail.steps.CellCreationSteps; import com.graywolf336.jail.steps.CellCreationSteps;
import com.graywolf336.jail.steps.JailCreationSteps; import com.graywolf336.jail.steps.JailCreationSteps;
@ -35,6 +38,7 @@ public class JailManager {
private HashMap<String, Jail> jails; private HashMap<String, Jail> jails;
private HashMap<String, CreationPlayer> jailCreators; private HashMap<String, CreationPlayer> jailCreators;
private HashMap<String, CreationPlayer> cellCreators; private HashMap<String, CreationPlayer> cellCreators;
private HashMap<String, ConfirmPlayer> confirms;
private JailCreationSteps jcs; private JailCreationSteps jcs;
private CellCreationSteps ccs; private CellCreationSteps ccs;
@ -43,6 +47,7 @@ public class JailManager {
this.jails = new HashMap<String, Jail>(); this.jails = new HashMap<String, Jail>();
this.jailCreators = new HashMap<String, CreationPlayer>(); this.jailCreators = new HashMap<String, CreationPlayer>();
this.cellCreators = new HashMap<String, CreationPlayer>(); this.cellCreators = new HashMap<String, CreationPlayer>();
this.confirms = new HashMap<String, ConfirmPlayer>();
this.jcs = new JailCreationSteps(); this.jcs = new JailCreationSteps();
this.ccs = new CellCreationSteps(); this.ccs = new CellCreationSteps();
} }
@ -213,6 +218,51 @@ public class JailManager {
} }
} }
/**
* Clears a {@link Jail} of all its prisoners if the jail is provided, otherwise it releases all the prisoners in all the jails.
*
* @param jail The name of the jail to release the prisoners in, null if wanting to clear all.
* @return The resulting message to be sent to the caller of this method.
*/
public String clearJailOfPrisoners(String jail) {
//If they don't pass in a jail name, clear all the jails
if(jail != null) {
Jail j = getJail(jail);
if(j != null) {
for(Prisoner p : j.getAllPrisoners()) {
getPlugin().getPrisonerManager().releasePrisoner(getPlugin().getServer().getPlayerExact(p.getName()), p);
}
return getPlugin().getJailIO().getLanguageString(LangString.PRISONERSCLEARED, j.getName());
}else {
return getPlugin().getJailIO().getLanguageString(LangString.NOJAIL, jail);
}
}else {
return clearAllJailsOfAllPrisoners();
}
}
/**
* Clears all the {@link Jail jails} of prisoners by releasing them.
*
* @return The resulting message to be sent to the caller of this method.
*/
public String clearAllJailsOfAllPrisoners() {
//No name of a jail has been passed, so release all of the prisoners in all the jails
if(getJails().size() == 0) {
return getPlugin().getJailIO().getLanguageString(LangString.NOJAILS);
}else {
for(Jail j : getJails()) {
for(Prisoner p : j.getAllPrisoners()) {
getPlugin().getPrisonerManager().releasePrisoner(getPlugin().getServer().getPlayerExact(p.getName()), p);
}
}
return getPlugin().getJailIO().getLanguageString(LangString.PRISONERSCLEARED, getPlugin().getJailIO().getLanguageString(LangString.ALLJAILS));
}
}
/** /**
* Returns whether or not the player is creating a jail or a cell. * Returns whether or not the player is creating a jail or a cell.
* *
@ -342,4 +392,36 @@ public class JailManager {
public CellCreationSteps getCellCreationSteps() { public CellCreationSteps getCellCreationSteps() {
return this.ccs; return this.ccs;
} }
/** Adds something to the confirming list. */
public void addConfirming(String name, ConfirmPlayer confirmer) {
this.confirms.put(name, confirmer);
}
/** Removes a name from the confirming list. */
public void removeConfirming(String name) {
this.confirms.remove(name);
}
/** Checks if the given name is confirming something. */
public boolean isConfirming(String name) {
if(this.confirmingHasExpired(name)) this.removeConfirming(name);
return this.confirms.containsKey(name);
}
/** Returns true if the confirmation has expired, false if it is still valid. */
public boolean confirmingHasExpired(String name) {
return this.confirms.get(name).getExpiryTime() <= (System.currentTimeMillis() + 5000L);
}
/** Returns the original arguments for what we are confirming. */
public String[] getOriginalArgs(String name) {
return this.confirms.get(name).getArguments();
}
/** Returns what the given name is confirming. */
public Confirmation getWhatIsConfirming(String name) {
return this.confirms.get(name).getConfirming();
}
} }

View File

@ -23,7 +23,6 @@ import com.graywolf336.jail.command.subcommands.JailListCellsCommand;
import com.graywolf336.jail.command.subcommands.JailListCommand; import com.graywolf336.jail.command.subcommands.JailListCommand;
import com.graywolf336.jail.command.subcommands.JailMuteCommand; import com.graywolf336.jail.command.subcommands.JailMuteCommand;
import com.graywolf336.jail.command.subcommands.JailReloadCommand; import com.graywolf336.jail.command.subcommands.JailReloadCommand;
import com.graywolf336.jail.command.subcommands.JailRemoveCellCommand;
import com.graywolf336.jail.command.subcommands.JailStatusCommand; import com.graywolf336.jail.command.subcommands.JailStatusCommand;
import com.graywolf336.jail.command.subcommands.JailStopCommand; import com.graywolf336.jail.command.subcommands.JailStopCommand;
import com.graywolf336.jail.command.subcommands.JailTeleInCommand; import com.graywolf336.jail.command.subcommands.JailTeleInCommand;
@ -94,10 +93,12 @@ public class JailHandler {
CommandInfo i = c.getClass().getAnnotation(CommandInfo.class); CommandInfo i = c.getClass().getAnnotation(CommandInfo.class);
// First, let's check if the sender has permission for the command. // First, let's check if the sender has permission for the command.
if(!sender.hasPermission(i.permission())) { if(!i.permission().isEmpty()) {
jailmanager.getPlugin().debug("Sender has no permission."); if(!sender.hasPermission(i.permission())) {
sender.sendMessage(jailmanager.getPlugin().getJailIO().getLanguageString(LangString.NOPERMISSION)); jailmanager.getPlugin().debug("Sender has no permission.");
return true; sender.sendMessage(jailmanager.getPlugin().getJailIO().getLanguageString(LangString.NOPERMISSION));
return true;
}
} }
// Next, let's check if we need a player and then if the sender is actually a player // Next, let's check if we need a player and then if the sender is actually a player
@ -179,7 +180,6 @@ public class JailHandler {
load(JailListCommand.class); load(JailListCommand.class);
load(JailMuteCommand.class); load(JailMuteCommand.class);
load(JailReloadCommand.class); load(JailReloadCommand.class);
load(JailRemoveCellCommand.class);
load(JailStatusCommand.class); load(JailStatusCommand.class);
load(JailStopCommand.class); load(JailStopCommand.class);
load(JailTeleInCommand.class); load(JailTeleInCommand.class);

View File

@ -3,10 +3,10 @@ package com.graywolf336.jail.command.subcommands;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import com.graywolf336.jail.JailManager; import com.graywolf336.jail.JailManager;
import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.ConfirmPlayer;
import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.command.Command; import com.graywolf336.jail.command.Command;
import com.graywolf336.jail.command.CommandInfo; import com.graywolf336.jail.command.CommandInfo;
import com.graywolf336.jail.enums.Confirmation;
import com.graywolf336.jail.enums.LangString; import com.graywolf336.jail.enums.LangString;
@CommandInfo( @CommandInfo(
@ -21,31 +21,11 @@ public class JailClearCommand implements Command {
// If Jail is specified unjails all the prisoners from that Jail (new feature) otherwise it unjails all the prisoners from all the jails // If Jail is specified unjails all the prisoners from that Jail (new feature) otherwise it unjails all the prisoners from all the jails
public boolean execute(JailManager jm, CommandSender sender, String... args) { public boolean execute(JailManager jm, CommandSender sender, String... args) {
if(args.length == 2) { if(jm.isConfirming(sender.getName())) {
Jail j = jm.getJail(args[1]); sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.ALREADY));
if(j != null) {
for(Prisoner p : j.getAllPrisoners()) {
jm.getPlugin().getPrisonerManager().releasePrisoner(jm.getPlugin().getServer().getPlayerExact(p.getName()), p);
}
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PRISONERSCLEARED, j.getName()));
}else {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOJAIL, args[1]));
}
}else { }else {
if(jm.getJails().size() == 0) { jm.addConfirming(sender.getName(), new ConfirmPlayer(sender.getName(), args, Confirmation.CLEAR));
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOJAILS)); sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.START));
}else {
for(Jail j : jm.getJails()) {
for(Prisoner p : j.getAllPrisoners()) {
jm.getPlugin().getPrisonerManager().releasePrisoner(jm.getPlugin().getServer().getPlayerExact(p.getName()), p);
}
}
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.PRISONERSCLEARED,
jm.getPlugin().getJailIO().getLanguageString(LangString.ALLJAILS)));
}
} }
return true; return true;

View File

@ -0,0 +1,52 @@
package com.graywolf336.jail.command.subcommands;
import org.bukkit.command.CommandSender;
import com.graywolf336.jail.JailManager;
import com.graywolf336.jail.command.Command;
import com.graywolf336.jail.command.CommandInfo;
import com.graywolf336.jail.enums.LangString;
@CommandInfo(
maxArgs = 0,
minimumArgs = 0,
needsPlayer = false,
pattern = "confirm|con",
permission = "",
usage = "/jail confirm"
)
public class JailConfirmCommand implements Command{
public boolean execute(JailManager jm, CommandSender sender, String... args) {
//Check if the sender is actually confirming something.
if(jm.isConfirming(sender.getName())) {
if(jm.confirmingHasExpired(sender.getName())) {
//Their confirmation time frame has closed
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.EXPIRED));
}else {
switch(jm.getWhatIsConfirming(sender.getName())) {
case CLEAR:
//Copy the original arguments for easy access
String[] cArgs = jm.getOriginalArgs(sender.getName());
//Clear a jail if the args length is two, else send null
String msg = jm.clearJailOfPrisoners(cArgs.length == 2 ? cArgs[1] : null);
//Send the message we got back
sender.sendMessage(msg);
//Remove them from confirming so they can't do it again
jm.removeConfirming(sender.getName());
break;
default:
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOTHING));
jm.removeConfirming(sender.getName());
break;
}
}
}else {
//They aren't confirming anything right now.
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOTHING));
}
return true;
}
}

View File

@ -145,7 +145,18 @@ public enum LangString {
/** The simple word transferring to be put in other parts. */ /** The simple word transferring to be put in other parts. */
TRANSFERRING ("general"), TRANSFERRING ("general"),
/** The message sent whenever someone does a command we don't know. */ /** The message sent whenever someone does a command we don't know. */
UNKNOWNCOMMAND ("general"); UNKNOWNCOMMAND ("general"),
//Confirming action messages.
/** The message sent when the sender is already confirming something. */
ALREADY ("confirm"),
/** The message sent when their confirmation has expired. */
EXPIRED ("confirm"),
/** The message sent to the sender when they tried to confirm something but don't have anything to confirm. */
NOTHING ("confirm"),
/** The message sent to the sender when they type something and need to confirm it. */
START ("confirm");
private String section, name; private String section, name;

View File

@ -7,6 +7,11 @@ language:
interactionblocks: 'interacting with a block' interactionblocks: 'interacting with a block'
interactionitems: 'interacting with an item' interactionitems: 'interacting with an item'
moving: 'trying to escape' moving: 'trying to escape'
confirm:
already: "&cYou are already confirming something else, please type '&b/jail confirm&c' to confirm it."
expired: '&cYour confirmation expired already, retype what you are trying to confirm.'
nothing: '&cYou are not confirming anything.'
start: "&cPlease type '&b/jail confirm&c' to confirm we should continue."
general: general:
alljails: 'all the jails' alljails: 'all the jails'
cellremovalunsuccessful: '&cThe removal of cell %0% from jail %1% was unsuccessful because there is a prisoner in there still. Release or transfer before trying to remove the cell again.' cellremovalunsuccessful: '&cThe removal of cell %0% from jail %1% was unsuccessful because there is a prisoner in there still. Release or transfer before trying to remove the cell again.'