Search in sources :

Example 1 with InventoryAction

use of com.almuradev.almura.feature.exchange.InventoryAction in project Almura by AlmuraDev.

the class ExchangeOfferScreen method onTransactionComplete.

@Subscribe
private void onTransactionComplete(UIExchangeOfferContainer.TransactionCompletedEvent event) {
    final InventoryAction.Direction direction = event.targetSide == BasicDualListContainer.SideType.LEFT ? InventoryAction.Direction.TO_INVENTORY : InventoryAction.Direction.TO_LISTING;
    final InventoryAction.Direction oppositeDirection = event.targetSide == BasicDualListContainer.SideType.LEFT ? InventoryAction.Direction.TO_LISTING : InventoryAction.Direction.TO_INVENTORY;
    // Filter out relevant actions
    final List<InventoryAction> filteredActions = this.inventoryActions.stream().filter(a -> UIExchangeOfferContainer.TransferType.isStackEqualIgnoreSize(a.getStack(), event.stack) && a.getDirection() == oppositeDirection).collect(Collectors.toList());
    // Determine what we need to remove
    int removed = 0;
    int toRemoveCount = event.stack.getQuantity();
    // If we can still remove more, iterate through all remaining stacks and attempt to pull what we can
    if (toRemoveCount > 0) {
        for (InventoryAction action : filteredActions) {
            // Stop if we don't have anymore to remove
            if (toRemoveCount <= 0) {
                break;
            }
            // Determine how much we are taking
            final int toTake = Math.min(action.getStack().getQuantity(), toRemoveCount);
            removed += toTake;
            toRemoveCount -= toTake;
            // Take the amount
            action.getStack().setQuantity(action.getStack().getQuantity() - toTake);
            if (action.getStack().isEmpty()) {
                this.inventoryActions.remove(action);
            }
        }
    }
    // Otherwise we'll balance to a net zero as this means that the items were once added from one direction to another.
    if (removed < event.stack.getQuantity()) {
        final int toAdd = event.stack.getQuantity() - removed;
        // Add a new action or add the quantity to an existing one.
        final InventoryAction existingAction = this.inventoryActions.stream().filter(a -> UIExchangeOfferContainer.TransferType.isStackEqualIgnoreSize(a.getStack(), event.stack) && a.getDirection() == direction).findAny().orElse(null);
        if (existingAction == null) {
            final InventoryAction newAction = new InventoryAction(direction, event.stack);
            newAction.getStack().setQuantity(toAdd);
            this.inventoryActions.add(newAction);
        } else {
            existingAction.getStack().setQuantity(existingAction.getStack().getQuantity() + event.stack.getQuantity());
        }
    }
}
Also used : UIButton(net.malisis.core.client.gui.component.interaction.UIButton) VanillaStack(com.almuradev.almura.shared.item.VanillaStack) ClientExchangeManager(com.almuradev.almura.feature.exchange.client.ClientExchangeManager) InventoryAction(com.almuradev.almura.feature.exchange.InventoryAction) TextFormatting(net.minecraft.util.text.TextFormatting) UIButtonBuilder(net.malisis.core.client.gui.component.interaction.button.builder.UIButtonBuilder) UIExchangeOfferContainer(com.almuradev.almura.feature.exchange.client.gui.component.UIExchangeOfferContainer) BasicVanillaStack(com.almuradev.almura.shared.item.BasicVanillaStack) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) BasicForm(net.malisis.core.client.gui.component.container.BasicForm) I18n(net.minecraft.client.resources.I18n) Inject(javax.inject.Inject) ItemStack(net.minecraft.item.ItemStack) List(java.util.List) Minecraft(net.minecraft.client.Minecraft) Side(net.minecraftforge.fml.relauncher.Side) Anchor(net.malisis.core.client.gui.Anchor) BasicDualListContainer(net.malisis.core.client.gui.component.container.BasicDualListContainer) Subscribe(com.google.common.eventbus.Subscribe) BasicScreen(net.malisis.core.client.gui.BasicScreen) NonNullList(net.minecraft.util.NonNullList) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) Exchange(com.almuradev.almura.feature.exchange.Exchange) InventoryAction(com.almuradev.almura.feature.exchange.InventoryAction) Subscribe(com.google.common.eventbus.Subscribe)

Example 2 with InventoryAction

use of com.almuradev.almura.feature.exchange.InventoryAction in project Almura by AlmuraDev.

