From b3615979bed704c58b11b55d02433ccf43773c35 Mon Sep 17 00:00:00 2001 From: HeroGamers Date: Fri, 2 Jun 2023 01:05:47 +0200 Subject: [PATCH] Less code smell --- src/main/java/dk/fido2603/mydog/MyDog.java | 5 +-- src/main/java/dk/fido2603/mydog/MyDogAPI.java | 2 -- .../mydog/managers/CommandManager.java | 31 ++++++++++--------- .../fido2603/mydog/managers/DogManager.java | 6 ++-- .../mydog/managers/TeleportationManager.java | 7 +++++ .../java/dk/fido2603/mydog/objects/Dog.java | 22 ++++++------- .../dk/fido2603/mydog/utils/ColorUtils.java | 4 +++ .../dk/fido2603/mydog/utils/TimeUtils.java | 4 +++ .../mydog/utils/versioning/TesterFactory.java | 3 ++ .../utils/versioning/VersionFactory.java | 4 +++ 10 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/main/java/dk/fido2603/mydog/MyDog.java b/src/main/java/dk/fido2603/mydog/MyDog.java index a4b9822..4e819c3 100644 --- a/src/main/java/dk/fido2603/mydog/MyDog.java +++ b/src/main/java/dk/fido2603/mydog/MyDog.java @@ -38,7 +38,7 @@ /* */ /* https://github.com/DogOnFire/MyHorse */ public class MyDog extends JavaPlugin { - public static MyDog instance; + private static MyDog instance; public static boolean pluginEnabled = false; private static final int pluginId = 18606; @@ -131,7 +131,7 @@ public class MyDog extends JavaPlugin { "Wally", "Walter", "Watson", "Willy", "Winston", "Woody", "Wrigley", "Wyatt", "Yogi", "Yoshi", "Yukon", "Zane", "Zeus", "Ziggy"); - public Map dogLevels = new HashMap<>(); + public final Map dogLevels = new HashMap<>(); private static Economy economy = null; private CommandManager commands = null; @@ -190,6 +190,7 @@ private boolean setupEconomy() { return true; } + @Override public void onDisable() { saveSettings(); reloadSettings(); diff --git a/src/main/java/dk/fido2603/mydog/MyDogAPI.java b/src/main/java/dk/fido2603/mydog/MyDogAPI.java index 39408c9..526c784 100644 --- a/src/main/java/dk/fido2603/mydog/MyDogAPI.java +++ b/src/main/java/dk/fido2603/mydog/MyDogAPI.java @@ -5,8 +5,6 @@ import dk.fido2603.mydog.objects.Dog; import org.bukkit.entity.Wolf; -import dk.fido2603.mydog.MyDog; - public class MyDogAPI { private final MyDog plugin; diff --git a/src/main/java/dk/fido2603/mydog/managers/CommandManager.java b/src/main/java/dk/fido2603/mydog/managers/CommandManager.java index 1ef44b3..1dd53ee 100644 --- a/src/main/java/dk/fido2603/mydog/managers/CommandManager.java +++ b/src/main/java/dk/fido2603/mydog/managers/CommandManager.java @@ -483,7 +483,7 @@ private boolean commandDogList(CommandSender sender) { sender.sendMessage(ChatColor.YELLOW + "---------------- " + this.plugin.getDescription().getFullName() + " ----------------"); for (Map.Entry entry : dogsSorted.entrySet()) { - Wolf wolf = (Wolf) plugin.getServer().getEntity(((Dog) entry.getValue()).getDogId()); + Wolf wolf = (Wolf) plugin.getServer().getEntity(entry.getValue().getDogId()); String healthString = ""; if (wolf != null) { double health = wolf.getHealth(); @@ -498,7 +498,7 @@ private boolean commandDogList(CommandSender sender) { healthString = " " + ChatColor.BLUE + "(HP: " + df.format(health) + "/" + df.format(maxHealth) + ")"; } - sender.sendMessage(ChatColor.AQUA + "#" + ((Dog) entry.getValue()).getIdentifier() + ChatColor.WHITE + " - " + ChatColor.AQUA + ((Dog) entry.getValue()).getDogName() + healthString); + sender.sendMessage(ChatColor.AQUA + "#" + entry.getValue().getIdentifier() + ChatColor.WHITE + " - " + ChatColor.AQUA + entry.getValue().getDogName() + healthString); } return true; } @@ -512,7 +512,7 @@ private boolean commandDogDead(CommandSender sender) { sender.sendMessage(ChatColor.YELLOW + "---------------- " + this.plugin.getDescription().getFullName() + " ----------------"); for (Map.Entry entry : dogsSorted.entrySet()) { - sender.sendMessage(ChatColor.AQUA + "#" + ((Dog) entry.getValue()).getIdentifier() + ChatColor.WHITE + " - " + ChatColor.AQUA + ((Dog) entry.getValue()).getDogName() + ChatColor.WHITE + " LVL " + ((Dog) entry.getValue()).getLevel() + " " + ChatColor.GREEN + " $" + ((Dog) entry.getValue()).getRevivalPrice()); + sender.sendMessage(ChatColor.AQUA + "#" + entry.getValue().getIdentifier() + ChatColor.WHITE + " - " + ChatColor.AQUA + entry.getValue().getDogName() + ChatColor.WHITE + " LVL " + entry.getValue().getLevel() + " " + ChatColor.GREEN + " $" + entry.getValue().getRevivalPrice()); } return true; } @@ -526,10 +526,10 @@ private boolean commandDogRename(CommandSender sender, String[] args) { return false; } - String name = args[2]; + StringBuilder name = new StringBuilder(args[2]); for (int i = 3; i < args.length; i++) { - name += " " + args[i]; + name.append(" ").append(args[i]); } Dog dog = MyDog.getDogManager().getDog(dogIdentifier, ((Player) sender).getUniqueId()); @@ -538,12 +538,12 @@ private boolean commandDogRename(CommandSender sender, String[] args) { return false; } - if (name.isEmpty() || name.length() > 16) { + if ((name.length() == 0) || name.length() > 16) { sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.RED + "Please choose a name between 1 and 16 characters for your Dog!"); return false; } - if (!dog.setDogName(name)) { + if (!dog.setDogName(name.toString())) { sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.RED + "An error occured! Could not set Dog name!"); return false; } @@ -723,8 +723,9 @@ private boolean commandDogStats(CommandSender sender, int dogIdentifier) { Map levels = plugin.dogLevels; - for (Integer levelInt : levels.keySet()) { - int levelExp = levels.get(levelInt).exp; + for (Map.Entry levelSet : levels.entrySet()) { + int levelInt = levelSet.getKey(); + int levelExp = levelSet.getValue().exp; // If experience is under the experience needed to level up if (exp < levelExp) { @@ -1055,7 +1056,7 @@ private boolean commandTrade(CommandSender sender, int dogIdentifier, Player rec ((Player) sender).getDisplayName() + ChatColor.GOLD + " is offering " + dog.getDogColor() + dog.getDogName() + ChatColor.GOLD + (plugin.useLevels ? (" (" + ChatColor.AQUA + "Level " + dog.getLevel() + ChatColor.GOLD + ")") : "") + " for " + ChatColor.AQUA + - ((price > 0.0D ? (df.format(price) + "$") : "free")) + ChatColor.GOLD + "!\n\n" + + (price > 0.0D ? (df.format(price) + "$") : "free") + ChatColor.GOLD + "!\n\n" + ChatColor.GOLD + "Accept the trade request with " + ChatColor.AQUA + "/md tradeaccept\n" + ChatColor.GOLD + "Decline the trade request with " + ChatColor.AQUA + "/md tradedecline\n" + ChatColor.GOLD + "Request expires in 30 seconds!"); @@ -1084,7 +1085,7 @@ public List onTabComplete(CommandSender sender, Command cmd, String alia return Collections.emptyList(); } - List result = new ArrayList(); + List result = new ArrayList<>(); Player player = null; if (sender instanceof Player) { @@ -1092,7 +1093,7 @@ public List onTabComplete(CommandSender sender, Command cmd, String alia } if (args.length == 1 && (cmd.getName().equalsIgnoreCase("mydog") || cmd.getName().equalsIgnoreCase("dog") || cmd.getName().equalsIgnoreCase("dogs") || cmd.getName().equalsIgnoreCase("md"))) { - List arg1 = new ArrayList(); + List arg1 = new ArrayList<>(); if (player == null || (player.isOp() || MyDog.getPermissionsManager().hasPermission(player, "mydog.help"))) { arg1.add("help"); } @@ -1171,7 +1172,7 @@ public List onTabComplete(CommandSender sender, Command cmd, String alia } } - if (player != null && ((plugin.allowRevival && args[0].equalsIgnoreCase("revive")))) { + if (player != null && (plugin.allowRevival && args[0].equalsIgnoreCase("revive"))) { List dogs = MyDog.getDogManager().getDeadDogs(player.getUniqueId()); for (Dog dog : dogs) { arg2.add(Integer.toString(dog.getIdentifier())); @@ -1190,7 +1191,7 @@ public List onTabComplete(CommandSender sender, Command cmd, String alia Iterable SECOND_ARGUMENTS = arg2; StringUtil.copyPartialMatches(args[1], SECOND_ARGUMENTS, result); } else if (args.length == 3) { - List arg3 = new ArrayList(); + List arg3 = new ArrayList<>(); if (args[0].equalsIgnoreCase("rename")) { arg3.add(""); @@ -1211,7 +1212,7 @@ public List onTabComplete(CommandSender sender, Command cmd, String alia Iterable THIRD_ARGUMENTS = arg3; StringUtil.copyPartialMatches(args[2], THIRD_ARGUMENTS, result); } else if (args.length == 4) { - List arg4 = new ArrayList(); + List arg4 = new ArrayList<>(); if (args[0].equalsIgnoreCase("trade")) { arg4.add("[price]"); diff --git a/src/main/java/dk/fido2603/mydog/managers/DogManager.java b/src/main/java/dk/fido2603/mydog/managers/DogManager.java index 0a5f1df..cfac33d 100644 --- a/src/main/java/dk/fido2603/mydog/managers/DogManager.java +++ b/src/main/java/dk/fido2603/mydog/managers/DogManager.java @@ -259,7 +259,7 @@ public boolean hasTrade(UUID recipient) { public HashMap getTrade(UUID recipient) { if (!hasTrade(recipient)) { - return null; + return new HashMap<>(); } return dogTrades.get(recipient); @@ -267,7 +267,7 @@ public HashMap getTrade(UUID recipient) { public Dog getTradeDog(UUID recipient) { HashMap trade = getTrade(recipient); - if (trade == null) { + if (trade == null || trade.isEmpty()) { return null; } @@ -285,7 +285,7 @@ public Double getTradePrice(UUID recipient) { public boolean acceptTrade(Player accepter) { HashMap dogTrade = getTrade(accepter.getUniqueId()); - if (dogTrade == null) { + if (dogTrade == null || dogTrade.isEmpty()) { return false; } diff --git a/src/main/java/dk/fido2603/mydog/managers/TeleportationManager.java b/src/main/java/dk/fido2603/mydog/managers/TeleportationManager.java index c7644e3..b4d8de3 100644 --- a/src/main/java/dk/fido2603/mydog/managers/TeleportationManager.java +++ b/src/main/java/dk/fido2603/mydog/managers/TeleportationManager.java @@ -108,6 +108,13 @@ private HashMap teleportTameableExperimental(Entity e, Locati Tameable tameableEntity = (Tameable) e; + teleportResult.put(false, safeLocation); + return teleportResult; + + if (tameableEntity == null || tameableEntity.isDead() || !(tameableEntity.getOwner() instanceof Player)) { + + } + if (tameableEntity != null && !tameableEntity.isDead() && tameableEntity.getOwner() instanceof Player) { Sittable sittingEntity = (Sittable) e; Player player = (Player) tameableEntity.getOwner(); diff --git a/src/main/java/dk/fido2603/mydog/objects/Dog.java b/src/main/java/dk/fido2603/mydog/objects/Dog.java index fa89320..39adae6 100644 --- a/src/main/java/dk/fido2603/mydog/objects/Dog.java +++ b/src/main/java/dk/fido2603/mydog/objects/Dog.java @@ -34,6 +34,7 @@ public class Dog { private final String pattern = "dd-MM-yyyy HH:mm"; private final DateFormat formatter = new SimpleDateFormat(pattern); + private final static Random random = new Random(); // For new dogs public Dog(Wolf dog, Player dogOwner, int dogUID, int level) { @@ -59,10 +60,8 @@ public Dog(Wolf dog, Player dogOwner, String customName, DyeColor collarColorImp } // Generate a random Collar Color and set the Dog's Color - if (collarColorImport == null) { - if (MyDog.instance().randomCollarColor) { - dog.setCollarColor(ColorUtils.randomDyeColor()); - } + if (collarColorImport == null && MyDog.instance().randomCollarColor) { + dog.setCollarColor(ColorUtils.randomDyeColor()); } this.collarColor = dog.getCollarColor(); @@ -166,7 +165,7 @@ public Date getBirthday() { try { return formatter.parse(MyDog.getDogManager().getDogsConfig().getString(dogId.toString() + ".Birthday")); } catch (ParseException e) { - e.printStackTrace(); + MyDog.instance().log("Failed to parse dog birthday."); return null; } } @@ -220,18 +219,16 @@ public void pet(Player player) { } MyDog.instance().logDebug("Petting dog."); - - Random rand = new Random(); String pettingString = MyDog.instance().pettingString.replace("{dogNameColor}", "&" + getDogColor().getChar()).replace("{dogName}", getDogName()); player.sendMessage(ChatColor.translateAlternateColorCodes('&', pettingString)); - if (rand.nextInt(10) == 1) { + if (random.nextInt(10) == 1) { wolf.playEffect(EntityEffect.WOLF_SHAKE); pettingString = MyDog.instance().pettingSplashString.replace("{dogNameColor}", "&" + getDogColor().getChar()).replace("{dogName}", getDogName()); player.sendMessage(ChatColor.translateAlternateColorCodes('&', pettingString)); } MyDog.getParticleUtils().newPettingParticle(wolf); - Sound sound = PETTING_SOUNDS.get(rand.nextInt(PETTING_SOUNDS.size())); + Sound sound = PETTING_SOUNDS.get(random.nextInt(PETTING_SOUNDS.size())); player.playSound(player.getLocation(), sound, 3.0F, 1.0F); } @@ -368,7 +365,7 @@ public boolean setOwner(Player player) { } public int getRevivalPrice() { - return (int) (level * MyDog.instance().revivalPrice); + return level * MyDog.instance().revivalPrice; } public int getLevel() { @@ -406,8 +403,9 @@ public void setExperience(int exp) { int newLevel = 1; Map levels = MyDog.instance().dogLevels; - for (Integer levelInt : levels.keySet()) { - int levelExp = levels.get(levelInt).exp; + for (Map.Entry levelSet : levels.entrySet()) { + int levelInt = levelSet.getKey(); + int levelExp = levelSet.getValue().exp; // Amount of exp must be higher or equals to exp to level // The level must be higher than the level the Dog had before // The new level variable must be smaller than the level checking against diff --git a/src/main/java/dk/fido2603/mydog/utils/ColorUtils.java b/src/main/java/dk/fido2603/mydog/utils/ColorUtils.java index 5ec4342..56dba3c 100644 --- a/src/main/java/dk/fido2603/mydog/utils/ColorUtils.java +++ b/src/main/java/dk/fido2603/mydog/utils/ColorUtils.java @@ -8,6 +8,10 @@ public class ColorUtils { private static final Random random = new Random(); + private ColorUtils() { + throw new IllegalStateException("Utility class"); + } + public static ChatColor getChatColorFromDyeColor(DyeColor dyeColor) { switch (dyeColor) { case BLACK: diff --git a/src/main/java/dk/fido2603/mydog/utils/TimeUtils.java b/src/main/java/dk/fido2603/mydog/utils/TimeUtils.java index 615d8aa..ab233db 100644 --- a/src/main/java/dk/fido2603/mydog/utils/TimeUtils.java +++ b/src/main/java/dk/fido2603/mydog/utils/TimeUtils.java @@ -3,6 +3,10 @@ import java.util.concurrent.TimeUnit; public class TimeUtils { + private TimeUtils() { + throw new IllegalStateException("Utility class"); + } + // From my other plugin, Semi-Hardcore public static String parseMillisToUFString(long millis) { long days = TimeUnit.MILLISECONDS.toDays(millis); diff --git a/src/main/java/dk/fido2603/mydog/utils/versioning/TesterFactory.java b/src/main/java/dk/fido2603/mydog/utils/versioning/TesterFactory.java index 30a79f3..4464473 100644 --- a/src/main/java/dk/fido2603/mydog/utils/versioning/TesterFactory.java +++ b/src/main/java/dk/fido2603/mydog/utils/versioning/TesterFactory.java @@ -5,6 +5,9 @@ // From DogOnFire's Versioning in Werewolf // https://github.com/DogOnFire/Werewolf public class TesterFactory { + private TesterFactory() { + throw new IllegalStateException("Utility class"); + } @SuppressWarnings("rawtypes") public static Tester getNewTester(Plugin plugin) { diff --git a/src/main/java/dk/fido2603/mydog/utils/versioning/VersionFactory.java b/src/main/java/dk/fido2603/mydog/utils/versioning/VersionFactory.java index df475bc..bf5c3b6 100644 --- a/src/main/java/dk/fido2603/mydog/utils/versioning/VersionFactory.java +++ b/src/main/java/dk/fido2603/mydog/utils/versioning/VersionFactory.java @@ -6,6 +6,10 @@ // From DogOnFire's Versioning in Werewolf // https://github.com/DogOnFire/Werewolf public class VersionFactory { + private VersionFactory() { + throw new IllegalStateException("Utility class"); + } + /** * Factory method used when you want to construct a Version object via a * Plugin object.