Search in sources :

Example 1 with CompoundBinaryTag

use of com.sk89q.worldedit.util.nbt.CompoundBinaryTag 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 CompoundBinaryTag

use of com.sk89q.worldedit.util.nbt.CompoundBinaryTag in project FastAsyncWorldEdit by IntellectualSites.

the class PaperweightAdapter method createEntity.

@Nullable
@Override
public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state) {
    checkNotNull(location);
    checkNotNull(state);
    CraftWorld craftWorld = ((CraftWorld) location.getWorld());
    ServerLevel worldServer = craftWorld.getHandle();
    Entity createdEntity = createEntityFromId(state.getType().getId(), craftWorld.getHandle());
    if (createdEntity != null) {
        CompoundBinaryTag nativeTag = state.getNbt();
        if (nativeTag != null) {
            net.minecraft.nbt.CompoundTag tag = (net.minecraft.nbt.CompoundTag) fromNativeBinary(nativeTag);
            for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
                tag.remove(name);
            }
            readTagIntoEntity(tag, createdEntity);
        }
        createdEntity.absMoveTo(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
        worldServer.addFreshEntity(createdEntity, SpawnReason.CUSTOM);
        return createdEntity.getBukkitEntity();
    } else {
        return null;
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) StructureBlockEntity(net.minecraft.world.level.block.entity.StructureBlockEntity) Entity(net.minecraft.world.entity.Entity) CraftEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftEntity) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag) Nullable(javax.annotation.Nullable)

Example 3 with CompoundBinaryTag

use of com.sk89q.worldedit.util.nbt.CompoundBinaryTag in project FastAsyncWorldEdit by IntellectualSites.

the class NbtUtils method getInt.

/**
 * Get an integer from a tag.
 *
 * @param tag the tag to read from
 * @param key the key to look for
 * @return child tag
 * @throws InvalidFormatException if the format of the items is invalid
 * @since TODO
 */
public static int getInt(CompoundBinaryTag tag, String key) throws InvalidFormatException {
    BinaryTag childTag = tag.get(key);
    if (childTag == null) {
        throw new InvalidFormatException("Missing a \"" + key + "\" tag");
    }
    BinaryTagType<?> type = childTag.type();
    if (type == BinaryTagTypes.INT) {
        return ((IntBinaryTag) childTag).intValue();
    }
    if (type == BinaryTagTypes.BYTE) {
        return ((ByteBinaryTag) childTag).intValue();
    }
    if (type == BinaryTagTypes.SHORT) {
        return ((ShortBinaryTag) childTag).intValue();
    }
    throw new InvalidFormatException(key + " tag is not of int, short or byte tag type.");
}
Also used : ShortBinaryTag(com.sk89q.worldedit.util.nbt.ShortBinaryTag) ByteBinaryTag(com.sk89q.worldedit.util.nbt.ByteBinaryTag) BinaryTag(com.sk89q.worldedit.util.nbt.BinaryTag) IntBinaryTag(com.sk89q.worldedit.util.nbt.IntBinaryTag) ByteBinaryTag(com.sk89q.worldedit.util.nbt.ByteBinaryTag) ShortBinaryTag(com.sk89q.worldedit.util.nbt.ShortBinaryTag) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag) IntBinaryTag(com.sk89q.worldedit.util.nbt.IntBinaryTag) InvalidFormatException(com.sk89q.worldedit.world.storage.InvalidFormatException)

Example 4 with CompoundBinaryTag

use of com.sk89q.worldedit.util.nbt.CompoundBinaryTag in project FastAsyncWorldEdit by IntellectualSites.

the class WorldNativeAccess method setBlock.

