The jail stick is now usable.

Closes #16.
This commit is contained in:
graywolf336 2014-03-08 23:39:11 -06:00
parent 64c4298cca
commit 39cc95b96d
7 changed files with 270 additions and 4 deletions

View File

@ -35,12 +35,22 @@ public class JailStickManager {
for(String s : config.getStringList("jailstick")) { for(String s : config.getStringList("jailstick")) {
String[] a = s.split(","); String[] a = s.split(",");
//Check if the jail given, if any, exists
if(!a[2].isEmpty()) {
if(!pl.getJailManager().isValidJail(a[2])) {
pl.getLogger().warning(s);
pl.getLogger().warning("The above jail stick configuration is invalid and references a jail that doesn't exist.");
continue;
}
}
try { try {
this.sticks.put(Material.getMaterial(a[0]), new Stick(a[2], a[1], Long.valueOf(a[3]))); this.sticks.put(Material.getMaterial(a[0]), new Stick(a[2], a[1], Long.valueOf(a[3])));
}catch (Exception e) { }catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
pl.getLogger().info(s); pl.getLogger().info(s);
pl.getLogger().severe("Unable to create a new stick for " + a[0] + ", see the exception above for details."); pl.getLogger().severe("Unable to create a new stick for " + a[0] + ", see the exception above for details.");
continue;
} }
} }
@ -105,7 +115,7 @@ public class JailStickManager {
* @param player to check if using one * @param player to check if using one
* @return true if the player is using a jail stick, false if not * @return true if the player is using a jail stick, false if not
*/ */
public boolean usingJailStick(Player player) { public boolean isUsingJailStick(Player player) {
return this.stickers.contains(player.getName()); return this.stickers.contains(player.getName());
} }
@ -115,7 +125,33 @@ public class JailStickManager {
* @param name of the player to check if using one * @param name of the player to check if using one
* @return true if the player is using a jail stick, false if not * @return true if the player is using a jail stick, false if not
*/ */
public boolean usingJailStick(String name) { public boolean isUsingJailStick(String name) {
return this.stickers.contains(name); return this.stickers.contains(name);
} }
/**
* Toggles whether the player is using a jail stick, returning the true if enabled false if disabled.
*
* @param name of the person to toggle
* @return true if we enabled it, false if we disabled it.
*/
public boolean toggleUsingStick(Player player) {
return this.toggleUsingStick(player.getName());
}
/**
* Toggles whether the player is using a jail stick, returning the true if enabled false if disabled.
*
* @param name of the person to toggle
* @return true if we enabled it, false if we disabled it.
*/
public boolean toggleUsingStick(String name) {
if(this.stickers.contains(name)) {
this.stickers.remove(name);
return false;
}else {
this.stickers.add(name);
return true;
}
}
} }

View File

@ -22,11 +22,11 @@ public class Stick {
return this.jail; return this.jail;
} }
public String getJailReason() { public String getReason() {
return this.reason; return this.reason;
} }
public long getJailTime() { public long getTime() {
return this.time; return this.time;
} }
} }

View File

@ -0,0 +1,30 @@
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 = true,
pattern = "stick",
permission = "jail.usercmd.jailstick",
usage = "/jail stick"
)
public class JailStickCommand implements Command {
public boolean execute(JailManager jm, CommandSender sender, String... args) throws Exception {
boolean using = jm.getPlugin().getJailStickManager().toggleUsingStick(sender.getName());
if(using) {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.JAILSTICKENABLED));
}else {
sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.JAILSTICKDISABLED));
}
return true;
}
}

View File

@ -124,6 +124,10 @@ public enum LangString {
JAILREMOVALUNSUCCESSFUL ("general"), JAILREMOVALUNSUCCESSFUL ("general"),
/** The message sent whenever a jail is successfully removed. */ /** The message sent whenever a jail is successfully removed. */
JAILREMOVED ("general"), JAILREMOVED ("general"),
/** The message sent whenever a player toggles using jail stick to disabled. */
JAILSTICKDISABLED ("general"),
/** The message sent whenever a player toggles using jail stick to enabled. */
JAILSTICKENABLED ("general"),
/** Message sent when doing something that requires a cell but the given name of a cell doesn't exist. */ /** Message sent when doing something that requires a cell but the given name of a cell doesn't exist. */
NOCELL ("general"), NOCELL ("general"),
/** Message sent when needing a cell or something and there are no cells. */ /** Message sent when needing a cell or something and there are no cells. */

View File

@ -0,0 +1,125 @@
package com.graywolf336.jail.events;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import com.graywolf336.jail.beans.Cell;
import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.beans.Stick;
/**
* Event thrown before we a player is jailed by someone hitting them with a {@link Stick jail stick}.
*
* <p />
*
* This event is called right before we actually jail a player, and is cancellable.
*
* @author graywolf336
* @since 3.0.0
* @version 1.0.0
*/
public class PrePrisonerJailedByJailStickEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false;
private Jail jail;
private Cell cell;
private Prisoner prisoner;
private Player player;
private String jailer, cancelMsg;
private Stick stick;
/**
* Creates a new {@link PrePrisonerJailedByJailStickEvent prisoner jailed by a jail stick event} for the given player before they get sent to jail.
*
* @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 prisoner The prisoner data.
* @param player The player being jailed.
* @param jailer The name of what jailed this prisoner.
* @param stick The {@link Stick jail stick} used.
*/
public PrePrisonerJailedByJailStickEvent(Jail jail, Cell cell, Prisoner prisoner, Player player, String jailer, Stick stick) {
this.jail = jail;
this.cell = cell;
this.prisoner = prisoner;
this.player = player;
this.jailer = jailer;
this.stick = stick;
this.cancelMsg = "";
}
/** Gets the {@link Jail} this prisoner is being sent to. */
public Jail getJail() {
return this.jail;
}
/** Gets the cell we're going to be sending the prisoner to. */
public Cell getCell() {
return this.cell;
}
/** Sets the cell we're going to be sending the prisoner to. */
public void setCell(Cell cell) {
this.cell = cell;
}
/** Gets the {@link Prisoner} data for this prisoner. */
public Prisoner getPrisoner() {
return this.prisoner;
}
/** Gets the instance of the player being jailed. */
public Player getPlayer() {
return this.player;
}
/** Gets the jailer who jailed this prisoner. */
public String getJailer() {
return this.jailer;
}
/**
* Sets who jailed this prisoner.
*
* @param jailer The name to put who is the jailer for this prisoner.
*/
public void setJailer(String jailer) {
this.jailer = jailer;
}
/** Gets the jail stick used. */
public Stick getJailStick() {
return this.stick;
}
/** Checks whether this event is cancelled or not. */
public boolean isCancelled() {
return this.cancelled;
}
/** Sets whether this event should be cancelled. */
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
/** Returns the cancelled message. */
public String getCancelledMessage() {
return this.cancelMsg;
}
/** Sets the cancelled message. */
public void setCancelledMessage(String msg) {
this.cancelMsg = msg;
}
public static HandlerList getHandlerList() {
return handlers;
}
public HandlerList getHandlers() {
return handlers;
}
}

