Search in sources :

Example 66 with ItemStack

use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.

the class ServerGamePacketListenerImplMixin method impl$onRightClickAtEntity.

@Inject(method = "handleInteract", cancellable = true, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;interactAt(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/InteractionHand;)Lnet/minecraft/world/InteractionResult;"))
public void impl$onRightClickAtEntity(final ServerboundInteractPacket p_147340_1, final CallbackInfo ci) {
    final Entity entity = p_147340_1.getTarget(this.player.getLevel());
    final ItemStack itemInHand = p_147340_1.getHand() == null ? ItemStack.EMPTY : this.player.getItemInHand(p_147340_1.getHand());
    final InteractEntityEvent.Secondary event = SpongeCommonEventFactory.callInteractEntityEventSecondary(this.player, itemInHand, entity, p_147340_1.getHand(), VecHelper.toVector3d(p_147340_1.getLocation()));
    if (event.isCancelled()) {
        ci.cancel();
    } else {
        this.impl$ignorePackets++;
    }
}
Also used : SignBlockEntity(net.minecraft.world.level.block.entity.SignBlockEntity) Entity(net.minecraft.world.entity.Entity) InteractEntityEvent(org.spongepowered.api.event.entity.InteractEntityEvent) ItemStack(net.minecraft.world.item.ItemStack) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 67 with ItemStack

use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.

the class ServerLevelMixin method impl$throwBroadcastEvent.

@Inject(method = "levelEvent", at = @At("HEAD"), cancellable = true)
private void impl$throwBroadcastEvent(final Player player, final int eventID, final BlockPos pos, final int dataID, CallbackInfo ci) {
    if (eventID == Constants.WorldEvents.PLAY_RECORD_EVENT && ShouldFire.PLAY_SOUND_EVENT_RECORD) {
        try (final CauseStackManager.StackFrame frame = Sponge.server().causeStackManager().pushCauseFrame()) {
            final BlockEntity tileEntity = this.shadow$getBlockEntity(pos);
            if (tileEntity instanceof JukeboxBlockEntity) {
                final JukeboxBlockEntity jukebox = (JukeboxBlockEntity) tileEntity;
                final ItemStack record = jukebox.getRecord();
                frame.pushCause(jukebox);
                frame.addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(record));
                if (!record.isEmpty()) {
                    final Optional<MusicDisc> recordProperty = ((org.spongepowered.api.item.inventory.ItemStack) (Object) record).get(Keys.MUSIC_DISC);
                    if (!recordProperty.isPresent()) {
                        // Safeguard for https://github.com/SpongePowered/SpongeCommon/issues/2337
                        return;
                    }
                    final MusicDisc recordType = recordProperty.get();
                    final PlaySoundEvent.Record event = SpongeCommonEventFactory.callPlaySoundRecordEvent(frame.currentCause(), jukebox, recordType, dataID);
                    if (event.isCancelled()) {
                        ci.cancel();
                    }
                }
            }
        }
    }
}
Also used : MusicDisc(org.spongepowered.api.effect.sound.music.MusicDisc) CauseStackManager(org.spongepowered.api.event.CauseStackManager) PlaySoundEvent(org.spongepowered.api.event.sound.PlaySoundEvent) JukeboxBlockEntity(net.minecraft.world.level.block.entity.JukeboxBlockEntity) ItemStack(net.minecraft.world.item.ItemStack) JukeboxBlockEntity(net.minecraft.world.level.block.entity.JukeboxBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 68 with ItemStack

use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.

the class ItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.APPLICABLE_POTION_EFFECTS).get(h -> {
        if (h.isEdible()) {
            final List<Pair<MobEffectInstance, Float>> itemEffects = h.getItem().getFoodProperties().getEffects();
            final WeightedTable<PotionEffect> effects = new WeightedTable<>();
            final ChanceTable<PotionEffect> chance = new ChanceTable<>();
            for (final Pair<MobEffectInstance, Float> effect : itemEffects) {
                chance.add((PotionEffect) effect.getFirst(), effect.getSecond());
            }
            effects.add(new NestedTableEntry<>(1, chance));
            return effects;
        }
        return null;
    }).create(Keys.BURN_TIME).get(h -> {
        final Integer burnTime = AbstractFurnaceBlockEntity.getFuel().get(h.getItem());
        if (burnTime != null && burnTime > 0) {
            return burnTime;
        }
        return null;
    }).create(Keys.CAN_HARVEST).get(h -> {
        final Item item = h.getItem();
        if (item instanceof DiggerItemAccessor && !(item instanceof PickaxeItem)) {
            final Set<Block> blocks = ((DiggerItemAccessor) item).accessor$blocks();
            return ImmutableSet.copyOf((Set<BlockType>) (Object) blocks);
        }
        final Set<BlockType> blockTypes = Registry.BLOCK.stream().filter(b -> item.isCorrectToolForDrops(b.defaultBlockState())).map(BlockType.class::cast).collect(ImmutableSet.toImmutableSet());
        return blockTypes.isEmpty() ? null : blockTypes;
    }).create(Keys.CONTAINER_ITEM).get(h -> (ItemType) h.getItem().getCraftingRemainingItem()).create(Keys.DISPLAY_NAME).get(h -> SpongeAdventure.asAdventure(h.getDisplayName())).create(Keys.CUSTOM_MODEL_DATA).get(h -> {
        final CompoundTag tag = h.getTag();
        if (tag == null || !tag.contains(Constants.Item.CUSTOM_MODEL_DATA, Constants.NBT.TAG_INT)) {
            return null;
        }
        return tag.getInt(Constants.Item.CUSTOM_MODEL_DATA);
    }).set((h, v) -> {
        final CompoundTag tag = h.getOrCreateTag();
        tag.putInt(Constants.Item.CUSTOM_MODEL_DATA, v);
    }).delete(h -> {
        final CompoundTag tag = h.getTag();
        if (tag != null) {
            tag.remove(Constants.Item.CUSTOM_MODEL_DATA);
        }
    }).create(Keys.CUSTOM_NAME).get(h -> {
        if (h.hasCustomHoverName()) {
            return SpongeAdventure.asAdventure(h.getHoverName());
        }
        if (h.getItem() == Items.WRITTEN_BOOK) {
            // When no custom name is set on a written book fallback to its title
            // The custom name has a higher priority than the title so no setter is needed.
            final CompoundTag tag = h.getTag();
            if (tag != null) {
                final String title = tag.getString(Constants.Item.Book.ITEM_BOOK_TITLE);
                return LegacyComponentSerializer.legacySection().deserialize(title);
            }
        }
        return null;
    }).set((h, v) -> h.setHoverName(SpongeAdventure.asVanilla(v))).delete(ItemStack::resetHoverName).create(Keys.IS_UNBREAKABLE).get(h -> {
        final CompoundTag tag = h.getTag();
        if (tag == null || !tag.contains(Constants.Item.ITEM_UNBREAKABLE, Constants.NBT.TAG_BYTE)) {
            return false;
        }
        return tag.getBoolean(Constants.Item.ITEM_UNBREAKABLE);
    }).set(ItemStackData::setIsUnbrekable).delete(h -> ItemStackData.setIsUnbrekable(h, false)).create(Keys.LORE).get(h -> {
        final CompoundTag tag = h.getTag();
        if (tag == null || !tag.contains(Constants.Item.ITEM_DISPLAY)) {
            return null;
        }
        final CompoundTag displayCompound = tag.getCompound(Constants.Item.ITEM_DISPLAY);
        final ListTag list = displayCompound.getList(Constants.Item.ITEM_LORE, Constants.NBT.TAG_STRING);
        return list.isEmpty() ? null : SpongeAdventure.json(list.stream().collect(NBTCollectors.toStringList()));
    }).set((h, v) -> {
        if (v.isEmpty()) {
            ItemStackData.deleteLore(h);
            return;
        }
        final ListTag list = SpongeAdventure.listTagJson(v);
        h.getOrCreateTagElement(Constants.Item.ITEM_DISPLAY).put(Constants.Item.ITEM_LORE, list);
    }).delete(ItemStackData::deleteLore).create(Keys.MAX_DURABILITY).get(h -> h.getItem().canBeDepleted() ? h.getItem().getMaxDamage() : null).supports(h -> h.getItem().canBeDepleted()).create(Keys.ITEM_DURABILITY).get(stack -> stack.getMaxDamage() - stack.getDamageValue()).set((stack, durability) -> stack.setDamageValue(stack.getMaxDamage() - durability)).supports(h -> h.getItem().canBeDepleted()).create(Keys.ITEM_RARITY).get(stack -> (ItemRarity) (Object) stack.getRarity()).create(Keys.REPLENISHED_FOOD).get(h -> {
        if (h.getItem().isEdible()) {
            final FoodProperties food = h.getItem().getFoodProperties();
            return food == null ? null : food.getNutrition();
        }
        return null;
    }).supports(h -> h.getItem().isEdible()).create(Keys.REPLENISHED_SATURATION).get(h -> {
        if (h.getItem().isEdible()) {
            final FoodProperties food = h.getItem().getFoodProperties();
            if (food != null) {
                // Translate's Minecraft's weird internal value to the actual saturation value
                return food.getSaturationModifier() * food.getNutrition() * 2.0;
            }
        }
        return null;
    }).supports(h -> h.getItem().isEdible());
}
Also used : Items(net.minecraft.world.item.Items) DiggerItemAccessor(org.spongepowered.common.accessor.world.item.DiggerItemAccessor) Constants(org.spongepowered.common.util.Constants) StringTag(net.minecraft.nbt.StringTag) Item(net.minecraft.world.item.Item) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) PickaxeItem(net.minecraft.world.item.PickaxeItem) NestedTableEntry(org.spongepowered.api.util.weighted.NestedTableEntry) Registry(net.minecraft.core.Registry) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) LegacyComponentSerializer(net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer) FoodProperties(net.minecraft.world.food.FoodProperties) ImmutableSet(com.google.common.collect.ImmutableSet) ChanceTable(org.spongepowered.api.util.weighted.ChanceTable) Set(java.util.Set) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) SpongeCommon(org.spongepowered.common.SpongeCommon) Pair(com.mojang.datafixers.util.Pair) AbstractFurnaceBlockEntity(net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity) Keys(org.spongepowered.api.data.Keys) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) ItemRarity(org.spongepowered.api.item.ItemRarity) NBTCollectors(org.spongepowered.common.util.NBTCollectors) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) BlockType(org.spongepowered.api.block.BlockType) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) Block(net.minecraft.world.level.block.Block) ItemStack(net.minecraft.world.item.ItemStack) ItemType(org.spongepowered.api.item.ItemType) ListTag(net.minecraft.nbt.ListTag) PickaxeItem(net.minecraft.world.item.PickaxeItem) DiggerItemAccessor(org.spongepowered.common.accessor.world.item.DiggerItemAccessor) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) ItemType(org.spongepowered.api.item.ItemType) Item(net.minecraft.world.item.Item) PickaxeItem(net.minecraft.world.item.PickaxeItem) ChanceTable(org.spongepowered.api.util.weighted.ChanceTable) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) Pair(com.mojang.datafixers.util.Pair) NestedTableEntry(org.spongepowered.api.util.weighted.NestedTableEntry) FoodProperties(net.minecraft.world.food.FoodProperties) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) ListTag(net.minecraft.nbt.ListTag) BlockType(org.spongepowered.api.block.BlockType) ItemStack(net.minecraft.world.item.ItemStack)

