From 2d518dfe2b5d766559844fa4762219be6c18ac59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sun, 19 Jul 2020 14:49:26 +0200 Subject: [PATCH 1/4] Clean up BlockUtil --- .../com/plotsquared/core/util/BlockUtil.java | 58 ++++++++++++++++--- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/util/BlockUtil.java b/Core/src/main/java/com/plotsquared/core/util/BlockUtil.java index a0f4e4f50..6d4c4079a 100644 --- a/Core/src/main/java/com/plotsquared/core/util/BlockUtil.java +++ b/Core/src/main/java/com/plotsquared/core/util/BlockUtil.java @@ -35,13 +35,20 @@ import com.sk89q.worldedit.world.block.BlockType; import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.FuzzyBlockState; import com.sk89q.worldedit.world.registry.LegacyMapper; + +import javax.annotation.Nonnegative; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.Map; +/** + * {@link BlockState} related utility methods + */ public final class BlockUtil { - private static ParserContext PARSER_CONTEXT = new ParserContext(); - private static InputParser PARSER; + + private static final ParserContext PARSER_CONTEXT = new ParserContext(); + private static final InputParser PARSER; static { PARSER_CONTEXT.setRestricted(false); @@ -53,15 +60,35 @@ public final class BlockUtil { private BlockUtil() { } - public static BlockState get(int id) { + /** + * Get a {@link BlockState} from a legacy id + * + * @param id Legacy ID + * @return Block state, or {@code null} + */ + @Nullable public static BlockState get(@Nonnegative final int id) { return LegacyMapper.getInstance().getBlockFromLegacy(id); } - public static BlockState get(int id, int data) { + /** + * Get a {@link BlockState} from a legacy id-data pair + * + * @param id Legacy ID + * @param data Legacy data + * @return Block state, or {@code null} + */ + @Nullable public static BlockState get(@Nonnegative final int id, final int data) { return LegacyMapper.getInstance().getBlockFromLegacy(id, data); } - public static BlockState get(String id) { + /** + * Get a {@link BlockState} from its ID + * + * @param id String or integer ID + * @return Parsed block state, or {@code null} if none + * could be parsed + */ + @Nullable public static BlockState get(@Nonnull String id) { if (id.length() == 1 && id.charAt(0) == '*') { return FuzzyBlockState.builder().type(BlockTypes.AIR).build(); } @@ -90,16 +117,29 @@ public final class BlockUtil { } } - public static BlockState[] parse(String commaDelimited) { - String[] split = commaDelimited.split(",(?![^\\(\\[]*[\\]\\)])"); - BlockState[] result = new BlockState[split.length]; + /** + * Parse a comma delimited list of block states + * + * @param commaDelimited List of block states + * @return Parsed block states + */ + @Nonnull public static BlockState[] parse(@Nonnull final String commaDelimited) { + final String[] split = commaDelimited.split(",(?![^\\(\\[]*[\\]\\)])"); + final BlockState[] result = new BlockState[split.length]; for (int i = 0; i < split.length; i++) { result[i] = get(split[i]); } return result; } - public static BlockState deserialize(@Nonnull final Map map) { + /** + * Deserialize a serialized {@link BlockState} + * + * @param map Serialized block state + * @return Deserialized block state, or {@code null} if the map is + * not a properly serialized block state + */ + @Nullable public static BlockState deserialize(@Nonnull final Map map) { if (map.containsKey("material")) { final Object object = map.get("material"); return get(object.toString()); From 87285e08dcd0f27dd6994956d60947fdb358b7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Thu, 23 Jul 2020 14:11:34 +0200 Subject: [PATCH 2/4] Fix plot ID issues --- .../com/plotsquared/core/plot/PlotId.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/plot/PlotId.java b/Core/src/main/java/com/plotsquared/core/plot/PlotId.java index 27004ba3c..d74d7aeec 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/PlotId.java +++ b/Core/src/main/java/com/plotsquared/core/plot/PlotId.java @@ -36,7 +36,7 @@ import java.util.Iterator; * Plot (X,Y) tuples for plot locations * within a plot area */ -public class PlotId { +public final class PlotId { private final int x; private final int y; @@ -48,7 +48,7 @@ public class PlotId { * @param x The plot x coordinate * @param y The plot y coordinate */ - private PlotId(int x, int y) { + private PlotId(final int x, final int y) { this.x = x; this.y = y; this.hash = (this.getX() << 16) | (this.getY() & 0xFFFF); @@ -61,20 +61,21 @@ public class PlotId { * @param y The plot y coordinate */ @Nonnull public static PlotId of(final int x, final int y) { - return PlotId.of(x, y); + return new PlotId(x, y); } /** * Get a Plot Id based on a string * * @param string to create id from - * @return the PlotId representation of the arguement + * @return the PlotId representation of the argument * @throws IllegalArgumentException if the string does not contain a valid PlotId */ - @Nonnull public static PlotId fromString(@Nonnull String string) { - PlotId plot = fromStringOrNull(string); - if (plot == null) + @Nonnull public static PlotId fromString(@Nonnull final String string) { + final PlotId plot = fromStringOrNull(string); + if (plot == null) { throw new IllegalArgumentException("Cannot create PlotID. String invalid."); + } return plot; } @@ -84,8 +85,8 @@ public class PlotId { * @param string ID string * @return Plot ID, or {@code null} if none could be parsed */ - @Nullable public static PlotId fromStringOrNull(@Nonnull String string) { - String[] parts = string.split("[;,.]"); + @Nullable public static PlotId fromStringOrNull(@Nonnull final String string) { + final String[] parts = string.split("[;,.]"); if (parts.length < 2) { return null; } @@ -94,7 +95,7 @@ public class PlotId { try { x = Integer.parseInt(parts[0]); y = Integer.parseInt(parts[1]); - } catch (NumberFormatException ignored) { + } catch (final NumberFormatException ignored) { return null; } return of(x, y); @@ -126,7 +127,7 @@ public class PlotId { * @return X component */ public int getX() { - return this.getX(); + return this.x; } /** @@ -135,7 +136,7 @@ public class PlotId { * @return Y component */ public int getY() { - return this.getY(); + return this.y; } /** @@ -144,8 +145,8 @@ public class PlotId { * @return Next plot ID */ @Nonnull public PlotId getNextId() { - int absX = Math.abs(x); - int absY = Math.abs(y); + final int absX = Math.abs(x); + final int absY = Math.abs(y); if (absX > absY) { if (x > 0) { return PlotId.of(x, y + 1); From 6ae1d28f80959f6af0d55a587ae699e04d099990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Thu, 23 Jul 2020 14:56:06 +0200 Subject: [PATCH 3/4] Fix WorldManager injection --- .../src/main/java/com/plotsquared/bukkit/BukkitPlatform.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java index 01cf8114e..23ed985c9 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java @@ -30,6 +30,7 @@ import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Key; import com.google.inject.Stage; +import com.google.inject.TypeLiteral; import com.plotsquared.bukkit.generator.BukkitPlotGenerator; import com.plotsquared.bukkit.inject.BackupModule; import com.plotsquared.bukkit.inject.BukkitModule; @@ -1144,4 +1145,8 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass; return BukkitWorld.of(worldName); } + @Override @Nonnull public PlatformWorldManager getWorldManager() { + return getInjector().getInstance(Key.get(new TypeLiteral>() {})); + } + } From 63ce3292aa530e16110c7aa5309e23b9af2c864c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Thu, 23 Jul 2020 15:03:27 +0200 Subject: [PATCH 4/4] Fix PlayerManager injection --- .../main/java/com/plotsquared/bukkit/BukkitPlatform.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java index 23ed985c9..bb27113c5 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java @@ -43,6 +43,7 @@ import com.plotsquared.bukkit.listener.SingleWorldListener; import com.plotsquared.bukkit.listener.WorldEvents; import com.plotsquared.bukkit.placeholder.PlaceholderFormatter; import com.plotsquared.bukkit.placeholder.Placeholders; +import com.plotsquared.bukkit.player.BukkitPlayer; import com.plotsquared.bukkit.player.BukkitPlayerManager; import com.plotsquared.bukkit.util.BukkitChatManager; import com.plotsquared.bukkit.util.BukkitUtil; @@ -105,6 +106,7 @@ import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.FileUtils; import com.plotsquared.core.util.PermHandler; import com.plotsquared.core.util.PlatformWorldManager; +import com.plotsquared.core.util.PlayerManager; import com.plotsquared.core.util.PremiumVerification; import com.plotsquared.core.util.ReflectionUtils; import com.plotsquared.core.util.SetupUtils; @@ -1141,7 +1143,7 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass; return names; } - @Override public com.plotsquared.core.location.World getPlatformWorld(@Nonnull final String worldName) { + @Override @Nonnull public com.plotsquared.core.location.World getPlatformWorld(@Nonnull final String worldName) { return BukkitWorld.of(worldName); } @@ -1149,4 +1151,8 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass; return getInjector().getInstance(Key.get(new TypeLiteral>() {})); } + @Override @Nonnull public PlayerManager, ? extends Player> getPlayerManager() { + return getInjector().getInstance(Key.get(new TypeLiteral>() {})); + } + }