Add game-mode filter

This commit is contained in:
GeorgH93 2017-01-28 23:08:10 +01:00
parent 3862592b4b
commit 3b99fb9150
9 changed files with 110 additions and 35 deletions

View File

@ -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!!!

View File

@ -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:

View File

@ -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:

View File

@ -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

View File

@ -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<GameMode> 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
{

View File

@ -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<GameMode> getAllowedGameModes()
{
Collection<GameMode> gameModes = new HashSet<>();
for(String string : config.getStringList("allowed_game_modes", new LinkedList<String>()))
{
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()
{

View File

@ -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())
{

View File

@ -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());
}
}
}

View File

@ -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<Player, Long> cooldowns = new HashMap<>();
public final Map<UUID, Long> cooldowns = new HashMap<>();
public String backpackTitleOther, backpackTitle;
public Message messageNoPermission, messageInvalidBackpack;