1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-30 09:49:51 +02:00

Cleanup of ranks and make it more resistant against exceptions from external code

This commit is contained in:
snowleo
2012-10-16 00:15:38 +02:00
parent 80bdae2cc9
commit 4f4f9d8702
4 changed files with 224 additions and 90 deletions

View File

@@ -1,5 +1,6 @@
package net.ess3.ranks; package net.ess3.ranks;
import java.util.logging.Level;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import net.ess3.api.IRanks; import net.ess3.api.IRanks;
import net.ess3.api.ISettings; import net.ess3.api.ISettings;
@@ -19,84 +20,130 @@ public class GMGroups extends AbstractRanks implements IRanks
this.ess = ess; this.ess = ess;
this.groupManager = (GroupManager)groupManager; this.groupManager = (GroupManager)groupManager;
} }
@Override private double getGMDouble(final CommandSender player, final String key, final double defaultValue)
public double getHealCooldown(CommandSender player)
{ {
AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player)); try
if (handler == null)
{ {
return 0; final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player));
if (handler == null)
{
return defaultValue;
}
return handler.getPermissionDouble(player.getName(), key);
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return defaultValue;
}
}
private int getGMInteger(final CommandSender player, final String key, final int defaultValue)
{
try
{
final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player));
if (handler == null)
{
return defaultValue;
}
return handler.getPermissionInteger(player.getName(), key);
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return defaultValue;
}
}
private String getGMString(final CommandSender player, final String key, final String defaultValue)
{
try
{
final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player));
if (handler == null)
{
return defaultValue;
}
return handler.getPermissionString(player.getName(), key);
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return defaultValue;
} }
return handler.getPermissionDouble(player.getName(), "healcooldown");
} }
@Override @Override
public double getTeleportCooldown(CommandSender player) public double getHealCooldown(final CommandSender player)
{ {
AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player)); return getGMDouble(player, "healcooldown", 0);
if (handler == null)
{
return 0;
}
return handler.getPermissionDouble(player.getName(), "teleportcooldown");
} }
@Override @Override
public double getTeleportDelay(CommandSender player) public double getTeleportCooldown(final CommandSender player)
{ {
AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player)); return getGMDouble(player, "teleportcooldown", 0);
if (handler == null)
{
return 0;
}
return handler.getPermissionDouble(player.getName(), "teleportdelay");
} }
@Override @Override
public String getPrefix(CommandSender player) public double getTeleportDelay(final CommandSender player)
{ {
AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player)); return getGMDouble(player, "teleportdelay", 0);
if (handler == null) }
@Override
public String getPrefix(final CommandSender player)
{
try
{ {
final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player));
if (handler == null)
{
return null;
}
return handler.getUserPrefix(player.getName());
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return null; return null;
} }
return handler.getUserPrefix(player.getName());
} }
@Override @Override
public String getSuffix(CommandSender player) public String getSuffix(final CommandSender player)
{ {
AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player)); try
if (handler == null)
{ {
final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player));
if (handler == null)
{
return null;
}
return handler.getUserSuffix(player.getName());
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return null; return null;
} }
return handler.getUserSuffix(player.getName());
} }
@Override @Override
public int getHomeLimit(CommandSender player) public int getHomeLimit(final CommandSender player)
{ {
AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player)); return getGMInteger(player, "homes", 1);
if (handler == null)
{
return 0;
}
return handler.getPermissionInteger(player.getName(), "homes");
} }
@Override @Override
protected String getRawChatFormat(final CommandSender player) protected String getRawChatFormat(final CommandSender player)
{ {
AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player)); String chatformat = getGMString(player, "chatformat", null);
if (handler != null) if (chatformat != null && !chatformat.isEmpty())
{ {
String chatformat = handler.getPermissionString(player.getName(), "chatformat"); return chatformat;
if (chatformat != null && !chatformat.isEmpty())
{
return chatformat;
}
} }
ISettings settings = ess.getSettings(); ISettings settings = ess.getSettings();
@@ -106,22 +153,38 @@ public class GMGroups extends AbstractRanks implements IRanks
@Override @Override
public String getMainGroup(CommandSender player) public String getMainGroup(CommandSender player)
{ {
final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player)); try
if (handler == null)
{ {
final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player));
if (handler == null)
{
return null;
}
return handler.getGroup(player.getName());
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return null; return null;
} }
return handler.getGroup(player.getName());
} }
@Override @Override
public boolean inGroup(CommandSender player, String groupname) public boolean inGroup(CommandSender player, String groupname)
{ {
final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player)); try
if (handler == null)
{ {
final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(getPlayer(player));
if (handler == null)
{
return false;
}
return handler.inGroup(player.getName(), groupname);
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return false; return false;
} }
return handler.inGroup(player.getName(), groupname);
} }
} }

