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

Less code smell #96

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
5 changes: 3 additions & 2 deletions src/main/java/dk/fido2603/mydog/MyDog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Integer, Level> dogLevels = new HashMap<>();
public final Map<Integer, Level> dogLevels = new HashMap<>();

private static Economy economy = null;
private CommandManager commands = null;
Expand Down Expand Up @@ -190,6 +190,7 @@ private boolean setupEconomy() {
return true;
}

@Override
public void onDisable() {
saveSettings();
reloadSettings();
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/dk/fido2603/mydog/MyDogAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
31 changes: 16 additions & 15 deletions src/main/java/dk/fido2603/mydog/managers/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ private boolean commandDogList(CommandSender sender) {

sender.sendMessage(ChatColor.YELLOW + "---------------- " + this.plugin.getDescription().getFullName() + " ----------------");
for (Map.Entry<Integer, Dog> 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();
Expand All @@ -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;
}
Expand All @@ -512,7 +512,7 @@ private boolean commandDogDead(CommandSender sender) {

sender.sendMessage(ChatColor.YELLOW + "---------------- " + this.plugin.getDescription().getFullName() + " ----------------");
for (Map.Entry<Integer, Dog> 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;
}
Expand All @@ -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());
Expand All @@ -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;
}
Expand Down Expand Up @@ -723,8 +723,9 @@ private boolean commandDogStats(CommandSender sender, int dogIdentifier) {

Map<Integer, Level> levels = plugin.dogLevels;

for (Integer levelInt : levels.keySet()) {
int levelExp = levels.get(levelInt).exp;
for (Map.Entry<Integer, Level> 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) {
Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -1084,15 +1085,15 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String alia
return Collections.emptyList();
}

List<String> result = new ArrayList<String>();
List<String> result = new ArrayList<>();

Player player = null;
if (sender instanceof Player) {
player = (Player) sender;
}

if (args.length == 1 && (cmd.getName().equalsIgnoreCase("mydog") || cmd.getName().equalsIgnoreCase("dog") || cmd.getName().equalsIgnoreCase("dogs") || cmd.getName().equalsIgnoreCase("md"))) {
List<String> arg1 = new ArrayList<String>();
List<String> arg1 = new ArrayList<>();
if (player == null || (player.isOp() || MyDog.getPermissionsManager().hasPermission(player, "mydog.help"))) {
arg1.add("help");
}
Expand Down Expand Up @@ -1171,7 +1172,7 @@ public List<String> 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<Dog> dogs = MyDog.getDogManager().getDeadDogs(player.getUniqueId());
for (Dog dog : dogs) {
arg2.add(Integer.toString(dog.getIdentifier()));
Expand All @@ -1190,7 +1191,7 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String alia
Iterable<String> SECOND_ARGUMENTS = arg2;
StringUtil.copyPartialMatches(args[1], SECOND_ARGUMENTS, result);
} else if (args.length == 3) {
List<String> arg3 = new ArrayList<String>();
List<String> arg3 = new ArrayList<>();

if (args[0].equalsIgnoreCase("rename")) {
arg3.add("<name>");
Expand All @@ -1211,7 +1212,7 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String alia
Iterable<String> THIRD_ARGUMENTS = arg3;
StringUtil.copyPartialMatches(args[2], THIRD_ARGUMENTS, result);
} else if (args.length == 4) {
List<String> arg4 = new ArrayList<String>();
List<String> arg4 = new ArrayList<>();

if (args[0].equalsIgnoreCase("trade")) {
arg4.add("[price]");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dk/fido2603/mydog/managers/DogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,15 @@ public boolean hasTrade(UUID recipient) {

public HashMap<String, Object> getTrade(UUID recipient) {
if (!hasTrade(recipient)) {
return null;
return new HashMap<>();
}

return dogTrades.get(recipient);
}

public Dog getTradeDog(UUID recipient) {
HashMap<String, Object> trade = getTrade(recipient);
if (trade == null) {
if (trade == null || trade.isEmpty()) {
return null;
}

Expand All @@ -285,7 +285,7 @@ public Double getTradePrice(UUID recipient) {
public boolean acceptTrade(Player accepter) {
HashMap<String, Object> dogTrade = getTrade(accepter.getUniqueId());

if (dogTrade == null) {
if (dogTrade == null || dogTrade.isEmpty()) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ private HashMap<Boolean, Location> 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();
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/dk/fido2603/mydog/objects/Dog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -406,8 +403,9 @@ public void setExperience(int exp) {
int newLevel = 1;
Map<Integer, LevelFactory.Level> levels = MyDog.instance().dogLevels;

for (Integer levelInt : levels.keySet()) {
int levelExp = levels.get(levelInt).exp;
for (Map.Entry<Integer, LevelFactory.Level> 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
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dk/fido2603/mydog/utils/ColorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dk/fido2603/mydog/utils/TimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br/>
Expand Down