Make event priority configurable for three key events

feat/data-edit-commands
William 2 years ago
parent 51cf7beeb8
commit 3bfea58f35

@ -0,0 +1,37 @@
package net.william278.husksync.listener;
import net.william278.husksync.config.Settings;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.jetbrains.annotations.NotNull;
public interface BukkitDeathEventListener extends Listener {
boolean handleEvent(@NotNull Settings.EventType type, @NotNull Settings.EventPriority priority);
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
default void onPlayerDeathHighest(@NotNull PlayerDeathEvent event) {
if (handleEvent(Settings.EventType.DEATH_LISTENER, Settings.EventPriority.HIGHEST)) {
handlePlayerDeath(event);
}
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
default void onPlayerDeath(@NotNull PlayerDeathEvent event) {
if (handleEvent(Settings.EventType.DEATH_LISTENER, Settings.EventPriority.NORMAL)) {
handlePlayerDeath(event);
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
default void onPlayerDeathLowest(@NotNull PlayerDeathEvent event) {
if (handleEvent(Settings.EventType.DEATH_LISTENER, Settings.EventPriority.NORMAL)) {
handlePlayerDeath(event);
}
}
void handlePlayerDeath(@NotNull PlayerDeathEvent player);
}

@ -1,6 +1,7 @@
package net.william278.husksync.listener; package net.william278.husksync.listener;
import net.william278.husksync.BukkitHuskSync; import net.william278.husksync.BukkitHuskSync;
import net.william278.husksync.config.Settings;
import net.william278.husksync.data.BukkitInventoryMap; import net.william278.husksync.data.BukkitInventoryMap;
import net.william278.husksync.data.BukkitSerializer; import net.william278.husksync.data.BukkitSerializer;
import net.william278.husksync.data.ItemData; import net.william278.husksync.data.ItemData;
@ -19,8 +20,6 @@ import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.world.WorldSaveEvent; import org.bukkit.event.world.WorldSaveEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -28,35 +27,31 @@ import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class BukkitEventListener extends EventListener implements Listener { public class BukkitEventListener extends EventListener implements BukkitJoinEventListener, BukkitQuitEventListener,
BukkitDeathEventListener, Listener {
public BukkitEventListener(@NotNull BukkitHuskSync huskSync) { public BukkitEventListener(@NotNull BukkitHuskSync huskSync) {
super(huskSync); super(huskSync);
Bukkit.getServer().getPluginManager().registerEvents(this, huskSync); Bukkit.getServer().getPluginManager().registerEvents(this, huskSync);
} }
@EventHandler(priority = EventPriority.LOWEST) @Override
public void onPlayerJoin(@NotNull PlayerJoinEvent event) { public boolean handleEvent(@NotNull Settings.EventType type, @NotNull Settings.EventPriority priority) {
super.handlePlayerJoin(BukkitPlayer.adapt(event.getPlayer())); return plugin.getSettings().getEventPriority(type).equals(priority);
} }
@EventHandler(priority = EventPriority.LOWEST) @Override
public void onPlayerQuit(@NotNull PlayerQuitEvent event) { public void handlePlayerQuit(@NotNull BukkitPlayer player) {
super.handlePlayerQuit(BukkitPlayer.adapt(event.getPlayer())); super.handlePlayerQuit(player);
} }
@EventHandler(ignoreCancelled = true) @Override
public void onWorldSave(@NotNull WorldSaveEvent event) { public void handlePlayerJoin(@NotNull BukkitPlayer player) {
// Handle saving player data snapshots when the world saves super.handlePlayerJoin(player);
if (!plugin.getSettings().saveOnWorldSave) return;
CompletableFuture.runAsync(() -> super.saveOnWorldSave(event.getWorld().getPlayers()
.stream().map(BukkitPlayer::adapt)
.collect(Collectors.toList())));
} }
@EventHandler(ignoreCancelled = true) @Override
public void onPlayerDeath(PlayerDeathEvent event) { public void handlePlayerDeath(@NotNull PlayerDeathEvent event) {
final OnlineUser user = BukkitPlayer.adapt(event.getEntity()); final OnlineUser user = BukkitPlayer.adapt(event.getEntity());
// If the player is locked or the plugin disabling, clear their drops // If the player is locked or the plugin disabling, clear their drops
@ -77,6 +72,16 @@ public class BukkitEventListener extends EventListener implements Listener {
.thenAccept(serializedDrops -> super.saveOnPlayerDeath(user, new ItemData(serializedDrops))); .thenAccept(serializedDrops -> super.saveOnPlayerDeath(user, new ItemData(serializedDrops)));
} }
@EventHandler(ignoreCancelled = true)
public void onWorldSave(@NotNull WorldSaveEvent event) {
// Handle saving player data snapshots when the world saves
if (!plugin.getSettings().saveOnWorldSave) return;
CompletableFuture.runAsync(() -> super.saveOnWorldSave(event.getWorld().getPlayers()
.stream().map(BukkitPlayer::adapt)
.collect(Collectors.toList())));
}
/* /*
* Events to cancel if the player has not been set yet * Events to cancel if the player has not been set yet

@ -0,0 +1,38 @@
package net.william278.husksync.listener;
import net.william278.husksync.config.Settings;
import net.william278.husksync.player.BukkitPlayer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.jetbrains.annotations.NotNull;
public interface BukkitJoinEventListener extends Listener {
boolean handleEvent(@NotNull Settings.EventType type, @NotNull Settings.EventPriority priority);
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
default void onPlayerJoinHighest(@NotNull PlayerJoinEvent event) {
if (handleEvent(Settings.EventType.JOIN_LISTENER, Settings.EventPriority.HIGHEST)) {
handlePlayerJoin(BukkitPlayer.adapt(event.getPlayer()));
}
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
default void onPlayerJoin(@NotNull PlayerJoinEvent event) {
if (handleEvent(Settings.EventType.JOIN_LISTENER, Settings.EventPriority.NORMAL)) {
handlePlayerJoin(BukkitPlayer.adapt(event.getPlayer()));
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
default void onPlayerJoinLowest(@NotNull PlayerJoinEvent event) {
if (handleEvent(Settings.EventType.JOIN_LISTENER, Settings.EventPriority.NORMAL)) {
handlePlayerJoin(BukkitPlayer.adapt(event.getPlayer()));
}
}
void handlePlayerJoin(@NotNull BukkitPlayer player);
}

@ -0,0 +1,38 @@
package net.william278.husksync.listener;
import net.william278.husksync.config.Settings;
import net.william278.husksync.player.BukkitPlayer;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.NotNull;
public interface BukkitQuitEventListener extends Listener {
boolean handleEvent(@NotNull Settings.EventType type, @NotNull Settings.EventPriority priority);
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
default void onPlayerQuitHighest(@NotNull PlayerQuitEvent event) {
if (handleEvent(Settings.EventType.QUIT_LISTENER, Settings.EventPriority.HIGHEST)) {
handlePlayerQuit(BukkitPlayer.adapt(event.getPlayer()));
}
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
default void onPlayerQuit(@NotNull PlayerQuitEvent event) {
if (handleEvent(Settings.EventType.QUIT_LISTENER, Settings.EventPriority.NORMAL)) {
handlePlayerQuit(BukkitPlayer.adapt(event.getPlayer()));
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
default void onPlayerQuitLowest(@NotNull PlayerQuitEvent event) {
if (handleEvent(Settings.EventType.QUIT_LISTENER, Settings.EventPriority.NORMAL)) {
handlePlayerQuit(BukkitPlayer.adapt(event.getPlayer()));
}
}
void handlePlayerQuit(@NotNull BukkitPlayer player);
}

@ -20,7 +20,7 @@ import java.util.Optional;
Information: https://william278.net/project/husksync Information: https://william278.net/project/husksync
Documentation: https://william278.net/docs/husksync""", Documentation: https://william278.net/docs/husksync""",
versionField = "config_version", versionNumber = 2) versionField = "config_version", versionNumber = 3)
public class Settings { public class Settings {
// Top-level settings // Top-level settings
@ -125,6 +125,15 @@ public class Settings {
.orElse(feature.enabledByDefault); .orElse(feature.enabledByDefault);
} }
@YamlKey("synchronization.event_priorities")
public Map<String, EventPriority> synchronizationEventPriorities = EventType.getDefaults();
@NotNull
public EventPriority getEventPriority(@NotNull Settings.EventType eventType) {
return Optional.ofNullable(synchronizationEventPriorities.get(eventType.name().toLowerCase()))
.orElse(EventPriority.NORMAL);
}
/** /**
* Represents the names of tables in the database * Represents the names of tables in the database
@ -143,6 +152,7 @@ public class Settings {
return Map.entry(name().toLowerCase(), defaultName); return Map.entry(name().toLowerCase(), defaultName);
} }
@SuppressWarnings("unchecked")
private static Map<String, String> getDefaults() { private static Map<String, String> getDefaults() {
return Map.ofEntries(Arrays.stream(values()) return Map.ofEntries(Arrays.stream(values())
.map(TableName::toEntry) .map(TableName::toEntry)
@ -178,6 +188,8 @@ public class Settings {
return Map.entry(name().toLowerCase(), enabledByDefault); return Map.entry(name().toLowerCase(), enabledByDefault);
} }
@SuppressWarnings("unchecked")
private static Map<String, Boolean> getDefaults() { private static Map<String, Boolean> getDefaults() {
return Map.ofEntries(Arrays.stream(values()) return Map.ofEntries(Arrays.stream(values())
.map(SynchronizationFeature::toEntry) .map(SynchronizationFeature::toEntry)
@ -185,4 +197,49 @@ public class Settings {
} }
} }
/**
* Represents events that HuskSync listens to, with a configurable priority listener
*/
public enum EventType {
JOIN_LISTENER(EventPriority.LOWEST),
QUIT_LISTENER(EventPriority.LOWEST),
DEATH_LISTENER(EventPriority.NORMAL);
private final EventPriority defaultPriority;
EventType(@NotNull EventPriority defaultPriority) {
this.defaultPriority = defaultPriority;
}
private Map.Entry<String, EventPriority> toEntry() {
return Map.entry(name(), defaultPriority);
}
@SuppressWarnings("unchecked")
private static Map<String, EventPriority> getDefaults() {
return Map.ofEntries(Arrays.stream(values())
.map(EventType::toEntry)
.toArray(Map.Entry[]::new));
}
}
/**
* Represents priorities for events that HuskSync listens to
*/
public enum EventPriority {
/**
* Listens and processes the event execution last
*/
HIGHEST,
/**
* Listens in between {@link #HIGHEST} and {@link #LOWEST} priority marked
*/
NORMAL,
/**
* Listens and processes the event execution first
*/
LOWEST
}
} }
Loading…
Cancel
Save