Skip to content

Commit

Permalink
fix(trigger): drop items when inventory is full
Browse files Browse the repository at this point in the history
Signed-off-by: SettingDust <[email protected]>
  • Loading branch information
SettingDust committed Jun 6, 2022
1 parent cf6eb2d commit f0db822
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package team.ebi.epicbanitem.trigger;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

import org.spongepowered.api.data.Keys;
import org.spongepowered.api.data.Transaction;
Expand Down Expand Up @@ -34,7 +35,6 @@ public EquipRestrictionTrigger() {
}

@Listener
@SuppressWarnings("IsCancelled")
public void onChangeEntityEquipment(
final ChangeEntityEquipmentEvent event,
final @First Equipable equipable,
Expand All @@ -49,24 +49,32 @@ public void onChangeEntityEquipment(
if (item.isEmpty()) return;
final var location = entity.serverLocation();
final var cause = event.cause();
final var cancelled = new AtomicBoolean(false);
Optional<ItemStackSnapshot> processed = this.processItemCancellable(
event,
location.world(),
cause.first(Subject.class).orElse(null),
cause.first(Audience.class).orElse(null),
item);
item,
ignored -> cancelled.set(true));

if (processed.isPresent()) {
if (event.isCancelled() || slot.isValidItem(processed.get().type()))
if (cancelled.get()) {
transaction.setCustom(ItemStackSnapshot.empty());
location.spawnEntities(InventoryUtils.offerOrDrop(
carrier.inventory(), location, processed.get().createStack()));
else if (!event.isCancelled()) transaction.setCustom(processed.get());
} else {
if (event.isCancelled())
} else if (slot.isValidItem(processed.get().type())) transaction.setCustom(processed.get());
else {
transaction.setCustom(ItemStackSnapshot.empty());
location.spawnEntities(InventoryUtils.offerOrDrop(
carrier.inventory(),
location,
transaction.defaultReplacement().createStack()));
carrier.inventory(), location, processed.get().createStack()));
}
} else if (cancelled.get()) {
location.spawnEntities(InventoryUtils.offerOrDrop(
carrier.inventory(),
location,
transaction.finalReplacement().createStack()));
transaction.setCustom(ItemStackSnapshot.empty());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void onSpawnEntity(
}

@Listener
public void onServerSideConnectionJoin(final ServerSideConnectionEvent.Join event) {
public void onServerSideConnectionJoin(ServerSideConnectionEvent.Join event) {
final var player = event.player();
final var world = player.world();
final var location = player.serverLocation();
Expand All @@ -65,16 +65,17 @@ public void onServerSideConnectionJoin(final ServerSideConnectionEvent.Join even
}

private void handleInventory(
final Event event,
final ServerWorld world,
final Audience audience,
final Subject subject,
final ServerLocation location,
final CarriedInventory<? extends Carrier> inventory) {
Event event,
ServerWorld world,
Audience audience,
Subject subject,
ServerLocation location,
CarriedInventory<? extends Carrier> inventory) {
inventory.slots().stream().filter(it -> it.freeCapacity() == 0).forEach(it -> this.processItem(
event, world, subject, audience, it.peek().createSnapshot())
.map(ItemStackSnapshot::createStack)
.ifPresent(item -> {
// TODO Sponge won't tell me what type the slot is
if (it instanceof FilteringSlot slot && slot.isValidItem(item))
location.spawnEntities(InventoryUtils.offerOrDrop(inventory, location, item));
else it.set(item);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/team/ebi/epicbanitem/util/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import org.spongepowered.api.entity.Item;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.entity.StandardInventory;
import org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult;
import org.spongepowered.api.world.server.ServerLocation;

import com.google.common.collect.Lists;

public final class InventoryUtils {
public static List<Item> offerOrDrop(Inventory inventory, ServerLocation location, ItemStack... item) {
if (inventory instanceof StandardInventory standardInventory) inventory = standardInventory.primary();
final var result = inventory.offer(item);
List<Item> items = Lists.newArrayList();
if (!result.type().equals(InventoryTransactionResult.Type.SUCCESS))
Expand Down

0 comments on commit f0db822

Please sign in to comment.