Skip to content

Commit

Permalink
refactored listener registration and unregistration for quit listener
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolf2323 committed Oct 12, 2023
1 parent 9287e32 commit 1de71fa
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions src/main/java/org/betonquest/betonquest/events/FolderEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.betonquest.betonquest.exceptions.InstructionParseException;
import org.betonquest.betonquest.exceptions.QuestRuntimeException;
import org.betonquest.betonquest.id.EventID;
import org.betonquest.betonquest.utils.PlayerConverter;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
Expand All @@ -17,18 +16,16 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

/**
* Folder event is a collection of other events, that can be run after a delay and the events can be randomly chosen to
* run or not.
*/
@SuppressWarnings("PMD.CommentRequired")
public class FolderEvent extends QuestEvent implements Listener {
public class FolderEvent extends QuestEvent {
private final Random randomGenerator = new Random();

private final VariableNumber delay;
Expand All @@ -45,8 +42,6 @@ public class FolderEvent extends QuestEvent implements Listener {

private final boolean cancelOnLogout;

private final Set<UUID> cancelled;

public FolderEvent(final Instruction instruction) throws InstructionParseException {
super(instruction, false);
staticness = true;
Expand All @@ -58,7 +53,6 @@ public FolderEvent(final Instruction instruction) throws InstructionParseExcepti
ticks = instruction.hasArgument("ticks");
minutes = instruction.hasArgument("minutes");
cancelOnLogout = instruction.hasArgument("cancelOnLogout");
cancelled = new HashSet<>();
}

@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity", "PMD.CognitiveComplexity"})
Expand Down Expand Up @@ -88,12 +82,12 @@ protected Void execute(final Profile profile) throws QuestRuntimeException {
BetonQuest.event(profile, event);
}
} else if (execPeriod == null) {
register();
final FolderEventCanceller eventCanceller = createFolderEventCanceller(profile);
new BukkitRunnable() {
@Override
public void run() {
unregister();
if (cancelled.remove(profile.getProfileUUID())) {
eventCanceller.destroy();
if (eventCanceller.isCancelled()) {
return;
}
for (final EventID event : chosenList) {
Expand All @@ -107,13 +101,13 @@ public void run() {
BetonQuest.event(profile, event);
}
if (!chosenList.isEmpty()) {
register();
final FolderEventCanceller eventCanceller = createFolderEventCanceller(profile);
new BukkitRunnable() {
@Override
public void run() {
final EventID event = chosenList.pollFirst();
if (cancelled.remove(profile.getProfileUUID()) || event == null) {
unregister();
if (eventCanceller.isCancelled() || event == null) {
eventCanceller.destroy();
this.cancel();
return;
}
Expand All @@ -125,6 +119,14 @@ public void run() {
return null;
}

private FolderEventCanceller createFolderEventCanceller(final Profile profile) {
if (cancelOnLogout) {
return new QuitListener(profile.getProfileUUID());
} else {
return () -> false;
}
}

private Long getInTicks(final VariableNumber timeVariable, final Profile profile) throws QuestRuntimeException {
if (timeVariable == null) {
return null;
Expand All @@ -143,22 +145,40 @@ private Long getInTicks(final VariableNumber timeVariable, final Profile profile
return time;
}

private void register() {
if (cancelOnLogout) {
BetonQuest.getInstance().getServer().getPluginManager().registerEvents(this, BetonQuest.getInstance());
private interface FolderEventCanceller {
boolean isCancelled();

default void destroy() {
// Empty
}
}

private void unregister() {
if (cancelOnLogout) {
PlayerQuitEvent.getHandlerList().unregister(this);
/**
* Registers the quit listener if the event should be cancelled on logout.
*/
private static class QuitListener implements FolderEventCanceller, Listener {
private final UUID playerUuid;

private boolean cancelled;

public QuitListener(final UUID playerUuid) {
this.playerUuid = playerUuid;
BetonQuest.getInstance().getServer().getPluginManager().registerEvents(this, BetonQuest.getInstance());
}
}

@EventHandler
public void onPlayerQuit(final PlayerQuitEvent event) {
if (cancelOnLogout) {
cancelled.add(PlayerConverter.getID(event.getPlayer()).getProfileUUID());
@EventHandler
public void onPlayerQuit(final PlayerQuitEvent event) {
cancelled = cancelled || event.getPlayer().getUniqueId().equals(playerUuid);
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void destroy() {
PlayerQuitEvent.getHandlerList().unregister(this);
}
}
}

0 comments on commit 1de71fa

Please sign in to comment.