Clean up BlockUtil

This commit is contained in:
Alexander Söderberg 2020-07-19 14:49:26 +02:00
parent b3a63123fd
commit cfd71457d2

View File

@ -35,13 +35,20 @@ import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.block.FuzzyBlockState; import com.sk89q.worldedit.world.block.FuzzyBlockState;
import com.sk89q.worldedit.world.registry.LegacyMapper; import com.sk89q.worldedit.world.registry.LegacyMapper;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map; import java.util.Map;
/**
* {@link BlockState} related utility methods
*/
public final class BlockUtil { public final class BlockUtil {
private static ParserContext PARSER_CONTEXT = new ParserContext();
private static InputParser<BaseBlock> PARSER; private static final ParserContext PARSER_CONTEXT = new ParserContext();
private static final InputParser<BaseBlock> PARSER;
static { static {
PARSER_CONTEXT.setRestricted(false); PARSER_CONTEXT.setRestricted(false);
@ -53,15 +60,35 @@ public final class BlockUtil {
private 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); 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); 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) == '*') { if (id.length() == 1 && id.charAt(0) == '*') {
return FuzzyBlockState.builder().type(BlockTypes.AIR).build(); 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(",(?![^\\(\\[]*[\\]\\)])"); * Parse a comma delimited list of block states
BlockState[] result = new BlockState[split.length]; *
* @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++) { for (int i = 0; i < split.length; i++) {
result[i] = get(split[i]); result[i] = get(split[i]);
} }
return result; return result;
} }
public static BlockState deserialize(@Nonnull final Map<String, Object> 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<String, Object> map) {
if (map.containsKey("material")) { if (map.containsKey("material")) {
final Object object = map.get("material"); final Object object = map.get("material");
return get(object.toString()); return get(object.toString());