Skip to content

Commit df738b2

Browse files
authored
fix: missing fallback if current plot is not explicitly overridden (#4779)
1 parent b7b2525 commit df738b2

File tree

7 files changed

+119
-92
lines changed

7 files changed

+119
-92
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public boolean onCommand(PlotPlayer<?> player, String[] args) {
9090
}
9191

9292
public boolean regenPlot(PlotPlayer<?> player) {
93-
PlotArea area = player.getCurrentPlot().getArea();
93+
PlotArea area = player.getContextualPlotArea();
9494
if (area == null) {
9595
player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
9696
return false;
@@ -145,9 +145,10 @@ public boolean regenRegion(PlotPlayer<?> player, String[] args) {
145145
return false;
146146
}
147147

148-
PlotArea area = player.getCurrentPlot().getArea();
148+
PlotArea area = player.getContextualPlotArea();
149149
if (area == null) {
150150
player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
151+
return false;
151152
}
152153
Plot plot = player.getCurrentPlot();
153154
PlotManager manager = area.getPlotManager();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ public boolean onCommand(PlotPlayer<?> player, String[] args) {
150150
page = 0;
151151
}
152152

153-
String world = player.getCurrentPlot().getWorldName();
154-
PlotArea area = player.getCurrentPlot().getArea();
153+
PlotArea area = player.getContextualPlotArea();
154+
String world = area != null ? area.getWorldName() : "";
155155
String arg = args[0].toLowerCase();
156156
final boolean[] sort = new boolean[]{true};
157157

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ public Load(
6868

6969
@Override
7070
public boolean onCommand(final PlotPlayer<?> player, final String[] args) {
71-
final String world = player.getCurrentPlot().getWorldName();
72-
if (!this.plotAreaManager.hasPlotArea(world)) {
73-
player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
74-
return false;
75-
}
7671
final Plot plot = player.getCurrentPlot();
7772
if (plot == null) {
7873
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public String getId() {
7878

7979
@Override
8080
public boolean set(PlotPlayer<?> player, final Plot plot, String value) {
81-
final PlotArea plotArea = player.getCurrentPlot().getArea();
81+
final PlotArea plotArea = player.getContextualPlotArea();
8282
if (plotArea == null) {
8383
return false;
8484
}

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

Lines changed: 111 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,7 @@ public String toString() {
290290
*
291291
* @return the plot the player is standing on or null if standing on a road or not in a {@link PlotArea}
292292
*/
293-
@Nullable
294-
public Plot getCurrentPlot() {
293+
public @Nullable Plot getCurrentPlot() {
295294
try (final MetaDataAccess<Plot> lastPlotAccess =
296295
this.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
297296
if (lastPlotAccess.get().orElse(null) == null && !Settings.Enabled_Components.EVENTS) {
@@ -320,7 +319,7 @@ public int getAllowedPlots() {
320319
*/
321320
public int getPlotCount() {
322321
if (!Settings.Limit.GLOBAL) {
323-
return getPlotCount(getCurrentPlot().getWorldName());
322+
return getPlotCount(getContextualWorldName());
324323
}
325324
final AtomicInteger count = new AtomicInteger(0);
326325
final UUID uuid = getUUID();
@@ -340,7 +339,7 @@ public int getPlotCount() {
340339

341340
public int getClusterCount() {
342341
if (!Settings.Limit.GLOBAL) {
343-
return getClusterCount(getCurrentPlot().getWorldName());
342+
return getClusterCount(getContextualWorldName());
344343
}
345344
final AtomicInteger count = new AtomicInteger(0);
346345
this.plotAreaManager.forEachPlotArea(value -> {
@@ -353,6 +352,34 @@ public int getClusterCount() {
353352
return count.get();
354353
}
355354

355+
/**
356+
* {@return the world name at the player's contextual position}
357+
* The contextual position can be affected when using a command with
358+
* an explicit plot override, e.g., `/plot &ltid&gt info`.
359+
*/
360+
private @NonNull String getContextualWorldName() {
361+
Plot current = getCurrentPlot();
362+
if (current != null) {
363+
return current.getWorldName();
364+
}
365+
return getLocation().getWorldName();
366+
}
367+
368+
/**
369+
* {@return the plot area at the player's contextual position}
370+
* The contextual position can be affected when using a command with
371+
* an explicit plot override, e.g., `/plot &ltid&gt info`.
372+
*
373+
* @since TODO
374+
*/
375+
public @Nullable PlotArea getContextualPlotArea() {
376+
Plot current = getCurrentPlot();
377+
if (current != null) {
378+
return current.getArea();
379+
}
380+
return getLocation().getPlotArea();
381+
}
382+
356383
/**
357384
* Get the number of plots this player owns in the world.
358385
*
@@ -683,86 +710,87 @@ public Set<Plot> getPlots(String world) {
683710

684711
public void populatePersistentMetaMap() {
685712
if (Settings.Enabled_Components.PERSISTENT_META) {
686-
DBFunc.getPersistentMeta(getUUID(), new RunnableVal<>() {
687-
@Override
688-
public void run(Map<String, byte[]> value) {
689-
try {
690-
PlotPlayer.this.metaMap = value;
691-
if (value.isEmpty()) {
692-
return;
693-
}
713+
DBFunc.getPersistentMeta(
714+
getUUID(), new RunnableVal<>() {
715+
@Override
716+
public void run(Map<String, byte[]> value) {
717+
try {
718+
PlotPlayer.this.metaMap = value;
719+
if (value.isEmpty()) {
720+
return;
721+
}
694722

695-
if (PlotPlayer.this.getAttribute("debug")) {
696-
debugModeEnabled.add(PlotPlayer.this);
697-
}
723+
if (PlotPlayer.this.getAttribute("debug")) {
724+
debugModeEnabled.add(PlotPlayer.this);
725+
}
698726

699-
if (!Settings.Teleport.ON_LOGIN) {
700-
return;
701-
}
702-
PlotAreaManager manager = PlotPlayer.this.plotAreaManager;
727+
if (!Settings.Teleport.ON_LOGIN) {
728+
return;
729+
}
730+
PlotAreaManager manager = PlotPlayer.this.plotAreaManager;
703731

704-
if (!(manager instanceof SinglePlotAreaManager)) {
705-
return;
706-
}
707-
PlotArea area = ((SinglePlotAreaManager) manager).getArea();
708-
boolean V2 = false;
709-
byte[] arr = PlotPlayer.this.getPersistentMeta("quitLoc");
710-
if (arr == null) {
711-
arr = PlotPlayer.this.getPersistentMeta("quitLocV2");
712-
if (arr == null) {
713-
return;
714-
}
715-
V2 = true;
716-
removePersistentMeta("quitLocV2");
717-
} else {
718-
removePersistentMeta("quitLoc");
719-
}
732+
if (!(manager instanceof SinglePlotAreaManager)) {
733+
return;
734+
}
735+
PlotArea area = ((SinglePlotAreaManager) manager).getArea();
736+
boolean V2 = false;
737+
byte[] arr = PlotPlayer.this.getPersistentMeta("quitLoc");
738+
if (arr == null) {
739+
arr = PlotPlayer.this.getPersistentMeta("quitLocV2");
740+
if (arr == null) {
741+
return;
742+
}
743+
V2 = true;
744+
removePersistentMeta("quitLocV2");
745+
} else {
746+
removePersistentMeta("quitLoc");
747+
}
720748

721-
if (!getMeta("teleportOnLogin", true)) {
722-
return;
723-
}
724-
ByteBuffer quitWorld = ByteBuffer.wrap(arr);
725-
final int plotX = quitWorld.getShort();
726-
final int plotZ = quitWorld.getShort();
727-
PlotId id = PlotId.of(plotX, plotZ);
728-
int x = quitWorld.getInt();
729-
int y = V2 ? quitWorld.getShort() : (quitWorld.get() & 0xFF);
730-
int z = quitWorld.getInt();
731-
Plot plot = area.getOwnedPlot(id);
732-
733-
if (plot == null) {
734-
return;
735-
}
749+
if (!getMeta("teleportOnLogin", true)) {
750+
return;
751+
}
752+
ByteBuffer quitWorld = ByteBuffer.wrap(arr);
753+
final int plotX = quitWorld.getShort();
754+
final int plotZ = quitWorld.getShort();
755+
PlotId id = PlotId.of(plotX, plotZ);
756+
int x = quitWorld.getInt();
757+
int y = V2 ? quitWorld.getShort() : (quitWorld.get() & 0xFF);
758+
int z = quitWorld.getInt();
759+
Plot plot = area.getOwnedPlot(id);
760+
761+
if (plot == null) {
762+
return;
763+
}
736764

737-
final Location location = Location.at(plot.getWorldName(), x, y, z);
738-
if (plot.isLoaded()) {
739-
TaskManager.runTask(() -> {
740-
if (getMeta("teleportOnLogin", true)) {
741-
teleport(location, TeleportCause.LOGIN);
742-
sendMessage(
743-
TranslatableCaption.of("teleport.teleported_to_plot"));
765+
final Location location = Location.at(plot.getWorldName(), x, y, z);
766+
if (plot.isLoaded()) {
767+
TaskManager.runTask(() -> {
768+
if (getMeta("teleportOnLogin", true)) {
769+
teleport(location, TeleportCause.LOGIN);
770+
sendMessage(
771+
TranslatableCaption.of("teleport.teleported_to_plot"));
772+
}
773+
});
774+
} else if (!PlotSquared.get().isMainThread(Thread.currentThread())) {
775+
if (getMeta("teleportOnLogin", true)) {
776+
plot.teleportPlayer(
777+
PlotPlayer.this,
778+
result -> TaskManager.runTask(() -> {
779+
if (getMeta("teleportOnLogin", true)) {
780+
if (plot.isLoaded()) {
781+
teleport(location, TeleportCause.LOGIN);
782+
sendMessage(TranslatableCaption
783+
.of("teleport.teleported_to_plot"));
784+
}
785+
}
786+
})
787+
);
788+
}
744789
}
745-
});
746-
} else if (!PlotSquared.get().isMainThread(Thread.currentThread())) {
747-
if (getMeta("teleportOnLogin", true)) {
748-
plot.teleportPlayer(
749-
PlotPlayer.this,
750-
result -> TaskManager.runTask(() -> {
751-
if (getMeta("teleportOnLogin", true)) {
752-
if (plot.isLoaded()) {
753-
teleport(location, TeleportCause.LOGIN);
754-
sendMessage(TranslatableCaption
755-
.of("teleport.teleported_to_plot"));
756-
}
757-
}
758-
})
759-
);
790+
} catch (Throwable e) {
791+
LOGGER.error("Error populating persistent meta for player {}", PlotPlayer.this.getName(), e);
760792
}
761793
}
762-
} catch (Throwable e) {
763-
LOGGER.error("Error populating persistent meta for player {}", PlotPlayer.this.getName(), e);
764-
}
765-
}
766794
}
767795
);
768796
}
@@ -832,7 +860,8 @@ <T> void setPersistentMeta(
832860
}
833861

834862
@SuppressWarnings("unchecked")
835-
@Nullable <T> T getPersistentMeta(final @NonNull MetaDataKey<T> key) {
863+
@Nullable
864+
<T> T getPersistentMeta(final @NonNull MetaDataKey<T> key) {
836865
final byte[] value = this.getPersistentMeta(key.toString());
837866
if (value == null) {
838867
return null;
@@ -1002,9 +1031,11 @@ public final CompletableFuture<Void> sendMessage(
10021031
if (throwable != null) {
10031032
sendMessage(
10041033
TranslatableCaption.of("errors.error"),
1005-
TagResolver.resolver("value", Tag.inserting(
1006-
Component.text("Failed to resolve asynchronous caption replacements")
1007-
))
1034+
TagResolver.resolver(
1035+
"value", Tag.inserting(
1036+
Component.text("Failed to resolve asynchronous caption replacements")
1037+
)
1038+
)
10081039
);
10091040
LOGGER.error("Failed to resolve asynchronous tagresolver(s) for " + caption, throwable);
10101041
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ public void setOwnerAbs(final @Nullable UUID owner) {
541541
*
542542
* @return World name
543543
*/
544-
public @Nullable String getWorldName() {
544+
public @NonNull String getWorldName() {
545545
return area.getWorldName();
546546
}
547547

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public SinglePlot(
5858
}
5959

6060
@Override
61-
public String getWorldName() {
61+
public @NonNull String getWorldName() {
6262
return getId().toUnderscoreSeparatedString();
6363
}
6464

0 commit comments

Comments
 (0)