Skip to content

Commit 375d2d2

Browse files
committed
feat: introduce PlotAreaAccessor and WorldNameAccessor interfaces for improved plot and world name access
1 parent aaba2b5 commit 375d2d2

File tree

8 files changed

+74
-9
lines changed

8 files changed

+74
-9
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.plotsquared.core.accessor;
2+
3+
import com.plotsquared.core.plot.PlotArea;
4+
5+
6+
/**
7+
* An accessor to get the plot area.
8+
*
9+
* @since 7.5.7
10+
* @version 1.0.0
11+
* @author IntellectualSites
12+
* @author TheMeinerLP
13+
*/
14+
public interface PlotAreaAccessor {
15+
/**
16+
* Gets the plot world object for this plot<br>
17+
* - The generic PlotArea object can be casted to its respective class for more control (e.g. HybridPlotWorld)
18+
*
19+
* @return PlotArea
20+
*/
21+
PlotArea getArea();
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.plotsquared.core.accessor;
2+
3+
import org.checkerframework.checker.nullness.qual.NonNull;
4+
5+
/**
6+
* An accessor to get the world name.
7+
*
8+
* @since 7.5.7
9+
* @version 1.0.0
10+
* @author IntellectualSites
11+
* @author TheMeinerLP
12+
*/
13+
public interface WorldNameAccessor {
14+
15+
/**
16+
* Get the name of the world for this object
17+
*
18+
* @return World name
19+
*/
20+
@NonNull
21+
String getWorldName();
22+
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.plotsquared.core.configuration.caption.CaptionHolder;
2626
import com.plotsquared.core.configuration.caption.TranslatableCaption;
2727
import com.plotsquared.core.database.DBFunc;
28+
import com.plotsquared.core.accessor.PlotAreaAccessor;
29+
import com.plotsquared.core.accessor.WorldNameAccessor;
2830
import com.plotsquared.core.permissions.Permission;
2931
import com.plotsquared.core.player.PlotPlayer;
3032
import com.plotsquared.core.plot.Plot;
@@ -56,6 +58,7 @@
5658
import java.util.Iterator;
5759
import java.util.LinkedList;
5860
import java.util.List;
61+
import java.util.Optional;
5962
import java.util.UUID;
6063
import java.util.concurrent.ExecutionException;
6164
import java.util.concurrent.TimeUnit;
@@ -150,8 +153,14 @@ public boolean onCommand(PlotPlayer<?> player, String[] args) {
150153
page = 0;
151154
}
152155

153-
String world = player.getCurrentPlot().getWorldName();
154-
PlotArea area = player.getCurrentPlot().getArea();
156+
String world = Optional
157+
.ofNullable(player.getCurrentPlot())
158+
.map(WorldNameAccessor.class::cast)
159+
.orElse(player.getLocation())
160+
.getWorldName();
161+
PlotArea area = Optional.ofNullable(player.getCurrentPlot())
162+
.map(PlotAreaAccessor.class::cast)
163+
.orElse(player).getArea();
155164
String arg = args[0].toLowerCase();
156165
final boolean[] sort = new boolean[]{true};
157166

Core/src/main/java/com/plotsquared/core/location/Location.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@
2121
import com.google.common.base.Objects;
2222
import com.google.common.base.Preconditions;
2323
import com.plotsquared.core.PlotSquared;
24+
import com.plotsquared.core.accessor.WorldNameAccessor;
2425
import com.plotsquared.core.plot.Plot;
2526
import com.plotsquared.core.plot.PlotArea;
2627
import com.sk89q.worldedit.math.BlockVector2;
2728
import com.sk89q.worldedit.math.BlockVector3;
2829
import org.checkerframework.checker.nullness.qual.NonNull;
2930
import org.checkerframework.checker.nullness.qual.Nullable;
31+
import org.jetbrains.annotations.NotNull;
3032
import org.khelekore.prtree.MBR;
3133
import org.khelekore.prtree.SimpleMBR;
3234

3335
/**
3436
* An unmodifiable 6-tuple (world,x,y,z,yaw,pitch)
3537
*/
3638
@SuppressWarnings("unused")
37-
public sealed class Location extends BlockLoc implements Comparable<Location> permits UncheckedWorldLocation {
39+
public sealed class Location extends BlockLoc implements Comparable<Location>, WorldNameAccessor permits UncheckedWorldLocation {
3840

3941
private final float yaw;
4042
private final float pitch;
@@ -213,7 +215,7 @@ private Location(
213215
*
214216
* @return World name
215217
*/
216-
public @NonNull String getWorldName() {
218+
public @NonNull @NotNull String getWorldName() {
217219
return this.world.getName();
218220
}
219221

Core/src/main/java/com/plotsquared/core/location/UncheckedWorldLocation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.intellectualsites.annotations.DoNotUse;
2222
import com.sk89q.worldedit.math.BlockVector3;
2323
import org.checkerframework.checker.nullness.qual.NonNull;
24+
import org.jetbrains.annotations.NotNull;
2425

2526
/**
2627
* Used internally for generation to reference locations in worlds that "don't exist yet". There is no guarantee that the world
@@ -75,7 +76,7 @@ private UncheckedWorldLocation(
7576

7677
@Override
7778
@DoNotUse
78-
public @NonNull String getWorldName() {
79+
public @NonNull @NotNull String getWorldName() {
7980
return this.worldName;
8081
}
8182

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.plotsquared.core.database.DBFunc;
3535
import com.plotsquared.core.events.TeleportCause;
3636
import com.plotsquared.core.location.Location;
37+
import com.plotsquared.core.accessor.PlotAreaAccessor;
3738
import com.plotsquared.core.permissions.NullPermissionProfile;
3839
import com.plotsquared.core.permissions.PermissionHandler;
3940
import com.plotsquared.core.permissions.PermissionProfile;
@@ -87,7 +88,7 @@
8788
/**
8889
* The abstract class supporting {@code BukkitPlayer} and {@code SpongePlayer}.
8990
*/
90-
public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer, LocaleHolder {
91+
public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer, LocaleHolder, PlotAreaAccessor {
9192

9293
private static final String NON_EXISTENT_CAPTION = "<red>PlotSquared does not recognize the caption: ";
9394

@@ -416,6 +417,11 @@ public PlotArea getApplicablePlotArea() {
416417
return plot.getArea();
417418
}
418419

420+
@Override
421+
public PlotArea getArea() {
422+
return getApplicablePlotArea();
423+
}
424+
419425
@Override
420426
public @NonNull RequiredType getSuperCaller() {
421427
return RequiredType.PLAYER;

Core/src/main/java/com/plotsquared/core/plot/Plot.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.plotsquared.core.location.BlockLoc;
3838
import com.plotsquared.core.location.Direction;
3939
import com.plotsquared.core.location.Location;
40+
import com.plotsquared.core.accessor.WorldNameAccessor;
4041
import com.plotsquared.core.permissions.Permission;
4142
import com.plotsquared.core.player.ConsolePlayer;
4243
import com.plotsquared.core.player.PlotPlayer;
@@ -78,6 +79,7 @@
7879
import org.apache.logging.log4j.Logger;
7980
import org.checkerframework.checker.nullness.qual.NonNull;
8081
import org.checkerframework.checker.nullness.qual.Nullable;
82+
import org.jetbrains.annotations.NotNull;
8183

8284
import java.lang.ref.Cleaner;
8385
import java.text.DecimalFormat;
@@ -115,7 +117,7 @@
115117
* - Using the `new` operator will create an unclaimed plot instance
116118
* - Use the methods from the PlotArea/PS/Location etc to get existing plots
117119
*/
118-
public class Plot {
120+
public class Plot implements WorldNameAccessor {
119121

120122
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + Plot.class.getSimpleName());
121123
private static final DecimalFormat FLAG_DECIMAL_FORMAT = new DecimalFormat("0");
@@ -541,7 +543,7 @@ public void setOwnerAbs(final @Nullable UUID owner) {
541543
*
542544
* @return World name
543545
*/
544-
public @Nullable String getWorldName() {
546+
public @Nullable @NotNull String getWorldName() {
545547
return area.getWorldName();
546548
}
547549

Core/src/main/java/com/plotsquared/core/plot/world/SinglePlot.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.sk89q.worldedit.math.BlockVector3;
2828
import com.sk89q.worldedit.regions.CuboidRegion;
2929
import org.checkerframework.checker.nullness.qual.NonNull;
30+
import org.jetbrains.annotations.NotNull;
3031

3132
import java.util.Collection;
3233
import java.util.Collections;
@@ -58,7 +59,7 @@ public SinglePlot(
5859
}
5960

6061
@Override
61-
public String getWorldName() {
62+
public @NotNull String getWorldName() {
6263
return getId().toUnderscoreSeparatedString();
6364
}
6465

0 commit comments

Comments
 (0)