Search in sources :

Example 1 with ItemType

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;
}
Also used : InputParseException(com.sk89q.worldedit.extension.input.InputParseException) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) ItemType(com.sk89q.worldedit.world.item.ItemType) IOException(java.io.IOException) NoMatchException(com.sk89q.worldedit.extension.input.NoMatchException) BaseItem(com.sk89q.worldedit.blocks.BaseItem) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag)

Example 2 with ItemType

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
    }
}
Also used : BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) ItemType(com.sk89q.worldedit.world.item.ItemType) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 3 with ItemType

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) {
            }
        }
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) BlockFactory(com.sk89q.worldedit.extension.factory.BlockFactory) VectorAdapter(com.sk89q.worldedit.util.gson.VectorAdapter) ItemType(com.sk89q.worldedit.world.item.ItemType) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) IOException(java.io.IOException) InputParseException(com.sk89q.worldedit.extension.input.InputParseException) BlockState(com.sk89q.worldedit.world.block.BlockState) BlockType(com.sk89q.worldedit.world.block.BlockType) TypeToken(com.google.gson.reflect.TypeToken) DataFixer(com.sk89q.worldedit.world.DataFixer) ParserContext(com.sk89q.worldedit.extension.input.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashBiMap(com.google.common.collect.HashBiMap) Int2ObjectArrayMap(it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap)

Example 4 with ItemType

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;
}
Also used : ItemType(com.sk89q.worldedit.world.item.ItemType)

Example 5 with ItemType

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);
    }
}
Also used : BlockType(com.sk89q.worldedit.world.block.BlockType) ItemType(com.sk89q.worldedit.world.item.ItemType)

Aggregations

ItemType (com.sk89q.worldedit.world.item.ItemType)5 BaseItemStack (com.sk89q.worldedit.blocks.BaseItemStack)2 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)2 BlockType (com.sk89q.worldedit.world.block.BlockType)2 IOException (java.io.IOException)2 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 TypeToken (com.google.gson.reflect.TypeToken)1 BaseItem (com.sk89q.worldedit.blocks.BaseItem)1 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)1 BlockFactory (com.sk89q.worldedit.extension.factory.BlockFactory)1 NoMatchException (com.sk89q.worldedit.extension.input.NoMatchException)1 ParserContext (com.sk89q.worldedit.extension.input.ParserContext)1 VectorAdapter (com.sk89q.worldedit.util.gson.VectorAdapter)1 CompoundBinaryTag (com.sk89q.worldedit.util.nbt.CompoundBinaryTag)1 DataFixer (com.sk89q.worldedit.world.DataFixer)1 BlockState (com.sk89q.worldedit.world.block.BlockState)1 Int2ObjectArrayMap (it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap)1