use of com.sk89q.worldedit.world.item.ItemType in project FastAsyncWorldEdit by IntellectualSites.
the class DefaultItemParser method parseFromInput.
@Override
public BaseItem parseFromInput(String input, ParserContext context) throws InputParseException {
ItemType itemType;
CompoundBinaryTag itemNbtData = null;
BaseItem item = null;
// Legacy matcher
if (context.isTryingLegacy()) {
try {
String[] split = input.split(":");
if (split.length == 0) {
throw new InputParseException(Caption.of("worldedit.error.parser.invalid-colon"));
} else if (split.length == 1) {
itemType = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(split[0]));
} else {
itemType = LegacyMapper.getInstance().getItemFromLegacy(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
}
if (itemType != null) {
item = new BaseItem(itemType);
}
} catch (NumberFormatException ignored) {
}
}
if (item == null) {
String typeString;
String nbtString = null;
int nbtStart = input.indexOf('{');
if (nbtStart == -1) {
typeString = input;
} else {
typeString = input.substring(0, nbtStart);
if (nbtStart + 1 >= input.length()) {
throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.hanging-lbracket", TextComponent.of(nbtStart)));
}
int stateEnd = input.lastIndexOf('}');
if (stateEnd < 0) {
throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.missing-rbracket"));
}
nbtString = input.substring(nbtStart);
}
if ("hand".equalsIgnoreCase(typeString)) {
BaseItemStack heldItem = getItemInHand(context.requireActor(), HandSide.MAIN_HAND);
// FAWE start
itemType = heldItem.getType();
itemNbtData = heldItem.getNbt();
// FAWE end
} else if ("offhand".equalsIgnoreCase(typeString)) {
BaseItemStack heldItem = getItemInHand(context.requireActor(), HandSide.OFF_HAND);
// FAWE start
itemType = heldItem.getType();
itemNbtData = heldItem.getNbt();
// FAWE end
} else {
itemType = ItemTypes.get(typeString.toLowerCase(Locale.ROOT));
}
// FAWE start
if (itemType == null) {
throw new NoMatchException(TranslatableComponent.of("worldedit.error.unknown-item", TextComponent.of(input)));
}
if (nbtString != null) {
try {
CompoundBinaryTag otherTag = TagStringIO.get().asCompound(nbtString);
if (itemNbtData == null) {
itemNbtData = otherTag;
} else {
itemNbtData.put(NbtUtils.getCompoundBinaryTagValues(otherTag));
}
} catch (IOException e) {
throw new NoMatchException(TranslatableComponent.of("worldedit.error.invalid-nbt", TextComponent.of(input), TextComponent.of(e.getMessage())));
}
}
item = new BaseItem(itemType, itemNbtData == null ? null : LazyReference.computed(itemNbtData));
}
return item;
}
use of com.sk89q.worldedit.world.item.ItemType in project FastAsyncWorldEdit by IntellectualSites.
the class SelectionCommands method wand.
@Command(name = "/wand", desc = "Get the wand object")
@CommandPermissions("worldedit.wand")
public void wand(Player player, LocalSession session, @Switch(name = 'n', desc = "Get a navigation wand") boolean navWand) throws WorldEditException {
// FAWE start
session.loadDefaults(player, true);
// FAWE end
String wandId = navWand ? session.getNavWandItem() : session.getWandItem();
if (wandId == null) {
wandId = navWand ? we.getConfiguration().navigationWand : we.getConfiguration().wandItem;
}
ItemType itemType = ItemTypes.parse(wandId);
if (itemType == null) {
player.print(Caption.of("worldedit.wand.invalid"));
return;
}
player.giveItem(new BaseItemStack(itemType, 1));
// FAWE start - instance-iate session
if (navWand) {
session.setTool(itemType, NavigationWand.INSTANCE);
player.print(Caption.of("worldedit.wand.navwand.info"));
} else {
session.setTool(itemType, SelectionWand.INSTANCE);
player.print(Caption.of("worldedit.wand.selwand.info"));
// FAWE end
}
}
use of com.sk89q.worldedit.world.item.ItemType in project FastAsyncWorldEdit by IntellectualSites.
the class LegacyMapper method loadFromResource.
/**
* Attempt to load the data from file.
*
* @throws IOException thrown on I/O error
*/
private void loadFromResource() throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
Gson gson = gsonBuilder.disableHtmlEscaping().create();
URL url = resourceLoader.getResource(LegacyMapper.class, "legacy.json");
if (url == null) {
throw new IOException("Could not find legacy.json");
}
String data = Resources.toString(url, Charset.defaultCharset());
LegacyDataFile dataFile = gson.fromJson(data, new TypeToken<LegacyDataFile>() {
}.getType());
DataFixer fixer = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataFixer();
ParserContext parserContext = new ParserContext();
parserContext.setPreferringWildcard(false);
parserContext.setRestricted(false);
// This is legacy. Don't match itself.
parserContext.setTryLegacy(false);
for (Map.Entry<String, String> blockEntry : dataFile.blocks.entrySet()) {
String id = blockEntry.getKey();
final String value = blockEntry.getValue();
// FAWE start
Integer combinedId = getCombinedId(blockEntry.getKey());
blockEntries.put(id, value);
// FAWE end
BlockState state = null;
// FAWE start
try {
state = BlockState.get(null, blockEntry.getValue());
BlockType type = state.getBlockType();
if (type.hasProperty(PropertyKey.WATERLOGGED)) {
state = state.with(PropertyKey.WATERLOGGED, false);
}
} catch (InputParseException f) {
BlockFactory blockFactory = WorldEdit.getInstance().getBlockFactory();
// if fixer is available, try using that first, as some old blocks that were renamed share names with new blocks
if (fixer != null) {
try {
String newEntry = fixer.fixUp(DataFixer.FixTypes.BLOCK_STATE, value, Constants.DATA_VERSION_MC_1_13_2);
state = blockFactory.parseFromInput(newEntry, parserContext).toImmutableState();
} catch (InputParseException ignored) {
}
}
// if it's still null, the fixer was unavailable or failed
if (state == null) {
try {
state = blockFactory.parseFromInput(value, parserContext).toImmutableState();
} catch (InputParseException ignored) {
}
}
// if it's still null, both fixer and default failed
if (state == null) {
LOGGER.error("Unknown block: {}. Neither the DataFixer nor defaulting worked to recognize this block.", value);
} else {
// it's not null so one of them succeeded, now use it
blockToStringMap.put(state, id);
stringToBlockMap.put(id, state);
}
}
// FAWE start
if (state != null) {
blockArr[combinedId] = state.getInternalId();
blockStateToLegacyId4Data.put(state.getInternalId(), combinedId);
blockStateToLegacyId4Data.putIfAbsent(state.getInternalBlockTypeId(), combinedId);
}
}
for (int id = 0; id < 256; id++) {
int combinedId = id << 4;
int base = blockArr[combinedId];
if (base != 0) {
for (int data_ = 0; data_ < 16; data_++, combinedId++) {
if (blockArr[combinedId] == 0) {
blockArr[combinedId] = base;
}
}
}
}
for (Map.Entry<String, String> itemEntry : dataFile.items.entrySet()) {
String id = itemEntry.getKey();
String value = itemEntry.getValue();
ItemType type = ItemTypes.get(value);
if (type == null && fixer != null) {
value = fixer.fixUp(DataFixer.FixTypes.ITEM_TYPE, value, Constants.DATA_VERSION_MC_1_13_2);
type = ItemTypes.get(value);
}
if (type == null) {
LOGGER.error("Unknown item: {}. Neither the DataFixer nor defaulting worked to recognize this item.", value);
} else {
try {
itemMap.put(getCombinedId(id), type);
} catch (Exception ignored) {
}
}
}
}
use of com.sk89q.worldedit.world.item.ItemType in project WorldGuard by EngineHub.
the class WorldConfiguration method convertLegacyItem.
public String convertLegacyItem(String legacy) {
String[] splitter = legacy.split(":", 2);
try {
int id;
byte data;
if (splitter.length == 1) {
id = Integer.parseInt(splitter[0]);
data = 0;
} else {
id = Integer.parseInt(splitter[0]);
data = Byte.parseByte(splitter[1]);
}
ItemType legacyItem = LegacyMapper.getInstance().getItemFromLegacy(id, data);
if (legacyItem != null) {
return legacyItem.getId();
}
} catch (NumberFormatException ignored) {
}
final ItemType itemType = ItemTypes.get(legacy);
if (itemType != null) {
return itemType.getId();
}
return null;
}
use of com.sk89q.worldedit.world.item.ItemType in project WorldGuard by EngineHub.
the class TargetMatcherParser method fromInput.
public TargetMatcher fromInput(String input) throws TargetMatcherParseException {
input = input.toLowerCase().trim();
BlockType blockType = BlockTypes.get(input);
if (blockType != null) {
if (blockType.hasItemType()) {
return new ItemBlockMatcher(blockType);
} else {
return new BlockMatcher(blockType);
}
} else {
ItemType itemType = ItemTypes.get(input);
if (itemType == null) {
throw new TargetMatcherParseException("Unknown block or item name: " + input);
}
return new ItemMatcher(itemType);
}
}
Aggregations