001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.util; 020 021import com.sk89q.worldedit.WorldEdit; 022import com.sk89q.worldedit.extension.input.InputParseException; 023import com.sk89q.worldedit.extension.input.ParserContext; 024import com.sk89q.worldedit.internal.registry.InputParser; 025import com.sk89q.worldedit.world.block.BaseBlock; 026import com.sk89q.worldedit.world.block.BlockState; 027import com.sk89q.worldedit.world.block.BlockType; 028import com.sk89q.worldedit.world.block.BlockTypes; 029import com.sk89q.worldedit.world.block.FuzzyBlockState; 030import com.sk89q.worldedit.world.registry.LegacyMapper; 031import org.checkerframework.checker.nullness.qual.NonNull; 032import org.checkerframework.checker.nullness.qual.Nullable; 033 034import java.util.List; 035 036/** 037 * {@link BlockState} related utility methods 038 */ 039public final class BlockUtil { 040 041 private static final ParserContext PARSER_CONTEXT = new ParserContext(); 042 private static final InputParser<BaseBlock> PARSER; 043 044 static { 045 PARSER_CONTEXT.setRestricted(false); 046 PARSER_CONTEXT.setPreferringWildcard(false); 047 PARSER_CONTEXT.setTryLegacy(true); 048 List<InputParser<BaseBlock>> parsers = WorldEdit.getInstance().getBlockFactory().getParsers(); 049 PARSER = parsers.get(parsers.size() - 1); // Default parser is always at the end 050 } 051 052 private BlockUtil() { 053 } 054 055 /** 056 * Get a {@link BlockState} from a legacy id 057 * 058 * @param id Legacy ID 059 * @return Block state, or {@code null} 060 */ 061 public static @Nullable BlockState get(final int id) { 062 return LegacyMapper.getInstance().getBlockFromLegacy(id); 063 } 064 065 /** 066 * Get a {@link BlockState} from a legacy id-data pair 067 * 068 * @param id Legacy ID 069 * @param data Legacy data 070 * @return Block state, or {@code null} 071 */ 072 public static @Nullable BlockState get(final int id, final int data) { 073 return LegacyMapper.getInstance().getBlockFromLegacy(id, data); 074 } 075 076 /** 077 * Get a {@link BlockState} from its ID 078 * 079 * @param id String or integer ID 080 * @return Parsed block state, or {@code null} if none 081 * could be parsed 082 */ 083 public static @Nullable BlockState get(final @NonNull String id) { 084 if (id.length() == 1 && id.charAt(0) == '*') { 085 return FuzzyBlockState.builder().type(BlockTypes.AIR).build(); 086 } 087 String mutableId; 088 mutableId = id.toLowerCase(); 089 BlockType type = BlockTypes.get(mutableId); 090 if (type != null) { 091 return type.getDefaultState(); 092 } 093 if (Character.isDigit(mutableId.charAt(0))) { 094 String[] split = mutableId.split(":"); 095 if (MathMan.isInteger(split[0])) { 096 if (split.length == 2) { 097 if (MathMan.isInteger(split[1])) { 098 return get(Integer.parseInt(split[0]), Integer.parseInt(split[1])); 099 } 100 } else { 101 return get(Integer.parseInt(split[0])); 102 } 103 } 104 } 105 try { 106 BaseBlock block = PARSER.parseFromInput(mutableId, PARSER_CONTEXT); 107 return block.toImmutableState(); 108 } catch (InputParseException e) { 109 return null; 110 } 111 } 112 113}