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.fancyinnovations.com/releases")
maven(url = "https://repo.lushplugins.org/releases")
maven(url = "https://repo.inventivetalent.org/repository/maven-snapshots/")
maven(url = "https://repo.extendedclip.com/releases/")
maven(url = "https://repo.inventivetalent.org/repository/maven-snapshots/") // for cloud command framework
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")
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 {

View File

@@ -1,8 +1,9 @@
package de.oliver.fancynpcs.listeners;
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.PlotArea;
import de.oliver.fancylib.translations.Translator;
import de.oliver.fancynpcs.FancyNpcs;
import de.oliver.fancynpcs.api.Npc;
@@ -18,9 +19,46 @@ import java.util.Map;
public class PlayerNpcsListener implements Listener {
private static final boolean isUsingPlotSquared = FancyNpcs.getInstance().isUsingPlotSquared();
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
public void onNpcCreate(NpcCreateEvent event) {
@@ -28,15 +66,13 @@ public class PlayerNpcsListener implements Listener {
return;
}
if (isUsingPlotSquared) {
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_create_failure_not_owned_plot").send(player);
event.setCancelled(true);
return;
}
boolean isOwner = checkNpcOwnership(player, event.getNpc().getData().getLocation());
if (!isOwner) {
translator.translate("player_npcs_create_failure_not_owned_plot").send(player);
event.setCancelled(true);
return;
}
int maxNpcs = FancyNpcs.getInstance().getFancyNpcConfig().getMaxNpcsPerPermission()
.entrySet().stream()
.filter(entry -> player.hasPermission(entry.getKey()))
@@ -59,6 +95,7 @@ public class PlayerNpcsListener implements Listener {
@EventHandler
public void onNpcRemove(NpcRemoveEvent event) {
if (!(event.getSender() instanceof Player player)) {
FancyNpcs.getInstance().getFancyLogger().warn("NpcRemoveEvent sender is not a Player!");
return;
}
@@ -72,22 +109,19 @@ public class PlayerNpcsListener implements Listener {
@EventHandler
public void onNpcModify(NpcModifyEvent event) {
if (!(event.getModifier() instanceof Player player)) {
FancyNpcs.getInstance().getFancyLogger().warn("NpcModifyEvent modifier is not a Player!");
return;
}
if (!event.getNpc().getData().getCreator().equals(player.getUniqueId()) && !player.hasPermission("fancynpcs.admin")) {
translator.translate("player_npcs_cannot_modify_npc").send(player);
if (!(event.getNewValue() instanceof org.bukkit.Location location)) {
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);
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);
}
}
}
}