Skip to content

Commit 26e9111

Browse files
fancynpcs (player-npcs): Refactor NPC plot ownership checks
1 parent c306265 commit 26e9111

3 files changed

Lines changed: 61 additions & 25 deletions

File tree

plugins/fancynpcs/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.7.1.292
1+
2.7.1.293

plugins/fancynpcs/build.gradle.kts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ allprojects {
4444
maven(url = "https://repo.papermc.io/repository/maven-public/")
4545
maven(url = "https://repo.fancyinnovations.com/releases")
4646
maven(url = "https://repo.lushplugins.org/releases")
47-
maven(url = "https://repo.inventivetalent.org/repository/maven-snapshots/")
48-
maven(url = "https://repo.extendedclip.com/releases/")
47+
maven(url = "https://repo.inventivetalent.org/repository/maven-snapshots/") // for cloud command framework
48+
maven(url = "https://repo.extendedclip.com/releases/") // for PlaceholderAPI
49+
maven(url = "https://maven.enginehub.org/repo/") // for WorldEdit
4950
}
5051
}
5152

@@ -85,7 +86,8 @@ dependencies {
8586
implementation("org.mineskin:java-client-jsoup:3.0.3-SNAPSHOT")
8687

8788
compileOnly("me.clip:placeholderapi:2.11.6")
88-
compileOnly("com.intellectualsites.plotsquared:plotsquared-core:7.5.2")
89+
compileOnly("com.intellectualsites.plotsquared:plotsquared-core:7.5.6")
90+
compileOnly("com.sk89q.worldedit:worldedit-bukkit:7.3.14")
8991
}
9092

9193
paper {

plugins/fancynpcs/src/main/java/de/oliver/fancynpcs/listeners/PlayerNpcsListener.java

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package de.oliver.fancynpcs.listeners;
22

33
import com.plotsquared.core.PlotSquared;
4-
import com.plotsquared.core.player.PlotPlayer;
4+
import com.plotsquared.core.location.Location;
55
import com.plotsquared.core.plot.Plot;
6+
import com.plotsquared.core.plot.PlotArea;
67
import de.oliver.fancylib.translations.Translator;
78
import de.oliver.fancynpcs.FancyNpcs;
89
import de.oliver.fancynpcs.api.Npc;
@@ -18,25 +19,60 @@
1819

1920
public class PlayerNpcsListener implements Listener {
2021

22+
private static final boolean isUsingPlotSquared = FancyNpcs.getInstance().isUsingPlotSquared();
2123
private final Translator translator = FancyNpcs.getInstance().getTranslator();
2224

23-
private static final boolean isUsingPlotSquared = FancyNpcs.getInstance().isUsingPlotSquared();
25+
/**
26+
* Checks if the player is the owner of the plot where the NPC is located.
27+
*
28+
* @return true if the player is the owner of the plot or if PlotSquared is not used, false otherwise.
29+
*/
30+
public static boolean checkNpcOwnership(Player player, org.bukkit.Location loc) {
31+
if (!isUsingPlotSquared) {
32+
return true;
33+
}
34+
35+
if (loc == null || player == null) {
36+
return false;
37+
}
38+
39+
if (player.hasPermission("fancynpcs.admin")) {
40+
return true;
41+
}
42+
43+
Location npcLoc = Location.at(
44+
loc.getWorld().getName(),
45+
loc.getBlockX(),
46+
loc.getBlockY(),
47+
loc.getBlockZ()
48+
);
49+
50+
PlotArea plotArea = PlotSquared.platform()
51+
.plotAreaManager()
52+
.getPlotArea(npcLoc);
53+
54+
if (plotArea == null) {
55+
return false;
56+
}
57+
58+
Plot plot = plotArea.getOwnedPlot(npcLoc);
59+
60+
return plot != null && plot.isOwner(player.getUniqueId());
61+
}
2462

2563
@EventHandler
2664
public void onNpcCreate(NpcCreateEvent event) {
2765
if (!(event.getCreator() instanceof Player player)) {
2866
return;
2967
}
3068

31-
if (isUsingPlotSquared) {
32-
PlotPlayer<?> plotPlayer = PlotSquared.platform().playerManager().getPlayer(player.getUniqueId());
33-
Plot currentPlot = plotPlayer.getCurrentPlot();
34-
if ((currentPlot == null || !currentPlot.isOwner(player.getUniqueId())) && !player.hasPermission("fancynpcs.admin")) {
35-
translator.translate("player_npcs_create_failure_not_owned_plot").send(player);
36-
event.setCancelled(true);
37-
return;
38-
}
69+
boolean isOwner = checkNpcOwnership(player, event.getNpc().getData().getLocation());
70+
if (!isOwner) {
71+
translator.translate("player_npcs_create_failure_not_owned_plot").send(player);
72+
event.setCancelled(true);
73+
return;
3974
}
75+
4076
int maxNpcs = FancyNpcs.getInstance().getFancyNpcConfig().getMaxNpcsPerPermission()
4177
.entrySet().stream()
4278
.filter(entry -> player.hasPermission(entry.getKey()))
@@ -59,6 +95,7 @@ public void onNpcCreate(NpcCreateEvent event) {
5995
@EventHandler
6096
public void onNpcRemove(NpcRemoveEvent event) {
6197
if (!(event.getSender() instanceof Player player)) {
98+
FancyNpcs.getInstance().getFancyLogger().warn("NpcRemoveEvent sender is not a Player!");
6299
return;
63100
}
64101

@@ -72,22 +109,19 @@ public void onNpcRemove(NpcRemoveEvent event) {
72109
@EventHandler
73110
public void onNpcModify(NpcModifyEvent event) {
74111
if (!(event.getModifier() instanceof Player player)) {
112+
FancyNpcs.getInstance().getFancyLogger().warn("NpcModifyEvent modifier is not a Player!");
75113
return;
76114
}
77115

78-
if (!event.getNpc().getData().getCreator().equals(player.getUniqueId()) && !player.hasPermission("fancynpcs.admin")) {
79-
translator.translate("player_npcs_cannot_modify_npc").send(player);
80-
event.setCancelled(true);
116+
if (!(event.getNewValue() instanceof org.bukkit.Location location)) {
117+
FancyNpcs.getInstance().getFancyLogger().warn("NpcModifyEvent newValue is not a Location!");
81118
return;
82119
}
83-
if (isUsingPlotSquared && event.getModification() == NpcModifyEvent.NpcModification.LOCATION) {
84-
PlotPlayer<?> plotPlayer = PlotSquared.platform().playerManager().getPlayer(player.getUniqueId());
85-
Plot currentPlot = plotPlayer.getCurrentPlot();
86-
87-
if ((currentPlot == null || !currentPlot.isOwner(player.getUniqueId())) && !player.hasPermission("fancynpcs.admin")) {
88-
translator.translate("player_npcs_cannot_move_npc").send(player);
89-
event.setCancelled(true);
90-
}
120+
121+
boolean isOwner = checkNpcOwnership(player, location);
122+
if (!isOwner) {
123+
translator.translate("player_npcs_cannot_move_npc").send(player);
124+
event.setCancelled(true);
91125
}
92126
}
93127
}

0 commit comments

Comments
 (0)