Cleanup code

- Removed some code duplication.
This commit is contained in:
Pim van der Loos 2019-04-20 14:58:51 +02:00
parent 4b5de17bb6
commit da360b3d28
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
3 changed files with 40 additions and 68 deletions

View File

@ -49,7 +49,6 @@ public class CommandHandler implements CommandExecutor
String tier = null;
Player receiver;
boolean allowed = false;
ArmorTier armorTier = ArmorTier.NONE;
if (args.length == 1)
{
receiver = player;
@ -66,38 +65,14 @@ public class CommandHandler implements CommandExecutor
tier = args[1];
}
if (tier.equalsIgnoreCase("leather"))
{
armorTier = ArmorTier.LEATHER;
if (player.hasPermission("armoredelytra.give.leather"))
allowed = true;
}
else if (tier.equalsIgnoreCase("gold"))
{
armorTier = ArmorTier.GOLD;
if (player.hasPermission("armoredelytra.give.gold"))
allowed = true;
}
else if (tier.equalsIgnoreCase("chain"))
{
armorTier = ArmorTier.CHAIN;
if (player.hasPermission("armoredelytra.give.chain"))
allowed = true;
}
else if (tier.equalsIgnoreCase("iron"))
{
armorTier = ArmorTier.IRON;
if (player.hasPermission("armoredelytra.give.iron"))
allowed = true;
}
else if (tier.equalsIgnoreCase("diamond"))
{
armorTier = ArmorTier.DIAMOND;
if (player.hasPermission("armoredelytra.give.diamond"))
allowed = true;
}
ArmorTier armorTier = ArmorTier.valueOfName(tier.toLowerCase());
if (armorTier != null)
allowed = player.hasPermission("armoredelytra.give." + ArmorTier.getName(armorTier));
else
{
plugin.messagePlayer(player, plugin.getMyMessages().getString("MESSAGES.UnsupportedTier"));
return false;
}
if (allowed)
{
@ -106,7 +81,7 @@ public class CommandHandler implements CommandExecutor
plugin.giveArmoredElytraToPlayer(receiver, newElytra);
}
else
plugin.messagePlayer(player, plugin.fillInArmorTierInStringNoColor(plugin.getMyMessages().getString("MESSAGES.UnsupportedTier"), armorTier));
plugin.messagePlayer(player, plugin.fillInArmorTierInStringNoColor(plugin.getMyMessages().getString("MESSAGES.NoGivePermission"), armorTier));
return true;
}
}
@ -125,20 +100,9 @@ public class CommandHandler implements CommandExecutor
if (Bukkit.getPlayer(args[0]) != null)
{
player = Bukkit.getPlayer(args[0]);
ArmorTier armorTier = ArmorTier.NONE;
if (tier.equalsIgnoreCase("leather"))
armorTier = ArmorTier.LEATHER;
else if (tier.equalsIgnoreCase("gold"))
armorTier = ArmorTier.GOLD;
else if (tier.equalsIgnoreCase("chain"))
armorTier = ArmorTier.CHAIN;
else if (tier.equalsIgnoreCase("iron"))
armorTier = ArmorTier.IRON;
else if (tier.equalsIgnoreCase("diamond"))
armorTier = ArmorTier.DIAMOND;
else
// TODO: Return a more informative message.
ArmorTier armorTier = ArmorTier.valueOfName(tier.toLowerCase());
if (armorTier == null)
return false;
plugin.elytraReceivedMessage(player, armorTier);

View File

@ -1,26 +1,32 @@
package nl.pim16aap2.armoredElytra.util;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Material;
public enum ArmorTier
{
// Tier: armor-value, armor-toughness, repair
NONE (0 , 0 , null ),
LEATHER (3 , 0 , Material.LEATHER ),
GOLD (5 , 0 , Material.GOLD_INGOT),
CHAIN (5 , 0 , Material.IRON_INGOT),
IRON (6 , 0 , Material.IRON_INGOT),
DIAMOND (8 , 2 , Material.DIAMOND );
NONE (0 , 0 , null , ""),
LEATHER (3 , 0 , Material.LEATHER , "leather"),
GOLD (5 , 0 , Material.GOLD_INGOT, "gold"),
CHAIN (5 , 0 , Material.IRON_INGOT, "chain"),
IRON (6 , 0 , Material.IRON_INGOT, "iron"),
DIAMOND (8 , 2 , Material.DIAMOND , "diamond");
private int armor;
private int toughness;
private Material repair;
private final int armor;
private final int toughness;
private final Material repair;
private final String name;
private static Map<String, ArmorTier> map = new HashMap<>();
private ArmorTier (int armor, int toughness, Material repair)
private ArmorTier (int armor, int toughness, Material repair, String name)
{
this.armor = armor;
this.toughness = toughness;
this.repair = repair;
this.armor = armor;
this.toughness = toughness;
this.repair = repair;
this.name = name;
}
// return the armor value of a tier.
@ -31,4 +37,14 @@ public enum ArmorTier
// return the repair item of a tier
public static Material getRepairItem (ArmorTier tier) { return tier.repair; }
public static String getName (ArmorTier tier) { return tier.name; }
public static ArmorTier valueOfName (String name) { return map.get(name); }
static
{
for (ArmorTier tier : ArmorTier.values())
map.put(tier.name, tier);
}
}

View File

@ -72,19 +72,11 @@ public class Util
public static boolean playerHasCraftPerm(Player player, ArmorTier armorTier)
{
return ((armorTier == ArmorTier.LEATHER && player.hasPermission("armoredelytra.craft.leather")) ||
(armorTier == ArmorTier.GOLD && player.hasPermission("armoredelytra.craft.gold" )) ||
(armorTier == ArmorTier.CHAIN && player.hasPermission("armoredelytra.craft.chain" )) ||
(armorTier == ArmorTier.IRON && player.hasPermission("armoredelytra.craft.iron" )) ||
(armorTier == ArmorTier.DIAMOND && player.hasPermission("armoredelytra.craft.diamond")));
return player.hasPermission("armoredelytra.craft." + ArmorTier.getName(armorTier));
}
public static boolean playerHasWearPerm(Player player, ArmorTier armorTier)
{
return ((armorTier == ArmorTier.LEATHER && player.hasPermission("armoredelytra.wear.leather" )) ||
(armorTier == ArmorTier.GOLD && player.hasPermission("armoredelytra.wear.gold" )) ||
(armorTier == ArmorTier.CHAIN && player.hasPermission("armoredelytra.wear.chain" )) ||
(armorTier == ArmorTier.IRON && player.hasPermission("armoredelytra.wear.iron" )) ||
(armorTier == ArmorTier.DIAMOND && player.hasPermission("armoredelytra.wear.diamond" )));
return player.hasPermission("armoredelytra.wear." + ArmorTier.getName(armorTier));
}
}