Skip to content

Commit e2b6324

Browse files
committed
feat: implement better add/trust/deny/remove events
- closes #3509
1 parent 8edc5c1 commit e2b6324

21 files changed

+824
-40
lines changed

Core/src/main/java/com/plotsquared/core/command/Add.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.plotsquared.core.configuration.Settings;
2424
import com.plotsquared.core.configuration.caption.TranslatableCaption;
2525
import com.plotsquared.core.database.DBFunc;
26+
import com.plotsquared.core.events.PlayerPlotAddRemoveEvent;
27+
import com.plotsquared.core.events.Result;
2628
import com.plotsquared.core.permissions.Permission;
2729
import com.plotsquared.core.player.PlotPlayer;
2830
import com.plotsquared.core.plot.Plot;
@@ -151,20 +153,41 @@ public CompletableFuture<Boolean> execute(
151153
return;
152154
}
153155
// Success
154-
confirm.run(this, () -> {
155-
for (UUID uuid : uuids) {
156-
if (uuid != DBFunc.EVERYONE) {
157-
if (!plot.removeTrusted(uuid)) {
158-
if (plot.getDenied().contains(uuid)) {
159-
plot.removeDenied(uuid);
156+
confirm.run(
157+
this, () -> {
158+
for (UUID uuid : uuids) {
159+
if (this.eventDispatcher.callPlayerAdd(
160+
player,
161+
plot,
162+
uuid,
163+
PlayerPlotAddRemoveEvent.Reason.COMMAND
164+
).getEventResult() == Result.DENY) {
165+
player.sendMessage(
166+
TranslatableCaption.of("events.event_denied"),
167+
TagResolver.resolver("value", Tag.inserting(Component.text("Add")))
168+
);
169+
continue;
160170
}
171+
if (uuid != DBFunc.EVERYONE) {
172+
if (!plot.removeTrusted(uuid)) {
173+
if (plot.getDenied().contains(uuid)) {
174+
plot.removeDenied(uuid);
175+
}
176+
}
177+
}
178+
plot.addMember(uuid);
179+
this.eventDispatcher.callMember(player, plot, uuid, true);
180+
this.eventDispatcher.callPostAdded(
181+
player,
182+
plot,
183+
uuid,
184+
false,
185+
PlayerPlotAddRemoveEvent.Reason.COMMAND
186+
);
187+
player.sendMessage(TranslatableCaption.of("member.member_added"));
161188
}
162-
}
163-
plot.addMember(uuid);
164-
this.eventDispatcher.callMember(player, plot, uuid, true);
165-
player.sendMessage(TranslatableCaption.of("member.member_added"));
166-
}
167-
}, null);
189+
}, null
190+
);
168191
} catch (final Throwable exception) {
169192
future.completeExceptionally(exception);
170193
return;

Core/src/main/java/com/plotsquared/core/command/Deny.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.plotsquared.core.configuration.Settings;
2424
import com.plotsquared.core.configuration.caption.TranslatableCaption;
2525
import com.plotsquared.core.database.DBFunc;
26+
import com.plotsquared.core.events.PlayerPlotAddRemoveEvent;
27+
import com.plotsquared.core.events.Result;
2628
import com.plotsquared.core.events.TeleportCause;
2729
import com.plotsquared.core.location.Location;
2830
import com.plotsquared.core.permissions.Permission;
@@ -124,21 +126,29 @@ public boolean onCommand(PlotPlayer<?> player, String[] args) {
124126
);
125127
return;
126128
} else {
129+
if (this.eventDispatcher
130+
.callPlayerDeny(player, plot, uuid, PlayerPlotAddRemoveEvent.Reason.COMMAND)
131+
.getEventResult() == Result.DENY) {
132+
player.sendMessage(
133+
TranslatableCaption.of("events.event_denied"),
134+
TagResolver.resolver("value", Tag.inserting(Component.text("Deny")))
135+
);
136+
continue;
137+
}
127138
if (uuid != DBFunc.EVERYONE) {
128139
plot.removeMember(uuid);
129140
plot.removeTrusted(uuid);
130141
}
131142
plot.addDenied(uuid);
132143
this.eventDispatcher.callDenied(player, plot, uuid, true);
144+
this.eventDispatcher.callPostDenied(player, plot, uuid, true, PlayerPlotAddRemoveEvent.Reason.COMMAND);
133145
if (!uuid.equals(DBFunc.EVERYONE)) {
134146
handleKick(PlotSquared.platform().playerManager().getPlayerIfExists(uuid), plot);
135147
} else {
136148
for (PlotPlayer<?> plotPlayer : plot.getPlayersInPlot()) {
137-
// Ignore plot-owners
138-
if (plot.isAdded(plotPlayer.getUUID())) {
139-
continue;
149+
if (plot.isDenied(plotPlayer.getUUID())) {
150+
handleKick(plotPlayer, plot);
140151
}
141-
handleKick(plotPlayer, plot);
142152
}
143153
}
144154
}

Core/src/main/java/com/plotsquared/core/command/Leave.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import com.google.inject.Inject;
2222
import com.plotsquared.core.configuration.caption.TranslatableCaption;
23+
import com.plotsquared.core.events.PlayerPlotAddRemoveEvent;
24+
import com.plotsquared.core.events.Result;
2325
import com.plotsquared.core.player.PlotPlayer;
2426
import com.plotsquared.core.plot.Plot;
2527
import com.plotsquared.core.util.EventDispatcher;
@@ -61,11 +63,22 @@ public CompletableFuture<Boolean> execute(
6163
} else {
6264
UUID uuid = player.getUUID();
6365
if (plot.isAdded(uuid)) {
66+
if (this.eventDispatcher
67+
.callPlayerRemove(player, plot, uuid, PlayerPlotAddRemoveEvent.Reason.COMMAND)
68+
.getEventResult() == Result.DENY) {
69+
player.sendMessage(
70+
TranslatableCaption.of("events.event_denied"),
71+
TagResolver.resolver("value", Tag.inserting(Component.text("Leave")))
72+
);
73+
return CompletableFuture.completedFuture(true);
74+
}
6475
if (plot.removeTrusted(uuid)) {
6576
this.eventDispatcher.callTrusted(player, plot, uuid, false);
77+
this.eventDispatcher.callPostTrusted(player, plot, uuid, false, PlayerPlotAddRemoveEvent.Reason.COMMAND);
6678
}
6779
if (plot.removeMember(uuid)) {
6880
this.eventDispatcher.callMember(player, plot, uuid, false);
81+
this.eventDispatcher.callPostAdded(player, plot, uuid, false, PlayerPlotAddRemoveEvent.Reason.COMMAND);
6982
}
7083
player.sendMessage(
7184
TranslatableCaption.of("member.plot_left"),

Core/src/main/java/com/plotsquared/core/command/Remove.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import com.google.inject.Inject;
2222
import com.plotsquared.core.configuration.caption.TranslatableCaption;
2323
import com.plotsquared.core.database.DBFunc;
24+
import com.plotsquared.core.events.PlayerPlotAddRemoveEvent;
25+
import com.plotsquared.core.events.Result;
2426
import com.plotsquared.core.permissions.Permission;
2527
import com.plotsquared.core.player.PlotPlayer;
2628
import com.plotsquared.core.plot.Plot;
@@ -82,33 +84,48 @@ public boolean onCommand(PlotPlayer<?> player, String[] args) {
8284
return;
8385
} else if (!uuids.isEmpty()) {
8486
for (UUID uuid : uuids) {
87+
if (this.eventDispatcher
88+
.callPlayerRemove(player, plot, uuid, PlayerPlotAddRemoveEvent.Reason.COMMAND)
89+
.getEventResult() == Result.DENY) {
90+
player.sendMessage(
91+
TranslatableCaption.of("events.event_denied"),
92+
TagResolver.resolver("value", Tag.inserting(Component.text("Remove")))
93+
);
94+
continue;
95+
}
8596
if (plot.getTrusted().contains(uuid)) {
8697
if (plot.removeTrusted(uuid)) {
8798
this.eventDispatcher.callTrusted(player, plot, uuid, false);
99+
this.eventDispatcher.callPostTrusted(player, plot, uuid, false, PlayerPlotAddRemoveEvent.Reason.COMMAND);
88100
count++;
89101
}
90102
} else if (plot.getMembers().contains(uuid)) {
91103
if (plot.removeMember(uuid)) {
92104
this.eventDispatcher.callMember(player, plot, uuid, false);
105+
this.eventDispatcher.callPostAdded(player, plot, uuid, false, PlayerPlotAddRemoveEvent.Reason.COMMAND);
93106
count++;
94107
}
95108
} else if (plot.getDenied().contains(uuid)) {
96109
if (plot.removeDenied(uuid)) {
97110
this.eventDispatcher.callDenied(player, plot, uuid, false);
111+
this.eventDispatcher.callPostDenied(player, plot, uuid, true, PlayerPlotAddRemoveEvent.Reason.COMMAND);
98112
count++;
99113
}
100114
} else if (uuid == DBFunc.EVERYONE) {
101115
count += plot.getTrusted().size();
102116
if (plot.removeTrusted(uuid)) {
103117
this.eventDispatcher.callTrusted(player, plot, uuid, false);
118+
this.eventDispatcher.callPostTrusted(player, plot, uuid, false, PlayerPlotAddRemoveEvent.Reason.COMMAND);
104119
}
105120
count += plot.getMembers().size();
106121
if (plot.removeMember(uuid)) {
107122
this.eventDispatcher.callMember(player, plot, uuid, false);
123+
this.eventDispatcher.callPostAdded(player, plot, uuid, false, PlayerPlotAddRemoveEvent.Reason.COMMAND);
108124
}
109125
count += plot.getDenied().size();
110126
if (plot.removeDenied(uuid)) {
111127
this.eventDispatcher.callDenied(player, plot, uuid, false);
128+
this.eventDispatcher.callPostDenied(player, plot, uuid, true, PlayerPlotAddRemoveEvent.Reason.COMMAND);
112129
}
113130
}
114131
}

Core/src/main/java/com/plotsquared/core/command/Trust.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import com.plotsquared.core.configuration.Settings;
2424
import com.plotsquared.core.configuration.caption.TranslatableCaption;
2525
import com.plotsquared.core.database.DBFunc;
26+
import com.plotsquared.core.events.PlayerPlotAddRemoveEvent;
27+
import com.plotsquared.core.events.Result;
2628
import com.plotsquared.core.permissions.Permission;
2729
import com.plotsquared.core.player.PlotPlayer;
2830
import com.plotsquared.core.plot.Plot;
@@ -150,23 +152,43 @@ public CompletableFuture<Boolean> execute(
150152
return;
151153
}
152154
// Success
153-
confirm.run(this, () -> {
154-
for (UUID uuid : uuids) {
155-
if (uuid != DBFunc.EVERYONE) {
156-
if (!currentPlot.removeMember(uuid)) {
157-
if (currentPlot.getDenied().contains(uuid)) {
158-
currentPlot.removeDenied(uuid);
155+
confirm.run(
156+
this, () -> {
157+
for (UUID uuid : uuids) {
158+
if (this.eventDispatcher
159+
.callPlayerTrust(player, currentPlot, uuid, PlayerPlotAddRemoveEvent.Reason.COMMAND)
160+
.getEventResult() == Result.DENY) {
161+
player.sendMessage(
162+
TranslatableCaption.of("events.event_denied"),
163+
TagResolver.resolver("value", Tag.inserting(Component.text("Trust")))
164+
);
165+
return;
159166
}
167+
168+
if (uuid != DBFunc.EVERYONE) {
169+
if (!currentPlot.removeMember(uuid)) {
170+
if (currentPlot.getDenied().contains(uuid)) {
171+
currentPlot.removeDenied(uuid);
172+
}
173+
}
174+
}
175+
currentPlot.addTrusted(uuid);
176+
this.eventDispatcher.callTrusted(player, currentPlot, uuid, true);
177+
this.eventDispatcher.callPostTrusted(
178+
player,
179+
currentPlot,
180+
uuid,
181+
false,
182+
PlayerPlotAddRemoveEvent.Reason.COMMAND
183+
);
184+
player.sendMessage(TranslatableCaption.of("trusted.trusted_added"));
160185
}
161-
}
162-
currentPlot.addTrusted(uuid);
163-
this.eventDispatcher.callTrusted(player, currentPlot, uuid, true);
164-
player.sendMessage(TranslatableCaption.of("trusted.trusted_added"));
165-
}
166-
}, null);
186+
}, null
187+
);
167188
}
168-
future.complete(true);
169-
});
189+
future.complete(true);
190+
}
191+
);
170192
return CompletableFuture.completedFuture(true);
171193
}
172194

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* PlotSquared, a land and world management plugin for Minecraft.
3+
* Copyright (C) IntellectualSites <https://intellectualsites.com>
4+
* Copyright (C) IntellectualSites team and contributors
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package com.plotsquared.core.events;
20+
21+
import com.plotsquared.core.player.PlotPlayer;
22+
import com.plotsquared.core.plot.Plot;
23+
24+
import java.util.UUID;
25+
26+
/**
27+
* Called when a player is going to be added to a plot
28+
*
29+
* @since TODO
30+
*/
31+
public class PlayerPlotAddEvent extends PlayerPlotAddRemoveCancellableEvent {
32+
33+
/**
34+
* Called when a player will be added to a plot
35+
*
36+
* @param initiator Player that initiated the event
37+
* @param plot Plot in which the event occurred
38+
* @param player Player that will be added
39+
* @param reason The reason for the add
40+
*/
41+
public PlayerPlotAddEvent(PlotPlayer<?> initiator, Plot plot, UUID player, Reason reason) {
42+
super(initiator, plot, player, reason);
43+
}
44+
45+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* PlotSquared, a land and world management plugin for Minecraft.
3+
* Copyright (C) IntellectualSites <https://intellectualsites.com>
4+
* Copyright (C) IntellectualSites team and contributors
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package com.plotsquared.core.events;
20+
21+
import com.plotsquared.core.player.PlotPlayer;
22+
import com.plotsquared.core.plot.Plot;
23+
import org.checkerframework.checker.nullness.qual.Nullable;
24+
25+
import java.util.UUID;
26+
27+
/**
28+
* Parent class for the varies events regarding a player being added/removed/denied/trusted
29+
*
30+
* @since TODO
31+
*/
32+
public class PlayerPlotAddRemoveCancellableEvent extends PlayerPlotAddRemoveEvent implements CancellablePlotEvent {
33+
34+
private Result eventResult;
35+
36+
protected PlayerPlotAddRemoveCancellableEvent(
37+
final PlotPlayer<?> initiator,
38+
Plot plot,
39+
final UUID player,
40+
final Reason reason
41+
) {
42+
super(initiator, plot, player, reason);
43+
}
44+
45+
@Override
46+
public @Nullable Result getEventResult() {
47+
return eventResult;
48+
}
49+
50+
@Override
51+
public void setEventResult(@Nullable final Result eventResult) {
52+
this.eventResult = eventResult;
53+
}
54+
55+
}

0 commit comments

Comments
 (0)