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:
parent
f6686fee8d
commit
54bea41d05
@ -7,9 +7,12 @@ import org.bukkit.Location;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.graywolf336.jail.beans.ConfirmPlayer;
|
||||
import com.graywolf336.jail.beans.CreationPlayer;
|
||||
import com.graywolf336.jail.beans.Jail;
|
||||
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.JailCreationSteps;
|
||||
|
||||
@ -35,6 +38,7 @@ public class JailManager {
|
||||
private HashMap<String, Jail> jails;
|
||||
private HashMap<String, CreationPlayer> jailCreators;
|
||||
private HashMap<String, CreationPlayer> cellCreators;
|
||||
private HashMap<String, ConfirmPlayer> confirms;
|
||||
private JailCreationSteps jcs;
|
||||
private CellCreationSteps ccs;
|
||||
|
||||
@ -43,6 +47,7 @@ public class JailManager {
|
||||
this.jails = new HashMap<String, Jail>();
|
||||
this.jailCreators = new HashMap<String, CreationPlayer>();
|
||||
this.cellCreators = new HashMap<String, CreationPlayer>();
|
||||
this.confirms = new HashMap<String, ConfirmPlayer>();
|
||||
this.jcs = new JailCreationSteps();
|
||||
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.
|
||||
*
|
||||
@ -342,4 +392,36 @@ public class JailManager {
|
||||
public CellCreationSteps getCellCreationSteps() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ import com.graywolf336.jail.command.subcommands.JailListCellsCommand;
|
||||
import com.graywolf336.jail.command.subcommands.JailListCommand;
|
||||
import com.graywolf336.jail.command.subcommands.JailMuteCommand;
|
||||
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.JailStopCommand;
|
||||
import com.graywolf336.jail.command.subcommands.JailTeleInCommand;
|
||||
@ -94,11 +93,13 @@ public class JailHandler {
|
||||
CommandInfo i = c.getClass().getAnnotation(CommandInfo.class);
|
||||
|
||||
// First, let's check if the sender has permission for the command.
|
||||
if(!i.permission().isEmpty()) {
|
||||
if(!sender.hasPermission(i.permission())) {
|
||||
jailmanager.getPlugin().debug("Sender has no permission.");
|
||||
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
|
||||
if(i.needsPlayer() && !(sender instanceof Player)) {
|
||||
@ -179,7 +180,6 @@ public class JailHandler {
|
||||
load(JailListCommand.class);
|
||||
load(JailMuteCommand.class);
|
||||
load(JailReloadCommand.class);
|
||||
load(JailRemoveCellCommand.class);
|
||||
load(JailStatusCommand.class);
|
||||
load(JailStopCommand.class);
|
||||
load(JailTeleInCommand.class);
|
||||
|
@ -3,10 +3,10 @@ package com.graywolf336.jail.command.subcommands;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import com.graywolf336.jail.JailManager;
|
||||
import com.graywolf336.jail.beans.Jail;
|
||||
import com.graywolf336.jail.beans.Prisoner;
|
||||
import com.graywolf336.jail.beans.ConfirmPlayer;
|
||||
import com.graywolf336.jail.command.Command;
|
||||
import com.graywolf336.jail.command.CommandInfo;
|
||||
import com.graywolf336.jail.enums.Confirmation;
|
||||
import com.graywolf336.jail.enums.LangString;
|
||||
|
||||
@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
|
||||
public boolean execute(JailManager jm, CommandSender sender, String... args) {
|
||||
if(args.length == 2) {
|
||||
Jail j = jm.getJail(args[1]);
|
||||
|
||||
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()));
|
||||
if(jm.isConfirming(sender.getName())) {
|
||||
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.ALREADY));
|
||||
}else {
|
||||
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOJAIL, args[1]));
|
||||
}
|
||||
}else {
|
||||
if(jm.getJails().size() == 0) {
|
||||
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOJAILS));
|
||||
}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)));
|
||||
}
|
||||
jm.addConfirming(sender.getName(), new ConfirmPlayer(sender.getName(), args, Confirmation.CLEAR));
|
||||
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.START));
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -145,7 +145,18 @@ public enum LangString {
|
||||
/** The simple word transferring to be put in other parts. */
|
||||
TRANSFERRING ("general"),
|
||||
/** 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;
|
||||
|
||||
|
@ -7,6 +7,11 @@ language:
|
||||
interactionblocks: 'interacting with a block'
|
||||
interactionitems: 'interacting with an item'
|
||||
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:
|
||||
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.'
|
||||
|
Loading…
Reference in New Issue
Block a user