Re-adds handcuffing, fixes config reading and fixes a maven dependency

This commit is contained in:
Kristian Knarvik 2022-03-11 02:23:20 +01:00
parent 544286bd5e
commit 14df4f6d39
6 changed files with 68 additions and 85 deletions

View File

@ -88,7 +88,7 @@
<dependency> <dependency>
<groupId>org.jetbrains</groupId> <groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>
<version>RELEASE</version> <version>23.0.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- End of Test Dependencies --> <!-- End of Test Dependencies -->

View File

@ -162,12 +162,11 @@ public class JailMain extends JavaPlugin {
} }
private List<String> getHeader() { private List<String> getHeader() {
String sep = System.getProperty("line.separator");
List<String> header = new ArrayList<>(); List<String> header = new ArrayList<>();
header.add("###################" + sep header.add("###################");
+ "Jail v" + this.getDescription().getVersion() + " config file" + sep header.add("Jail v" + this.getDescription().getVersion() + " config file");
+ "Note: You -must- use spaces instead of tabs!" + sep + header.add("Note: You -must- use spaces instead of tabs!");
"###################"); header.add("###################");
return header; return header;
} }

View File

@ -17,34 +17,35 @@ import javax.swing.*;
* @since 2.x.x * @since 2.x.x
*/ */
public class JailTimer { public class JailTimer {
private final JailMain pl;
private final JailMain jailMain;
private Timer timer; private Timer timer;
private Long lastTime; private Long lastTime;
private Long afkTime = 0L; private Long afkTime = 0L;
protected JailTimer(JailMain plugin) { protected JailTimer(JailMain plugin) {
this.pl = plugin; this.jailMain = plugin;
try { try {
afkTime = Util.getTime(pl.getConfig().getString(Settings.MAXAFKTIME.getPath())); afkTime = Util.getTime(jailMain.getConfig().getString(Settings.MAXAFKTIME.getPath()));
} catch (Exception e) { } catch (Exception e) {
pl.getLogger().severe("Error while processing the max afk time: " + e.getMessage()); jailMain.getLogger().severe("Error while processing the max afk time: " + e.getMessage());
} }
this.lastTime = System.currentTimeMillis(); this.lastTime = System.currentTimeMillis();
if (pl.getConfig().getBoolean(Settings.USEBUKKITTIMER.getPath())) { if (jailMain.getConfig().getBoolean(Settings.USEBUKKITTIMER.getPath())) {
pl.getLogger().info("Using the Bukkit Scheduler."); jailMain.getLogger().info("Using the Bukkit Scheduler.");
pl.getServer().getScheduler().runTaskTimerAsynchronously(pl, new TimeEvent(), 200, 200); jailMain.getServer().getScheduler().runTaskTimerAsynchronously(jailMain, new TimeEvent(), 200, 200);
} else { } else {
pl.getLogger().info("Using the Java Timer."); jailMain.getLogger().info("Using the Java Timer.");
timer = new Timer(10000, event -> pl.getServer().getScheduler().scheduleSyncDelayedTask(pl, new TimeEvent())); timer = new Timer(10000, event -> jailMain.getServer().getScheduler().scheduleSyncDelayedTask(jailMain, new TimeEvent()));
timer.start(); timer.start();
} }
//Save all the jail information every minute, not every 10 seconds //Save all the jail information every minute, not every 10 seconds
pl.getServer().getScheduler().runTaskTimerAsynchronously(pl, () -> { jailMain.getServer().getScheduler().runTaskTimerAsynchronously(jailMain, () -> {
for (Jail j : pl.getJailManager().getJails()) { for (Jail j : jailMain.getJailManager().getJails()) {
pl.getJailIO().saveJail(j); jailMain.getJailIO().saveJail(j);
} }
}, 1200L, 1200L); }, 1200L, 1200L);
} }
@ -63,58 +64,58 @@ public class JailTimer {
long timePassed = System.currentTimeMillis() - lastTime; long timePassed = System.currentTimeMillis() - lastTime;
lastTime = System.currentTimeMillis(); lastTime = System.currentTimeMillis();
for (Jail j : pl.getJailManager().getJails()) { for (Jail jail : jailMain.getJailManager().getJails()) {
for (Prisoner p : j.getAllPrisoners().values()) { for (Prisoner prisoner : jail.getAllPrisoners().values()) {
//only execute this code if the prisoner's time is more than 0 milliseconds //only execute this code if the prisoner's time is more than 0 milliseconds
//and they don't have any offline pending things //and they don't have any offline pending things
if (p.getRemainingTime() > 0 && !p.isOfflinePending()) { if (prisoner.getRemainingTime() > 0 && !prisoner.isOfflinePending()) {
final Player player = pl.getServer().getPlayer(p.getUUID()); final Player player = jailMain.getServer().getPlayer(prisoner.getUUID());
//Check if the player is offline //Check if the player is offline
if (player == null) { if (player == null) {
//if they are offline AND the config has counting down the time //if they are offline AND the config has counting down the time
//while the prisoner is offline, then let's do it //while the prisoner is offline, then let's do it
if (pl.getConfig().getBoolean(Settings.COUNTDOWNTIMEOFFLINE.getPath())) { if (jailMain.getConfig().getBoolean(Settings.COUNTDOWNTIMEOFFLINE.getPath())) {
//Set their remaining time but if it is less than zero, set it to zero //Set their remaining time but if it is less than zero, set it to zero
long before = p.getRemainingTime(); long before = prisoner.getRemainingTime();
long after = Math.max(0, p.getRemainingTime() - timePassed); long after = Math.max(0, prisoner.getRemainingTime() - timePassed);
PrisonerTimeChangeEvent event = new PrisonerTimeChangeEvent(j, j.getCellPrisonerIsIn(p.getUUID()), p, player, before, after); PrisonerTimeChangeEvent event = new PrisonerTimeChangeEvent(jail, jail.getCellPrisonerIsIn(prisoner.getUUID()), prisoner, player, before, after);
pl.getServer().getPluginManager().callEvent(event); jailMain.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) { if (!event.isCancelled()) {
after = event.getTimeAfterChange(); after = event.getTimeAfterChange();
p.setRemainingTime(after); prisoner.setRemainingTime(after);
if (p.getRemainingTime() == 0) { if (prisoner.getRemainingTime() == 0) {
pl.getPrisonerManager().schedulePrisonerRelease(p); jailMain.getPrisonerManager().schedulePrisonerRelease(prisoner);
} }
} }
} }
} else { } else {
if (afkTime > 0) { if (afkTime > 0) {
p.setAFKTime(p.getAFKTime() + timePassed); prisoner.setAFKTime(prisoner.getAFKTime() + timePassed);
if (p.getAFKTime() > afkTime) { if (prisoner.getAFKTime() > afkTime) {
p.setAFKTime(0); prisoner.setAFKTime(0);
//This is so we kick players on the main thread //This is so we kick players on the main thread
//instead of on the async thread(s), as spigot //instead of on the async thread(s), as spigot
//has a protection against this enabled. //has a protection against this enabled.
pl.getServer().getScheduler().scheduleSyncDelayedTask(pl, () -> player.kickPlayer(Lang.AFKKICKMESSAGE.get())); jailMain.getServer().getScheduler().scheduleSyncDelayedTask(jailMain, () -> player.kickPlayer(Lang.AFKKICKMESSAGE.get()));
} }
} }
//The prisoner isn't offline, so let's count down //The prisoner isn't offline, so let's count down
//Set their remaining time but if it is less than zero, set it to zero //Set their remaining time but if it is less than zero, set it to zero
long before = p.getRemainingTime(); long before = prisoner.getRemainingTime();
long after = Math.max(0, p.getRemainingTime() - timePassed); long after = Math.max(0, prisoner.getRemainingTime() - timePassed);
PrisonerTimeChangeEvent event = new PrisonerTimeChangeEvent(j, j.getCellPrisonerIsIn(p.getUUID()), p, player, before, after); PrisonerTimeChangeEvent event = new PrisonerTimeChangeEvent(jail, jail.getCellPrisonerIsIn(prisoner.getUUID()), prisoner, player, before, after);
pl.getServer().getPluginManager().callEvent(event); jailMain.getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) { if (!event.isCancelled()) {
after = event.getTimeAfterChange(); after = event.getTimeAfterChange();
p.setRemainingTime(after); prisoner.setRemainingTime(after);
if (p.getRemainingTime() == 0) { if (prisoner.getRemainingTime() == 0) {
pl.getPrisonerManager().schedulePrisonerRelease(p); jailMain.getPrisonerManager().schedulePrisonerRelease(prisoner);
} }
} }
} }

View File

@ -35,8 +35,8 @@ import java.util.List;
public class JailTransferCommand implements Command { public class JailTransferCommand implements Command {
private final List<String> commands = Arrays.asList("p", "j", "c"); private final List<String> commands = Arrays.asList("p", "j", "c");
public boolean execute(JailManager jm, CommandSender sender, String... args) { public boolean execute(JailManager jailManager, CommandSender sender, String... args) {
if (jm.getJails().isEmpty()) { if (jailManager.getJails().isEmpty()) {
sender.sendMessage(Lang.NOJAILS.get()); sender.sendMessage(Lang.NOJAILS.get());
return true; return true;
} }
@ -60,26 +60,26 @@ public class JailTransferCommand implements Command {
if (params.getPlayer() == null) { if (params.getPlayer() == null) {
sender.sendMessage(Lang.PROVIDEAPLAYER.get(Lang.TRANSFERRING)); sender.sendMessage(Lang.PROVIDEAPLAYER.get(Lang.TRANSFERRING));
return true; return true;
} else if (!jm.isPlayerJailedByLastKnownUsername(params.getPlayer())) { } else if (!jailManager.isPlayerJailedByLastKnownUsername(params.getPlayer())) {
sender.sendMessage(Lang.NOTJAILED.get(params.getPlayer())); sender.sendMessage(Lang.NOTJAILED.get(params.getPlayer()));
return true; return true;
} }
jm.getPlugin().debug("Checking everything before we transfer: " + params.getPlayer()); jailManager.getPlugin().debug("Checking everything before we transfer: " + params.getPlayer());
//If they didn't provide a jail, tell them we need one //If they didn't provide a jail, tell them we need one
if (params.getJail() == null) { if (params.getJail() == null) {
sender.sendMessage(Lang.PROVIDEAJAIL.get(Lang.TRANSFERRING)); sender.sendMessage(Lang.PROVIDEAJAIL.get(Lang.TRANSFERRING));
return true; return true;
} else if (!jm.isValidJail(params.getJail())) { } else if (!jailManager.isValidJail(params.getJail())) {
//Check if the jail they did provided is not a valid jail //Check if the jail they did provided is not a valid jail
sender.sendMessage(Lang.NOJAIL.get(params.getJail())); sender.sendMessage(Lang.NOJAIL.get(params.getJail()));
return true; return true;
} }
jm.getPlugin().debug("They provided a valid jail, so let's check the target cell."); jailManager.getPlugin().debug("They provided a valid jail, so let's check the target cell.");
Jail target = jm.getJail(params.getJail()); Jail target = jailManager.getJail(params.getJail());
Cell targetCell = null; Cell targetCell = null;
//Check if they provided a cell and if so does it exist //Check if they provided a cell and if so does it exist
@ -95,7 +95,7 @@ public class JailTransferCommand implements Command {
sender.sendMessage(Lang.CELLNOTEMPTY.get(params.getCell())); sender.sendMessage(Lang.CELLNOTEMPTY.get(params.getCell()));
//But suggest the first empty cell we find //But suggest the first empty cell we find
Cell suggestedCell = jm.getJail(params.getCell()).getFirstEmptyCell(); Cell suggestedCell = jailManager.getJail(params.getCell()).getFirstEmptyCell();
if (suggestedCell != null) { if (suggestedCell != null) {
sender.sendMessage(Lang.SUGGESTEDCELL.get(params.getCell(), suggestedCell.getName())); sender.sendMessage(Lang.SUGGESTEDCELL.get(params.getCell(), suggestedCell.getName()));
} else { } else {
@ -107,14 +107,14 @@ public class JailTransferCommand implements Command {
} }
} }
jm.getPlugin().debug("Calling the PrePrisonerTransferredEvent, jail and cell check all came out clean."); jailManager.getPlugin().debug("Calling the PrePrisonerTransferredEvent, jail and cell check all came out clean.");
Prisoner p = jm.getPrisonerByLastKnownName(params.getPlayer()); Prisoner p = jailManager.getPrisonerByLastKnownName(params.getPlayer());
//Throw the custom event before transferring them, allowing another plugin to cancel it. //Throw the custom event before transferring them, allowing another plugin to cancel it.
PrePrisonerTransferredEvent event = new PrePrisonerTransferredEvent(jm.getJailPlayerIsIn(p.getUUID()), PrePrisonerTransferredEvent event = new PrePrisonerTransferredEvent(jailManager.getJailPlayerIsIn(p.getUUID()),
jm.getJailPlayerIsIn(p.getUUID()).getCellPrisonerIsIn(p.getUUID()), jailManager.getJailPlayerIsIn(p.getUUID()).getCellPrisonerIsIn(p.getUUID()),
target, targetCell, p, jm.getPlugin().getServer().getPlayer(p.getUUID()), sender.getName()); target, targetCell, p, jailManager.getPlugin().getServer().getPlayer(p.getUUID()), sender.getName());
jm.getPlugin().getServer().getPluginManager().callEvent(event); jailManager.getPlugin().getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) { if (event.isCancelled()) {
if (event.getCancelledMessage().isEmpty()) { if (event.getCancelledMessage().isEmpty()) {
@ -128,8 +128,8 @@ public class JailTransferCommand implements Command {
} }
//Start the transferring of the prisoner //Start the transferring of the prisoner
jm.getPlugin().getPrisonerManager().transferPrisoner(jm.getJailPlayerIsIn(p.getUUID()), jailManager.getPlugin().getPrisonerManager().transferPrisoner(jailManager.getJailPlayerIsIn(p.getUUID()),
jm.getJailPlayerIsIn(p.getUUID()).getCellPrisonerIsIn(p.getUUID()), jailManager.getJailPlayerIsIn(p.getUUID()).getCellPrisonerIsIn(p.getUUID()),
target, targetCell, p); target, targetCell, p);
//Send the messages to the sender, if no cell then say that but if cell send that as well //Send the messages to the sender, if no cell then say that but if cell send that as well

View File

@ -17,6 +17,7 @@ import org.jetbrains.annotations.NotNull;
* @since 3.0.0 * @since 3.0.0
*/ */
public class PrisonerTimeChangeEvent extends Event implements Cancellable { public class PrisonerTimeChangeEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false; private boolean cancelled = false;
private final Jail jail; private final Jail jail;
@ -37,6 +38,7 @@ public class PrisonerTimeChangeEvent extends Event implements Cancellable {
* @param after the time after it changed * @param after the time after it changed
*/ */
public PrisonerTimeChangeEvent(Jail jail, Cell cell, Prisoner prisoner, Player player, long before, long after) { public PrisonerTimeChangeEvent(Jail jail, Cell cell, Prisoner prisoner, Player player, long before, long after) {
super(true);
this.jail = jail; this.jail = jail;
this.cell = cell; this.cell = cell;
this.prisoner = prisoner; this.prisoner = prisoner;

View File

@ -26,14 +26,12 @@ public class HandCuffListener implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent event) { public void onPlayerMove(PlayerMoveEvent event) {
if (false) {
return;
}
if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId())) { if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId())) {
Location to = jailMain.getHandCuffManager().getLocation(event.getPlayer().getUniqueId()); Location to = jailMain.getHandCuffManager().getLocation(event.getPlayer().getUniqueId());
if (event.getTo() != null) {
to.setPitch(event.getTo().getPitch()); to.setPitch(event.getTo().getPitch());
to.setYaw(event.getTo().getYaw()); to.setYaw(event.getTo().getYaw());
}
tos.put(event.getPlayer().getName(), to); tos.put(event.getPlayer().getName(), to);
event.getPlayer().teleport(to); event.getPlayer().teleport(to);
@ -47,14 +45,13 @@ public class HandCuffListener implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerTeleport(PlayerTeleportEvent event) { public void onPlayerTeleport(PlayerTeleportEvent event) {
if (false) {
return;
}
if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId()) && event.getTo() != tos.get(event.getPlayer().getName())) { if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId()) && event.getTo() != tos.get(event.getPlayer().getName())) {
Location to = jailMain.getHandCuffManager().getLocation(event.getPlayer().getUniqueId()); Location to = jailMain.getHandCuffManager().getLocation(event.getPlayer().getUniqueId());
if (event.getTo() != null) {
to.setPitch(event.getTo().getPitch()); to.setPitch(event.getTo().getPitch());
to.setYaw(event.getTo().getYaw()); to.setYaw(event.getTo().getYaw());
}
tos.put(event.getPlayer().getName(), to); tos.put(event.getPlayer().getName(), to);
event.getPlayer().teleport(to); event.getPlayer().teleport(to);
@ -68,10 +65,6 @@ public class HandCuffListener implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void playerChat(AsyncPlayerChatEvent event) { public void playerChat(AsyncPlayerChatEvent event) {
if (false) {
return;
}
if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId()) && !event.getPlayer().hasPermission("jail.command.handcuff")) { if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId()) && !event.getPlayer().hasPermission("jail.command.handcuff")) {
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.RED + "You are handcuffed and aren't allowed to talk!"); event.getPlayer().sendMessage(ChatColor.RED + "You are handcuffed and aren't allowed to talk!");
@ -80,10 +73,6 @@ public class HandCuffListener implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void blockBreak(BlockBreakEvent event) { public void blockBreak(BlockBreakEvent event) {
if (false) {
return;
}
if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId())) { if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId())) {
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.RED + "You are handcuffed and aren't allowed to break blocks!"); event.getPlayer().sendMessage(ChatColor.RED + "You are handcuffed and aren't allowed to break blocks!");
@ -92,10 +81,6 @@ public class HandCuffListener implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void blockPlace(BlockPlaceEvent event) { public void blockPlace(BlockPlaceEvent event) {
if (false) {
return;
}
if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId())) { if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId())) {
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.RED + "You are handcuffed and aren't allowed to place blocks!"); event.getPlayer().sendMessage(ChatColor.RED + "You are handcuffed and aren't allowed to place blocks!");
@ -104,10 +89,6 @@ public class HandCuffListener implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void preCommands(PlayerCommandPreprocessEvent event) { public void preCommands(PlayerCommandPreprocessEvent event) {
if (false) {
return;
}
if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId()) && !event.getPlayer().hasPermission("jail.command.handcuff") && (!event.getMessage().startsWith("/r") || !event.getMessage().startsWith("/reply"))) { if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId()) && !event.getPlayer().hasPermission("jail.command.handcuff") && (!event.getMessage().startsWith("/r") || !event.getMessage().startsWith("/reply"))) {
event.setCancelled(true); event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.RED + "You are handcuffed and aren't allowed to use commands!"); event.getPlayer().sendMessage(ChatColor.RED + "You are handcuffed and aren't allowed to use commands!");