Search in sources :

Example 31 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class SpongeEntityArchetype method getPosition.

public Optional<Vector3d> getPosition() {
    if (this.position != null) {
        return Optional.of(this.position);
    }
    if (!this.compound.contains(Constants.Entity.ENTITY_POSITION, Constants.NBT.TAG_LIST)) {
        return Optional.empty();
    }
    try {
        final ListTag pos = this.compound.getList(Constants.Entity.ENTITY_POSITION, Constants.NBT.TAG_DOUBLE);
        final double x = pos.getDouble(0);
        final double y = pos.getDouble(1);
        final double z = pos.getDouble(2);
        this.position = new Vector3d(x, y, z);
        return Optional.of(this.position);
    } catch (final Exception e) {
        return Optional.empty();
    }
}
Also used : Vector3d(org.spongepowered.math.vector.Vector3d) ListTag(net.minecraft.nbt.ListTag)

Example 32 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class SpongeEntityArchetype method getRotation.

private Vector3d getRotation() {
    final ListTag tag = this.compound.getList("Rotation", 5);
    final float rotationYaw = tag.getFloat(0);
    final float rotationPitch = tag.getFloat(1);
    return new Vector3d(rotationPitch, rotationYaw, 0);
}
Also used : Vector3d(org.spongepowered.math.vector.Vector3d) ListTag(net.minecraft.nbt.ListTag)

Example 33 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class DamageEventUtil method createAttackEnchantmentFunction.

public static List<DamageFunction> createAttackEnchantmentFunction(final net.minecraft.world.item.ItemStack heldItem, final MobType creatureAttribute, final float attackStrength) {
    if (heldItem.isEmpty()) {
        return Collections.emptyList();
    }
    final ListTag enchantmentCompounds = heldItem.getEnchantmentTags();
    if (enchantmentCompounds.isEmpty()) {
        return Collections.emptyList();
    }
    final Map<Enchantment, Collection<Integer>> enchantments = new HashMap<>();
    for (int i = 0; i < enchantmentCompounds.size(); ++i) {
        final String id = enchantmentCompounds.getCompound(i).getString("id");
        final int enchantmentLevel = enchantmentCompounds.getCompound(i).getInt("lvl");
        final Enchantment enchantment = Registry.ENCHANTMENT.get(new ResourceLocation(id));
        if (enchantment != null) {
            enchantments.computeIfAbsent(enchantment, k -> new ArrayList<>()).add(enchantmentLevel);
        }
    }
    if (enchantments.isEmpty()) {
        return Collections.emptyList();
    }
    final List<DamageFunction> damageModifierFunctions = new ArrayList<>();
    final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(heldItem);
    for (final Map.Entry<Enchantment, Collection<Integer>> enchantment : enchantments.entrySet()) {
        final DamageModifier enchantmentModifier = DamageModifier.builder().type(DamageModifierTypes.WEAPON_ENCHANTMENT).cause(Cause.of(EventContext.empty(), snapshot, enchantment)).build();
        final DoubleUnaryOperator enchantmentFunction = (damage) -> {
            double totalDamage = 0;
            for (final int level : enchantment.getValue()) {
                totalDamage += (double) enchantment.getKey().getDamageBonus(level, creatureAttribute) * attackStrength;
            }
            return totalDamage;
        };
        damageModifierFunctions.add(new DamageFunction(enchantmentModifier, enchantmentFunction));
    }
    return damageModifierFunctions;
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) LivingEntity(net.minecraft.world.entity.LivingEntity) AABB(net.minecraft.world.phys.AABB) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) MobType(net.minecraft.world.entity.MobType) EventContext(org.spongepowered.api.event.EventContext) Registry(net.minecraft.core.Registry) ChunkSource(net.minecraft.world.level.chunk.ChunkSource) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Player(net.minecraft.world.entity.player.Player) List(java.util.List) BlockPos(net.minecraft.core.BlockPos) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) Optional(java.util.Optional) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Enchantment(net.minecraft.world.item.enchantment.Enchantment) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) BlockState(net.minecraft.world.level.block.state.BlockState) HashMap(java.util.HashMap) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) MobEffects(net.minecraft.world.effect.MobEffects) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) DamageSource(net.minecraft.world.damagesource.DamageSource) EnchantmentHelper(net.minecraft.world.item.enchantment.EnchantmentHelper) CombatRules(net.minecraft.world.damagesource.CombatRules) Server(org.spongepowered.api.Server) CauseStackManager(org.spongepowered.api.event.CauseStackManager) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) Cause(org.spongepowered.api.event.Cause) Entity(net.minecraft.world.entity.Entity) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) Mth(net.minecraft.util.Mth) Collections(java.util.Collections) ListTag(net.minecraft.nbt.ListTag) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ListTag(net.minecraft.nbt.ListTag) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) ResourceLocation(net.minecraft.resources.ResourceLocation) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) Collection(java.util.Collection) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Enchantment(net.minecraft.world.item.enchantment.Enchantment) Map(java.util.Map) HashMap(java.util.HashMap) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 34 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class WritableBookItemMixin_LimitBookSize method impl$useMaxBookSizeFromConfig.

