Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code smell improvements #95

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions src/main/java/dk/fido2603/mydog/MyDog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Integer, Level> dogLevels = new HashMap<Integer, Level>();
public Map<Integer, Level> dogLevels = new HashMap<>();

private static Economy economy = null;
private CommandManager commands = null;
Expand Down Expand Up @@ -185,7 +187,7 @@ private boolean setupEconomy() {
return false;
}
economy = rsp.getProvider();
return economy != null;
return true;
}

public void onDisable() {
Expand All @@ -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;
Expand All @@ -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<Permission> permissionProvider = plugin.getServer().getServicesManager().getRegistration(Permission.class);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/dk/fido2603/mydog/MyDogAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 5 additions & 9 deletions src/main/java/dk/fido2603/mydog/listeners/DamageListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}
}
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 12 additions & 15 deletions src/main/java/dk/fido2603/mydog/listeners/WolfMainListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 23 additions & 7 deletions src/main/java/dk/fido2603/mydog/managers/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) + ")";
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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()));
}
Expand Down Expand Up @@ -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) {
Expand Down
Loading