diff --git a/src/main/java/dk/fido2603/mydog/MyDog.java b/src/main/java/dk/fido2603/mydog/MyDog.java index 6460662..5cc40bc 100644 --- a/src/main/java/dk/fido2603/mydog/MyDog.java +++ b/src/main/java/dk/fido2603/mydog/MyDog.java @@ -69,6 +69,8 @@ public class MyDog extends JavaPlugin { public String deadDogLevelString = ", and got to &4Level {level}&c"; public String commandComehereString = "&6&l[{chatPrefix}] &r&6Come here! Good doggo, {dogNameColor}{dogName}&6!"; public String tameLimitString = "&c&l[{chatPrefix}] &r&cTaming failed! Looks like you have reached your limit of dogs! You can maybe set some dead dogs free, or revive some?"; + public String pettingString = "&6Who's a good doggo?! {dogNameColor}{dogName}&6 is!"; + public String pettingSplashString = "{dogNameColor}{dogName}&6 splashes water all over you!"; private static MyDog plugin; private static FileConfiguration config = null; diff --git a/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener.java b/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener.java index 32d1417..724d040 100644 --- a/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener.java +++ b/src/main/java/dk/fido2603/mydog/listeners/WolfMainListener.java @@ -141,8 +141,8 @@ public void onWolfPlayerInteract(PlayerInteractEntityEvent event) { return; } - if (onWolfPlayerInteractToggleMode(dog, item, player) == 2) { - plugin.logDebug("ToggleMode event: Cancelled"); + if (onWolfPlayerInteractPet(dog, item, player) == 2) { + plugin.logDebug("Petting event: Cancelled"); event.setCancelled(true); return; } @@ -240,8 +240,8 @@ public int onWolfPlayerInteractFeed(Dog dog, Wolf wolf, ItemStack item, Player p healthPoints = 2.0; break; default: - if (player.isSneaking() && dog.getOwnerId().equals(player.getUniqueId())) { - dog.toggleMode(); + if (player.isSneaking()) { + dog.pet(player); return 2; // Cancel event } break; @@ -364,14 +364,14 @@ public int onWolfPlayerInteractChangeColor(Dog dog, Wolf wolf, ItemStack item, P return 0; // OK } - public int onWolfPlayerInteractToggleMode(Dog dog, ItemStack item, Player player) { + public int onWolfPlayerInteractPet(Dog dog, ItemStack item, Player player) { if (dog == null || item == null || player == null) { return 0; // OK } if (item.getType().equals(Material.AIR)) { - if (player.isSneaking() && dog.getOwnerId().equals(player.getUniqueId())) { - dog.toggleMode(); + if (player.isSneaking()) { + dog.pet(player); return 2; // Cancel event } } diff --git a/src/main/java/dk/fido2603/mydog/managers/CommandManager.java b/src/main/java/dk/fido2603/mydog/managers/CommandManager.java index 2da473a..796be74 100644 --- a/src/main/java/dk/fido2603/mydog/managers/CommandManager.java +++ b/src/main/java/dk/fido2603/mydog/managers/CommandManager.java @@ -174,7 +174,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String } } - return commandDogSit(sender, dogIdentifier); + return commandDogStandSit(sender, dogIdentifier, true); } if ((args[0].equalsIgnoreCase("stand"))) { if ((!player.isOp()) && (!MyDog.getPermissionsManager().hasPermission(player, "mydog.sit"))) { @@ -194,7 +194,47 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String } } - return commandDogStand(sender, dogIdentifier); + return commandDogStandSit(sender, dogIdentifier, false); + } + if ((args[0].equalsIgnoreCase("attack"))) { + if ((!player.isOp()) && (!MyDog.getPermissionsManager().hasPermission(player, "mydog.togglemode"))) { + return false; + } + + int dogIdentifier; + if (args[1].equalsIgnoreCase("all")) { + dogIdentifier = -1; + } + else { + try { + dogIdentifier = Integer.parseInt(args[1]); + } catch (NumberFormatException e) { + sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.RED + "Please enter a valid ID! Check /mydog dogs"); + return true; + } + } + + return commandDogDefendAttack(sender, dogIdentifier, true); + } + if ((args[0].equalsIgnoreCase("defend"))) { + if ((!player.isOp()) && (!MyDog.getPermissionsManager().hasPermission(player, "mydog.togglemode"))) { + return false; + } + + int dogIdentifier; + if (args[1].equalsIgnoreCase("all")) { + dogIdentifier = -1; + } + else { + try { + dogIdentifier = Integer.parseInt(args[1]); + } catch (NumberFormatException e) { + sender.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.RED + "Please enter a valid ID! Check /mydog dogs"); + return true; + } + } + + return commandDogDefendAttack(sender, dogIdentifier, false); } if ((args[0].equalsIgnoreCase("revive"))) { if (!plugin.allowRevival || ((!player.isOp()) && (!MyDog.getPermissionsManager().hasPermission(player, "mydog.revive")))) { @@ -305,6 +345,10 @@ private boolean commandList(CommandSender sender) { sender.sendMessage(ChatColor.AQUA + "/mydog sit " + ChatColor.WHITE + " - Tells your dog(s) to sit and keep their position(s)"); sender.sendMessage(ChatColor.AQUA + "/mydog stand " + ChatColor.WHITE + " - Tells your dog(s) to stand up and roam free"); } + if ((sender.isOp()) || (MyDog.getPermissionsManager().hasPermission(player, "mydog.togglemode"))) { + sender.sendMessage(ChatColor.AQUA + "/mydog attack " + ChatColor.WHITE + " - Tells your dog(s) to attack any mobs nearby"); + sender.sendMessage(ChatColor.AQUA + "/mydog defend " + ChatColor.WHITE + " - Tells your dog(s) to defend you"); + } if ((sender.isOp()) || (MyDog.getPermissionsManager().hasPermission(player, "mydog.stats"))) { sender.sendMessage(ChatColor.AQUA + "/mydog info " + ChatColor.WHITE + " - Gets stats and other info about a Dog you own"); } @@ -486,17 +530,22 @@ private boolean commandDogFree(CommandSender sender, int dogIdentifier) { return true; } - private boolean commandDogStand(CommandSender sender, int dogIdentifier) { + private boolean commandDogDefendAttack(CommandSender sender, int dogIdentifier, boolean toAttack) { List dogs = new ArrayList<>(); + String mode = "Defend me"; + if (toAttack) { + mode = "Attack"; + } + if (dogIdentifier == -1) { dogs = MyDog.getDogManager().getAliveDogs(((Player) sender).getUniqueId()); - sender.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.AQUA + "Stand, my dogs!"); + sender.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.AQUA + mode + ", my dogs!"); } else { dogs.add(MyDog.getDogManager().getDog(dogIdentifier, ((Player) sender).getUniqueId())); if (dogs.get(0) != null) { - sender.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.AQUA + "Stand, " + dogs.get(0).getDogColor() + dogs.get(0).getDogName() + ChatColor.RESET + ChatColor.AQUA + "!"); + sender.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.AQUA + mode + ", " + dogs.get(0).getDogColor() + dogs.get(0).getDogName() + ChatColor.RESET + ChatColor.AQUA + "!"); } } @@ -506,23 +555,28 @@ private boolean commandDogStand(CommandSender sender, int dogIdentifier) { return false; } - dog.sit(false); + dog.setAngryMode(toAttack); } return true; } - private boolean commandDogSit(CommandSender sender, int dogIdentifier) { + private boolean commandDogStandSit(CommandSender sender, int dogIdentifier, boolean toSit) { List dogs = new ArrayList<>(); + String mode = "Stand"; + if (toSit) { + mode = "Sit"; + } + if (dogIdentifier == -1) { dogs = MyDog.getDogManager().getAliveDogs(((Player) sender).getUniqueId()); - sender.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.AQUA + "Sit, my dogs!"); + sender.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.AQUA + mode + ", my dogs!"); } else { dogs.add(MyDog.getDogManager().getDog(dogIdentifier, ((Player) sender).getUniqueId())); if (dogs.get(0) != null) { - sender.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.AQUA + "Sit, " + dogs.get(0).getDogColor() + dogs.get(0).getDogName() + ChatColor.RESET + ChatColor.AQUA + "!"); + sender.sendMessage(ChatColor.AQUA + "" + ChatColor.BOLD + "[" + plugin.getChatPrefix() + "] " + ChatColor.RESET + ChatColor.AQUA + mode + ", " + dogs.get(0).getDogColor() + dogs.get(0).getDogName() + ChatColor.RESET + ChatColor.AQUA + "!"); } } @@ -532,7 +586,7 @@ private boolean commandDogSit(CommandSender sender, int dogIdentifier) { return false; } - dog.sit(true); + dog.sit(toSit); } return true; @@ -834,6 +888,10 @@ public List onTabComplete(CommandSender sender, Command cmd, String alia arg1.add("sit"); arg1.add("stand"); } + if (player == null || (player.isOp() || MyDog.getPermissionsManager().hasPermission(player, "mydog.togglemode"))) { + arg1.add("attack"); + arg1.add("defend"); + } if (plugin.allowRevival && (player == null || (player.isOp() || MyDog.getPermissionsManager().hasPermission(player, "mydog.dead")))) { arg1.add("dead"); } @@ -869,7 +927,7 @@ public List onTabComplete(CommandSender sender, Command cmd, String alia } } - if (player != null && (args[0].equalsIgnoreCase("sit") || args[0].equalsIgnoreCase("stand") || args[0].equalsIgnoreCase("comehere"))) { + if (player != null && (args[0].equalsIgnoreCase("sit") || args[0].equalsIgnoreCase("stand") || args[0].equalsIgnoreCase("comehere") || args[0].equalsIgnoreCase("attack") || args[0].equalsIgnoreCase("defend"))) { arg2.add("all"); List dogs = MyDog.getDogManager().getAliveDogs(player.getUniqueId()); diff --git a/src/main/java/dk/fido2603/mydog/objects/Dog.java b/src/main/java/dk/fido2603/mydog/objects/Dog.java index f251e75..9f9e79e 100644 --- a/src/main/java/dk/fido2603/mydog/objects/Dog.java +++ b/src/main/java/dk/fido2603/mydog/objects/Dog.java @@ -2,9 +2,7 @@ import dk.fido2603.mydog.MyDog; import dk.fido2603.mydog.utils.ColorUtils; -import org.bukkit.ChatColor; -import org.bukkit.DyeColor; -import org.bukkit.Location; +import org.bukkit.*; import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; import org.bukkit.entity.Player; @@ -13,13 +11,12 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Map; -import java.util.UUID; +import java.util.*; public class Dog { private static final String ANGRY_MODE = ChatColor.GRAY + "[" + ChatColor.RED + "⚔" + ChatColor.GRAY + "]"; private static final String DEFENCE_MODE = ChatColor.GRAY + "[" + ChatColor.GREEN + "⛨" + ChatColor.GRAY + "]"; + private static final List PETTING_SOUNDS = Arrays.asList(Sound.ENTITY_WOLF_PANT, Sound.ENTITY_WOLF_AMBIENT); private UUID dogId; private UUID dogOwnerId; @@ -208,35 +205,54 @@ public boolean setDogColor(DyeColor color) { this.dogName = getDogName(); } - - if (MyDog.instance().useLevels && MyDog.instance().showLevelsInNametag) { - String dogNamePlate = nameColor + dogName + ChatColor.GRAY + " [" + ChatColor.GOLD + "" + this.level + ChatColor.GRAY + "]" + (isAngry() ? ANGRY_MODE : DEFENCE_MODE); - MyDog.instance().logDebug("Setting customName to: " + dogNamePlate); - dog.setCustomName(dogNamePlate); - } else { - MyDog.instance().logDebug("Setting customName to: " + nameColor + dogName); - dog.setCustomName(nameColor + dogName); - } - - if (MyDog.instance().onlyShowNametagOnHover) { - dog.setCustomNameVisible(false); - } else { - dog.setCustomNameVisible(true); - } + setDogCustomName(); } MyDog.getDogManager().saveTimed(); return true; } + public void pet(Player player) { + if (player == null || (!player.isOp() && !MyDog.getPermissionsManager().hasPermission(player, "mydog.pet"))) { + return; + } + + if (!player.getUniqueId().equals(dogOwnerId) && !MyDog.getPermissionsManager().hasPermission(player, "mydog.pet.others")) { + return; + } + + Wolf dog = (Wolf) MyDog.instance().getServer().getEntity(dogId); + if (dog == null) { + return; + } + + 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) { + dog.playEffect(EntityEffect.WOLF_SHAKE); + pettingString = MyDog.instance().pettingSplashString.replace("{dogNameColor}", "&" + getDogColor().getChar()).replace("{dogName}", getDogName()); + player.sendMessage(ChatColor.translateAlternateColorCodes('&', pettingString)); + } + MyDog.getParticleUtils().newPettingParticle(dog); + Sound sound = PETTING_SOUNDS.get(rand.nextInt(PETTING_SOUNDS.size())); + player.playSound(player.getLocation(), sound, 3.0F, 1.0F); + } + public void toggleMode() { + setAngryMode(!isAngry()); + } + + 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(!isAngry()); - dog.setCustomName(nameColor + dogName + ChatColor.GRAY + " [" + ChatColor.GOLD + "" + this.level + ChatColor.GRAY + "]" + (isAngry() ? ANGRY_MODE : DEFENCE_MODE)); - dog.setCustomNameVisible(!MyDog.instance().onlyShowNametagOnHover); + this.setIsAngry(angry); + setDogCustomName(); MyDog.getDogManager().saveTimed(); } @@ -280,29 +296,7 @@ public boolean setDogName(String name) { this.dogName = name; MyDog.getDogManager().getDogsConfig().set(dogId.toString() + ".Name", dogName); - Wolf dog = (Wolf) MyDog.instance().getServer().getEntity(dogId); - - if (dog == null) { - MyDog.instance().logDebug("Dog is null"); - return false; - } - - if (dogName.isEmpty()) { - this.dogName = getDogName(); - } - - if (MyDog.instance().useLevels && MyDog.instance().showLevelsInNametag) { - MyDog.instance().logDebug("Setting customName to: " + nameColor + dogName + ChatColor.GRAY + " [" + ChatColor.GOLD + "" + this.level + ChatColor.GRAY + "]" + (isAngry() ? ANGRY_MODE : DEFENCE_MODE)); - dog.setCustomName(nameColor + dogName + ChatColor.GRAY + " [" + ChatColor.GOLD + "" + this.level + ChatColor.GRAY + "]" + (isAngry() ? ANGRY_MODE : DEFENCE_MODE)); - } else { - MyDog.instance().logDebug("Setting customName to: " + nameColor + dogName); - dog.setCustomName(nameColor + dogName); - } - if (MyDog.instance().onlyShowNametagOnHover) { - dog.setCustomNameVisible(false); - } else { - dog.setCustomNameVisible(true); - } + setDogCustomName(); } MyDog.getDogManager().saveTimed(); return true; @@ -416,6 +410,18 @@ public void setExperience(int exp) { MyDog.getDogManager().saveTimed(); } + public String getDogCustomName() { + if (MyDog.getDogManager().getDogsConfig().contains(dogId.toString())) { + if (MyDog.instance().useLevels && MyDog.instance().showLevelsInNametag) { + return (nameColor + dogName + ChatColor.GRAY + " [" + ChatColor.GOLD + "" + this.level + ChatColor.GRAY + "]" + (isAngry() ? ANGRY_MODE : DEFENCE_MODE)); + } + else { + return nameColor + dogName; + } + } + return null; + } + 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)) { @@ -426,13 +432,10 @@ public boolean setDogCustomName() { Wolf dog = (Wolf) MyDog.instance().getServer().getEntity(dogId); if (MyDog.getDogManager().getDogsConfig().contains(dogId.toString())) { - if (MyDog.instance().useLevels && MyDog.instance().showLevelsInNametag) { - MyDog.instance().logDebug("Setting customName to: " + nameColor + dogName + ChatColor.GRAY + " [" + ChatColor.GOLD + "" + this.level + ChatColor.GRAY + "]" + (isAngry() ? ANGRY_MODE : DEFENCE_MODE)); - dog.setCustomName(nameColor + dogName + ChatColor.GRAY + " [" + ChatColor.GOLD + "" + this.level + ChatColor.GRAY + "]" + (isAngry() ? ANGRY_MODE : DEFENCE_MODE)); - } else { - MyDog.instance().logDebug("Setting customName to: " + nameColor + dogName); - dog.setCustomName(nameColor + dogName); - } + String customName = getDogCustomName(); + MyDog.instance().logDebug("Setting customName to: " + customName); + dog.setCustomName(customName); + if (MyDog.instance().onlyShowNametagOnHover) { dog.setCustomNameVisible(false); } else { diff --git a/src/main/java/dk/fido2603/mydog/utils/ParticleUtils.java b/src/main/java/dk/fido2603/mydog/utils/ParticleUtils.java index 9ee15b4..90e956f 100644 --- a/src/main/java/dk/fido2603/mydog/utils/ParticleUtils.java +++ b/src/main/java/dk/fido2603/mydog/utils/ParticleUtils.java @@ -89,6 +89,32 @@ public void run() { }.runTaskTimer(plugin, 0, 1); } + public void newPettingParticle(Entity entity) { + double r = 0.8; + + new BukkitRunnable() { + double t = 0; + + public void run() { + Location loc = entity.getLocation(); + loc.setY(loc.getY() + 0.5); + t = t + Math.PI / 4; + double x = r * Math.cos(t); + //double y = 0.05*t; + double y = Math.random() * r; + double z = r * Math.sin(t); + + loc.add(x, y, z); + loc.getWorld().spawnParticle(Particle.HEART, loc, 1); + loc.subtract(x, y, z); + + if (t > Math.PI * 4) { + this.cancel(); + } + } + }.runTaskTimer(plugin, 0, 1); + } + /*public void newLevelUpParticle2(Location loc) { new BukkitRunnable() diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 08ec28d..fde99c0 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -38,6 +38,9 @@ permissions: mydog.dead: true mydog.revive: true mydog.sit: true + mydog.togglemode: true + mydog.pet: true + mydog.pet.others: true mydog.limit.*: description: Give limits to the amount of dogs a player can tame. default: false @@ -68,6 +71,15 @@ permissions: mydog.setid: description: Player can set a custom ID to their Dogs default: true + mydog.togglemode: + description: Player can toggle dogs between attack and defend mode + default: true + mydog.pet: + description: Player can pet their dogs + default: true + mydog.pet.others: + description: Player can pet others dogs + default: true mydog.editlevel: description: Player can set the level of their dogs default: false