@Inject(method = "makeSureTagIsValid", cancellable = true, at = @At(value = "RETURN"))
private static void impl$useMaxBookSizeFromConfig(final CompoundTag p_150930_0_, final CallbackInfoReturnable<Boolean> cir) {
    if (cir.getReturnValue()) {
        final ListTag listnbt = p_150930_0_.getList("pages", 8);
        final int size = IntStream.range(0, listnbt.size()).mapToObj(listnbt::getString).mapToInt(s -> s.getBytes(StandardCharsets.UTF_8).length).sum();
        if (size > SpongeConfigs.getCommon().get().exploits.maxBookSize) {
            cir.setReturnValue(false);
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) Inject(org.spongepowered.asm.mixin.injection.Inject) Redirect(org.spongepowered.asm.mixin.injection.Redirect) StandardCharsets(java.nio.charset.StandardCharsets) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) CompoundTag(net.minecraft.nbt.CompoundTag) Mixin(org.spongepowered.asm.mixin.Mixin) Constant(org.spongepowered.asm.mixin.injection.Constant) SpongeConfigs(org.spongepowered.common.applaunch.config.core.SpongeConfigs) ModifyConstant(org.spongepowered.asm.mixin.injection.ModifyConstant) LocalCapture(org.spongepowered.asm.mixin.injection.callback.LocalCapture) ListTag(net.minecraft.nbt.ListTag) WritableBookItem(net.minecraft.world.item.WritableBookItem) At(org.spongepowered.asm.mixin.injection.At) ListTag(net.minecraft.nbt.ListTag) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 35 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class BannerBlockEntityMixin method bridge$setLayers.

@Override
public void bridge$setLayers(final List<BannerPatternLayer> layers) {
    this.impl$patternLayers = NonNullList.create();
    this.impl$patternLayers.addAll(layers);
    this.itemPatterns = new ListTag();
    for (final BannerPatternLayer layer : this.impl$patternLayers) {
        this.itemPatterns.add(ShieldItemStackData.layerToNbt(layer));
    }
    this.impl$markDirtyAndUpdate();
}
Also used : BannerPatternLayer(org.spongepowered.api.data.meta.BannerPatternLayer) ListTag(net.minecraft.nbt.ListTag)

Aggregations

ListTag (net.minecraft.nbt.ListTag)52 CompoundTag (net.minecraft.nbt.CompoundTag)38 ItemStack (net.minecraft.world.item.ItemStack)10 TagCompound (de.keyle.knbt.TagCompound)8 TagList (de.keyle.knbt.TagList)8 List (java.util.List)7 IntArrayTag (net.minecraft.nbt.IntArrayTag)7 ResourceLocation (net.minecraft.resources.ResourceLocation)7 TagString (de.keyle.knbt.TagString)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 StringTag (net.minecraft.nbt.StringTag)5 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)4 IntTag (net.minecraft.nbt.IntTag)4 Tag (net.minecraft.nbt.Tag)4 ByteTag (net.minecraft.nbt.ByteTag)3 DoubleTag (net.minecraft.nbt.DoubleTag)3 FloatTag (net.minecraft.nbt.FloatTag)3 LongTag (net.minecraft.nbt.LongTag)3 ShortTag (net.minecraft.nbt.ShortTag)3