From 39cc95b96d33bbf3957c9c25103324bb973ef471 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Sat, 8 Mar 2014 23:39:11 -0600 Subject: [PATCH] The jail stick is now usable. Closes #16. --- .../graywolf336/jail/JailStickManager.java | 40 +++++- .../com/graywolf336/jail/beans/Stick.java | 4 +- .../command/subcommands/JailStickCommand.java | 30 +++++ .../graywolf336/jail/enums/LangString.java | 4 + .../PrePrisonerJailedByJailStickEvent.java | 125 ++++++++++++++++++ .../jail/listeners/PlayerListener.java | 69 ++++++++++ src/main/resources/en.yml | 2 + 7 files changed, 270 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/graywolf336/jail/command/subcommands/JailStickCommand.java create mode 100644 src/main/java/com/graywolf336/jail/events/PrePrisonerJailedByJailStickEvent.java diff --git a/src/main/java/com/graywolf336/jail/JailStickManager.java b/src/main/java/com/graywolf336/jail/JailStickManager.java index eac1c2b..ec28c66 100644 --- a/src/main/java/com/graywolf336/jail/JailStickManager.java +++ b/src/main/java/com/graywolf336/jail/JailStickManager.java @@ -35,12 +35,22 @@ public class JailStickManager { for(String s : config.getStringList("jailstick")) { 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 { this.sticks.put(Material.getMaterial(a[0]), new Stick(a[2], a[1], Long.valueOf(a[3]))); }catch (Exception e) { e.printStackTrace(); pl.getLogger().info(s); 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 * @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()); } @@ -115,7 +125,33 @@ public class JailStickManager { * @param name of the player to check if using one * @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); } + + /** + * 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; + } + } } diff --git a/src/main/java/com/graywolf336/jail/beans/Stick.java b/src/main/java/com/graywolf336/jail/beans/Stick.java index 4a9d351..0876097 100644 --- a/src/main/java/com/graywolf336/jail/beans/Stick.java +++ b/src/main/java/com/graywolf336/jail/beans/Stick.java @@ -22,11 +22,11 @@ public class Stick { return this.jail; } - public String getJailReason() { + public String getReason() { return this.reason; } - public long getJailTime() { + public long getTime() { return this.time; } } diff --git a/src/main/java/com/graywolf336/jail/command/subcommands/JailStickCommand.java b/src/main/java/com/graywolf336/jail/command/subcommands/JailStickCommand.java new file mode 100644 index 0000000..a05e710 --- /dev/null +++ b/src/main/java/com/graywolf336/jail/command/subcommands/JailStickCommand.java @@ -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; + } +} diff --git a/src/main/java/com/graywolf336/jail/enums/LangString.java b/src/main/java/com/graywolf336/jail/enums/LangString.java index 5823a16..c937149 100644 --- a/src/main/java/com/graywolf336/jail/enums/LangString.java +++ b/src/main/java/com/graywolf336/jail/enums/LangString.java @@ -124,6 +124,10 @@ public enum LangString { JAILREMOVALUNSUCCESSFUL ("general"), /** The message sent whenever a jail is successfully removed. */ 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. */ NOCELL ("general"), /** Message sent when needing a cell or something and there are no cells. */ diff --git a/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedByJailStickEvent.java b/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedByJailStickEvent.java new file mode 100644 index 0000000..d70f9db --- /dev/null +++ b/src/main/java/com/graywolf336/jail/events/PrePrisonerJailedByJailStickEvent.java @@ -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}. + * + *

+ * + * 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; + } +} diff --git a/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java b/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java index 0019fbe..51f4455 100644 --- a/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java +++ b/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java @@ -3,12 +3,14 @@ package com.graywolf336.jail.listeners; import java.util.HashSet; import java.util.Set; +import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; +import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.FoodLevelChangeEvent; import org.bukkit.event.player.AsyncPlayerChatEvent; 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.Jail; import com.graywolf336.jail.beans.Prisoner; +import com.graywolf336.jail.beans.Stick; import com.graywolf336.jail.enums.LangString; import com.graywolf336.jail.enums.Settings; +import com.graywolf336.jail.events.PrePrisonerJailedByJailStickEvent; public class PlayerListener implements Listener { 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()); + } + } + } + } + } + } + } + } } diff --git a/src/main/resources/en.yml b/src/main/resources/en.yml index 7b7b0da..866c599 100644 --- a/src/main/resources/en.yml +++ b/src/main/resources/en.yml @@ -19,6 +19,8 @@ language: jailing: '&9jailing' 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.' + jailstickdisabled: '&3Jail stick usage: &cdisabled' + jailstickenabled: '&3Jail stick usage: &aenabled' nocell: '&cNo cell found by the name of %0% in the jail %1%.' nocells: '&cNo cells found in the jail %0%.' nojail: '&cNo jail found by the name of %0%.'