fancynpcs (player-npcs): Refactor NPC plot ownership checks

This commit is contained in:
Oliver
2025-09-02 19:29:03 +02:00
parent c3062652f5
commit 26e91119e2
3 changed files with 62 additions and 26 deletions

View File

@@ -1 +1 @@
2.7.1.292 2.7.1.293

View File

@@ -44,8 +44,9 @@ allprojects {
maven(url = "https://repo.papermc.io/repository/maven-public/") maven(url = "https://repo.papermc.io/repository/maven-public/")
maven(url = "https://repo.fancyinnovations.com/releases") maven(url = "https://repo.fancyinnovations.com/releases")
maven(url = "https://repo.lushplugins.org/releases") maven(url = "https://repo.lushplugins.org/releases")
maven(url = "https://repo.inventivetalent.org/repository/maven-snapshots/") maven(url = "https://repo.inventivetalent.org/repository/maven-snapshots/") // for cloud command framework
maven(url = "https://repo.extendedclip.com/releases/") maven(url = "https://repo.extendedclip.com/releases/") // for PlaceholderAPI
maven(url = "https://maven.enginehub.org/repo/") // for WorldEdit
} }
} }
@@ -85,7 +86,8 @@ dependencies {
implementation("org.mineskin:java-client-jsoup:3.0.3-SNAPSHOT") implementation("org.mineskin:java-client-jsoup:3.0.3-SNAPSHOT")
compileOnly("me.clip:placeholderapi:2.11.6") compileOnly("me.clip:placeholderapi:2.11.6")
compileOnly("com.intellectualsites.plotsquared:plotsquared-core:7.5.2") compileOnly("com.intellectualsites.plotsquared:plotsquared-core:7.5.6")
compileOnly("com.sk89q.worldedit:worldedit-bukkit:7.3.14")
} }
paper { paper {

View File

@@ -1,8 +1,9 @@
package de.oliver.fancynpcs.listeners; package de.oliver.fancynpcs.listeners;
import com.plotsquared.core.PlotSquared; import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.location.Location;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea;
import de.oliver.fancylib.translations.Translator; import de.oliver.fancylib.translations.Translator;
import de.oliver.fancynpcs.FancyNpcs; import de.oliver.fancynpcs.FancyNpcs;
import de.oliver.fancynpcs.api.Npc; import de.oliver.fancynpcs.api.Npc;
@@ -18,9 +19,46 @@ import java.util.Map;
public class PlayerNpcsListener implements Listener { public class PlayerNpcsListener implements Listener {
private static final boolean isUsingPlotSquared = FancyNpcs.getInstance().isUsingPlotSquared();
private final Translator translator = FancyNpcs.getInstance().getTranslator(); private final Translator translator = FancyNpcs.getInstance().getTranslator();
private static final boolean isUsingPlotSquared = FancyNpcs.getInstance().isUsingPlotSquared(); /**
* Checks if the player is the owner of the plot where the NPC is located.
*
* @return true if the player is the owner of the plot or if PlotSquared is not used, false otherwise.
*/
public static boolean checkNpcOwnership(Player player, org.bukkit.Location loc) {
if (!isUsingPlotSquared) {
return true;
}
if (loc == null || player == null) {
return false;
}
if (player.hasPermission("fancynpcs.admin")) {
return true;
}
Location npcLoc = Location.at(
loc.getWorld().getName(),
loc.getBlockX(),
loc.getBlockY(),
loc.getBlockZ()
);
PlotArea plotArea = PlotSquared.platform()
.plotAreaManager()
.getPlotArea(npcLoc);
if (plotArea == null) {
return false;
}
Plot plot = plotArea.getOwnedPlot(npcLoc);
return plot != null && plot.isOwner(player.getUniqueId());
}
@EventHandler @EventHandler
public void onNpcCreate(NpcCreateEvent event) { public void onNpcCreate(NpcCreateEvent event) {
@@ -28,15 +66,13 @@ public class PlayerNpcsListener implements Listener {
return; return;
} }
if (isUsingPlotSquared) { boolean isOwner = checkNpcOwnership(player, event.getNpc().getData().getLocation());
PlotPlayer<?> plotPlayer = PlotSquared.platform().playerManager().getPlayer(player.getUniqueId()); if (!isOwner) {
Plot currentPlot = plotPlayer.getCurrentPlot(); translator.translate("player_npcs_create_failure_not_owned_plot").send(player);
if ((currentPlot == null || !currentPlot.isOwner(player.getUniqueId())) && !player.hasPermission("fancynpcs.admin")) { event.setCancelled(true);
translator.translate("player_npcs_create_failure_not_owned_plot").send(player); return;
event.setCancelled(true);
return;
}
} }
int maxNpcs = FancyNpcs.getInstance().getFancyNpcConfig().getMaxNpcsPerPermission() int maxNpcs = FancyNpcs.getInstance().getFancyNpcConfig().getMaxNpcsPerPermission()
.entrySet().stream() .entrySet().stream()
.filter(entry -> player.hasPermission(entry.getKey())) .filter(entry -> player.hasPermission(entry.getKey()))
@@ -59,6 +95,7 @@ public class PlayerNpcsListener implements Listener {
@EventHandler @EventHandler
public void onNpcRemove(NpcRemoveEvent event) { public void onNpcRemove(NpcRemoveEvent event) {
if (!(event.getSender() instanceof Player player)) { if (!(event.getSender() instanceof Player player)) {
FancyNpcs.getInstance().getFancyLogger().warn("NpcRemoveEvent sender is not a Player!");
return; return;
} }
@@ -72,22 +109,19 @@ public class PlayerNpcsListener implements Listener {
@EventHandler @EventHandler
public void onNpcModify(NpcModifyEvent event) { public void onNpcModify(NpcModifyEvent event) {
if (!(event.getModifier() instanceof Player player)) { if (!(event.getModifier() instanceof Player player)) {
FancyNpcs.getInstance().getFancyLogger().warn("NpcModifyEvent modifier is not a Player!");
return; return;
} }
if (!event.getNpc().getData().getCreator().equals(player.getUniqueId()) && !player.hasPermission("fancynpcs.admin")) { if (!(event.getNewValue() instanceof org.bukkit.Location location)) {
translator.translate("player_npcs_cannot_modify_npc").send(player); FancyNpcs.getInstance().getFancyLogger().warn("NpcModifyEvent newValue is not a Location!");
return;
}
boolean isOwner = checkNpcOwnership(player, location);
if (!isOwner) {
translator.translate("player_npcs_cannot_move_npc").send(player);
event.setCancelled(true); event.setCancelled(true);
return;
}
if (isUsingPlotSquared && event.getModification() == NpcModifyEvent.NpcModification.LOCATION) {
PlotPlayer<?> plotPlayer = PlotSquared.platform().playerManager().getPlayer(player.getUniqueId());
Plot currentPlot = plotPlayer.getCurrentPlot();
if ((currentPlot == null || !currentPlot.isOwner(player.getUniqueId())) && !player.hasPermission("fancynpcs.admin")) {
translator.translate("player_npcs_cannot_move_npc").send(player);
event.setCancelled(true);
}
} }
} }
} }