Skip to content

Commit

Permalink
Configuration for the message added
Browse files Browse the repository at this point in the history
  • Loading branch information
GaetanOff committed Jan 6, 2021
1 parent 73bc6a4 commit abd0275
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 67 deletions.
22 changes: 17 additions & 5 deletions com/gaetan/staffpin/StaffPlugin.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.gaetan.staffpin;

import com.gaetan.api.message.Message;
import com.gaetan.api.plugin.GCore;
import com.gaetan.staffpin.command.PinCommand;
import com.gaetan.staffpin.config.ConfigManager;
import com.gaetan.staffpin.data.PlayerData;
import com.gaetan.staffpin.listener.PlayerListener;
import com.gaetan.staffpin.runnable.MoveRunnable;
import com.google.common.collect.Maps;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import java.util.Map;
Expand All @@ -16,32 +19,41 @@ public final class StaffPlugin extends GCore {
*/
private final Map<Player, PlayerData> players = Maps.newConcurrentMap();

/**
* Reference to the ConfigManager
*/
private ConfigManager configManager;

/**
* Method to launch the plugin
* Note: This is the same as the classic onEnable
*/
@Override
protected void onPluginStart() {
this.registerCommands(new PinCommand(this));
this.saveDefaultConfig();
this.configManager = new ConfigManager(this);
this.registerCommands(new PinCommand(this, this.configManager));
}

/**
* This is trigger when the server finished loading
*/
@Override
protected void onPluginLoad() {
final FileConfiguration config = this.getConfig();

this.getServer().getOnlinePlayers().stream()
.filter(player -> player.hasPermission("pin.use"))
.forEach(player -> player.kickPlayer("Merci de ne pas reload avec StafPin."));
.filter(player -> player.hasPermission(config.getString("permission.pin")))
.forEach(player -> player.kickPlayer(Message.tl(config.getString("lang.cant_reload"))));
}

/**
* Method to register listener
*/
@Override
protected void registerListener() {
new PlayerListener(this);
new MoveRunnable(this);
new PlayerListener(this, this.configManager);
new MoveRunnable(this, this.configManager);
}

/**
Expand Down
31 changes: 22 additions & 9 deletions com/gaetan/staffpin/command/PinCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.gaetan.api.command.utils.target.CommandTarget;
import com.gaetan.api.message.Message;
import com.gaetan.staffpin.StaffPlugin;
import com.gaetan.staffpin.config.ConfigManager;
import com.gaetan.staffpin.data.PlayerData;
import com.gaetan.staffpin.enums.Lang;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

Expand All @@ -16,37 +16,50 @@ public final class PinCommand {
*/
private final StaffPlugin staffPlugin;

/**
* Reference to the ConfigManager
*/
private final ConfigManager configManager;

/**
* Constructor for the PinCommand class.
*
* @param staffPlugin Reference to the main class
* @param staffPlugin Reference to the main class
* @param configManager Reference to the ConfigManager class
*/
public PinCommand(final StaffPlugin staffPlugin) {
public PinCommand(final StaffPlugin staffPlugin, final ConfigManager configManager) {
this.staffPlugin = staffPlugin;
this.configManager = configManager;
}

/**
* Command to show the usage message.
*
* @param context The command argument
*/
@Command(name = "pin", permission = "pin.use", target = CommandTarget.PLAYER)
@Command(name = "pin", target = CommandTarget.PLAYER)
public void handleCommand(final Context<ConsoleCommandSender> context) {
this.usage((Player) context.getSender());
final Player player = (Player) context.getSender();

if (player.hasPermission(this.configManager.getPinPermission()))
this.usage(player);
}

/**
* Command to set your pin.
*
* @param context The command argument
*/
@Command(name = "pin.set", permission = "pin.use", target = CommandTarget.PLAYER)
@Command(name = "pin.set", target = CommandTarget.PLAYER)
public void handleCommand_Set(final Context<ConsoleCommandSender> context, final String pin) {
final Player player = (Player) context.getSender();
final PlayerData playerData = this.staffPlugin.getPlayer(player);

playerData.setPin(pin);
Message.tell(player, Lang.PIN_SET.getText());
if (player.hasPermission(this.configManager.getPinPermission())) {
final PlayerData playerData = this.staffPlugin.getPlayer(player);

playerData.setPin(pin);
Message.tell(player, this.configManager.getPinSet());
}
}

/**
Expand Down
109 changes: 109 additions & 0 deletions com/gaetan/staffpin/config/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.gaetan.staffpin.config;

import com.gaetan.api.message.Message;
import com.gaetan.staffpin.StaffPlugin;
import org.bukkit.configuration.file.FileConfiguration;

public final class ConfigManager {
/**
* Reference to the main class
*/
private final StaffPlugin staffPlugin;

/**
* Cache for the config
*/
private String enterPin, timeFinish,
correctPin, incorrectPin, noPin, pinSet, pinPermission;

/**
* Constructor for the ConfigManager class.
*
* @param staffPlugin Reference to the main class
*/
public ConfigManager(final StaffPlugin staffPlugin) {
this.staffPlugin = staffPlugin;

this.load();
}

/**
* Method to cache the config
*/
private void load() {
final FileConfiguration config = this.staffPlugin.getConfig();
final String prefix = Message.tl(config.getString("lang.prefix"));

this.enterPin = prefix + Message.tl(config.getString("lang.enter_ping"));
this.timeFinish = prefix + Message.tl(config.getString("lang.time_finish"));
this.correctPin = prefix + Message.tl(config.getString("lang.pin.correct"));
this.incorrectPin = prefix + Message.tl(config.getString("lang.pin.incorrect"));
this.noPin = prefix + Message.tl(config.getString("lang.pin.dont_have"));
this.pinSet = prefix + Message.tl(config.getString("lang.pin.set"));

this.pinPermission = config.getString("permission.pin");
}

/**
* Getter to get the message for enter the pin.
*
* @return The enter message
*/
public String getEnterPin() {
return this.enterPin;
}

/**
* Getter to get the message when the cooldown get his end.
*
* @return The end message
*/
public String getTimeFinish() {
return this.timeFinish;
}

/**
* Getter to get the correct pin message.
*
* @return The correct pin message
*/
public String getCorrectPin() {
return this.correctPin;
}

/**
* Getter to get the incorrect pin message.
*
* @return The incorrect pin message
*/
public String getIncorrectPin() {
return this.incorrectPin;
}

/**
* Getter to get the no pin message.
*
* @return The nopin message
*/
public String getNoPin() {
return this.noPin;
}

/**
* Getter to get the pin set message.
*
* @return The pin set message
*/
public String getPinSet() {
return this.pinSet;
}

/**
* Getter to get the pin permission.
*
* @return The pin permission
*/
public String getPinPermission() {
return this.pinPermission;
}
}
17 changes: 10 additions & 7 deletions com/gaetan/staffpin/data/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.gaetan.api.message.Message;
import com.gaetan.api.runnable.TaskUtil;
import com.gaetan.staffpin.StaffPlugin;
import com.gaetan.staffpin.enums.Lang;
import com.gaetan.staffpin.config.ConfigManager;
import com.gaetan.staffpin.runnable.LoadPlayerConfig;
import com.gaetan.staffpin.runnable.SavePlayerConfig;
import org.bukkit.GameMode;
Expand All @@ -14,6 +14,7 @@

public final class PlayerData {
private final StaffPlugin staffPlugin;
private final ConfigManager configManager;
private final Player player;
private String pin;
private boolean login;
Expand All @@ -24,11 +25,13 @@ public final class PlayerData {
/**
* Constructor for the PlayerData class.
*
* @param player The data of this player
* @param staffPlugin Reference to the main class
* @param player The data of this player
* @param staffPlugin Reference to the main class
* @param configManager Reference to the ConfigManager class
*/
public PlayerData(final Player player, final StaffPlugin staffPlugin) {
public PlayerData(final Player player, final StaffPlugin staffPlugin, final ConfigManager configManager) {
this.staffPlugin = staffPlugin;
this.configManager = configManager;
this.player = player;
this.pin = null;
}
Expand All @@ -44,7 +47,7 @@ private void save() {
* Method to load the pin from the config and cache-it
*/
public void load() {
this.staffPlugin.getServer().getScheduler().runTaskAsynchronously(this.staffPlugin, new LoadPlayerConfig(this.staffPlugin, this));
this.staffPlugin.getServer().getScheduler().runTaskAsynchronously(this.staffPlugin, new LoadPlayerConfig(this.staffPlugin, this, this.configManager));
}

/**
Expand Down Expand Up @@ -73,7 +76,7 @@ public void join() {
* Method when player enter the correct code in the chat
*/
public void correct() {
Message.tell(this.player, Lang.ENTER_SUCESS.getText());
Message.tell(this.player, this.configManager.getCorrectPin());

this.setLogin(true);
this.clearInventory();
Expand All @@ -91,7 +94,7 @@ public void correct() {
private void pinCooldown() {
TaskUtil.runLater(() -> {
if (!this.isLogin())
this.player.kickPlayer(Lang.TIME_EXCED.getText());
this.player.kickPlayer(this.configManager.getTimeFinish());

}, 400L);
}
Expand Down
28 changes: 0 additions & 28 deletions com/gaetan/staffpin/enums/Lang.java

This file was deleted.

19 changes: 13 additions & 6 deletions com/gaetan/staffpin/listener/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.gaetan.api.message.Message;
import com.gaetan.api.runnable.TaskUtil;
import com.gaetan.staffpin.StaffPlugin;
import com.gaetan.staffpin.config.ConfigManager;
import com.gaetan.staffpin.data.PlayerData;
import com.gaetan.staffpin.enums.Lang;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -20,13 +20,20 @@ public final class PlayerListener implements Listener {
*/
private final StaffPlugin staffPlugin;

/**
* Reference to the ConfigManager
*/
private final ConfigManager configManager;

/**
* Constructor for the PlayerListener class.
*
* @param staffPlugin Reference to te main class
* @param staffPlugin Reference to te main class
* @param configManager Reference to the ConfigManager class
*/
public PlayerListener(final StaffPlugin staffPlugin) {
public PlayerListener(final StaffPlugin staffPlugin, final ConfigManager configManager) {
this.staffPlugin = staffPlugin;
this.configManager = configManager;
this.staffPlugin.getServer().getPluginManager().registerEvents(this, this.staffPlugin);
}

Expand All @@ -35,7 +42,7 @@ public void onJoin(final PlayerJoinEvent event) {
final Player player = event.getPlayer();

if (player.hasPermission("pin.use")) {
final PlayerData playerData = new PlayerData(player, this.staffPlugin);
final PlayerData playerData = new PlayerData(player, this.staffPlugin, this.configManager);
this.staffPlugin.getPlayers().put(player, playerData);

playerData.join();
Expand Down Expand Up @@ -65,7 +72,7 @@ public void onChat(final AsyncPlayerChatEvent event) {
return;
}

TaskUtil.run((() -> player.kickPlayer(Lang.ENTER_FAILED.getText())));
TaskUtil.run((() -> player.kickPlayer(this.configManager.getIncorrectPin())));
}
}
}
Expand All @@ -76,7 +83,7 @@ public void onCommand(final PlayerCommandPreprocessEvent event) {

if (player.hasPermission("pin.use")) {
if (this.staffPlugin.getPlayers().containsKey(player) && !this.staffPlugin.getPlayer(player).isLogin()) {
Message.tell(player, Lang.ENTER_PING.getText());
Message.tell(player, this.configManager.getEnterPin());
event.setCancelled(true);
}
}
Expand Down
Loading

0 comments on commit abd0275

Please sign in to comment.