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>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<version>23.0.0</version>
<scope>compile</scope>
</dependency>
<!-- End of Test Dependencies -->

View File

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

View File

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

View File

@ -35,8 +35,8 @@ import java.util.List;
public class JailTransferCommand implements Command {
private final List<String> commands = Arrays.asList("p", "j", "c");
public boolean execute(JailManager jm, CommandSender sender, String... args) {
if (jm.getJails().isEmpty()) {
public boolean execute(JailManager jailManager, CommandSender sender, String... args) {
if (jailManager.getJails().isEmpty()) {
sender.sendMessage(Lang.NOJAILS.get());
return true;
}
@ -60,26 +60,26 @@ public class JailTransferCommand implements Command {
if (params.getPlayer() == null) {
sender.sendMessage(Lang.PROVIDEAPLAYER.get(Lang.TRANSFERRING));
return true;
} else if (!jm.isPlayerJailedByLastKnownUsername(params.getPlayer())) {
} else if (!jailManager.isPlayerJailedByLastKnownUsername(params.getPlayer())) {
sender.sendMessage(Lang.NOTJAILED.get(params.getPlayer()));
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 (params.getJail() == null) {
sender.sendMessage(Lang.PROVIDEAJAIL.get(Lang.TRANSFERRING));
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
sender.sendMessage(Lang.NOJAIL.get(params.getJail()));
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;
//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()));
//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) {
sender.sendMessage(Lang.SUGGESTEDCELL.get(params.getCell(), suggestedCell.getName()));
} else {
@ -107,14 +107,14 @@ public class JailTransferCommand implements Command {
}
}
jm.getPlugin().debug("Calling the PrePrisonerTransferredEvent, jail and cell check all came out clean.");
Prisoner p = jm.getPrisonerByLastKnownName(params.getPlayer());
jailManager.getPlugin().debug("Calling the PrePrisonerTransferredEvent, jail and cell check all came out clean.");
Prisoner p = jailManager.getPrisonerByLastKnownName(params.getPlayer());
//Throw the custom event before transferring them, allowing another plugin to cancel it.
PrePrisonerTransferredEvent event = new PrePrisonerTransferredEvent(jm.getJailPlayerIsIn(p.getUUID()),
jm.getJailPlayerIsIn(p.getUUID()).getCellPrisonerIsIn(p.getUUID()),
target, targetCell, p, jm.getPlugin().getServer().getPlayer(p.getUUID()), sender.getName());
jm.getPlugin().getServer().getPluginManager().callEvent(event);
PrePrisonerTransferredEvent event = new PrePrisonerTransferredEvent(jailManager.getJailPlayerIsIn(p.getUUID()),
jailManager.getJailPlayerIsIn(p.getUUID()).getCellPrisonerIsIn(p.getUUID()),
target, targetCell, p, jailManager.getPlugin().getServer().getPlayer(p.getUUID()), sender.getName());
jailManager.getPlugin().getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
if (event.getCancelledMessage().isEmpty()) {
@ -128,8 +128,8 @@ public class JailTransferCommand implements Command {
}
//Start the transferring of the prisoner
jm.getPlugin().getPrisonerManager().transferPrisoner(jm.getJailPlayerIsIn(p.getUUID()),
jm.getJailPlayerIsIn(p.getUUID()).getCellPrisonerIsIn(p.getUUID()),
jailManager.getPlugin().getPrisonerManager().transferPrisoner(jailManager.getJailPlayerIsIn(p.getUUID()),
jailManager.getJailPlayerIsIn(p.getUUID()).getCellPrisonerIsIn(p.getUUID()),
target, targetCell, p);
//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
*/
public class PrisonerTimeChangeEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false;
private final Jail jail;
@ -37,6 +38,7 @@ public class PrisonerTimeChangeEvent extends Event implements Cancellable {
* @param after the time after it changed
*/
public PrisonerTimeChangeEvent(Jail jail, Cell cell, Prisoner prisoner, Player player, long before, long after) {
super(true);
this.jail = jail;
this.cell = cell;
this.prisoner = prisoner;

View File

@ -26,14 +26,12 @@ public class HandCuffListener implements Listener {
@EventHandler(ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent event) {
if (false) {
return;
}
if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId())) {
Location to = jailMain.getHandCuffManager().getLocation(event.getPlayer().getUniqueId());
to.setPitch(event.getTo().getPitch());
to.setYaw(event.getTo().getYaw());
if (event.getTo() != null) {
to.setPitch(event.getTo().getPitch());
to.setYaw(event.getTo().getYaw());
}
tos.put(event.getPlayer().getName(), to);
event.getPlayer().teleport(to);
@ -47,14 +45,13 @@ public class HandCuffListener implements Listener {
@EventHandler(ignoreCancelled = true)
public void onPlayerTeleport(PlayerTeleportEvent event) {
if (false) {
return;
}
if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId()) && event.getTo() != tos.get(event.getPlayer().getName())) {
Location to = jailMain.getHandCuffManager().getLocation(event.getPlayer().getUniqueId());
to.setPitch(event.getTo().getPitch());
to.setYaw(event.getTo().getYaw());
if (event.getTo() != null) {
to.setPitch(event.getTo().getPitch());
to.setYaw(event.getTo().getYaw());
}
tos.put(event.getPlayer().getName(), to);
event.getPlayer().teleport(to);
@ -68,10 +65,6 @@ public class HandCuffListener implements Listener {
@EventHandler(ignoreCancelled = true)
public void playerChat(AsyncPlayerChatEvent event) {
if (false) {
return;
}
if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId()) && !event.getPlayer().hasPermission("jail.command.handcuff")) {
event.setCancelled(true);
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)
public void blockBreak(BlockBreakEvent event) {
if (false) {
return;
}
if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
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)
public void blockPlace(BlockPlaceEvent event) {
if (false) {
return;
}
if (jailMain.getHandCuffManager().isHandCuffed(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
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)
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"))) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.RED + "You are handcuffed and aren't allowed to use commands!");