default <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 position, B block, SideEffectSet sideEffects) throws WorldEditException {
    checkNotNull(position);
    checkNotNull(block);
    setCurrentSideEffectSet(sideEffects);
    int x = position.getBlockX();
    int y = position.getBlockY();
    int z = position.getBlockZ();
    // First set the block
    NC chunk = getChunk(x >> 4, z >> 4);
    NP pos = getPosition(x, y, z);
    NBS old = getBlockState(chunk, pos);
    NBS newState = toNative(block.toImmutableState());
    // change block prior to placing if it should be fixed
    if (sideEffects.shouldApply(SideEffect.VALIDATION)) {
        newState = getValidBlockForPosition(newState, pos);
    }
    NBS successState = setBlockState(chunk, pos, newState);
    boolean successful = successState != null;
    // Create the TileEntity
    if (successful || old == newState) {
        if (block instanceof BaseBlock) {
            BaseBlock baseBlock = (BaseBlock) block;
            // FAWE start - use CompoundBinaryTag over CompoundTag
            CompoundBinaryTag tag = baseBlock.getNbt();
            if (tag != null) {
                tag = tag.put(ImmutableMap.of("id", StringBinaryTag.of(baseBlock.getNbtId()), "x", IntBinaryTag.of(position.getX()), "y", IntBinaryTag.of(position.getY()), "z", IntBinaryTag.of(position.getZ())));
                // update if TE changed as well
                successful = updateTileEntity(pos, tag);
            }
        // FAWE end
        }
    }
    if (successful) {
        if (sideEffects.getState(SideEffect.LIGHTING) == SideEffect.State.ON) {
            updateLightingForBlock(pos);
        }
        markAndNotifyBlock(pos, chunk, old, newState, sideEffects);
    }
    return successful;
}
Also used : BaseBlock(com.sk89q.worldedit.world.block.BaseBlock) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag)

Example 5 with CompoundBinaryTag

use of com.sk89q.worldedit.util.nbt.CompoundBinaryTag in project FastAsyncWorldEdit by IntellectualSites.

the class AnvilChunk13 method populateEntities.

/**
 * Used to load the biomes.
 */
private void populateEntities() throws DataException {
    entities = new ArrayList<>();
    if (rootTag.get("Entities") == null) {
        return;
    }
    ListBinaryTag tags = NbtUtils.getChildTag(rootTag, "Entities", BinaryTagTypes.LIST);
    for (BinaryTag tag : tags) {
        if (!(tag instanceof CompoundBinaryTag)) {
            throw new InvalidFormatException("CompoundTag expected in Entities");
        }
        CompoundBinaryTag t = (CompoundBinaryTag) tag;
        entities.add(new BaseEntity(EntityTypes.get(t.getString("id")), LazyReference.computed(t)));
    }
}
Also used : ListBinaryTag(com.sk89q.worldedit.util.nbt.ListBinaryTag) BaseEntity(com.sk89q.worldedit.entity.BaseEntity) IntBinaryTag(com.sk89q.worldedit.util.nbt.IntBinaryTag) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag) BinaryTag(com.sk89q.worldedit.util.nbt.BinaryTag) ListBinaryTag(com.sk89q.worldedit.util.nbt.ListBinaryTag) InvalidFormatException(com.sk89q.worldedit.world.storage.InvalidFormatException) CompoundBinaryTag(com.sk89q.worldedit.util.nbt.CompoundBinaryTag)

Aggregations

CompoundBinaryTag (com.sk89q.worldedit.util.nbt.CompoundBinaryTag)20 BinaryTag (com.sk89q.worldedit.util.nbt.BinaryTag)8 IntBinaryTag (com.sk89q.worldedit.util.nbt.IntBinaryTag)7 ListBinaryTag (com.sk89q.worldedit.util.nbt.ListBinaryTag)7 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)6 InvalidFormatException (com.sk89q.worldedit.world.storage.InvalidFormatException)6 BaseEntity (com.sk89q.worldedit.entity.BaseEntity)5 DataException (com.sk89q.worldedit.world.DataException)4 IOException (java.io.IOException)4 ByteBinaryTag (com.sk89q.worldedit.util.nbt.ByteBinaryTag)3 ShortBinaryTag (com.sk89q.worldedit.util.nbt.ShortBinaryTag)3 BlockState (com.sk89q.worldedit.world.block.BlockState)3 BaseItemStack (com.sk89q.worldedit.blocks.BaseItemStack)2 BlockVector2 (com.sk89q.worldedit.math.BlockVector2)2 Location (com.sk89q.worldedit.util.Location)2 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)2 Chunk (com.sk89q.worldedit.world.chunk.Chunk)2 MissingChunkException (com.sk89q.worldedit.world.storage.MissingChunkException)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2