Search in sources :

Example 1 with BaseItemStack

use of com.sk89q.worldedit.blocks.BaseItemStack in project FastAsyncWorldEdit by IntellectualSites.

the class ForgeAdapter method adapt.

public static ItemStack adapt(BaseItemStack baseItemStack) {
    CompoundNBT forgeCompound = null;
    if (baseItemStack.getNbtData() != null) {
        forgeCompound = NBTConverter.toNative(baseItemStack.getNbtData());
    }
    final ItemStack itemStack = new ItemStack(adapt(baseItemStack.getType()), baseItemStack.getAmount());
    itemStack.setTag(forgeCompound);
    return itemStack;
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) ItemStack(net.minecraft.item.ItemStack)

Example 2 with BaseItemStack

use of com.sk89q.worldedit.blocks.BaseItemStack 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 3 with BaseItemStack

use of com.sk89q.worldedit.blocks.BaseItemStack in project FastAsyncWorldEdit by IntellectualSites.

the class ForgeWorld method useItem.

@Override
public boolean useItem(BlockVector3 position, BaseItem item, Direction face) {
    ItemStack stack = ForgeAdapter.adapt(new BaseItemStack(item.getType(), item.getNbtData(), 1));
    ServerWorld world = (ServerWorld) getWorld();
    final WorldEditFakePlayer fakePlayer;
    try {
        fakePlayer = fakePlayers.get(world);
    } catch (ExecutionException ignored) {
        return false;
    }
    fakePlayer.setHeldItem(Hand.MAIN_HAND, stack);
    fakePlayer.setLocationAndAngles(position.getBlockX(), position.getBlockY(), position.getBlockZ(), (float) face.toVector().toYaw(), (float) face.toVector().toPitch());
    final BlockPos blockPos = ForgeAdapter.toBlockPos(position);
    final BlockRayTraceResult rayTraceResult = new BlockRayTraceResult(ForgeAdapter.toVec3(position), ForgeAdapter.adapt(face), blockPos, false);
    ItemUseContext itemUseContext = new ItemUseContext(fakePlayer, Hand.MAIN_HAND, rayTraceResult);
    ActionResultType used = stack.onItemUse(itemUseContext);
    if (used != ActionResultType.SUCCESS) {
        // try activating the block
        ActionResultType resultType = getWorld().getBlockState(blockPos).onBlockActivated(world, fakePlayer, Hand.MAIN_HAND, rayTraceResult);
        if (resultType.isSuccessOrConsume()) {
            used = resultType;
        } else {
            used = stack.getItem().onItemRightClick(world, fakePlayer, Hand.MAIN_HAND).getType();
        }
    }
    return used == ActionResultType.SUCCESS;
}
Also used : ServerWorld(net.minecraft.world.server.ServerWorld) ItemUseContext(net.minecraft.item.ItemUseContext) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) ActionResultType(net.minecraft.util.ActionResultType) BlockPos(net.minecraft.util.math.BlockPos) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) ItemStack(net.minecraft.item.ItemStack) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with BaseItemStack

use of com.sk89q.worldedit.blocks.BaseItemStack in project FastAsyncWorldEdit by IntellectualSites.

the class PaperweightAdapter method simulateItemUse.