Example 69 with ItemStack

use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.

the class MapInfoItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.MAP_INFO).supports(item -> item.getItem() instanceof MapItem).get(itemStack -> {
        if (itemStack.getTag() == null) {
            return null;
        }
        return (MapInfo) ((Level) Sponge.server().worldManager().defaultWorld()).getMapData(Constants.Map.MAP_PREFIX + itemStack.getTag().getInt(Constants.Map.MAP_ID));
    }).set((itemStack, mapInfo) -> {
        @Nullable CompoundTag nbt = itemStack.getTag();
        if (nbt == null) {
            nbt = new CompoundTag();
        }
        nbt.putInt(Constants.Map.MAP_ID, ((MapItemSavedDataBridge) mapInfo).bridge$getMapId());
        itemStack.setTag(nbt);
    });
}
Also used : Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) MapInfo(org.spongepowered.api.map.MapInfo) Constants(org.spongepowered.common.util.Constants) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) MapItem(net.minecraft.world.item.MapItem) Sponge(org.spongepowered.api.Sponge) ItemStack(net.minecraft.world.item.ItemStack) MapItemSavedDataBridge(org.spongepowered.common.bridge.world.storage.MapItemSavedDataBridge) Level(net.minecraft.world.level.Level) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Level(net.minecraft.world.level.Level) MapItem(net.minecraft.world.item.MapItem) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 70 with ItemStack

