Search in sources :

Example 21 with NbtList

use of net.minecraft.nbt.NbtList in project Liminal-Library by LudoCrypt.

the class NbtPlacerUtil method spawnEntities.

public NbtPlacerUtil spawnEntities(ChunkRegion region, BlockPos pos, BlockRotation rotation) {
    this.entities.forEach((nbtElement) -> {
        NbtCompound entityCompound = (NbtCompound) nbtElement;
        NbtList nbtPos = entityCompound.getList("blockPos", 3);
        Vec3d realPosition = rotate(new Vec3d(nbtPos.getInt(0), nbtPos.getInt(1), nbtPos.getInt(2)), rotation).subtract(Vec3d.of(lowestPos)).add(pos.getX(), pos.getY(), pos.getZ());
        NbtCompound nbt = entityCompound.getCompound("nbt").copy();
        nbt.remove("Pos");
        nbt.remove("UUID");
        NbtList posList = new NbtList();
        posList.add(NbtDouble.of(realPosition.x));
        posList.add(NbtDouble.of(realPosition.y));
        posList.add(NbtDouble.of(realPosition.z));
        nbt.put("Pos", posList);
        NbtList rotationList = new NbtList();
        NbtList entityRotationList = nbt.getList("Rotation", 5);
        float yawRotation = applyRotation(entityRotationList.getFloat(0), rotation);
        rotationList.add(NbtFloat.of(yawRotation));
        rotationList.add(NbtFloat.of(entityRotationList.getFloat(1)));
        nbt.remove("Rotation");
        nbt.put("Rotation", rotationList);
        if (nbt.contains("Facing")) {
            Direction dir = rotation.rotate(Direction.fromHorizontal(nbt.getByte("Facing")));
            nbt.remove("Facing");
            nbt.putByte("Facing", (byte) dir.getHorizontal());
        }
        getEntity(region, nbt).ifPresent((entity) -> {
            entity.refreshPositionAndAngles(realPosition.x, realPosition.y, realPosition.z, yawRotation, entity.getPitch());
            region.spawnEntity(entity);
        });
    });
    return this;
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) NbtList(net.minecraft.nbt.NbtList) Direction(net.minecraft.util.math.Direction) Vec3d(net.minecraft.util.math.Vec3d)

Example 22 with NbtList

use of net.minecraft.nbt.NbtList in project frame-fabric by moddingplayground.

the class BannerBlockEntityMixin method cleanFrameBannerPattern.

/**
 * Handles removing Frame banner patterns instead of vanilla banner patterns
 * when a banner is cleaned in a cauldron. Yes, this is an "inject-and-cancel"
 * callback. Let me know if there are incompatibilities.
 */
@Inject(method = "loadFromItemStack", at = @At("HEAD"), cancellable = true)
private static void cleanFrameBannerPattern(ItemStack stack, CallbackInfo info) {
    NbtCompound beTag = stack.getSubNbt("BlockEntityTag");
    if (beTag != null) {
        NbtList framePatterns = beTag.getList(FrameBannerPatternAccess.NBT_KEY, 10);
        NbtList patterns = beTag.getList("Patterns", 10);
        boolean cleaned = false;
        if (!framePatterns.isEmpty()) {
            // determine if the last banner pattern is the topmost
            int lastIndex = framePatterns.getCompound(framePatterns.size() - 1).getInt("Index");
            if (lastIndex >= patterns.size()) {
                framePatterns.remove(framePatterns.size() - 1);
                cleaned = true;
            }
        }
        if (!cleaned && !patterns.isEmpty()) {
            patterns.remove(patterns.size() - 1);
        }
        if (framePatterns.isEmpty()) {
            if (patterns.isEmpty()) {
                stack.removeSubNbt("BlockEntityTag");
            } else {
                beTag.remove(FrameBannerPatternAccess.NBT_KEY);
            }
        } else if (patterns.isEmpty()) {
            beTag.remove("Patterns");
        }
    }
    info.cancel();
}
Also used : NbtCompound(net.minecraft.nbt.NbtCompound) NbtList(net.minecraft.nbt.NbtList) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 23 with NbtList

use of net.minecraft.nbt.NbtList in project frame-fabric by moddingplayground.

the class BannerBlockEntityMixin method frame_setBannerPatternNbt.