View File

@ -3,12 +3,14 @@ package com.graywolf336.jail.listeners;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent; import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
@ -21,8 +23,10 @@ import com.graywolf336.jail.Util;
import com.graywolf336.jail.beans.Cell; import com.graywolf336.jail.beans.Cell;
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.beans.Stick;
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.PrePrisonerJailedByJailStickEvent;
public class PlayerListener implements Listener { public class PlayerListener implements Listener {
private JailMain pl; private JailMain pl;
@ -149,4 +153,69 @@ public class PlayerListener implements Listener {
} }
} }
} }
@EventHandler(ignoreCancelled=true)
public void jailStickHandling(EntityDamageByEntityEvent event) {
//If the damager and the entity getting damage is not a player,
//we don't want to handle it in this method
if(!(event.getDamager() instanceof Player) || !(event.getEntity() instanceof Player)) return;
Player attacker = (Player) event.getDamager();
Player player = (Player) event.getEntity();
if(pl.getJailStickManager().isUsingJailStick(attacker)) {
if(pl.getJailStickManager().isValidStick(attacker.getItemInHand().getType())) {
if(attacker.hasPermission("jail.usejailstick." + attacker.getItemInHand().getType().toString().toLowerCase())) {
//The person the attacker is trying to jail stick is already jailed, don't handle that
if(pl.getJailManager().isPlayerJailed(player.getName())) {
attacker.sendMessage(pl.getJailIO().getLanguageString(LangString.ALREADYJAILED, player.getName()));
}else {
if(player.hasPermission("jail.cantbejailed")) {
attacker.sendMessage(pl.getJailIO().getLanguageString(LangString.CANTBEJAILED));
}else {
Stick s = pl.getJailStickManager().getStick(attacker.getItemInHand().getType());
Prisoner p = new Prisoner(player.getName(),
pl.getConfig().getBoolean(Settings.AUTOMATICMUTE.getPath()),
s.getTime(), attacker.getName(), s.getReason());
PrePrisonerJailedByJailStickEvent jEvent = new PrePrisonerJailedByJailStickEvent(
pl.getJailManager().getJail(s.getJail()), null, p, player, attacker.getName(), s);
pl.getServer().getPluginManager().callEvent(jEvent);
if(jEvent.isCancelled()) {
if(jEvent.getCancelledMessage().isEmpty())
attacker.sendMessage(pl.getJailIO().getLanguageString(LangString.CANCELLEDBYANOTHERPLUGIN, player.getName()));
else
attacker.sendMessage(jEvent.getCancelledMessage());
}else {
//recall data from the event
Jail j = jEvent.getJail();
Cell c = jEvent.getCell();
p = jEvent.getPrisoner();
player = jEvent.getPlayer();
//Player is not online
if(player == null) {
attacker.sendMessage(pl.getJailIO().getLanguageString(LangString.OFFLINEJAIL,
new String[] { p.getName(), String.valueOf(p.getRemainingTimeInMinutes()) }));
}else {
//Player *is* online
attacker.sendMessage(pl.getJailIO().getLanguageString(LangString.ONLINEJAIL,
new String[] { p.getName(), String.valueOf(p.getRemainingTimeInMinutes()) }));
}
try {
pl.getPrisonerManager().prepareJail(j, c, player, p);
} catch (Exception e) {
attacker.sendMessage(ChatColor.RED + e.getMessage());
}
}
}
}
}
}
}
}
} }

View File

@ -19,6 +19,8 @@ language:
jailing: '&9jailing' jailing: '&9jailing'
jailremovalunsuccessful: '&cThe removal of the jail %0% was unsuccessful because there are prisoners in there, please release or transfer them first.' jailremovalunsuccessful: '&cThe removal of the jail %0% was unsuccessful because there are prisoners in there, please release or transfer them first.'
jailremoved: '&9Jail %0% has been successfully deleted.' jailremoved: '&9Jail %0% has been successfully deleted.'
jailstickdisabled: '&3Jail stick usage: &cdisabled'
jailstickenabled: '&3Jail stick usage: &aenabled'
nocell: '&cNo cell found by the name of %0% in the jail %1%.' nocell: '&cNo cell found by the name of %0% in the jail %1%.'
nocells: '&cNo cells found in the jail %0%.' nocells: '&cNo cells found in the jail %0%.'
nojail: '&cNo jail found by the name of %0%.' nojail: '&cNo jail found by the name of %0%.'