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