View File

@@ -11,7 +11,7 @@ import net.ess3.storage.StorageObject;
public class RankOptions implements StorageObject public class RankOptions implements StorageObject
{ {
@Comment("Message format of chat messages") @Comment("Message format of chat messages")
private String messageFormat; private String chatFormat;
@Comment("Prefix for name") @Comment("Prefix for name")
private String prefix; private String prefix;
@Comment("Suffix for name") @Comment("Suffix for name")

View File

@@ -17,14 +17,15 @@ import org.bukkit.command.CommandSender;
public class RanksStorage extends AsyncStorageObjectHolder<Ranks> implements IRanks public class RanksStorage extends AsyncStorageObjectHolder<Ranks> implements IRanks
{ {
private AbstractRanks abstractRanks = new AbstractRanks() { private AbstractRanks abstractRanks = new AbstractRanks()
{
@Override @Override
protected String getRawChatFormat(CommandSender sender) protected String getRawChatFormat(final CommandSender sender)
{ {
return RanksStorage.this.getRawChatFormat(sender); return RanksStorage.this.getRawChatFormat(sender);
} }
}; };
@Override @Override
public void finishRead() public void finishRead()
{ {
@@ -150,9 +151,9 @@ public class RanksStorage extends AsyncStorageObjectHolder<Ranks> implements IRa
{ {
for (Entry<String, RankOptions> groupOptions : getGroups(player)) for (Entry<String, RankOptions> groupOptions : getGroups(player))
{ {
if (groupOptions.getValue().getMessageFormat() != null) if (groupOptions.getValue().getChatFormat() != null)
{ {
return groupOptions.getValue().getMessageFormat(); return groupOptions.getValue().getChatFormat();
} }
} }
@@ -161,7 +162,7 @@ public class RanksStorage extends AsyncStorageObjectHolder<Ranks> implements IRa
} }
@Override @Override
public boolean inGroup(CommandSender player, String groupname) public boolean inGroup(final CommandSender player, final String groupname)
{ {
for (Entry<String, RankOptions> groupOptions : getGroups(player)) for (Entry<String, RankOptions> groupOptions : getGroups(player))
{ {
@@ -174,7 +175,7 @@ public class RanksStorage extends AsyncStorageObjectHolder<Ranks> implements IRa
} }
@Override @Override
public String getMainGroup(CommandSender player) public String getMainGroup(final CommandSender player)
{ {
for (Entry<String, RankOptions> groupOptions : getGroups(player)) for (Entry<String, RankOptions> groupOptions : getGroups(player))
{ {

View File

@@ -1,5 +1,6 @@
package net.ess3.ranks; package net.ess3.ranks;
import java.util.logging.Level;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import net.ess3.api.IRanks; import net.ess3.api.IRanks;
import net.ess3.api.ISettings; import net.ess3.api.ISettings;
@@ -16,86 +17,155 @@ public class VaultGroups extends AbstractRanks implements IRanks
{ {
this.ess = ess; this.ess = ess;
} }
private <T> T getServiceProvider(Class<T> clazz) { private <T> T getServiceProvider(final Class<T> clazz)
RegisteredServiceProvider<T> provider = ess.getServer().getServicesManager().getRegistration(clazz); {
final RegisteredServiceProvider<T> provider = ess.getServer().getServicesManager().getRegistration(clazz);
return provider.getProvider(); return provider.getProvider();
} }
@Override private double getVaultDouble(final CommandSender player, final String key, final double defaultValue)
public double getHealCooldown(CommandSender player)
{ {
Chat chat = getServiceProvider(Chat.class); try
return chat.getPlayerInfoDouble(getPlayer(player), "healcooldown", 0); {
final Chat chat = getServiceProvider(Chat.class);
return chat.getPlayerInfoDouble(getPlayer(player), key, defaultValue);
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return defaultValue;
}
}
private int getVaultInteger(final CommandSender player, final String key, final int defaultValue)
{
try
{
final Chat chat = getServiceProvider(Chat.class);
return chat.getPlayerInfoInteger(getPlayer(player), key, defaultValue);
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return defaultValue;
}
}
private String getVaultString(final CommandSender player, final String key, final String defaultValue)
{
try
{
final Chat chat = getServiceProvider(Chat.class);
return chat.getPlayerInfoString(getPlayer(player), key, defaultValue);
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return defaultValue;
}
} }
@Override @Override
public double getTeleportCooldown(CommandSender player) public double getHealCooldown(final CommandSender player)
{ {
Chat chat = getServiceProvider(Chat.class); return getVaultDouble(player, "healcooldown", 0);
return chat.getPlayerInfoDouble(getPlayer(player), "teleportcooldown", 0);
} }
@Override @Override
public double getTeleportDelay(CommandSender player) public double getTeleportCooldown(final CommandSender player)
{ {
Chat chat = getServiceProvider(Chat.class); return getVaultDouble(player, "teleportcooldown", 0);
return chat.getPlayerInfoDouble(getPlayer(player), "teleportdelay", 0);
} }
@Override @Override
public String getPrefix(CommandSender player) public double getTeleportDelay(final CommandSender player)
{ {
Chat chat = getServiceProvider(Chat.class); return getVaultDouble(player, "teleportdelay", 0);
return chat.getPlayerPrefix(getPlayer(player));
} }
@Override @Override
public String getSuffix(CommandSender player) public String getPrefix(final CommandSender player)
{ {
Chat chat = getServiceProvider(Chat.class); try
return chat.getPlayerSuffix(getPlayer(player)); {
final Chat chat = getServiceProvider(Chat.class);
return chat.getPlayerPrefix(getPlayer(player));
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return null;
}
} }
@Override @Override
public int getHomeLimit(CommandSender player) public String getSuffix(final CommandSender player)
{ {
Chat chat = getServiceProvider(Chat.class); try
return chat.getPlayerInfoInteger(getPlayer(player), "homes", 0); {
final Chat chat = getServiceProvider(Chat.class);
return chat.getPlayerSuffix(getPlayer(player));
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return null;
}
}
@Override
public int getHomeLimit(final CommandSender player)
{
return getVaultInteger(player, "homes", 1);
} }
@Override @Override
protected String getRawChatFormat(final CommandSender player) protected String getRawChatFormat(final CommandSender player)
{ {
Chat chat = getServiceProvider(Chat.class); final String chatformat = getVaultString(player, "chatformat", null);
String chatformat = chat.getPlayerInfoString(getPlayer(player), "chatformat", "");
if (chatformat != null && !chatformat.isEmpty()) if (chatformat != null && !chatformat.isEmpty())
{ {
return chatformat; return chatformat;
} }
ISettings settings = ess.getSettings(); final ISettings settings = ess.getSettings();
return settings.getData().getChat().getDefaultFormat(); return settings.getData().getChat().getDefaultFormat();
} }
@Override @Override
public String getMainGroup(CommandSender player) public String getMainGroup(final CommandSender player)
{ {
Chat chat = getServiceProvider(Chat.class); try
return chat.getPrimaryGroup(getPlayer(player)); {
final Chat chat = getServiceProvider(Chat.class);
return chat.getPrimaryGroup(getPlayer(player));
}
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
return null;
}
} }
@Override @Override
public boolean inGroup(CommandSender player, String groupname) public boolean inGroup(final CommandSender player, final String groupname)
{ {
Chat chat = getServiceProvider(Chat.class); try
for (String group : chat.getPlayerGroups(getPlayer(player)))
{ {
if (group.equalsIgnoreCase(groupname)) final Chat chat = getServiceProvider(Chat.class);
for (String group : chat.getPlayerGroups(getPlayer(player)))
{ {
return true; if (group.equalsIgnoreCase(groupname))
{
return true;
}
} }
} }
catch (Exception e)
{
ess.getLogger().log(Level.WARNING, e.getMessage(), e);
}
return false; return false;
} }
} }