use of net.minecraft.nbt.CompoundTag in project MyPet by xXKeyleXx.
the class ConfigItem method load.
@Override
public void load(MaterialHolder material, String data) {
ResourceLocation key = new ResourceLocation(material.getId());
Item item = Registry.ITEM.get(key);
// TODO AIR now?
if (item == null) {
Block block = Registry.BLOCK.get(key);
item = block.asItem();
}
if (item == null) {
return;
}
net.minecraft.world.item.ItemStack is = new net.minecraft.world.item.ItemStack(item, 1);
if (data != null) {
CompoundTag tag = null;
String nbtString = data.trim();
if (nbtString.startsWith("{") && nbtString.endsWith("}")) {
try {
tag = TagParser.parseTag(nbtString);
} catch (Exception e) {
MyPetApi.getLogger().warning("Error" + ChatColor.RESET + " in config: " + ChatColor.UNDERLINE + e.getLocalizedMessage() + ChatColor.RESET + " caused by:");
MyPetApi.getLogger().warning(item.getDescriptionId() + " " + nbtString);
}
if (tag != null) {
is.setTag(tag);
}
}
}
this.item = CraftItemStack.asCraftMirror(is);
}
use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.
the class SpongeBlockEntityArchetypeBuilder method blockEntity.
@Override
public BlockEntityArchetype.Builder blockEntity(final BlockEntity blockEntity) {
if (!(Objects.requireNonNull(blockEntity, "BlockEntity cannot be null!") instanceof net.minecraft.world.level.block.entity.BlockEntity)) {
throw new IllegalArgumentException("BlockEntity is not compatible with this implementation!");
}
final CompoundTag compound = new CompoundTag();
((net.minecraft.world.level.block.entity.BlockEntity) blockEntity).save(compound);
compound.remove(Constants.Sponge.BlockSnapshot.TILE_ENTITY_POSITION_X);
compound.remove(Constants.Sponge.BlockSnapshot.TILE_ENTITY_POSITION_Y);
compound.remove(Constants.Sponge.BlockSnapshot.TILE_ENTITY_POSITION_Z);
compound.remove(Constants.Item.BLOCK_ENTITY_ID);
this.data = NBTTranslator.INSTANCE.translate(compound);
this.blockState = blockEntity.block();
this.type = blockEntity.type();
return this;
}
use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.
the class NbtLegacyHoverEventSerializer method serializeShowItem.
@Override
@NonNull
public Component serializeShowItem(final HoverEvent.@NonNull ShowItem input) throws IOException {
final CompoundTag tag = new CompoundTag();
tag.putString(NbtLegacyHoverEventSerializer.ITEM_TYPE, input.item().asString());
tag.putByte(NbtLegacyHoverEventSerializer.ITEM_COUNT, (byte) input.count());
if (input.nbt() != null) {
try {
tag.put(NbtLegacyHoverEventSerializer.ITEM_TAG, input.nbt().get(NbtLegacyHoverEventSerializer.SNBT_CODEC));
} catch (final CommandSyntaxException ex) {
throw new IOException(ex);
}
}
return Component.text(NbtLegacyHoverEventSerializer.SNBT_CODEC.encode(tag));
}
use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.
the class NbtLegacyHoverEventSerializer method deserializeShowItem.
@Override
public HoverEvent.ShowItem deserializeShowItem(final Component input) throws IOException {
final String rawContent = PlainTextComponentSerializer.plainText().serialize(input);
try {
final CompoundTag contents = NbtLegacyHoverEventSerializer.SNBT_CODEC.decode(rawContent);
final CompoundTag tag = contents.getCompound(NbtLegacyHoverEventSerializer.ITEM_TAG);
return HoverEvent.ShowItem.of(Key.key(contents.getString(NbtLegacyHoverEventSerializer.ITEM_TYPE)), contents.contains(NbtLegacyHoverEventSerializer.ITEM_COUNT) ? contents.getByte(NbtLegacyHoverEventSerializer.ITEM_COUNT) : 1, tag.isEmpty() ? null : BinaryTagHolder.encode(tag, NbtLegacyHoverEventSerializer.SNBT_CODEC));
} catch (final CommandSyntaxException ex) {
throw new IOException(ex);
}
}
use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.
the class MobSpawnerData method setEntities.
private static void setEntities(final BaseSpawnerAccessor logic, final WeightedTable<EntityArchetype> table) {
logic.accessor$spawnPotentials().clear();
for (final TableEntry<EntityArchetype> entry : table) {
if (!(entry instanceof WeightedObject)) {
continue;
}
final WeightedObject<EntityArchetype> object = (WeightedObject<EntityArchetype>) entry;
final CompoundTag compound = NBTTranslator.INSTANCE.translate(object.get().entityData());
if (!compound.contains(Constants.Entity.ENTITY_TYPE_ID)) {
final ResourceKey key = (ResourceKey) (Object) net.minecraft.world.entity.EntityType.getKey((net.minecraft.world.entity.EntityType<?>) object.get().type());
compound.putString(Constants.Entity.ENTITY_TYPE_ID, key.toString());
}
logic.accessor$spawnPotentials().add(new SpawnData((int) entry.weight(), compound));
}
}
Aggregations