11package de .oliver .fancynpcs .listeners ;
22
33import com .plotsquared .core .PlotSquared ;
4- import com .plotsquared .core .player . PlotPlayer ;
4+ import com .plotsquared .core .location . Location ;
55import com .plotsquared .core .plot .Plot ;
6+ import com .plotsquared .core .plot .PlotArea ;
67import de .oliver .fancylib .translations .Translator ;
78import de .oliver .fancynpcs .FancyNpcs ;
89import de .oliver .fancynpcs .api .Npc ;
1819
1920public 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