Skip to content

Commit

Permalink
plugin: Change how PlayerModel is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelillo15 committed Sep 10, 2024
1 parent 3d9efd8 commit ae4e456
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +33,7 @@ public class PaperPlayerWrapper implements PlayerWrapper {
private Logger logger;
@Inject
private AtomicReference<Database> db;
protected PlayerModel playerModel;
private final WrapperState state = new WrapperState();

@Override
Expand Down Expand Up @@ -158,7 +156,7 @@ public Builder setPlayer(Player player) {
public Builder addState(Class<? extends PlayerState> 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");
}
Expand All @@ -167,11 +165,24 @@ public Builder addState(Class<? extends PlayerState> 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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -352,7 +353,7 @@ private void checkStaffModeState() {
if (staffDataModel.isStaffMode()) {
saveInventory();
saveLocation();
enableStaffMode(true);
scheduler.async(() -> enableStaffMode(true));
}

staffChatAsDefault = staffDataModel.isStaffChatEnabled();
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}
Expand All @@ -525,14 +530,24 @@ public StaffPaperPlayerWrapper.Builder addState(Class<? extends PlayerState> 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,112 @@
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<Player> playerWrapperManager;
@Inject
private AtomicReference<Database> db;
@Inject
private Injector injector;
@Inject
private Logger logger;
@Inject
private Scheduler scheduler;
@Inject
private ConfigurationContainer<BukkitConfig> config;

@EventHandler(
priority = EventPriority.LOWEST
)
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<PlayerModel> 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;
}
}

This file was deleted.

0 comments on commit ae4e456

Please sign in to comment.