From ae4e4569b934bccc5ed1b49fc7c2da0482f61604 Mon Sep 17 00:00:00 2001 From: Angelillo15 Date: Tue, 10 Sep 2024 13:10:59 +0200 Subject: [PATCH] plugin: Change how PlayerModel is loaded --- .../com/nookure/staff/paper/NookureStaff.java | 5 -- .../staff/paper/PaperPlayerWrapper.java | 36 ++++---- .../staff/paper/StaffPaperPlayerWrapper.java | 21 ++++- .../staff/paper/listener/OnPlayerJoin.java | 85 ++++++++++++++++--- .../listener/player/OnPlayerDataJoin.java | 60 ------------- 5 files changed, 109 insertions(+), 98 deletions(-) delete mode 100644 NookureStaff-Paper/src/main/java/com/nookure/staff/paper/listener/player/OnPlayerDataJoin.java diff --git a/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/NookureStaff.java b/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/NookureStaff.java index 1a7c154e..eb30b88e 100644 --- a/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/NookureStaff.java +++ b/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/NookureStaff.java @@ -36,7 +36,6 @@ import com.nookure.staff.paper.listener.freeze.OnFreezePlayerMove; import com.nookure.staff.paper.listener.freeze.OnFreezePlayerQuit; import com.nookure.staff.paper.listener.freeze.OnPlayerChatFreeze; -import com.nookure.staff.paper.listener.player.OnPlayerDataJoin; import com.nookure.staff.paper.listener.server.OnServerBroadcast; import com.nookure.staff.paper.listener.staff.OnPlayerInStaffChatTalk; import com.nookure.staff.paper.listener.staff.OnShiftAndRightClick; @@ -205,10 +204,6 @@ private void loadBukkitListeners() { registerListener(OnPlayerInStaffChatTalk.class); } - if (config.get().modules.isPlayerData()) { - registerListener(OnPlayerDataJoin.class); - } - if (config.get().modules.isPlayerData() && config.get().modules.isUserNotes()) { registerListener(OnPlayerNoteJoin.class); } diff --git a/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/PaperPlayerWrapper.java b/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/PaperPlayerWrapper.java index d13757da..60c4a9d6 100644 --- a/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/PaperPlayerWrapper.java +++ b/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/PaperPlayerWrapper.java @@ -17,16 +17,13 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; -import org.spongepowered.configurate.objectmapping.meta.Comment; -import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Set; import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; public class PaperPlayerWrapper implements PlayerWrapper { - @Comment("Package protected value") Player player; @Inject private JavaPlugin plugin; @@ -36,6 +33,7 @@ public class PaperPlayerWrapper implements PlayerWrapper { private Logger logger; @Inject private AtomicReference db; + protected PlayerModel playerModel; private final WrapperState state = new WrapperState(); @Override @@ -158,7 +156,7 @@ public Builder setPlayer(Player player) { public Builder addState(Class state) { PlayerState playerState; try { - playerState = state.getConstructor(PlayerWrapper.class).newInstance(playerWrapper); + playerState = state.getConstructor(PlayerWrapper.class).newInstance(playerWrapper); } catch (Exception e) { throw new IllegalStateException("An error occurred while adding the state"); } @@ -167,11 +165,24 @@ public Builder addState(Class state) { return this; } + public Builder setModel(PlayerModel model) { + if (model == null) { + return this; + } + + playerWrapper.playerModel = model; + return this; + } + public PaperPlayerWrapper build() { if (playerWrapper.player == null) { throw new IllegalStateException("Player cannot be null"); } + if (playerWrapper.playerModel == null) { + playerWrapper.playerModel = playerWrapper.getPlayerModel(); + } + states.forEach((k, v) -> playerWrapper.state.setState(v)); return playerWrapper; @@ -180,22 +191,11 @@ public PaperPlayerWrapper build() { @Override public PlayerModel getPlayerModel() { - PlayerModel model = null; - - try { - model = db.get().find(PlayerModel.class).where().eq("uuid", player.getUniqueId()).findOne(); - if (model == null) { - throw new IllegalStateException("Player model not found"); - } - - return model; - } catch (Exception e) { - logger.severe("An error occurred while getting the player model"); - logger.severe("Is the player model feature enabled?"); - logger.severe(e); + if (playerModel == null) { + throw new IllegalStateException("Player model module is disabled"); } - return model; + return playerModel; } @Override diff --git a/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/StaffPaperPlayerWrapper.java b/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/StaffPaperPlayerWrapper.java index 75172130..1bab03b7 100644 --- a/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/StaffPaperPlayerWrapper.java +++ b/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/StaffPaperPlayerWrapper.java @@ -18,6 +18,7 @@ import com.nookure.staff.api.manager.PlayerWrapperManager; import com.nookure.staff.api.manager.StaffItemsManager; import com.nookure.staff.api.messaging.EventMessenger; +import com.nookure.staff.api.model.PlayerModel; import com.nookure.staff.api.model.StaffDataModel; import com.nookure.staff.api.state.PlayerState; import com.nookure.staff.api.util.Scheduler; @@ -352,7 +353,7 @@ private void checkStaffModeState() { if (staffDataModel.isStaffMode()) { saveInventory(); saveLocation(); - enableStaffMode(true); + scheduler.async(() -> enableStaffMode(true)); } staffChatAsDefault = staffDataModel.isStaffChatEnabled(); @@ -365,7 +366,7 @@ public void toggleNightVision() { return; } - player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0, true, false)); + scheduler.sync(() -> player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, Integer.MAX_VALUE, 0, true, false))); } public void checkVanishState() { @@ -508,6 +509,10 @@ public static StaffPaperPlayerWrapper.Builder create(Injector injector) { } public StaffPaperPlayerWrapper.Builder setPlayer(Player player) { + if (player == null) { + return this; + } + playerWrapper.player = player; return this; } @@ -525,14 +530,24 @@ public StaffPaperPlayerWrapper.Builder addState(Class sta return this; } + public StaffPaperPlayerWrapper.Builder setModel(PlayerModel model) { + playerWrapper.playerModel = model; + return this; + } + public StaffPaperPlayerWrapper build() { if (playerWrapper.player == null) { throw new IllegalStateException("Player cannot be null"); } + if (playerWrapper.playerModel == null) { + playerWrapper.playerModel = playerWrapper.getPlayerModel(); + } + states.forEach((k, v) -> playerWrapper.getState().setState(v)); - playerWrapper.addExtensions(); + playerWrapper.scheduler.sync(playerWrapper::addExtensions); + playerWrapper.checkStaffModeState(); playerWrapper.checkVanishState(); diff --git a/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/listener/OnPlayerJoin.java b/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/listener/OnPlayerJoin.java index 6f9b0673..603e415c 100644 --- a/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/listener/OnPlayerJoin.java +++ b/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/listener/OnPlayerJoin.java @@ -4,23 +4,39 @@ import com.google.inject.Injector; import com.nookure.staff.api.Logger; import com.nookure.staff.api.Permissions; +import com.nookure.staff.api.config.ConfigurationContainer; +import com.nookure.staff.api.config.bukkit.BukkitConfig; import com.nookure.staff.api.manager.PlayerWrapperManager; +import com.nookure.staff.api.model.PlayerModel; import com.nookure.staff.api.state.PinState; +import com.nookure.staff.api.util.Scheduler; import com.nookure.staff.paper.PaperPlayerWrapper; import com.nookure.staff.paper.StaffPaperPlayerWrapper; +import io.ebean.Database; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; +import java.time.Instant; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; + public class OnPlayerJoin implements Listener { @Inject private PlayerWrapperManager playerWrapperManager; @Inject + private AtomicReference db; + @Inject private Injector injector; @Inject private Logger logger; + @Inject + private Scheduler scheduler; + @Inject + private ConfigurationContainer config; @EventHandler( priority = EventPriority.LOWEST @@ -28,27 +44,72 @@ public class OnPlayerJoin implements Listener { public void onPlayerJoin(PlayerJoinEvent event) { logger.debug("Player %s has joined the server", event.getPlayer().getName()); logger.debug("Creating player wrapper for %s", event.getPlayer().getName()); + scheduler.async(() -> { + if (event.getPlayer().hasPermission(Permissions.STAFF_PERMISSION)) { + StaffPaperPlayerWrapper playerWrapper = StaffPaperPlayerWrapper.Builder + .create(injector) + .setPlayer(event.getPlayer()) + .addState(PinState.class) + .setModel(getPlayerModel(event.getPlayer())) + .build(); + + playerWrapperManager.addPlayerWrapper(event.getPlayer(), playerWrapper, true); - if (event.getPlayer().hasPermission(Permissions.STAFF_PERMISSION)) { - StaffPaperPlayerWrapper playerWrapper = StaffPaperPlayerWrapper.Builder + logger.debug("Player %s has staff permission, adding staff player wrapper", event.getPlayer().getName()); + return; + } + + PaperPlayerWrapper playerWrapper = PaperPlayerWrapper.Builder .create(injector) .setPlayer(event.getPlayer()) - .addState(PinState.class) + .setModel(getPlayerModel(event.getPlayer())) .build(); - playerWrapperManager.addPlayerWrapper(event.getPlayer(), playerWrapper, true); + playerWrapperManager.addPlayerWrapper(event.getPlayer(), playerWrapper); + + logger.debug("Player %s does not have staff permission, adding player wrapper", event.getPlayer().getName()); + }); + } + + private PlayerModel getPlayerModel(Player p) { + if (config.get().modules.isPlayerData()) return null; + + Optional optional = db.get().find(PlayerModel.class).where().eq("uuid", p.getUniqueId()) + .findOneOrEmpty(); - logger.debug("Player %s has staff permission, adding staff player wrapper", event.getPlayer().getName()); - return; + if (optional.isPresent()) { + PlayerModel m = updatePlayer(optional.get(), p); + m.update(); + + return m; } - PaperPlayerWrapper playerWrapper = PaperPlayerWrapper.Builder - .create(injector) - .setPlayer(event.getPlayer()) - .build(); + PlayerModel player = updatePlayer(new PlayerModel(), p); + player.setUuid(p.getUniqueId()); + player.setFirstLogin(Instant.now()); + + try { + player.setFirstIp(Objects.requireNonNull(p.getAddress()).getAddress().getHostAddress()); + } catch (NullPointerException e) { + player.setFirstIp("0.0.0.0"); + } - playerWrapperManager.addPlayerWrapper(event.getPlayer(), playerWrapper); + player.save(); + + return player; + } + + + private PlayerModel updatePlayer(PlayerModel player, Player bukkitPlayer) { + player.setName(bukkitPlayer.getName()); + + try { + player.setLastIp(Objects.requireNonNull(bukkitPlayer.getAddress()).getAddress().getHostAddress()); + } catch (NullPointerException e) { + player.setLastIp("0.0.0.0"); + } - logger.debug("Player %s does not have staff permission, adding player wrapper", event.getPlayer().getName()); + player.setLastLogin(Instant.now()); + return player; } } diff --git a/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/listener/player/OnPlayerDataJoin.java b/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/listener/player/OnPlayerDataJoin.java deleted file mode 100644 index 909da371..00000000 --- a/NookureStaff-Paper/src/main/java/com/nookure/staff/paper/listener/player/OnPlayerDataJoin.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.nookure.staff.paper.listener.player; - -import com.google.inject.Inject; -import com.nookure.staff.api.model.PlayerModel; -import com.nookure.staff.api.util.Scheduler; -import io.ebean.Database; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerJoinEvent; - -import java.time.Instant; -import java.util.Objects; -import java.util.Optional; -import java.util.concurrent.atomic.AtomicReference; - -public class OnPlayerDataJoin implements Listener { - @Inject - private AtomicReference db; - @Inject - private Scheduler scheduler; - - @EventHandler - public void onPlayerJoin(PlayerJoinEvent event) { - scheduler.async(() -> { - Optional optional = db.get().find(PlayerModel.class).where().eq("uuid", event.getPlayer().getUniqueId()) - .findOneOrEmpty(); - - if (optional.isPresent()) { - updatePlayer(optional.get(), event.getPlayer()).update(); - return; - } - - PlayerModel player = updatePlayer(new PlayerModel(), event.getPlayer()); - player.setUuid(event.getPlayer().getUniqueId()); - player.setFirstLogin(Instant.now()); - - try { - player.setFirstIp(Objects.requireNonNull(event.getPlayer().getAddress()).getAddress().getHostAddress()); - } catch (NullPointerException e) { - player.setFirstIp("0.0.0.0"); - } - - player.save(); - }); - } - - private PlayerModel updatePlayer(PlayerModel player, Player bukkitPlayer) { - player.setName(bukkitPlayer.getName()); - - try { - player.setLastIp(Objects.requireNonNull(bukkitPlayer.getAddress()).getAddress().getHostAddress()); - } catch (NullPointerException e) { - player.setLastIp("0.0.0.0"); - } - - player.setLastLogin(Instant.now()); - return player; - } -}