use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.

the class BlockTypeItemStackData method set.

private static boolean set(final ItemStack stack, final String nbtKey, final Set<? extends BlockType> value) {
    if (value.isEmpty()) {
        stack.removeTagKey(nbtKey);
        return true;
    }
    final CompoundTag tag = stack.getOrCreateTag();
    final ListTag list = value.stream().map(type -> Registry.BLOCK.getKey((Block) type).toString()).collect(NBTCollectors.toStringTagList());
    tag.put(nbtKey, list);
    return true;
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Constants(org.spongepowered.common.util.Constants) Set(java.util.Set) NBTStreams(org.spongepowered.common.util.NBTStreams) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Registry(net.minecraft.core.Registry) Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) NBTCollectors(org.spongepowered.common.util.NBTCollectors) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) BlockType(org.spongepowered.api.block.BlockType) Block(net.minecraft.world.level.block.Block) ItemStack(net.minecraft.world.item.ItemStack) ListTag(net.minecraft.nbt.ListTag) Block(net.minecraft.world.level.block.Block) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

ItemStack (net.minecraft.world.item.ItemStack)178 Level (net.minecraft.world.level.Level)62 ItemEntity (net.minecraft.world.entity.item.ItemEntity)61 InteractionHand (net.minecraft.world.InteractionHand)58 Player (net.minecraft.world.entity.player.Player)57 Items (net.minecraft.world.item.Items)55 InvocationTargetException (java.lang.reflect.InvocationTargetException)54 InteractionResult (net.minecraft.world.InteractionResult)54 EntitySize (de.Keyle.MyPet.api.entity.EntitySize)52 MyPet (de.Keyle.MyPet.api.entity.MyPet)52 EntityDataAccessor (net.minecraft.network.syncher.EntityDataAccessor)44 EntityDataSerializers (net.minecraft.network.syncher.EntityDataSerializers)44 SynchedEntityData (net.minecraft.network.syncher.SynchedEntityData)44 ServerLevel (net.minecraft.server.level.ServerLevel)44 MyPetApi (de.Keyle.MyPet.MyPetApi)40 Pair (com.mojang.datafixers.util.Pair)39 Bukkit (org.bukkit.Bukkit)39 CraftItemStack (org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack)39 CraftItemStack (org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack)39 Util (de.Keyle.MyPet.api.Util)38