From 3b99fb91509ff515f39c3ec2eb4b992226fdb13c Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Sat, 28 Jan 2017 23:08:10 +0100 Subject: [PATCH] Add game-mode filter --- resources/config.yml | 4 +- resources/lang/de.yml | 10 ++- resources/lang/en.yml | 6 +- resources/plugin.yml | 3 + .../Minepacks/Bukkit/Commands/OnCommand.java | 66 ++++++++++++------- .../Minepacks/Bukkit/Database/Config.java | 42 +++++++++++- .../Minepacks/Bukkit/Database/Files.java | 2 +- .../Bukkit/Listener/EventListener.java | 9 +-- .../Minepacks/Bukkit/Minepacks.java | 3 +- 9 files changed, 110 insertions(+), 35 deletions(-) diff --git a/resources/config.yml b/resources/config.yml index 3dbb567..0d5923c 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -22,6 +22,8 @@ sync_cooldown: false drop_on_death: true # Defines the max amount of columns for a backpack max_size: 6 +# Defines in which game-modes a player can access his backpack (name or id) +allowed_game_modes: [ "SURVIVAL" ] # Controls for the auto pickup on full inventory function full_inventory: @@ -59,7 +61,7 @@ Database: User: minecraft Password: minecraft #The max amount of connections to the database the connection pool will open - MaxConnections: 4 + MaxConnections: 2 # Tables settings for shared tables when using MySQL - Advanced MySQL Settings # Use these settings only if you know what you are doing!!!! # Do only change this settings if you know what you are doing and have some basic MySQL knowledge!!! diff --git a/resources/lang/de.yml b/resources/lang/de.yml index ff926bc..9768fe2 100644 --- a/resources/lang/de.yml +++ b/resources/lang/de.yml @@ -1,14 +1,18 @@ Language: NotFromConsole: "&cDieser Befehl kann nicht in der Console ausgeführt werden." Ingame: - NoPermission: "Dir fehlen die Rechte dafür." + NoPermission: "&cDir fehlen die Rechte dafür." OwnBackpackClose: "Rucksack geschlossen." + OwnBackpackClose_SendMethod: "action_bar" #Parameter: {OwnerName}, {OwnerDisplayName} PlayerBackpackClose: "{OwnerName}'s Rucksack geschlossen." + PlayerBackpackClose_SendMethod: "action_bar" InvalidBackpack: "Rucksack fehlerhaft." BackpackCleaned: "Rucksack gelehrt." - #Parameter> {TimeLeft} time in seconds till he can reopen his backpack - Cooldown: "&2Bitte warte noch {TimeLeft} Sekunden bis du deinen Rucksack wieder öffnest." + #Parameter: {TimeLeft} time in seconds till he can reopen his backpack + Cooldown: "&2Bitte warte noch {TimeLeft} Sekunden bis du deinen Rucksack wieder öffnest." + #Parameter: {CurrentGameMode}, {AllowedGameModes} + WrongGameMode: "Du darfst deinen Rucksack in deinem aktuellem Game-Mode nicht öffnen." Shulkerboxes: NotAllowedInBackpack: "&cShulkerboxes sind im Rucksack nicht erlaubt." Commands: diff --git a/resources/lang/en.yml b/resources/lang/en.yml index b5e36c5..23f41c1 100644 --- a/resources/lang/en.yml +++ b/resources/lang/en.yml @@ -1,14 +1,18 @@ Language: - NotFromConsole: "&cCommand not useable from console." + NotFromConsole: "&cCommand not usable from console." Ingame: NoPermission: "&cYou don't have the Permission to do that." OwnBackpackClose: "Backpack closed!" + OwnBackpackClose_SendMethod: "action_bar" #Parameter: {OwnerName}, {OwnerDisplayName} PlayerBackpackClose: "{OwnerName}'s backpack closed!" + PlayerBackpackClose_SendMethod: "action_bar" InvalidBackpack: "Invalid backpack." BackpackCleaned: "Backpack cleaned." #Parameter> {TimeLeft} time in seconds till he can reopen his backpack Cooldown: "&2Please wait {TimeLeft} seconds till you reopen your backpack." + #Parameter: {CurrentGameMode}, {AllowedGameModes} + WrongGameMode: "You are not allowed to open your backpack in your current game-mode." Shulkerboxes: NotAllowedInBackpack: "&cShulkerboxes are not allowed in the backpack." Commands: diff --git a/resources/plugin.yml b/resources/plugin.yml index 4eaf4ad..cb89fd2 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -81,4 +81,7 @@ permissions: default: op backpack.noCooldown: description: Allows to bypass the cooldown to open the backpack. + default: op + backpack.ignoreGameMode: + description: Allows to bypass the game-mode restriction. default: op \ No newline at end of file diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Commands/OnCommand.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Commands/OnCommand.java index c5a8df9..155d18b 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Commands/OnCommand.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Commands/OnCommand.java @@ -24,68 +24,88 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.GameMode; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.Collection; + public class OnCommand implements CommandExecutor { private final Minepacks plugin; - private final Message messageNotFromConsole, messageBackpackCleaned, messageCooldown; + private final Message messageNotFromConsole, messageBackpackCleaned, messageCooldown, messageWrongGameMode; private final long cooldown; private final boolean syncCooldown; + private final Collection gameModes; + private final String allowedGameModes; public OnCommand(Minepacks mp) { plugin = mp; - messageNotFromConsole = plugin.lang.getMessage("NotFromConsole"); + messageNotFromConsole = plugin.lang.getMessage("NotFromConsole"); messageBackpackCleaned = plugin.lang.getMessage("Ingame.BackpackCleaned"); - messageCooldown = plugin.lang.getMessage("Ingame.Cooldown").replaceAll("\\{TimeLeft}", "%1$.1f"); + messageCooldown = plugin.lang.getMessage("Ingame.Cooldown").replaceAll("\\{TimeLeft}", "%1\\$.1f"); + messageWrongGameMode = plugin.lang.getMessage("Ingame.WrongGameMode").replaceAll("\\{CurrentGameMode}", "%1\\$s").replaceAll("\\{AllowedGameModes}", "%1\\$s"); cooldown = plugin.config.getCommandCooldown(); syncCooldown = plugin.config.isCommandCooldownSyncEnabled(); + gameModes = plugin.config.getAllowedGameModes(); + StringBuilder allowedGameModesBuilder = new StringBuilder(""); + for(GameMode gameMode : gameModes) + { + if(allowedGameModesBuilder.length() > 1) + { + allowedGameModesBuilder.append(", "); + } + allowedGameModesBuilder.append(gameMode.name().toLowerCase()); + } + allowedGameModes = allowedGameModesBuilder.toString(); } @SuppressWarnings("deprecation") @Override public boolean onCommand(CommandSender sender, Command cmd, String arg, String[] args) { - final Player player; - if (sender instanceof Player) - { - player = (Player) sender; - } - else + if (!(sender instanceof Player)) { messageNotFromConsole.send(sender); return true; } + final Player player = (Player) sender; if(args.length == 0) { // Open player backpack if(player.hasPermission("backpack")) { - if(cooldown > 0 && !player.hasPermission("backpack.noCooldown")) + if(gameModes.contains(player.getGameMode()) || player.hasPermission("backpack.ignoreGameMode")) { - if(plugin.cooldowns.containsKey(player)) + if(cooldown > 0 && !player.hasPermission("backpack.noCooldown")) { - long cd = plugin.cooldowns.get(player); - if(cd < System.currentTimeMillis()) + if(plugin.cooldowns.containsKey(player.getUniqueId())) { - cd = cd - System.currentTimeMillis(); - messageCooldown.send(sender, cd / 1000f); - return true; + long cd = plugin.cooldowns.get(player.getUniqueId()); + if(cd < System.currentTimeMillis()) + { + cd = cd - System.currentTimeMillis(); + messageCooldown.send(sender, cd / 1000f); + return true; + } } + final long cooldownTime = System.currentTimeMillis() + cooldown; + if(syncCooldown) + { + plugin.getDb().syncCooldown(player, cooldownTime); + } + plugin.cooldowns.put(player.getUniqueId(), cooldownTime); } - final long cooldownTime = System.currentTimeMillis() + cooldown; - if(syncCooldown) - { - plugin.getDb().syncCooldown(player, cooldownTime); - } - plugin.cooldowns.put(player, cooldownTime); + plugin.openBackpack(player, player, true); } - plugin.openBackpack(player, player, true); + else + { + messageWrongGameMode.send(player, player.getGameMode().name().toLowerCase(), allowedGameModes); + } } else { diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java index a647ce0..46dd281 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java @@ -21,8 +21,12 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.GameMode; import org.bukkit.plugin.java.JavaPlugin; +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedList; import java.util.Set; public class Config extends Configuration @@ -190,7 +194,7 @@ public boolean getAutoUpdate() public long getCommandCooldown() { - return config.getInt("command_cooldown", -1) * 1000; + return config.getInt("command_cooldown", -1) * 1000L; } public boolean isCommandCooldownSyncEnabled() @@ -198,6 +202,42 @@ public boolean isCommandCooldownSyncEnabled() return config.getBoolean("sync_cooldown", false); } + public Collection getAllowedGameModes() + { + Collection gameModes = new HashSet<>(); + for(String string : config.getStringList("allowed_game_modes", new LinkedList())) + { + GameMode gm = null; + try + { + //noinspection deprecation + gm = GameMode.getByValue(Integer.valueOf(string)); + } + catch(NumberFormatException ignored) {} + if(gm == null) + { + try + { + gm = GameMode.valueOf(string.toUpperCase()); + } + catch(IllegalArgumentException ignored) + { + logger.warning("Unknown game-mode '" + string + "'"); + } + } + if(gm != null) + { + gameModes.add(gm); + } + } + if(gameModes.size() < 1) + { + logger.info("No game-mode allowed, allowing " + GameMode.SURVIVAL.name()); + gameModes.add(GameMode.SURVIVAL); + } + return gameModes; + } + //region Full inventory handling public boolean getFullInvCollect() { diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java index e128fa6..5f0b010 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Files.java @@ -69,7 +69,7 @@ private void checkFiles() int len; for (File file : allFiles) { - if(maxAge > 0 && (new Date()).getTime() - file.lastModified() > maxAge) // Check if the file is older then x days + if(maxAge > 0 && System.currentTimeMillis() - file.lastModified() > maxAge) // Check if the file is older then x days { if(!file.delete()) { diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/EventListener.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/EventListener.java index 3d290cf..d788f41 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/EventListener.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Listener/EventListener.java @@ -25,6 +25,7 @@ import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; @@ -67,7 +68,7 @@ public void onClose(InventoryCloseEvent event) } } - @EventHandler + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onClick(InventoryClickEvent event) { if (event.getInventory() != null && event.getInventory().getHolder() instanceof Backpack && event.getWhoClicked() instanceof Player) @@ -84,12 +85,12 @@ public void onClick(InventoryClickEvent event) } } - @EventHandler + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onPlayerLeaveEvent(PlayerQuitEvent event) { - if(plugin.cooldowns.containsKey(event.getPlayer())) + if(plugin.cooldowns.containsKey(event.getPlayer().getUniqueId())) { - plugin.cooldowns.remove(event.getPlayer()); + plugin.cooldowns.remove(event.getPlayer().getUniqueId()); } } } \ No newline at end of file diff --git a/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java b/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java index 56b18b1..85b73e6 100644 --- a/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java +++ b/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java @@ -47,6 +47,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.UUID; public class Minepacks extends JavaPlugin implements MinepacksPlugin { @@ -57,7 +58,7 @@ public class Minepacks extends JavaPlugin implements MinepacksPlugin public Language lang; private Database database; - public final Map cooldowns = new HashMap<>(); + public final Map cooldowns = new HashMap<>(); public String backpackTitleOther, backpackTitle; public Message messageNoPermission, messageInvalidBackpack;