Search in sources :

Example 61 with ItemStack

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

the class InventoryEventFactory method callInventoryPickupEvent.

public static ItemStack callInventoryPickupEvent(final Container inventory, final ItemEntity item, final ItemStack stack) {
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(inventory);
        final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(stack);
        final ChangeInventoryEvent.Pickup.Pre event = SpongeEventFactory.createChangeInventoryEventPickupPre(frame.currentCause(), Optional.empty(), Collections.singletonList(snapshot), (Inventory) inventory, (Item) item, snapshot);
        SpongeCommon.post(event);
        if (event.isCancelled()) {
            return stack;
        }
        final int size = inventory.getContainerSize();
        final ItemStack[] prevInventory = new ItemStack[size];
        for (int i = 0; i < size; i++) {
            prevInventory[i] = inventory.getItem(i);
        }
        if (event.custom().isPresent()) {
            if (event.custom().get().isEmpty()) {
                return ItemStack.EMPTY;
            }
            boolean fullTransfer = true;
            for (final ItemStackSnapshot snap : event.custom().get()) {
                final ItemStack stackToAdd = ItemStackUtil.fromSnapshotToNative(snap);
                final ItemStack remaining = HopperBlockEntity.addItem(null, inventory, stackToAdd, null);
                if (!remaining.isEmpty()) {
                    fullTransfer = false;
                    break;
                }
            }
            if (!fullTransfer) {
                for (int i = 0; i < prevInventory.length; i++) {
                    inventory.setItem(i, prevInventory[i]);
                }
                return stack;
            }
            if (InventoryEventFactory.callInventoryPickupEvent(inventory, prevInventory)) {
                return ItemStack.EMPTY;
            }
            return stack;
        } else {
            final ItemStack remainder = HopperBlockEntity.addItem(null, inventory, stack, null);
            if (InventoryEventFactory.callInventoryPickupEvent(inventory, prevInventory)) {
                return remainder;
            }
            return stack;
        }
    }
}
Also used : CauseStackManager(org.spongepowered.api.event.CauseStackManager) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ItemStack(net.minecraft.world.item.ItemStack)

Example 62 with ItemStack

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

the class InventoryEventFactory method generateTransactions.

private static List<SlotTransaction> generateTransactions(@Nullable final Inventory inv, final Container inventory, final ItemStack[] previous) {
    if (inv == null) {
        return Collections.emptyList();
    }
    final List<SlotTransaction> trans = new ArrayList<>();
    List<org.spongepowered.api.item.inventory.Slot> slots = inv.slots();
    for (int i = 0; i < inventory.getContainerSize(); i++) {
        final org.spongepowered.api.item.inventory.Slot slot = slots.get(i);
        final ItemStack newStack = inventory.getItem(i);
        final ItemStack prevStack = previous[i];
        if (!ItemStack.matches(newStack, prevStack)) {
            trans.add(new SlotTransaction(slot, ItemStackUtil.snapshotOf(prevStack), ItemStackUtil.snapshotOf(newStack)));
        }
    }
    return trans;
}
Also used : ArrayList(java.util.ArrayList) Slot(org.spongepowered.api.item.inventory.Slot) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Slot(org.spongepowered.api.item.inventory.Slot) ItemStack(net.minecraft.world.item.ItemStack)

Example 63 with ItemStack

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

the class SpongeStonecuttingRecipeSerializer method fromJson.

@SuppressWarnings("unchecked")
@Override
public R fromJson(ResourceLocation recipeId, JsonObject json) {
    final String group = GsonHelper.getAsString(json, Constants.Recipe.GROUP, "");
    final Ingredient ingredient = IngredientUtil.spongeDeserialize(json.get(Constants.Recipe.STONECUTTING_INGREDIENT));
    final Function<Container, ItemStack> resultFunction = IngredientResultUtil.deserializeResultFunction(json);
    final ItemStack spongeStack = IngredientResultUtil.deserializeItemStack(json.getAsJsonObject(Constants.Recipe.SPONGE_RESULT));
    if (spongeStack != null) {
        return (R) new SpongeStonecuttingRecipe(recipeId, group, ingredient, spongeStack, resultFunction);
    }
    final String type = GsonHelper.getAsString(json, Constants.Recipe.RESULT);
    final int count = GsonHelper.getAsInt(json, Constants.Recipe.COUNT);
    final ItemStack itemstack = new ItemStack(Registry.ITEM.get(new ResourceLocation(type)), count);
    return (R) new SpongeStonecuttingRecipe(recipeId, group, ingredient, itemstack, resultFunction);
}
Also used : Container(net.minecraft.world.Container) Ingredient(net.minecraft.world.item.crafting.Ingredient) ResourceLocation(net.minecraft.resources.ResourceLocation) ItemStack(net.minecraft.world.item.ItemStack)

Example 64 with ItemStack

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

the class FireworkUtil method removeFireworkEffects.

public static boolean removeFireworkEffects(final Object object) {
    final ItemStack item = FireworkUtil.getItem(object);
    if (item.isEmpty()) {
        return false;
    }
    if (item.getItem() == Items.FIREWORK_STAR) {
        @Nullable final CompoundTag tag = item.getTag();
        if (tag == null) {
            return true;
        }
        tag.remove(Constants.Entity.Firework.EXPLOSION);
        return true;
    } else if (item.getItem() == Items.FIREWORK_ROCKET) {
        final CompoundTag fireworks = item.getOrCreateTagElement(Constants.Item.Fireworks.FIREWORKS);
        fireworks.remove(Constants.Item.Fireworks.EXPLOSIONS);
        return true;
    }
    return false;
}
Also used : SpongeItemStack(org.spongepowered.common.item.SpongeItemStack) ItemStack(net.minecraft.world.item.ItemStack) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 65 with ItemStack

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

the class FireworkUtil method getFireworkEffects.

public static Optional<List<FireworkEffect>> getFireworkEffects(final Object object) {
    final ItemStack item = FireworkUtil.getItem(object);
    if (item.isEmpty()) {
        return Optional.empty();
    }
    final List<FireworkEffect> effects;
    if (item.getItem() == Items.FIREWORK_ROCKET) {
        @Nullable final CompoundTag fireworks = item.getTagElement(Constants.Item.Fireworks.FIREWORKS);
        if (fireworks == null || !fireworks.contains(Constants.Item.Fireworks.EXPLOSIONS)) {
            return Optional.empty();
        }
        final ListTag effectCompounds = fireworks.getList(Constants.Item.Fireworks.EXPLOSIONS, Constants.NBT.TAG_COMPOUND);
        effects = NBTStreams.toCompounds(effectCompounds).map(FireworkUtil::fromCompound).collect(Collectors.toList());
    } else {
        @Nullable final FireworkEffect effect = FireworkUtil.getStarEffect(item);
        if (effect == null) {
            return Optional.empty();
        }
        effects = ImmutableList.of(effect);
    }
    return Optional.of(effects);
}
Also used : SpongeItemStack(org.spongepowered.common.item.SpongeItemStack) ItemStack(net.minecraft.world.item.ItemStack) FireworkEffect(org.spongepowered.api.item.FireworkEffect) ListTag(net.minecraft.nbt.ListTag) Nullable(org.checkerframework.checker.nullness.qual.Nullable) 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