From cfd71457d28217ca8c1d034adf81f36b45d46b93 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] 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());