Skip to content

Commit c0b1179

Browse files
committed
feat: allow for custom plot limit handling
1 parent 1a18adc commit c0b1179

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.plotsquared.core.events;
2+
3+
import com.plotsquared.core.player.PlotPlayer;
4+
import org.checkerframework.checker.index.qual.NonNegative;
5+
import org.checkerframework.checker.nullness.qual.NonNull;
6+
7+
/**
8+
* Called every time after PlotSquared calculated a players plot limit based on their permission.
9+
* <p>
10+
* May be used to grant a player more plots based on another rank or bought feature.
11+
*
12+
* @since TODO
13+
*/
14+
public class PlayerPlotLimitEvent {
15+
16+
private final PlotPlayer<?> player;
17+
18+
private int limit;
19+
20+
public PlayerPlotLimitEvent(@NonNull final PlotPlayer<?> player, @NonNegative final int limit) {
21+
this.player = player;
22+
this.limit = limit;
23+
}
24+
25+
/**
26+
* Overrides the previously calculated or set plot limit for {@link #player()}.
27+
*
28+
* @param limit The amount of plots a player may claim. Must be {@code 0} or greater.
29+
* @since TODO
30+
*/
31+
public void setLimit(@NonNegative final int limit) {
32+
if (limit < 0) {
33+
throw new IllegalArgumentException("Player plot limit must be greater or equal 0");
34+
}
35+
this.limit = limit;
36+
}
37+
38+
/**
39+
* Returns the previous set limit, if none was overridden before this event handler the default limit based on the players
40+
* permissions node is returned.
41+
*
42+
* @return The currently defined plot limit of this player.
43+
* @since TODO
44+
*/
45+
public @NonNegative int limit() {
46+
return limit;
47+
}
48+
49+
/**
50+
* The player for which the limit is queried.
51+
*
52+
* @return the player.
53+
* @since TODO
54+
*/
55+
public @NonNull PlotPlayer<?> player() {
56+
return player;
57+
}
58+
59+
}

Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ public Plot getCurrentPlot() {
306306
* @return number of allowed plots within the scope (globally, or in the player's current world as defined in the settings.yml)
307307
*/
308308
public int getAllowedPlots() {
309-
return hasPermissionRange("plots.plot", Settings.Limit.MAX_PLOTS);
309+
final int calculatedLimit = hasPermissionRange("plots.plot", Settings.Limit.MAX_PLOTS);
310+
return this.eventDispatcher.callPlayerPlotLimit(this, calculatedLimit).limit();
310311
}
311312

312313
/**

Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.plotsquared.core.events.PlayerLeavePlotEvent;
3131
import com.plotsquared.core.events.PlayerPlotDeniedEvent;
3232
import com.plotsquared.core.events.PlayerPlotHelperEvent;
33+
import com.plotsquared.core.events.PlayerPlotLimitEvent;
3334
import com.plotsquared.core.events.PlayerPlotTrustedEvent;
3435
import com.plotsquared.core.events.PlayerTeleportToPlotEvent;
3536
import com.plotsquared.core.events.PlotAutoMergeEvent;
@@ -308,6 +309,12 @@ public RemoveRoadEntityEvent callRemoveRoadEntity(Entity entity) {
308309
return event;
309310
}
310311

312+
public PlayerPlotLimitEvent callPlayerPlotLimit(PlotPlayer<?> player, int calculatedLimit) {
313+
PlayerPlotLimitEvent event = new PlayerPlotLimitEvent(player, calculatedLimit);
314+
eventBus.post(event);
315+
return event;
316+
}
317+
311318
public void doJoinTask(final PlotPlayer<?> player) {
312319
if (player == null) {
313320
return; //possible future warning message to figure out where we are retrieving null

0 commit comments

Comments
 (0)