Implemented Server Password feature
This commit is contained in:
parent
e2b72802d0
commit
84ec5838d4
@ -1,16 +1,10 @@
|
||||
package hu.ditservices;
|
||||
|
||||
import hu.ditservices.handlers.DITTabCompleter;
|
||||
import hu.ditservices.handlers.SaveHandler;
|
||||
import hu.ditservices.handlers.TabHandler;
|
||||
import hu.ditservices.handlers.*;
|
||||
import hu.ditservices.listeners.*;
|
||||
import hu.ditservices.utils.*;
|
||||
import hu.ditservices.utils.Math;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import hu.ditservices.handlers.CommandHandler;
|
||||
import hu.ditservices.listeners.ChatEvents;
|
||||
import hu.ditservices.listeners.LogChat;
|
||||
import hu.ditservices.listeners.LogCommand;
|
||||
import hu.ditservices.listeners.LogConnect;
|
||||
import com.tchristofferson.configupdater.ConfigUpdater;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
@ -26,8 +20,7 @@ import com.jeff_media.updatechecker.UserAgentBuilder;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.util.Collections;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public final class STPlugin extends JavaPlugin implements CommandExecutor, Listener {
|
||||
@ -37,6 +30,7 @@ public final class STPlugin extends JavaPlugin implements CommandExecutor, Liste
|
||||
private FileConfiguration config;
|
||||
|
||||
public long ServerStartTime;
|
||||
private ServerPasswordData serverPasswordData;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
@ -68,6 +62,14 @@ public final class STPlugin extends JavaPlugin implements CommandExecutor, Liste
|
||||
getServer().getPluginManager().registerEvents(new ChatEvents(this), this);
|
||||
this.log.info(ChatColor.stripColor(this.getPrefix()) + "Custom Advancement Messages enabled!");
|
||||
}
|
||||
|
||||
if (this.config.isSet("ServerPassword.enabled") && this.config.getBoolean("ServerPassword.enabled")) {
|
||||
this.serverPasswordData = new ServerPasswordData(this);
|
||||
PluginCommand sloginCommand = this.getCommand("slogin");
|
||||
sloginCommand.setExecutor(new LoginCommandHandler(this));
|
||||
getServer().getPluginManager().registerEvents(new ServerPasswordEvents(this), this);
|
||||
this.log.info(ChatColor.stripColor(this.getPrefix()) + "Server Password enabled!");
|
||||
}
|
||||
}
|
||||
|
||||
private void scheduleTasks() {
|
||||
@ -180,6 +182,10 @@ public final class STPlugin extends JavaPlugin implements CommandExecutor, Liste
|
||||
return returnText.toString();
|
||||
}
|
||||
|
||||
public ServerPasswordData getServerPasswordData() {
|
||||
return this.serverPasswordData;
|
||||
}
|
||||
|
||||
public boolean reload(){
|
||||
File configFile = new File(getDataFolder(), "config.yml");
|
||||
|
||||
|
@ -0,0 +1,62 @@
|
||||
package hu.ditservices.handlers;
|
||||
|
||||
import hu.ditservices.STPlugin;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class LoginCommandHandler implements CommandExecutor {
|
||||
|
||||
private final STPlugin plugin;
|
||||
|
||||
public LoginCommandHandler(STPlugin instance) {
|
||||
this.plugin = instance;
|
||||
}
|
||||
|
||||
// Method to authenticate the player
|
||||
public boolean authenticate(Player player, String password) {
|
||||
if (plugin.getServerPasswordData().getServerPassword().equals(password)) {
|
||||
if (plugin.getConfig().getBoolean("ServerPassword.preventInventory")) {
|
||||
player.getInventory().setContents(plugin.getServerPasswordData().getInventoryMap().get(player.getUniqueId()));
|
||||
player.getInventory().setArmorContents(plugin.getServerPasswordData().getArmorMap().get(player.getUniqueId()));
|
||||
}
|
||||
plugin.getServerPasswordData().getAuthenticatedPlayers().put(player.getUniqueId(), true);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage("Only players can execute this command.");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
UUID pUuid = player.getUniqueId();
|
||||
|
||||
if (args.length != 1) {
|
||||
player.sendMessage("Usage: /slogin <PASSWORD>");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (plugin.getServerPasswordData().getAuthenticatedPlayers().getOrDefault(pUuid,false)) {
|
||||
player.sendMessage("You are already authenticated.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.authenticate(player, args[0])) {
|
||||
player.sendMessage("You have been authenticated.");
|
||||
plugin.getServerPasswordData().getInventoryMap().remove(pUuid);
|
||||
plugin.getServerPasswordData().getArmorMap().remove(pUuid);
|
||||
} else {
|
||||
player.sendMessage("Incorrect password. Try again.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,87 @@
|
||||
package hu.ditservices.listeners;
|
||||
|
||||
import hu.ditservices.STPlugin;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class ServerPasswordEvents implements Listener {
|
||||
private STPlugin plugin;
|
||||
private FileConfiguration config;
|
||||
|
||||
public ServerPasswordEvents(STPlugin instance){
|
||||
this.plugin = instance;
|
||||
this.config = plugin.getConfig();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if ((!config.getBoolean("ServerPassword.rememberUntilRestart"))
|
||||
|| (!plugin.getServerPasswordData().getAuthenticatedPlayers().getOrDefault(player.getUniqueId(),false))) {
|
||||
plugin.getServerPasswordData().getAuthenticatedPlayers().put(player.getUniqueId(),false);
|
||||
if (config.getBoolean("ServerPassword.preventInventory")) {
|
||||
// Store and clear inventory
|
||||
plugin.getServerPasswordData().getInventoryMap().put(player.getUniqueId(), player.getInventory().getContents());
|
||||
plugin.getServerPasswordData().getArmorMap().put(player.getUniqueId(), player.getInventory().getArmorContents());
|
||||
player.getInventory().clear();
|
||||
player.getInventory().setArmorContents(null);
|
||||
}
|
||||
player.sendMessage(ChatColor.RED + "There is server password enabled on this server!");
|
||||
player.sendMessage("Note: This means the same password for all players.");
|
||||
BukkitRunnable authReminder = new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Check if the player is authenticated, default to false if not present.
|
||||
if (plugin.getServerPasswordData().getAuthenticatedPlayers().getOrDefault(player.getUniqueId(), false)) {
|
||||
this.cancel();
|
||||
return;
|
||||
}
|
||||
player.sendMessage(ChatColor.RED + "Please login using /slogin <password>");
|
||||
}
|
||||
};
|
||||
authReminder.runTaskTimer(plugin, 0L, 100L);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerMove(PlayerMoveEvent event) {
|
||||
if ((!plugin.getServerPasswordData().getAuthenticatedPlayers().getOrDefault(event.getPlayer().getUniqueId(),false))
|
||||
&& config.getBoolean("ServerPassword.preventMove")) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onBlockBreak(BlockBreakEvent event) {
|
||||
if ((!plugin.getServerPasswordData().getAuthenticatedPlayers().getOrDefault(event.getPlayer().getUniqueId(),false))
|
||||
&& config.getBoolean("ServerPassword.preventBuild")) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onBlockPlace(BlockPlaceEvent event) {
|
||||
if ((!plugin.getServerPasswordData().getAuthenticatedPlayers().getOrDefault(event.getPlayer().getUniqueId(),false))
|
||||
&& config.getBoolean("ServerPassword.preventBuild")) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
if ((!config.getBoolean("ServerPassword.rememberUntilRestart"))
|
||||
|| (!plugin.getServerPasswordData().getAuthenticatedPlayers().getOrDefault(event.getPlayer().getUniqueId(),false))) {
|
||||
plugin.getServerPasswordData().getAuthenticatedPlayers().remove(event.getPlayer().getUniqueId());
|
||||
}
|
||||
}
|
||||
}
|
40
src/main/java/hu/ditservices/utils/ServerPasswordData.java
Normal file
40
src/main/java/hu/ditservices/utils/ServerPasswordData.java
Normal file
@ -0,0 +1,40 @@
|
||||
package hu.ditservices.utils;
|
||||
|
||||
import hu.ditservices.STPlugin;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ServerPasswordData {
|
||||
private STPlugin plugin;
|
||||
public ServerPasswordData(STPlugin instance) {
|
||||
this.plugin = instance;
|
||||
}
|
||||
|
||||
// Maps to store inventory, armor, and authentication status
|
||||
private final Map<UUID, ItemStack[]> inventoryMap = new HashMap<>();
|
||||
private final Map<UUID, ItemStack[]> armorMap = new HashMap<>();
|
||||
private final Map<UUID, Boolean> authenticatedPlayers = new HashMap<>();
|
||||
|
||||
// Getter for the inventory map
|
||||
public Map<UUID, ItemStack[]> getInventoryMap() {
|
||||
return inventoryMap;
|
||||
}
|
||||
|
||||
// Getter for the armor map
|
||||
public Map<UUID, ItemStack[]> getArmorMap() {
|
||||
return armorMap;
|
||||
}
|
||||
|
||||
// Getter for the authenticated players map
|
||||
public Map<UUID, Boolean> getAuthenticatedPlayers() {
|
||||
return authenticatedPlayers;
|
||||
}
|
||||
|
||||
public String getServerPassword() {
|
||||
return this.plugin.getConfig().getString("ServerPassword.password");
|
||||
}
|
||||
}
|
||||
|
@ -81,6 +81,12 @@ ServerPassword:
|
||||
enabled: false
|
||||
password: 'changeme'
|
||||
exceptOps: true # Server operators don't need to login.
|
||||
rememberUntilRestart: true # Set this to 'false' to player need to provide password each login.
|
||||
# Otherwise, each player need to provide password only once until the server is restarted.
|
||||
# Below settings are preventing the related actions until the password was not given by the player if the setting is 'true'.
|
||||
preventInventory: true
|
||||
preventMove: true
|
||||
preventBuild: true
|
||||
|
||||
|
||||
# Cooldown options (only applicable for this plugin!)
|
||||
|
@ -53,8 +53,18 @@ commands:
|
||||
description: Plugin settings reload.
|
||||
usage: "/st reload"
|
||||
permission: st.reload
|
||||
slogin:
|
||||
description: Server password login.
|
||||
usage: "/slogin <PASSWORD>"
|
||||
permission: st.slogin
|
||||
|
||||
permissions:
|
||||
st.slogin:
|
||||
description: The server password login command.
|
||||
default: true
|
||||
st.slogin.skip:
|
||||
description: If has this permission the user dont have to provide server password upon connect.
|
||||
default: op
|
||||
st.help:
|
||||
description: Enables the help command.
|
||||
default: true
|
||||
|
Loading…
x
Reference in New Issue
Block a user