@Override
public void frame_setBannerPatternNbt(NbtList nbt) {
    frame_bannerPatternsTag = nbt;
    if (frame_bannerPatternsTag != null) {
        // validate NBT data, removing and/or resetting invalid data
        for (Iterator<NbtElement> itr = frame_bannerPatternsTag.iterator(); itr.hasNext(); ) {
            NbtCompound element = (NbtCompound) itr.next();
            Identifier id = Identifier.tryParse(element.getString("Pattern"));
            int colorId = element.getInt("Color");
            int index = element.getInt("Index");
            if (id == null || !FrameBannerPatterns.REGISTRY.getIds().contains(id)) {
                itr.remove();
            } else {
                int rtColorId = DyeColor.byId(colorId).getId();
                if (rtColorId != colorId) {
                    element.putInt("Color", rtColorId);
                }
                if (index < 0) {
                    element.putInt("Index", 0);
                }
            }
        }
        // the Java API requires that this sort be stable
        frame_bannerPatternsTag.sort(comparingInt(t -> ((NbtCompound) t).getInt("Index")));
    }
}
Also used : BlockEntity(net.minecraft.block.entity.BlockEntity) NbtElement(net.minecraft.nbt.NbtElement) Iterator(java.util.Iterator) FrameBannerPatternAccess(net.moddingplayground.frame.impl.bannerpatterns.FrameBannerPatternAccess) Unique(org.spongepowered.asm.mixin.Unique) Inject(org.spongepowered.asm.mixin.injection.Inject) NbtList(net.minecraft.nbt.NbtList) BlockPos(net.minecraft.util.math.BlockPos) BlockEntityType(net.minecraft.block.entity.BlockEntityType) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) ItemStack(net.minecraft.item.ItemStack) NbtCompound(net.minecraft.nbt.NbtCompound) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) FrameBannerPatterns(net.moddingplayground.frame.api.bannerpatterns.v0.FrameBannerPatterns) Mixin(org.spongepowered.asm.mixin.Mixin) DyeColor(net.minecraft.util.DyeColor) Identifier(net.minecraft.util.Identifier) BlockState(net.minecraft.block.BlockState) Comparator(java.util.Comparator) BannerBlockEntity(net.minecraft.block.entity.BannerBlockEntity) At(org.spongepowered.asm.mixin.injection.At) Identifier(net.minecraft.util.Identifier) NbtCompound(net.minecraft.nbt.NbtCompound) NbtElement(net.minecraft.nbt.NbtElement)

Example 24 with NbtList

use of net.minecraft.nbt.NbtList in project frame-fabric by moddingplayground.

the class ClientBannerBlockEntityMixin method putFrameBannerPatternsInPickStack.

/**
 * Adds Frame banner pattern data to the pick block stack.
 */
@Inject(method = "getPickStack", at = @At("RETURN"))
private void putFrameBannerPatternsInPickStack(CallbackInfoReturnable<ItemStack> info) {
    ItemStack stack = info.getReturnValue();
    NbtList tag = ((Internal) this).frame_getBannerPatternNbt();
    if (tag != null) {
        stack.getOrCreateSubNbt("BlockEntityTag").put(NBT_KEY, tag);
    }
}
Also used : NbtList(net.minecraft.nbt.NbtList) ItemStack(net.minecraft.item.ItemStack) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 25 with NbtList

use of net.minecraft.nbt.NbtList in project frame-fabric by moddingplayground.

the class BuiltinModelItemRendererMixin method setFrameBannerPatterns.

@Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/block/entity/BannerBlockEntityRenderer;renderCanvas(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/model/ModelPart;Lnet/minecraft/client/util/SpriteIdentifier;ZLjava/util/List;Z)V"))
private void setFrameBannerPatterns(ItemStack stack, ModelTransformation.Mode mode, MatrixStack matrices, VertexConsumerProvider vertices, int light, int overlay, CallbackInfo ci) {
    NbtList nbt = FrameBannerPatternConversions.getNbt(stack);
    FrameBannerPatternRenderContext.setFrameBannerPatterns(FrameBannerPatternConversions.makeData(nbt));
}
Also used : NbtList(net.minecraft.nbt.NbtList) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

NbtList (net.minecraft.nbt.NbtList)146 NbtCompound (net.minecraft.nbt.NbtCompound)79 NbtElement (net.minecraft.nbt.NbtElement)45 Identifier (net.minecraft.util.Identifier)37 ItemStack (net.minecraft.item.ItemStack)25 NbtString (net.minecraft.nbt.NbtString)16 LiteralText (net.minecraft.text.LiteralText)12 BlockPos (net.minecraft.util.math.BlockPos)11 Block (net.minecraft.block.Block)9 List (java.util.List)8 Item (net.minecraft.item.Item)7 Inject (org.spongepowered.asm.mixin.injection.Inject)7 IOException (java.io.IOException)6 Items (net.minecraft.item.Items)6 NbtIo (net.minecraft.nbt.NbtIo)6 StatusEffect (net.minecraft.entity.effect.StatusEffect)5 Text (net.minecraft.text.Text)5 Comparator (java.util.Comparator)4 Blocks (net.minecraft.block.Blocks)4 EntityType (net.minecraft.entity.EntityType)4