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;
}
}
}
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;
}
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);
}
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;
}
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);
}
Aggregations