Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
shantek committed Sep 8, 2024
1 parent 56c7b66 commit 7378d17
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/main/java/io/shantek/HorseGuard.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class HorseGuard extends JavaPlugin {
@Override
public void onEnable() {
this.configuration = new Configuration(this);
reloadHorseGuardConfig();

Objects.requireNonNull(getCommand("horse")).setExecutor(new HorseCommand(this));
Objects.requireNonNull(getCommand("horse")).setTabCompleter(new HorseTabCompleter(this));
Expand All @@ -32,6 +33,7 @@ public void onEnable() {

int pluginId = 23218;
Metrics metrics = new Metrics(this, pluginId);

}

@Override
Expand Down
39 changes: 13 additions & 26 deletions src/main/java/io/shantek/Listeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

public class Listeners implements Listener {

private final HorseGuard plugin; // Reference to the main plugin
private final HelperFunctions helperFunctions;

public Listeners(HorseGuard horseGuard) {
this.plugin = horseGuard;
this.helperFunctions = new HelperFunctions(horseGuard);
}

Expand All @@ -28,24 +30,10 @@ public void onEntityTame(EntityTameEvent event) {
UUID horseUUID = horse.getUniqueId();
UUID playerUUID = player.getUniqueId();

// If the horse isn't in the config, check if it has an owner
if (helperFunctions.getHorseOwner(horseUUID) == null && horse.isTamed() && horse.getOwner() != null) {
// Register the horse and owner in the config
Player horseOwner = (Player) horse.getOwner();
UUID ownerUUID = horseOwner.getUniqueId();
helperFunctions.setHorseOwner(horseUUID, ownerUUID);
player.sendMessage("This horse is owned by " + horseOwner.getName() + " and has now been registered.");

// If the player is not the owner and lacks bypass permissions, deny taming
if (!player.hasPermission("shantek.horseguard.ride")) {
event.setCancelled(true);
player.sendMessage("You cannot tame this horse as it is owned by " + horseOwner.getName() + ".");
return;
}
}

// Register the horse and player as the owner immediately
helperFunctions.setHorseOwner(horseUUID, playerUUID);
// If the horse is not owned, proceed with taming
player.sendMessage("You are now the owner of this horse.");
player.sendMessage(plugin.getMessagePrefix() + "You now own this horse. Need to trust another player? Type '/horse trust player' while sitting on the horse.");
}
}

Expand All @@ -63,25 +51,25 @@ public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (horse.getOwner() instanceof Player horseOwner) {
ownerUUID = horseOwner.getUniqueId();
helperFunctions.setHorseOwner(horseUUID, ownerUUID);
player.sendMessage("This horse is owned by " + horseOwner.getName() + " and has now been registered.");
player.sendMessage(plugin.getMessagePrefix() + "This horse is owned by " + horseOwner.getName() + " and has now been registered.");

// If the player is not the owner and lacks bypass permissions, deny interaction
if (!ownerUUID.equals(player.getUniqueId()) && !player.hasPermission("shantek.horseguard.ride")) {
event.setCancelled(true);
player.sendMessage("You cannot interact with this horse as it is owned by " + horseOwner.getName() + ".");
player.sendMessage(plugin.getMessagePrefix() + "You cannot interact with this horse as it is owned by " + horseOwner.getName() + ".");
return;
}
} else {
// Handle if the owner is not online (OfflinePlayer)
OfflinePlayer offlineOwner = (OfflinePlayer) horse.getOwner();
ownerUUID = offlineOwner.getUniqueId();
helperFunctions.setHorseOwner(horseUUID, ownerUUID);
player.sendMessage("This horse is owned by " + getOwnerName(ownerUUID) + " and has now been registered.");
player.sendMessage(plugin.getMessagePrefix() + "This horse is owned by " + getOwnerName(ownerUUID) + " and has now been registered.");

// If the player is not the owner and lacks bypass permissions, deny interaction
if (!ownerUUID.equals(player.getUniqueId()) && !player.hasPermission("shantek.horseguard.ride")) {
event.setCancelled(true);
player.sendMessage("You cannot interact with this horse as it is owned by " + getOwnerName(ownerUUID) + ".");
player.sendMessage(plugin.getMessagePrefix() + "You cannot interact with this horse as it is owned by " + getOwnerName(ownerUUID) + ".");
return;
}
}
Expand All @@ -100,7 +88,7 @@ public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
if (ownerUUID != null && !helperFunctions.isOwner(player, horse) && !helperFunctions.isPlayerTrusted(horseUUID, player.getUniqueId())) {
event.setCancelled(true);
String ownerName = getOwnerName(ownerUUID);
player.sendMessage("This horse is owned by " + ownerName + ". You cannot interact with it.");
player.sendMessage(plugin.getMessagePrefix() + "This horse is owned by " + ownerName + ". You cannot interact with it.");
}
}
}
Expand Down Expand Up @@ -128,12 +116,12 @@ public void onEntityDamage(EntityDamageByEntityEvent event) {
Player horseOwner = (Player) horse.getOwner();
ownerUUID = horseOwner.getUniqueId();
helperFunctions.setHorseOwner(horseUUID, ownerUUID);
damager.sendMessage("This horse is owned by " + horseOwner.getName() + " and has now been registered.");
damager.sendMessage(plugin.getMessagePrefix() + "This horse is owned by " + horseOwner.getName() + " and has now been registered.");

// If the player is not the owner and lacks bypass permissions, deny damage
if (!damager.hasPermission("shantek.horseguard.damage")) {
event.setCancelled(true);
damager.sendMessage("You cannot damage this horse as it is owned by " + horseOwner.getName() + ".");
damager.sendMessage(plugin.getMessagePrefix() + "You cannot damage this horse as it is owned by " + horseOwner.getName() + ".");
return;
}
}
Expand All @@ -147,13 +135,12 @@ public void onEntityDamage(EntityDamageByEntityEvent event) {
if (!ownerUUID.equals(damager.getUniqueId())) {
event.setCancelled(true);
String ownerName = getOwnerName(ownerUUID);
damager.sendMessage("This horse is owned by " + ownerName + ". You cannot damage it.");
damager.sendMessage(plugin.getMessagePrefix() + "This horse is owned by " + ownerName + ". You cannot damage it.");
}
} else {
// If the damager isn't a player, cancel the damage
event.setCancelled(true);
}
}
}

}

0 comments on commit 7378d17

Please sign in to comment.