the class ServerboundListItemsRequestPacket method readFrom.

@Override
public void readFrom(final ChannelBuf buf) {
    this.id = buf.readString();
    final int count = buf.readInteger();
    checkState(count > 0);
    this.actions = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        final InventoryAction.Direction direction = InventoryAction.Direction.valueOf(buf.readString());
        final ResourceLocation location = new ResourceLocation(buf.readString(), buf.readString());
        final Item item = ForgeRegistries.ITEMS.getValue(location);
        if (item == null) {
            new IOException("Unknown item id '" + location.toString() + "' when receiving list item! . Skipping...").printStackTrace();
            continue;
        }
        final int quantity = buf.readInteger();
        final int metadata = buf.readInteger();
        final int compoundDataLength = buf.readInteger();
        NBTTagCompound compound = null;
        if (compoundDataLength > 0) {
            try {
                compound = SerializationUtil.compoundFromBytes(buf.readBytes(compoundDataLength));
            } catch (IOException e) {
                e.printStackTrace();
                continue;
            }
        }
        final BasicVanillaStack stack = new BasicVanillaStack(item, quantity, metadata, compound);
        this.actions.add(new InventoryAction(direction, stack));
    }
}
Also used : Item(net.minecraft.item.Item) ResourceLocation(net.minecraft.util.ResourceLocation) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) BasicVanillaStack(com.almuradev.almura.shared.item.BasicVanillaStack) InventoryAction(com.almuradev.almura.feature.exchange.InventoryAction)

Example 3 with InventoryAction

use of com.almuradev.almura.feature.exchange.InventoryAction in project Almura by AlmuraDev.

the class ServerboundListItemsRequestPacket method writeTo.

@Override
public void writeTo(final ChannelBuf buf) {
    checkNotNull(this.id);
    checkNotNull(this.actions);
    checkState(!this.actions.isEmpty());
    buf.writeString(this.id);
    buf.writeInteger(this.actions.size());
    for (final InventoryAction action : this.actions) {
        final VirtualStack stack = action.getStack();
        final ResourceLocation location = stack.getItem().getRegistryName();
        if (location == null) {
            new IOException("Malformed resource location for Item '" + stack + "' when sending list item!").printStackTrace();
            continue;
        }
        final NBTTagCompound compound = stack.getCompound();
        byte[] compoundData = null;
        if (compound != null) {
            try {
                compoundData = SerializationUtil.toBytes(compound);
            } catch (IOException e) {
                e.printStackTrace();
                continue;
            }
        }
        buf.writeString(action.getDirection().name().toUpperCase());
        buf.writeString(location.getNamespace());
        buf.writeString(location.getPath());
        buf.writeInteger(stack.getQuantity());
        buf.writeInteger(stack.getMetadata());
        if (compoundData == null) {
            buf.writeInteger(0);
        } else {
            buf.writeInteger(compoundData.length);
            buf.writeBytes(compoundData);
        }
    }
}
Also used : VirtualStack(com.almuradev.almura.shared.item.VirtualStack) ResourceLocation(net.minecraft.util.ResourceLocation) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) InventoryAction(com.almuradev.almura.feature.exchange.InventoryAction)

Aggregations

InventoryAction (com.almuradev.almura.feature.exchange.InventoryAction)3 BasicVanillaStack (com.almuradev.almura.shared.item.BasicVanillaStack)2 IOException (java.io.IOException)2 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 ResourceLocation (net.minecraft.util.ResourceLocation)2 Exchange (com.almuradev.almura.feature.exchange.Exchange)1 ClientExchangeManager (com.almuradev.almura.feature.exchange.client.ClientExchangeManager)1 UIExchangeOfferContainer (com.almuradev.almura.feature.exchange.client.gui.component.UIExchangeOfferContainer)1 VanillaStack (com.almuradev.almura.shared.item.VanillaStack)1 VirtualStack (com.almuradev.almura.shared.item.VirtualStack)1 Subscribe (com.google.common.eventbus.Subscribe)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 Anchor (net.malisis.core.client.gui.Anchor)1 BasicScreen (net.malisis.core.client.gui.BasicScreen)1 BasicDualListContainer (net.malisis.core.client.gui.component.container.BasicDualListContainer)1 BasicForm (net.malisis.core.client.gui.component.container.BasicForm)1 UIButton (net.malisis.core.client.gui.component.interaction.UIButton)1