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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations