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