From 41e9e3c6789c0d16d0bde8ca9fe6b48d210f4e3a Mon Sep 17 00:00:00 2001 From: HeroGamers Date: Thu, 1 Jun 2023 22:17:41 +0200 Subject: [PATCH] Now with less smell --- src/main/java/dk/fido2603/mydog/MyDog.java | 44 ++++---- src/main/java/dk/fido2603/mydog/MyDogAPI.java | 12 +- .../mydog/listeners/DamageListener.java | 14 +-- .../mydog/listeners/WolfMainListener.java | 27 ++--- .../listeners/WolfMainListener_1_18.java | 2 +- .../mydog/managers/CommandManager.java | 30 +++-- .../fido2603/mydog/managers/DogManager.java | 66 ++++++----- .../mydog/managers/PermissionsManager.java | 12 +- .../mydog/managers/TeleportationManager.java | 7 +- .../java/dk/fido2603/mydog/objects/Dog.java | 104 ++++++++---------- .../fido2603/mydog/objects/LevelFactory.java | 2 +- .../fido2603/mydog/tasks/AttackModeTask.java | 2 +- .../dk/fido2603/mydog/tasks/DistanceTask.java | 4 +- .../dk/fido2603/mydog/utils/ColorUtils.java | 6 +- .../fido2603/mydog/utils/ParticleUtils.java | 18 ++- .../dk/fido2603/mydog/utils/TimeUtils.java | 2 +- .../mydog/utils/versioning/Tester.java | 2 +- .../mydog/utils/versioning/TesterFactory.java | 24 +--- .../mydog/utils/versioning/Version.java | 24 ++-- .../utils/versioning/VersionFactory.java | 2 +- 20 files changed, 202 insertions(+), 202 deletions(-) diff --git a/src/main/java/dk/fido2603/mydog/MyDog.java b/src/main/java/dk/fido2603/mydog/MyDog.java index 536dcc7..a4b9822 100644 --- a/src/main/java/dk/fido2603/mydog/MyDog.java +++ b/src/main/java/dk/fido2603/mydog/MyDog.java @@ -25,8 +25,10 @@ import org.bukkit.Server; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.*; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; @@ -129,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 Map dogLevels = new HashMap<>(); private static Economy economy = null; private CommandManager commands = null; @@ -185,7 +187,7 @@ private boolean setupEconomy() { return false; } economy = rsp.getProvider(); - return economy != null; + return true; } public void onDisable() { @@ -197,9 +199,9 @@ public void onDisable() { @Override public void onEnable() { - WolfMainListener tameListener = null; - WolfMainListener_1_18 tameListener_1_18 = null; - DamageListener damageListener = null; + WolfMainListener tameListener; + WolfMainListener_1_18 tameListener_1_18; + DamageListener damageListener; plugin = this; instance = this; @@ -220,7 +222,8 @@ public void onEnable() { PluginManager pm = getServer().getPluginManager(); // Check for Vault - if (pm.getPlugin("Vault") != null && pm.getPlugin("Vault").isEnabled()) { + Plugin vaultPlugin = pm.getPlugin("Vault"); + if (vaultPlugin != null && vaultPlugin.isEnabled()) { log("Vault detected."); setupEconomy(); RegisteredServiceProvider permissionProvider = plugin.getServer().getServicesManager().getRegistration(Permission.class); @@ -308,13 +311,16 @@ public void loadSettings() { // Levels if (config.getConfigurationSection("DogSettings.Levels") != null) { this.dogLevels.clear(); - for (String level : config.getConfigurationSection("DogSettings.Levels").getKeys(false)) { - if (config.getConfigurationSection("DogSettings.Levels." + level) != null) { - int exp = config.getInt("DogSettings.Levels." + level + ".Experience"); - double health = config.getInt("DogSettings.Levels." + level + ".Health"); - double damage = config.getInt("DogSettings.Levels." + level + ".Damage"); - - this.dogLevels.put(Integer.parseInt(level), getLevelFactory().newLevel(Integer.parseInt(level), exp, health, damage)); + ConfigurationSection levelsSection = config.getConfigurationSection("DogSettings.Levels"); + if (levelsSection != null) { + for (String level : levelsSection.getKeys(false)) { + if (config.getConfigurationSection("DogSettings.Levels." + level) != null) { + int exp = config.getInt("DogSettings.Levels." + level + ".Experience"); + double health = config.getInt("DogSettings.Levels." + level + ".Health"); + double damage = config.getInt("DogSettings.Levels." + level + ".Damage"); + + this.dogLevels.put(Integer.parseInt(level), getLevelFactory().newLevel(Integer.parseInt(level), exp, health, damage)); + } } } } else { @@ -348,13 +354,13 @@ public void loadSettings() { public void saveSettings() { config.set("Settings.ServerName", this.serverName); - config.set("Settings.Debug", Boolean.valueOf(this.debug)); + config.set("Settings.Debug", this.debug); config.set("Settings.ChatPrefix", this.chatPrefix); - config.set("Settings.InstantSaveConfig", Boolean.valueOf(this.instantSave)); - config.set("Settings.AutomaticTeleportation", Boolean.valueOf(this.automaticTeleportation)); - config.set("Settings.ExpandedSearch", Boolean.valueOf(this.expandedSearch)); - config.set("Settings.EnableExperimentalTeleport", Boolean.valueOf(this.experimentalTeleport)); - config.set("Settings.PlayerDistanceCheck", Boolean.valueOf(this.playerDistanceCheck)); + config.set("Settings.InstantSaveConfig", this.instantSave); + config.set("Settings.AutomaticTeleportation", this.automaticTeleportation); + config.set("Settings.ExpandedSearch", this.expandedSearch); + config.set("Settings.EnableExperimentalTeleport", this.experimentalTeleport); + config.set("Settings.PlayerDistanceCheck", this.playerDistanceCheck); config.set("DogSettings.RandomCollarColor", this.randomCollarColor); config.set("DogSettings.UseLevels", this.useLevels); config.set("DogSettings.TeleportOnWorldChange", this.teleportOnWorldChange); diff --git a/src/main/java/dk/fido2603/mydog/MyDogAPI.java b/src/main/java/dk/fido2603/mydog/MyDogAPI.java index 65bbee4..39408c9 100644 --- a/src/main/java/dk/fido2603/mydog/MyDogAPI.java +++ b/src/main/java/dk/fido2603/mydog/MyDogAPI.java @@ -2,25 +2,27 @@ import java.util.UUID; +import dk.fido2603.mydog.objects.Dog; import org.bukkit.entity.Wolf; import dk.fido2603.mydog.MyDog; public class MyDogAPI { - private MyDog plugin = null; + private final MyDog plugin; public MyDogAPI(MyDog p) { this.plugin = p; } public boolean isDog(UUID dogUUID) { - if (plugin.getServer().getEntity(dogUUID) instanceof Wolf) { - return MyDog.getDogManager().isDog(dogUUID); - } - return false; + return MyDog.getDogManager().isDog(dogUUID); } public boolean isDog(Wolf dog) { return MyDog.getDogManager().isDog(dog.getUniqueId()); } + + public Dog getDog(UUID dogUUID) { + return MyDog.getDogManager().getDog(dogUUID); + } } diff --git a/src/main/java/dk/fido2603/mydog/listeners/DamageListener.java b/src/main/java/dk/fido2603/mydog/listeners/DamageListener.java index f55824e..4c83b10 100644 --- a/src/main/java/dk/fido2603/mydog/listeners/DamageListener.java +++ b/src/main/java/dk/fido2603/mydog/listeners/DamageListener.java @@ -16,7 +16,7 @@ import org.bukkit.event.entity.EntityDamageEvent; public class DamageListener implements Listener { - private MyDog plugin = null; + private final MyDog plugin; public DamageListener(MyDog p) { this.plugin = p; @@ -75,16 +75,13 @@ public void onWolfEntityDamage2(EntityDamageByEntityEvent e) { AttributeInstance wolfMaxHealth = wolf.getAttribute(Attribute.GENERIC_MAX_HEALTH); - if (wolfMaxHealth.getValue() != health) { + if (wolfMaxHealth != null && wolfMaxHealth.getValue() != health) { wolfMaxHealth.setBaseValue(health); } if (wolf.getHealth() < health) { - if (wolf.getHealth() + healthPoints > health) { - wolf.setHealth(health); - } else { - wolf.setHealth(wolf.getHealth() + healthPoints); - } + // If health would overflow, just set full health + wolf.setHealth(Math.min(wolf.getHealth() + healthPoints, health)); plugin.logDebug("Gave the dog, " + dog.getDogName() + ", " + healthPoints + " in health."); } } @@ -187,11 +184,10 @@ public void onEntityDeath(EntityDamageEvent event) { case PLAYER: if (plugin.allowPlayerKillExp) { gainedExp = 70; - break; } else { gainedExp = 0; - break; } + break; case ELDER_GUARDIAN: case GIANT: gainedExp = 90; diff --git a/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener.java b/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener.java index 724d040..1c0d43c 100644 --- a/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener.java +++ b/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener.java @@ -24,7 +24,7 @@ import org.bukkit.scheduler.BukkitRunnable; public class WolfMainListener implements Listener { - private MyDog plugin = null; + private final MyDog plugin; public WolfMainListener(MyDog p) { this.plugin = p; @@ -52,7 +52,7 @@ public void onEntityTameEvent(EntityTameEvent event) { public void run() { plugin.logDebug("Running newTamedDog BukkitRunnable..."); Dog dog = MyDog.getDogManager().newDog(wolf, owner); - plugin.logDebug("New dog! Name: " + dog.getDogName() + " - DogId: " + dog.getDogId() + " - Owner: " + plugin.getServer().getPlayer(dog.getOwnerId()).getName() + " - OwnerId: " + dog.getOwnerId()); + plugin.logDebug("New dog! Name: " + dog.getDogName() + " - DogId: " + dog.getDogId() + " - Owner: " + Objects.requireNonNull(plugin.getServer().getPlayer(dog.getOwnerId())).getName() + " - OwnerId: " + dog.getOwnerId()); Location dogLocation = dog.getDogLocation(); plugin.logDebug("Dog Location = X: " + dogLocation.getX() + " Y: " + dogLocation.getY() + " Z: " + dogLocation.getZ()); @@ -122,9 +122,9 @@ public void onWolfPlayerInteract(PlayerInteractEntityEvent event) { Wolf wolf = (Wolf) entity; ItemStack item = null; - if (hand.equals(EquipmentSlot.HAND)) { + if (hand.equals(EquipmentSlot.HAND) && player.getEquipment() != null) { item = player.getEquipment().getItemInMainHand(); - } else if (hand.equals(EquipmentSlot.OFF_HAND)) { + } else if (hand.equals(EquipmentSlot.OFF_HAND) && player.getEquipment() != null) { item = player.getEquipment().getItemInOffHand(); } else { plugin.logDebug("No item in hand."); @@ -202,7 +202,7 @@ public int onWolfPlayerInteractAlreadyTamed(Dog dog, Wolf wolf, Player player) { plugin.logDebug("New already-tamed dog creation failed!"); return 1; // Error } - plugin.logDebug("New already-tamed dog! Name: " + dog.getDogName() + " - DogId: " + dog.getDogId() + " - Owner: " + plugin.getServer().getPlayer(dog.getOwnerId()).getName() + " - OwnerId: " + dog.getOwnerId()); + plugin.logDebug("New already-tamed dog! Name: " + dog.getDogName() + " - DogId: " + dog.getDogId() + " - Owner: " + Objects.requireNonNull(plugin.getServer().getPlayer(dog.getOwnerId())).getName() + " - OwnerId: " + dog.getOwnerId()); Location dogLocation = dog.getDogLocation(); plugin.logDebug("Dog Location = X: " + dogLocation.getX() + " Y: " + dogLocation.getY() + " Z: " + dogLocation.getZ()); @@ -268,16 +268,13 @@ public int onWolfPlayerInteractFeed(Dog dog, Wolf wolf, ItemStack item, Player p AttributeInstance wolfMaxHealth = wolf.getAttribute(Attribute.GENERIC_MAX_HEALTH); - if (wolfMaxHealth.getValue() != health) { + if (wolfMaxHealth != null && wolfMaxHealth.getValue() != health) { wolfMaxHealth.setBaseValue(health); } if (wolf.getHealth() >= 20.0 && wolf.getHealth() < health) { - if (wolf.getHealth() + healthPoints > health) { - wolf.setHealth(health); - } else { - wolf.setHealth(wolf.getHealth() + healthPoints); - } + // Avoid health overflow, so get min value + wolf.setHealth(Math.min(wolf.getHealth() + healthPoints, health)); plugin.logDebug("Gave the dog, " + dog.getDogName() + ", " + healthPoints + " in health."); if (player.getGameMode() != GameMode.CREATIVE) { item.setAmount(item.getAmount() - 1); @@ -384,7 +381,7 @@ public int onWolfPlayerInteractRename(Dog dog, ItemStack item, Player player) { } // Check if the player has a name_tag equipped - if (item.getType().equals(Material.NAME_TAG) && item.getItemMeta().hasDisplayName()) { + if (item.getType().equals(Material.NAME_TAG) && item.getItemMeta() != null && item.getItemMeta().hasDisplayName()) { if (!plugin.allowNametagRename || !dog.getOwnerId().equals(player.getUniqueId())) { plugin.logDebug("NametagRename is disabled or not owner trying to rename dog!"); return 2; // Cancel event @@ -402,13 +399,13 @@ public int onWolfPlayerInteractRename(Dog dog, ItemStack item, Player player) { @EventHandler(priority = EventPriority.LOWEST) public void onBreedEvent(EntityBreedEvent event) { - if (event.getEntity() == null || !(event.getEntity() instanceof Wolf) || /*(!MyDog.getDogManager().isDog(event.getMother().getUniqueId())) || (!MyDog.getDogManager().isDog(event.getFather().getUniqueId())) ||*/ !(event.getBreeder() instanceof Player)) { + if (!(event.getEntity() instanceof Wolf) || /*(!MyDog.getDogManager().isDog(event.getMother().getUniqueId())) || (!MyDog.getDogManager().isDog(event.getFather().getUniqueId())) ||*/ !(event.getBreeder() instanceof Player)) { plugin.logDebug("Entity breed return!"); return; } Wolf wolf = (Wolf) event.getEntity(); - Player ownerFind = null; + Player ownerFind; if (wolf.getOwner() != null) { ownerFind = (Player) wolf.getOwner(); @@ -429,7 +426,7 @@ public void onBreedEvent(EntityBreedEvent event) { public void run() { plugin.logDebug("Running newDogBreed BukkitRunnable..."); Dog dog = MyDog.getDogManager().newDog(wolf, owner); - plugin.logDebug("New dog! Name: " + dog.getDogName() + " - DogId: " + dog.getDogId() + " - Owner: " + plugin.getServer().getPlayer(dog.getOwnerId()).getName() + " - OwnerId: " + dog.getOwnerId()); + plugin.logDebug("New dog! Name: " + dog.getDogName() + " - DogId: " + dog.getDogId() + " - Owner: " + Objects.requireNonNull(plugin.getServer().getPlayer(dog.getOwnerId())).getName() + " - OwnerId: " + dog.getOwnerId()); Location dogLocation = dog.getDogLocation(); plugin.logDebug("Dog Location = X: " + dogLocation.getX() + " Y: " + dogLocation.getY() + " Z: " + dogLocation.getZ()); diff --git a/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener_1_18.java b/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener_1_18.java index 8159f19..54c0307 100644 --- a/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener_1_18.java +++ b/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener_1_18.java @@ -11,7 +11,7 @@ import java.util.List; public class WolfMainListener_1_18 implements Listener { - private MyDog plugin = null; + private final MyDog plugin; public WolfMainListener_1_18(MyDog p) { this.plugin = p; diff --git a/src/main/java/dk/fido2603/mydog/managers/CommandManager.java b/src/main/java/dk/fido2603/mydog/managers/CommandManager.java index 1bfbf31..1ef44b3 100644 --- a/src/main/java/dk/fido2603/mydog/managers/CommandManager.java +++ b/src/main/java/dk/fido2603/mydog/managers/CommandManager.java @@ -7,6 +7,7 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeInstance; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -17,7 +18,7 @@ import dk.fido2603.mydog.objects.LevelFactory.Level; public class CommandManager { - private MyDog plugin = null; + private final MyDog plugin; public CommandManager(MyDog p) { this.plugin = p; @@ -485,8 +486,15 @@ private boolean commandDogList(CommandSender sender) { Wolf wolf = (Wolf) plugin.getServer().getEntity(((Dog) entry.getValue()).getDogId()); String healthString = ""; if (wolf != null) { - double maxHealth = wolf.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); double health = wolf.getHealth(); + AttributeInstance maxHealthInstance = wolf.getAttribute(Attribute.GENERIC_MAX_HEALTH); + double maxHealth; + if (maxHealthInstance != null) { + maxHealth = maxHealthInstance.getValue(); + } + else { + maxHealth = health; + } healthString = " " + ChatColor.BLUE + "(HP: " + df.format(health) + "/" + df.format(maxHealth) + ")"; } @@ -709,7 +717,7 @@ private boolean commandDogStats(CommandSender sender, int dogIdentifier) { sender.sendMessage(ChatColor.AQUA + "Level: " + ChatColor.WHITE + dog.getLevel()); // Calculate and make experience string - String experienceString = ""; + String experienceString; double exp = dog.getExperience(); double maxExp = 0; @@ -752,8 +760,15 @@ private boolean commandDogStats(CommandSender sender, int dogIdentifier) { if (wolf != null) { // Health graphics - double maxHealth = wolf.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); double health = wolf.getHealth(); + AttributeInstance maxHealthInstance = wolf.getAttribute(Attribute.GENERIC_MAX_HEALTH); + double maxHealth; + if (maxHealthInstance != null) { + maxHealth = maxHealthInstance.getValue(); + } + else { + maxHealth = health; + } double percent = (health / maxHealth) * 100; @@ -762,12 +777,13 @@ private boolean commandDogStats(CommandSender sender, int dogIdentifier) { sender.sendMessage(ChatColor.AQUA + "Health: " + healthString + ChatColor.AQUA + "" + ChatColor.BOLD + " [" + ChatColor.DARK_AQUA + df.format(health) + ChatColor.AQUA + "" + ChatColor.BOLD + "/" + ChatColor.RESET + ChatColor.AQUA + df.format(maxHealth) + ChatColor.AQUA + "" + ChatColor.BOLD + "]"); - sender.sendMessage(ChatColor.AQUA + "Damage: " + ChatColor.WHITE + wolf.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE).getValue() + " HP"); + AttributeInstance attackDamage = wolf.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE); + sender.sendMessage(ChatColor.AQUA + "Damage: " + ChatColor.WHITE + (attackDamage != null ? attackDamage.getValue() : 0) + " HP"); } Location dogLoc = dog.getDogLocation(); if (dogLoc != null) { - sender.sendMessage(ChatColor.AQUA + "Last Seen at: " + ChatColor.DARK_AQUA + "World: " + ChatColor.WHITE + dogLoc.getWorld().getName() + + sender.sendMessage(ChatColor.AQUA + "Last Seen at: " + ChatColor.DARK_AQUA + "World: " + ChatColor.WHITE + (dogLoc.getWorld() != null ? dogLoc.getWorld().getName() : "Unknown World") + ChatColor.DARK_AQUA + " X: " + ChatColor.WHITE + df.format(dogLoc.getX()) + ChatColor.DARK_AQUA + " Y: " + ChatColor.WHITE + df.format(dogLoc.getY()) + ChatColor.DARK_AQUA + " Z: " + ChatColor.WHITE + df.format(dogLoc.getZ())); } @@ -866,7 +882,7 @@ private boolean commandDogComehere(CommandSender sender, int dogIdentifier) { Wolf wolf = null; Location dogLocation = dog.getDogLocation(); - Boolean useLocation = false; + boolean useLocation = false; if (dogLocation == null) { wolf = (Wolf) plugin.getServer().getEntity(dog.getDogId()); if (wolf == null) { diff --git a/src/main/java/dk/fido2603/mydog/managers/DogManager.java b/src/main/java/dk/fido2603/mydog/managers/DogManager.java index 93a49f4..0a5f1df 100644 --- a/src/main/java/dk/fido2603/mydog/managers/DogManager.java +++ b/src/main/java/dk/fido2603/mydog/managers/DogManager.java @@ -16,13 +16,13 @@ import org.bukkit.scheduler.BukkitRunnable; public class DogManager { - private MyDog plugin = null; + private final MyDog plugin; private FileConfiguration dogsConfig = null; private File dogsConfigFile = null; - private Random random = new Random(); + private final Random random = new Random(); private long lastSaveTime = 0L; - private HashMap> dogTrades = new HashMap<>(); + private final HashMap> dogTrades = new HashMap<>(); public DogManager(MyDog plugin) { this.plugin = plugin; @@ -66,6 +66,9 @@ public FileConfiguration getDogsConfig() { } public boolean isDog(UUID dogId) { + if (!(plugin.getServer().getEntity(dogId) instanceof Wolf)) { + return false; + } return dogsConfig.contains(dogId.toString()); } @@ -77,7 +80,7 @@ public void removeDog(UUID dogId) { } public boolean isUUIDDeadDog(UUID uuid) { - return dogsConfig.contains(uuid.toString()) && dogsConfig.getBoolean(uuid.toString() + ".isDead"); + return dogsConfig.contains(uuid.toString()) && dogsConfig.getBoolean(uuid + ".isDead"); } public void dogDied(UUID dogId) { @@ -99,21 +102,24 @@ public int dogsOwned(UUID playerId) { int dogs = 0; for (String dogUUID : dogsConfig.getKeys(false)) { plugin.logDebug(dogUUID); - UUID ownerId = UUID.fromString(dogsConfig.getString(dogUUID + ".Owner")); - if (ownerId.equals(playerId)) { - dogs++; + String ownerIdString = dogsConfig.getString(dogUUID + ".Owner"); + if (ownerIdString != null) { + UUID ownerId = UUID.fromString(ownerIdString); + if (ownerId.equals(playerId)) { + dogs++; + } } } return dogs; } - public void changeDogUUID(UUID oldDogID, UUID newDogID) { - Dog oldDog = getDog(oldDogID); - Dog newDog = null; - if (oldDog != null) { - - } - } +// public void changeDogUUID(UUID oldDogID, UUID newDogID) { +// Dog oldDog = getDog(oldDogID); +// Dog newDog = null; +// if (oldDog != null) { +// +// } +// } public boolean canTameMoreDogs(Player player) { if (player.isOp()) { @@ -139,18 +145,29 @@ public Dog newDog(Wolf dog, Player dogOwner, String customName, DyeColor collarC return new Dog(dog, dogOwner, customName, collarColor, dogID, null); } + public UUID getDogOwnerId(UUID dogId) { + if (!dogsConfig.contains(dogId.toString())) { + return null; + } + String ownerIdString = dogsConfig.getString(dogId + ".Owner"); + if (ownerIdString == null) { + return null; + } + return UUID.fromString(ownerIdString); + } + public Dog getDog(UUID dogId) { - if (dogsConfig.contains(dogId.toString())) { - return new Dog(dogId, UUID.fromString(dogsConfig.getString(dogId.toString() + ".Owner"))); + if (isDog(dogId)) { + return new Dog(dogId, getDogOwnerId(dogId)); } return null; } public Dog getDog(int dogIdentifier, UUID ownerId) { for (String dogIdString : dogsConfig.getKeys(false)) { - if (dogsConfig.getString(dogIdString + ".ID").equals(Integer.toString(dogIdentifier)) && dogsConfig.getString(dogIdString + ".Owner").contains(ownerId.toString())) { + if (Objects.equals(dogsConfig.getString(dogIdString + ".ID"), Integer.toString(dogIdentifier)) && ownerId.equals(getDogOwnerId(UUID.fromString(dogIdString)))) { UUID dogId = UUID.fromString(dogIdString); - return new Dog(dogId, UUID.fromString(dogsConfig.getString(dogId.toString() + ".Owner"))); + return new Dog(dogId, ownerId); } } @@ -158,7 +175,7 @@ public Dog getDog(int dogIdentifier, UUID ownerId) { } public List getDogs() { - List dogs = new ArrayList(); + List dogs = new ArrayList<>(); for (String dogIdString : dogsConfig.getKeys(false)) { UUID dogId = UUID.fromString(dogIdString); @@ -172,7 +189,7 @@ public List getDogs(UUID ownerId) { List dogs = new ArrayList<>(); for (String dogIdString : dogsConfig.getKeys(false)) { - if (dogsConfig.getString(dogIdString + ".Owner").contains(ownerId.toString())) { + if (ownerId.equals(getDogOwnerId(UUID.fromString(dogIdString)))) { UUID dogId = UUID.fromString(dogIdString); dogs.add(new Dog(dogId, ownerId)); } @@ -316,9 +333,7 @@ public boolean setNewId(Dog dog, int id) { if (!dog.setIdentifier(id)) { Dog dog2 = getDog(id, dog.getOwnerId()); if (dog2.setIdentifier(generateNewId(dog.getOwnerId()))) { - if (dog.setIdentifier(id)) { - return true; - } + return dog.setIdentifier(id); } } else { return true; @@ -353,7 +368,6 @@ public int generateNewId(UUID dogOwnerId) { plugin.logDebug("ok"); } else { plugin.logDebug("Dogs list is empty!"); - id = 1; } plugin.logDebug("Returning ID: " + id); @@ -364,14 +378,14 @@ public void handleNewLevel(Dog dog) { plugin.logDebug("Dog levelup! Level before: " + (dog.getLevel() - 1) + " - Level now: " + dog.getLevel()); UUID ownerId = dog.getOwnerId(); Player owner = plugin.getServer().getPlayer(ownerId); - if (owner.isOnline()) { + if (owner != null && owner.isOnline()) { String levelupString = plugin.levelUpString.replace("{chatPrefix}", plugin.getChatPrefix()).replace("{dogNameColor}", "&" + dog.getDogColor().getChar()).replace("{dogName}", dog.getDogName()).replace("{level}", Integer.toString(dog.getLevel())); /*owner.sendMessage(ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.DARK_PURPLE + "Your dog, " + dog.getDogColor() + dog.getDogName() + ChatColor.DARK_PURPLE + ", just leveled up to " + ChatColor.LIGHT_PURPLE + "Level " + dog.level + ChatColor.DARK_PURPLE + "!");*/ owner.sendMessage(ChatColor.translateAlternateColorCodes('&', levelupString)); MyDog.getParticleUtils().newLevelUpParticle(plugin.getServer().getEntity(dog.getDogId())); - Sound sound = null; + Sound sound; if (plugin.levelUpSound == null) { plugin.logDebug("Couldn't load the levelup sound, took Howl!"); sound = Sound.ENTITY_WOLF_HOWL; diff --git a/src/main/java/dk/fido2603/mydog/managers/PermissionsManager.java b/src/main/java/dk/fido2603/mydog/managers/PermissionsManager.java index f796d24..a231879 100644 --- a/src/main/java/dk/fido2603/mydog/managers/PermissionsManager.java +++ b/src/main/java/dk/fido2603/mydog/managers/PermissionsManager.java @@ -9,8 +9,8 @@ import org.bukkit.plugin.RegisteredServiceProvider; public class PermissionsManager { - private String pluginName = "null"; - private MyDog plugin; + private final String pluginName = "null"; + private final MyDog plugin; private Permission vaultPermission = null; private Chat vaultChat = null; @@ -19,10 +19,14 @@ public PermissionsManager(MyDog p) { if (p.vaultEnabled) { RegisteredServiceProvider permissionProvider = plugin.getServer().getServicesManager().getRegistration(Permission.class); - vaultPermission = permissionProvider.getProvider(); + if (permissionProvider != null) { + vaultPermission = permissionProvider.getProvider(); + } RegisteredServiceProvider chatProvider = plugin.getServer().getServicesManager().getRegistration(Chat.class); - vaultChat = chatProvider.getProvider(); + if (chatProvider != null) { + vaultChat = chatProvider.getProvider(); + } } } diff --git a/src/main/java/dk/fido2603/mydog/managers/TeleportationManager.java b/src/main/java/dk/fido2603/mydog/managers/TeleportationManager.java index f2f44f9..c7644e3 100644 --- a/src/main/java/dk/fido2603/mydog/managers/TeleportationManager.java +++ b/src/main/java/dk/fido2603/mydog/managers/TeleportationManager.java @@ -3,7 +3,6 @@ import dk.fido2603.mydog.MyDog; import dk.fido2603.mydog.objects.Dog; import net.md_5.bungee.api.ChatColor; -import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.Material; @@ -18,9 +17,9 @@ import java.util.UUID; public class TeleportationManager { - private MyDog plugin = null; - private List teleportingEntities = new ArrayList<>(); - private List entityChunks = new ArrayList<>(); + private final MyDog plugin; + private final List teleportingEntities = new ArrayList<>(); + private final List entityChunks = new ArrayList<>(); public TeleportationManager(MyDog p) { this.plugin = p; diff --git a/src/main/java/dk/fido2603/mydog/objects/Dog.java b/src/main/java/dk/fido2603/mydog/objects/Dog.java index 8f5b71c..fa89320 100644 --- a/src/main/java/dk/fido2603/mydog/objects/Dog.java +++ b/src/main/java/dk/fido2603/mydog/objects/Dog.java @@ -32,8 +32,8 @@ public class Dog { private Boolean isDead; private Boolean isAngry; - private String pattern = "dd-MM-yyyy HH:mm"; - private DateFormat formatter = new SimpleDateFormat(pattern); + private final String pattern = "dd-MM-yyyy HH:mm"; + private final DateFormat formatter = new SimpleDateFormat(pattern); // For new dogs public Dog(Wolf dog, Player dogOwner, int dogUID, int level) { @@ -90,7 +90,7 @@ public Dog(Wolf dog, Player dogOwner, String customName, DyeColor collarColorImp // For old, already created, dogs public Dog(Wolf dog) { - this(dog.getUniqueId(), dog.getOwner().getUniqueId()); + this(dog.getUniqueId(), Objects.requireNonNull(dog.getOwner()).getUniqueId()); } // For old, already created, dogs @@ -121,11 +121,12 @@ public String getDogName() { } public Wolf getWolf() { - if (MyDog.instance().getServer().getEntity(dogId) == null || !MyDog.instance().getServer().getEntity(dogId).isValid() || !(MyDog.instance().getServer().getEntity(dogId) instanceof Wolf)) { + Entity wolfEntity = MyDog.instance().getServer().getEntity(dogId); + if (wolfEntity == null || !wolfEntity.isValid() || !(wolfEntity instanceof Wolf)) { return null; } - return (Wolf) MyDog.instance().getServer().getEntity(dogId); + return (Wolf) wolfEntity; } /** @@ -175,14 +176,15 @@ public ChatColor getDogColor() { return nameColor; } - if (MyDog.instance().getServer().getEntity(dogId) == null || !MyDog.instance().getServer().getEntity(dogId).isValid()) { + Wolf wolf = getWolf(); + if (wolf == null) { if (MyDog.getDogManager().getDogsConfig().getString(dogId.toString() + ".NameChatColor") != null) { return ChatColor.valueOf(MyDog.getDogManager().getDogsConfig().getString(dogId.toString() + ".NameChatColor")); } return ChatColor.WHITE; } - return ColorUtils.getChatColorFromDyeColor(((Wolf) MyDog.instance().getServer().getEntity(dogId)).getCollarColor()); + return ColorUtils.getChatColorFromDyeColor(wolf.getCollarColor()); } public boolean setDogColor(DyeColor color) { @@ -190,23 +192,14 @@ public boolean setDogColor(DyeColor color) { return false; } if (MyDog.getDogManager().getDogsConfig().contains(dogId.toString())) { - ChatColor chatColor = ColorUtils.getChatColorFromDyeColor(color); - - this.nameColor = chatColor; + this.nameColor = ColorUtils.getChatColorFromDyeColor(color); MyDog.getDogManager().getDogsConfig().set(dogId.toString() + ".NameChatColor", nameColor.name()); - Wolf dog = (Wolf) MyDog.instance().getServer().getEntity(dogId); - - if (dog == null) { - MyDog.instance().logDebug("Dog is null"); - return false; - } - if (dogName == null) { this.dogName = getDogName(); } - setDogCustomName(); + return setDogCustomName(); } MyDog.getDogManager().saveTimed(); return true; @@ -221,8 +214,8 @@ public void pet(Player player) { return; } - Wolf dog = (Wolf) MyDog.instance().getServer().getEntity(dogId); - if (dog == null) { + Wolf wolf = getWolf(); + if (wolf == null) { return; } @@ -233,11 +226,11 @@ public void pet(Player player) { player.sendMessage(ChatColor.translateAlternateColorCodes('&', pettingString)); if (rand.nextInt(10) == 1) { - dog.playEffect(EntityEffect.WOLF_SHAKE); + wolf.playEffect(EntityEffect.WOLF_SHAKE); pettingString = MyDog.instance().pettingSplashString.replace("{dogNameColor}", "&" + getDogColor().getChar()).replace("{dogName}", getDogName()); player.sendMessage(ChatColor.translateAlternateColorCodes('&', pettingString)); } - MyDog.getParticleUtils().newPettingParticle(dog); + MyDog.getParticleUtils().newPettingParticle(wolf); Sound sound = PETTING_SOUNDS.get(rand.nextInt(PETTING_SOUNDS.size())); player.playSound(player.getLocation(), sound, 3.0F, 1.0F); } @@ -247,10 +240,6 @@ public void toggleMode() { } public void setAngryMode(boolean angry) { - Wolf dog = (Wolf) MyDog.instance().getServer().getEntity(dogId); - if (dog == null) { - return; - } MyDog.instance().logDebug("Toggling dog mode."); this.setIsAngry(angry); setDogCustomName(); @@ -258,12 +247,12 @@ public void setAngryMode(boolean angry) { } public void sit(boolean sit) { - Wolf dog = (Wolf) MyDog.instance().getServer().getEntity(dogId); - if (dog == null || !dog.isValid()) { + Wolf wolf = getWolf(); + if (wolf == null) { return; } - dog.setSitting(sit); + wolf.setSitting(sit); getDogLocation(); } @@ -304,8 +293,8 @@ public boolean setDogName(String name) { } public Location getDogLocation() { - Entity dogEntity = MyDog.instance().getServer().getEntity(dogId); - if (dogEntity == null || !dogEntity.isValid()) { + Wolf wolf = getWolf(); + if (wolf == null) { if (MyDog.getDogManager().getDogsConfig().getString(dogId.toString() + ".LastSeen.World") != null) { String lastSeenWorld = MyDog.getDogManager().getDogsConfig().getString(dogId.toString() + ".LastSeen.World"); if (lastSeenWorld == null) { @@ -316,25 +305,17 @@ public Location getDogLocation() { return null; } - this.location = dogEntity.getLocation(); - if (this.location.getWorld() != null) { - MyDog.getDogManager().getDogsConfig().set(dogId.toString() + ".LastSeen.World", location.getWorld().getName()); - } - MyDog.getDogManager().getDogsConfig().set(dogId.toString() + ".LastSeen.X", location.getX()); - MyDog.getDogManager().getDogsConfig().set(dogId.toString() + ".LastSeen.Y", location.getY()); - MyDog.getDogManager().getDogsConfig().set(dogId.toString() + ".LastSeen.Z", location.getZ()); - - MyDog.getDogManager().saveTimed(); - return location; + saveDogLocation(); + return this.location; } public boolean saveDogLocation() { - Entity dogEntity = MyDog.instance().getServer().getEntity(dogId); - if (dogEntity == null || !dogEntity.isValid()) { + Wolf wolf = getWolf(); + if (wolf == null) { return false; } - this.location = dogEntity.getLocation(); + this.location = wolf.getLocation(); if (this.location.getWorld() != null) { MyDog.getDogManager().getDogsConfig().set(dogId.toString() + ".LastSeen.World", location.getWorld().getName()); } @@ -363,9 +344,9 @@ public boolean setOwner(Player player) { return false; } - Wolf dog = (Wolf) MyDog.instance().getServer().getEntity(dogId); + Wolf wolf = getWolf(); - if (dog == null || !dog.isValid()) { + if (wolf == null) { return false; } @@ -379,7 +360,7 @@ public boolean setOwner(Player player) { this.dogIdentifier = MyDog.getDogManager().generateNewId(player.getUniqueId()); MyDog.getDogManager().getDogsConfig().set(dogId.toString() + ".Owner", dogOwnerId); MyDog.getDogManager().getDogsConfig().set(dogId.toString() + ".ID", dogIdentifier); - dog.setOwner(player); + wolf.setOwner(player); } MyDog.getDogManager().saveTimed(); @@ -463,23 +444,18 @@ public String getDogCustomName() { public boolean setDogCustomName() { MyDog.instance().logDebug("Setting custom name... dogId: " + dogId); - if (MyDog.instance().getServer().getEntity(dogId) == null || !MyDog.instance().getServer().getEntity(dogId).isValid() || !(MyDog.instance().getServer().getEntity(dogId) instanceof Wolf)) { + Wolf wolf = getWolf(); + if (wolf == null) { MyDog.instance().logDebug("Retuning false!"); return false; } - Wolf dog = (Wolf) MyDog.instance().getServer().getEntity(dogId); - if (MyDog.getDogManager().getDogsConfig().contains(dogId.toString())) { String customName = getDogCustomName(); MyDog.instance().logDebug("Setting customName to: " + customName); - dog.setCustomName(customName); + wolf.setCustomName(customName); - if (MyDog.instance().onlyShowNametagOnHover) { - dog.setCustomNameVisible(false); - } else { - dog.setCustomNameVisible(true); - } + wolf.setCustomNameVisible(!MyDog.instance().onlyShowNametagOnHover); MyDog.instance().logDebug("Returning true!"); return true; } @@ -497,7 +473,7 @@ public int getIdentifier() { public boolean setIdentifier(int id) { // If the ID is already used, return false for (String dogIdString : MyDog.getDogManager().getDogsConfig().getKeys(false)) { - if (MyDog.getDogManager().getDogsConfig().getString(dogIdString + ".ID").equals(Integer.toString(id)) && MyDog.getDogManager().getDogsConfig().getString(dogIdString + ".Owner").contains(dogOwnerId.toString())) { + if (Objects.equals(MyDog.getDogManager().getDogsConfig().getString(dogIdString + ".ID"), Integer.toString(id)) && Objects.requireNonNull(MyDog.getDogManager().getDogsConfig().getString(dogIdString + ".Owner")).contains(dogOwnerId.toString())) { return false; } } @@ -508,7 +484,7 @@ public boolean setIdentifier(int id) { } public boolean setHealth() { - Wolf wolf = (Wolf) MyDog.instance().getServer().getEntity(dogId); + Wolf wolf = getWolf(); if (wolf == null) { MyDog.instance().logDebug("Failed to set Dog health, Wolf entity is null!"); @@ -533,12 +509,15 @@ public boolean setHealth() { } AttributeInstance wolfMaxHealth = wolf.getAttribute(Attribute.GENERIC_MAX_HEALTH); + if (wolfMaxHealth == null) { + return false; + } + MyDog.instance().logDebug("Dog Maxhealth Before: " + wolfMaxHealth.getValue()); // wolfMaxHealth.setBaseValue((wolfMaxHealth.getValue()/(0.5*(getLevel()-1)))*(0.5*getLevel())); wolfMaxHealth.setBaseValue(health); wolf.setHealth(wolfMaxHealth.getValue()); MyDog.instance().logDebug("Dog Maxhealth After: " + wolfMaxHealth.getValue()); - return true; } @@ -583,9 +562,9 @@ public boolean setHealth() { // } public boolean setDamage() { - Wolf wolf = (Wolf) MyDog.instance().getServer().getEntity(dogId); + Wolf wolf = getWolf(); - if (wolf == null || !wolf.isValid()) { + if (wolf == null) { MyDog.instance().logDebug("Failed to set Dog damage, Wolf entity is null or invalid!"); return false; } @@ -608,6 +587,9 @@ public boolean setDamage() { } AttributeInstance wolfDamage = wolf.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE); + if (wolfDamage == null) { + return false; + } MyDog.instance().logDebug("Dog Damage Before: " + wolfDamage.getValue()); wolfDamage.setBaseValue(damage); MyDog.instance().logDebug("Dog Damage After: " + wolfDamage.getValue()); diff --git a/src/main/java/dk/fido2603/mydog/objects/LevelFactory.java b/src/main/java/dk/fido2603/mydog/objects/LevelFactory.java index 7abd518..81e4cd6 100644 --- a/src/main/java/dk/fido2603/mydog/objects/LevelFactory.java +++ b/src/main/java/dk/fido2603/mydog/objects/LevelFactory.java @@ -3,7 +3,7 @@ import dk.fido2603.mydog.MyDog; public class LevelFactory { - private MyDog plugin = null; + private final MyDog plugin; public LevelFactory(MyDog plugin) { this.plugin = plugin; diff --git a/src/main/java/dk/fido2603/mydog/tasks/AttackModeTask.java b/src/main/java/dk/fido2603/mydog/tasks/AttackModeTask.java index 2dd085e..504a6e2 100644 --- a/src/main/java/dk/fido2603/mydog/tasks/AttackModeTask.java +++ b/src/main/java/dk/fido2603/mydog/tasks/AttackModeTask.java @@ -7,7 +7,7 @@ import java.util.List; public class AttackModeTask implements Runnable { - private MyDog plugin; + private final MyDog plugin; public AttackModeTask(MyDog instance) { this.plugin = instance; diff --git a/src/main/java/dk/fido2603/mydog/tasks/DistanceTask.java b/src/main/java/dk/fido2603/mydog/tasks/DistanceTask.java index 1443364..dd5dd0c 100644 --- a/src/main/java/dk/fido2603/mydog/tasks/DistanceTask.java +++ b/src/main/java/dk/fido2603/mydog/tasks/DistanceTask.java @@ -11,7 +11,7 @@ import java.util.List; public class DistanceTask implements Runnable { - private MyDog plugin; + private final MyDog plugin; public DistanceTask(MyDog instance) { this.plugin = instance; @@ -26,7 +26,7 @@ public void run() { for (Dog dog : MyDog.getDogManager().getAliveDogs((player.getUniqueId()))) { Wolf wolf = (Wolf) plugin.getServer().getEntity(dog.getDogId()); if (wolf != null && !wolf.isSitting()) { - double distance = 0.0; + double distance; // if they are in two seperate worlds, it's safe to say that the distance is above 30 lol if (!player.getWorld().getUID().equals(wolf.getWorld().getUID())) { distance = 1000; diff --git a/src/main/java/dk/fido2603/mydog/utils/ColorUtils.java b/src/main/java/dk/fido2603/mydog/utils/ColorUtils.java index 7249739..5ec4342 100644 --- a/src/main/java/dk/fido2603/mydog/utils/ColorUtils.java +++ b/src/main/java/dk/fido2603/mydog/utils/ColorUtils.java @@ -6,16 +6,15 @@ import org.bukkit.DyeColor; public class ColorUtils { - private static Random random = new Random(); + private static final Random random = new Random(); public static ChatColor getChatColorFromDyeColor(DyeColor dyeColor) { switch (dyeColor) { case BLACK: + case BROWN: return ChatColor.BLACK; case BLUE: return ChatColor.DARK_BLUE; - case BROWN: - return ChatColor.BLACK; case CYAN: return ChatColor.BLUE; case GRAY: @@ -41,7 +40,6 @@ public static ChatColor getChatColorFromDyeColor(DyeColor dyeColor) { case YELLOW: return ChatColor.YELLOW; case WHITE: - return ChatColor.WHITE; default: return ChatColor.WHITE; } diff --git a/src/main/java/dk/fido2603/mydog/utils/ParticleUtils.java b/src/main/java/dk/fido2603/mydog/utils/ParticleUtils.java index 90e956f..40c7cd8 100644 --- a/src/main/java/dk/fido2603/mydog/utils/ParticleUtils.java +++ b/src/main/java/dk/fido2603/mydog/utils/ParticleUtils.java @@ -9,7 +9,7 @@ import dk.fido2603.mydog.MyDog; public class ParticleUtils { - private MyDog plugin = null; + private final MyDog plugin; public ParticleUtils(MyDog p) { this.plugin = p; @@ -18,7 +18,7 @@ public ParticleUtils(MyDog p) { public void newLevelUpParticle(Entity entity) { double r = 0.8; new BukkitRunnable() { - Particle.DustOptions dustOptions = new Particle.DustOptions(Color.AQUA, 1); + final Particle.DustOptions dustOptions = new Particle.DustOptions(Color.AQUA, 1); double t = 0; public void run() { @@ -31,6 +31,9 @@ public void run() { double z = r * Math.sin(t); loc.add(x, y, z); + if (loc.getWorld() == null) { + this.cancel(); + } loc.getWorld().spawnParticle(Particle.REDSTONE, loc, 1, dustOptions); loc.subtract(x, y, z); @@ -41,7 +44,7 @@ public void run() { }.runTaskTimer(plugin, 0, 1); new BukkitRunnable() { - Particle.DustOptions dustOptions = new Particle.DustOptions(Color.BLACK, 1); + final Particle.DustOptions dustOptions = new Particle.DustOptions(Color.BLACK, 1); double t = 0; public void run() { @@ -54,6 +57,9 @@ public void run() { double z = r * Math.sin(-t); loc.add(x, y, z); + if (loc.getWorld() == null) { + this.cancel(); + } loc.getWorld().spawnParticle(Particle.REDSTONE, loc, 1, dustOptions); loc.subtract(x, y, z); @@ -78,6 +84,9 @@ public void run() { loc.add(x, y, z); + if (loc.getWorld() == null) { + this.cancel(); + } loc.getWorld().spawnParticle(Particle.FLAME, loc, 1); loc.subtract(x, y, z); @@ -105,6 +114,9 @@ public void run() { double z = r * Math.sin(t); loc.add(x, y, z); + if (loc.getWorld() == null) { + this.cancel(); + } loc.getWorld().spawnParticle(Particle.HEART, loc, 1); loc.subtract(x, y, z); diff --git a/src/main/java/dk/fido2603/mydog/utils/TimeUtils.java b/src/main/java/dk/fido2603/mydog/utils/TimeUtils.java index 3071017..615d8aa 100644 --- a/src/main/java/dk/fido2603/mydog/utils/TimeUtils.java +++ b/src/main/java/dk/fido2603/mydog/utils/TimeUtils.java @@ -59,4 +59,4 @@ public static String parseMillisToUFString(long millis) { // Send the return string return String.format("%d %s", minutes, m); } -} \ No newline at end of file +} diff --git a/src/main/java/dk/fido2603/mydog/utils/versioning/Tester.java b/src/main/java/dk/fido2603/mydog/utils/versioning/Tester.java index 6ac4096..de01bba 100644 --- a/src/main/java/dk/fido2603/mydog/utils/versioning/Tester.java +++ b/src/main/java/dk/fido2603/mydog/utils/versioning/Tester.java @@ -3,5 +3,5 @@ // From DogOnFire's Versioning in Werewolf // https://github.com/DogOnFire/Werewolf public interface Tester { - public boolean isEnabled(T t); + boolean isEnabled(T t); } \ No newline at end of file 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 659fbb0..30a79f3 100644 --- a/src/main/java/dk/fido2603/mydog/utils/versioning/TesterFactory.java +++ b/src/main/java/dk/fido2603/mydog/utils/versioning/TesterFactory.java @@ -9,33 +9,15 @@ public class TesterFactory { @SuppressWarnings("rawtypes") public static Tester getNewTester(Plugin plugin) { if (plugin == null) { - return new Tester() { - - @Override - public boolean isEnabled(Plugin t) { - return false; - } - }; + return (Tester) t -> false; } else { - return new Tester() { - - @Override - public boolean isEnabled(Plugin t) { - return t.isEnabled(); - } - }; + return (Tester) Plugin::isEnabled; } } @SuppressWarnings("rawtypes") public static Tester getDefaultTester() { - return new Tester() { - - @Override - public boolean isEnabled(Object t) { - return true; - } - }; + return t -> true; } } \ No newline at end of file diff --git a/src/main/java/dk/fido2603/mydog/utils/versioning/Version.java b/src/main/java/dk/fido2603/mydog/utils/versioning/Version.java index 161c254..ddc1bf9 100644 --- a/src/main/java/dk/fido2603/mydog/utils/versioning/Version.java +++ b/src/main/java/dk/fido2603/mydog/utils/versioning/Version.java @@ -16,8 +16,8 @@ public class Version implements Comparable { * fail. */ @SuppressWarnings("rawtypes") - private Tester tester; - private Object object; + private final Tester tester; + private final Object object; final String version; private String separator = "[_.-]"; @@ -66,10 +66,7 @@ public boolean isCompatible(String minVersion) { if (!this.isEnabled()) return false; int x = compareTo(new Version(minVersion)); - if (x >= 0) { - return true; - } - return false; + return x >= 0; } /** @@ -89,18 +86,14 @@ public boolean isSupported(String maxVersion) { public int compareTo(Version whichVersion) { int[] currentVersion = parseVersion(this.version); int[] otherVersion = parseVersion(whichVersion.toString()); - int length = (currentVersion.length >= otherVersion.length) ? currentVersion.length : otherVersion.length; + int length = Math.max(currentVersion.length, otherVersion.length); for (int index = 0; index <= (length - 1); index = index + 1) { try { if (currentVersion[index] != otherVersion[index]) { return currentVersion[index] - otherVersion[index]; } } catch (IndexOutOfBoundsException ex) { - if (currentVersion.length > otherVersion.length) { - return currentVersion[index] - 0; - } else if (currentVersion.length < otherVersion.length) { - return 0 - otherVersion[index]; - } + return currentVersion[index]; } } return 0; @@ -118,7 +111,7 @@ private int[] parseVersion(String version) { for (int index = 0; index <= (stringArray.length - 1); index = index + 1) { String t = stringArray[index].replaceAll("\\D", ""); try { - temp[index] = Integer.valueOf(t); + temp[index] = Integer.parseInt(t); } catch (NumberFormatException ex) { temp[index] = 0; } @@ -146,7 +139,7 @@ public boolean search(String regex) { * Used to get a Sub-Version (or Development build).
*
* - * @param regex + * @param regex the regex * @return A completely new Version object. */ public Version getSubVersion(String regex) { @@ -163,7 +156,6 @@ public Version getSubVersion(String regex) { @Override public String toString() { - String v = (this.version == null) ? "" : this.version; - return v; + return (this.version == null) ? "" : this.version; } } \ No newline at end of file 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 65a0eee..df475bc 100644 --- a/src/main/java/dk/fido2603/mydog/utils/versioning/VersionFactory.java +++ b/src/main/java/dk/fido2603/mydog/utils/versioning/VersionFactory.java @@ -42,7 +42,7 @@ public static Version getServerVersion() { * net.minecraft.server.v1_X_RY package.
*/ public static Version getNmsVersion() { - String NMS = null; + String NMS; try { NMS = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; } catch (ArrayIndexOutOfBoundsException ex) {