diff --git a/src/main/java/hu/ditservices/STPlugin.java b/src/main/java/hu/ditservices/STPlugin.java index fee7629..1718614 100644 --- a/src/main/java/hu/ditservices/STPlugin.java +++ b/src/main/java/hu/ditservices/STPlugin.java @@ -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"); diff --git a/src/main/java/hu/ditservices/handlers/LoginCommandHandler.java b/src/main/java/hu/ditservices/handlers/LoginCommandHandler.java new file mode 100644 index 0000000..f2d9e74 --- /dev/null +++ b/src/main/java/hu/ditservices/handlers/LoginCommandHandler.java @@ -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 "); + 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; + } +} + diff --git a/src/main/java/hu/ditservices/listeners/ServerPasswordEvents.java b/src/main/java/hu/ditservices/listeners/ServerPasswordEvents.java new file mode 100644 index 0000000..53ed76e --- /dev/null +++ b/src/main/java/hu/ditservices/listeners/ServerPasswordEvents.java @@ -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 "); + } + }; + 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()); + } + } +} diff --git a/src/main/java/hu/ditservices/utils/ServerPasswordData.java b/src/main/java/hu/ditservices/utils/ServerPasswordData.java new file mode 100644 index 0000000..31380c2 --- /dev/null +++ b/src/main/java/hu/ditservices/utils/ServerPasswordData.java @@ -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 inventoryMap = new HashMap<>(); + private final Map armorMap = new HashMap<>(); + private final Map authenticatedPlayers = new HashMap<>(); + + // Getter for the inventory map + public Map getInventoryMap() { + return inventoryMap; + } + + // Getter for the armor map + public Map getArmorMap() { + return armorMap; + } + + // Getter for the authenticated players map + public Map getAuthenticatedPlayers() { + return authenticatedPlayers; + } + + public String getServerPassword() { + return this.plugin.getConfig().getString("ServerPassword.password"); + } +} + diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6f74143..f166c11 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -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!) diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2bab73d..dbdcf4a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -53,8 +53,18 @@ commands: description: Plugin settings reload. usage: "/st reload" permission: st.reload + slogin: + description: Server password login. + usage: "/slogin " + 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