Skip to content

Commit

Permalink
Merge pull request #87 from HeroGamers/pet-dogs-32
Browse files Browse the repository at this point in the history
Add petting dogs
  • Loading branch information
HeroGamers authored May 28, 2023
2 parents 7dc2b91 + 77c3a0b commit dbb5855
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 72 deletions.
2 changes: 2 additions & 0 deletions src/main/java/dk/fido2603/mydog/MyDog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/dk/fido2603/mydog/listeners/WolfMainListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
}
Expand Down
80 changes: 69 additions & 11 deletions src/main/java/dk/fido2603/mydog/managers/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"))) {
Expand All @@ -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")))) {
Expand Down Expand Up @@ -305,6 +345,10 @@ private boolean commandList(CommandSender sender) {
sender.sendMessage(ChatColor.AQUA + "/mydog sit <id | all>" + ChatColor.WHITE + " - Tells your dog(s) to sit and keep their position(s)");
sender.sendMessage(ChatColor.AQUA + "/mydog stand <id | all>" + 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 <id | all>" + ChatColor.WHITE + " - Tells your dog(s) to attack any mobs nearby");
sender.sendMessage(ChatColor.AQUA + "/mydog defend <id | all>" + ChatColor.WHITE + " - Tells your dog(s) to defend you");
}
if ((sender.isOp()) || (MyDog.getPermissionsManager().hasPermission(player, "mydog.stats"))) {
sender.sendMessage(ChatColor.AQUA + "/mydog info <id>" + ChatColor.WHITE + " - Gets stats and other info about a Dog you own");
}
Expand Down Expand Up @@ -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<Dog> 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 + "!");
}
}

Expand All @@ -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<Dog> 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 + "!");
}
}

Expand All @@ -532,7 +586,7 @@ private boolean commandDogSit(CommandSender sender, int dogIdentifier) {
return false;
}

dog.sit(true);
dog.sit(toSit);
}

return true;
Expand Down Expand Up @@ -834,6 +888,10 @@ public List<String> 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");
}
Expand Down Expand Up @@ -869,7 +927,7 @@ public List<String> 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<Dog> dogs = MyDog.getDogManager().getAliveDogs(player.getUniqueId());
Expand Down
111 changes: 57 additions & 54 deletions src/main/java/dk/fido2603/mydog/objects/Dog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Sound> PETTING_SOUNDS = Arrays.asList(Sound.ENTITY_WOLF_PANT, Sound.ENTITY_WOLF_AMBIENT);

private UUID dogId;
private UUID dogOwnerId;
Expand Down Expand Up @@ -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();
}

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

0 comments on commit dbb5855

Please sign in to comment.