@Override
public boolean simulateItemUse(org.bukkit.World world, BlockVector3 position, BaseItem item, Direction face) {
    CraftWorld craftWorld = (CraftWorld) world;
    ServerLevel worldServer = craftWorld.getHandle();
    ItemStack stack = CraftItemStack.asNMSCopy(BukkitAdapter.adapt(item instanceof BaseItemStack ? ((BaseItemStack) item) : new BaseItemStack(item.getType(), item.getNbtData(), 1)));
    stack.setTag((net.minecraft.nbt.CompoundTag) fromNative(item.getNbtData()));
    PaperweightFakePlayer fakePlayer;
    try {
        fakePlayer = fakePlayers.get(worldServer);
    } catch (ExecutionException ignored) {
        return false;
    }
    fakePlayer.setItemInHand(InteractionHand.MAIN_HAND, stack);
    fakePlayer.absMoveTo(position.getBlockX(), position.getBlockY(), position.getBlockZ(), (float) face.toVector().toYaw(), (float) face.toVector().toPitch());
    final BlockPos blockPos = new BlockPos(position.getBlockX(), position.getBlockY(), position.getBlockZ());
    final Vec3 blockVec = Vec3.atLowerCornerOf(blockPos);
    final net.minecraft.core.Direction enumFacing = adapt(face);
    BlockHitResult rayTrace = new BlockHitResult(blockVec, enumFacing, blockPos, false);
    UseOnContext context = new UseOnContext(fakePlayer, InteractionHand.MAIN_HAND, rayTrace);
    InteractionResult result = stack.useOn(context, InteractionHand.MAIN_HAND);
    if (result != InteractionResult.SUCCESS) {
        if (worldServer.getBlockState(blockPos).use(worldServer, fakePlayer, InteractionHand.MAIN_HAND, rayTrace).consumesAction()) {
            result = InteractionResult.SUCCESS;
        } else {
            result = stack.getItem().use(worldServer, fakePlayer, InteractionHand.MAIN_HAND).getResult();
        }
    }
    return result == InteractionResult.SUCCESS;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) UseOnContext(net.minecraft.world.item.context.UseOnContext) InteractionResult(net.minecraft.world.InteractionResult) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) Vec3(net.minecraft.world.phys.Vec3) BlockPos(net.minecraft.core.BlockPos) CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) ItemStack(net.minecraft.world.item.ItemStack) ExecutionException(java.util.concurrent.ExecutionException) BlockHitResult(net.minecraft.world.phys.BlockHitResult) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld)

Example 5 with BaseItemStack

use of com.sk89q.worldedit.blocks.BaseItemStack in project FastAsyncWorldEdit by IntellectualSites.

the class PaperweightAdapter method adapt.

@Override
public org.bukkit.inventory.ItemStack adapt(BaseItemStack item) {
    ItemStack stack = new ItemStack(Registry.ITEM.get(ResourceLocation.tryParse(item.getType().getId())), item.getAmount());
    stack.setTag(((net.minecraft.nbt.CompoundTag) fromNative(item.getNbtData())));
    return CraftItemStack.asCraftMirror(stack);
}
Also used : CraftItemStack(org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack) BaseItemStack(com.sk89q.worldedit.blocks.BaseItemStack) ItemStack(net.minecraft.world.item.ItemStack)

Aggregations

BaseItemStack (com.sk89q.worldedit.blocks.BaseItemStack)13 ItemStack (net.minecraft.item.ItemStack)4 ExecutionException (java.util.concurrent.ExecutionException)3 ItemStack (net.minecraft.world.item.ItemStack)3 CraftItemStack (org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack)3 CompoundBinaryTag (com.sk89q.worldedit.util.nbt.CompoundBinaryTag)2 ItemType (com.sk89q.worldedit.world.item.ItemType)2 BlockPos (net.minecraft.util.math.BlockPos)2 ItemStack (org.bukkit.inventory.ItemStack)2 BaseItem (com.sk89q.worldedit.blocks.BaseItem)1 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)1 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)1 NoMatchException (com.sk89q.worldedit.extension.input.NoMatchException)1 OutOfSpaceException (com.sk89q.worldedit.extent.inventory.OutOfSpaceException)1 IOException (java.io.IOException)1 BlockPos (net.minecraft.core.BlockPos)1 ItemUsageContext (net.minecraft.item.ItemUsageContext)1 ItemUseContext (net.minecraft.item.ItemUseContext)1 CompoundNBT (net.minecraft.nbt.CompoundNBT)1 ServerLevel (net.minecraft.server.level.ServerLevel)1