Skip to content

Commit

Permalink
Merge pull request #123 from Nookure/dev
Browse files Browse the repository at this point in the history
1.4.1
  • Loading branch information
Angelillo15 authored Sep 10, 2024
2 parents 96e4af9 + f8a4209 commit 0d5bb04
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 108 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ jobs:
distribution: 'temurin'
java-version: 21
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
uses: gradle/actions/setup-gradle@v4

- name: Execute Gradle build
run: |
chmod +x gradlew
./gradlew shadowJar
mkdir staging && cp build/libs/*.jar staging
- name: Upload the jar
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: NookureStaff
path: staging
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public final class Permissions {
* Permission to use set pin command
*/
public static final String SET_PIN_PERMISSION = "nookure.staff.setpin";
public static final String CHANGE_PIN_PERMISSION = "nookure.staff.changepin";
public static final String DELETE_PIN_PERMISSION = "nookure.staff.deletepin";

private Permissions() {
throw new UnsupportedOperationException("This class cannot be instantiated");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class ModulesPartials {
Enable or disable the pin code module.
This will allow you to set a pin code to the staff's accounts
and use it to login.
WARNING: STILL IN BETA
""")
private boolean pinCode = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ public class PinMessagePartial {
@Setting
public String changePinCommandUsage = "<red>Usage: /changepin <new pin> <-- Must be 4 digits.";
@Setting
public String pinDeleteCommandUsage = "<red>Usage: /deletepin <uuid|name>";
@Setting
public String pinDeleted = "<green>The PIN has been successfully deleted for {player}.";
@Setting
public String pinChanged = "<green>Your PIN has been changed to {pin}.";
@Setting
public String pinNotSet = "<red>You don't have a PIN set.";
public String youMustHaveToSetAPin = """
{center}<bold>◆ <red>Nookure <white>Network</white> ◆</bold>
{center}<gray>You <red>must</red> have to set a <red>PIN</red> before you can play on this server.
{center}<gray>Use <red><u><click:run_command:'/setpin'>/setpin</click></u></red> to set a PIN.
{center}<gray>You have {time} to set a PIN.
Expand Down
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 All @@ -57,6 +56,7 @@
import com.nookure.staff.paper.note.command.ParentNoteCommand;
import com.nookure.staff.paper.note.listener.OnPlayerNoteJoin;
import com.nookure.staff.paper.pin.command.ChangePin;
import com.nookure.staff.paper.pin.command.DeletePinCommand;
import com.nookure.staff.paper.pin.command.SetPinCommand;
import com.nookure.staff.paper.pin.listener.OnInventoryClose;
import com.nookure.staff.paper.task.FreezeSpamMessage;
Expand Down Expand Up @@ -204,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 Expand Up @@ -337,6 +333,7 @@ private void loadCommands() {
if (config.get().modules.isPinCode()) {
commandManager.registerCommand(injector.getInstance(SetPinCommand.class));
commandManager.registerCommand(injector.getInstance(ChangePin.class));
commandManager.registerCommand(injector.getInstance(DeletePinCommand.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,30 +509,45 @@ public static StaffPaperPlayerWrapper.Builder create(Injector injector) {
}

public StaffPaperPlayerWrapper.Builder setPlayer(Player player) {
if (player == null) {
return this;
}

playerWrapper.player = player;
return this;
}

public StaffPaperPlayerWrapper.Builder addState(Class<? extends PlayerState> state) {
PlayerState playerState;

try {
playerState = state.getConstructor(PlayerWrapper.class).newInstance(playerWrapper);
} catch (Exception e) {
throw new IllegalStateException("An error occurred while adding the state");
throw new IllegalStateException("An error occurred while adding the state", e);
}

states.put(state, playerState);
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())
.setModel(getPlayerModel(event.getPlayer()))
.addState(PinState.class)
.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;
}
}
Loading

0 comments on commit 0d5bb04

Please sign in to comment.