use of net.minecraft.world.inventory.Slot in project SpongeCommon by SpongePowered.
the class CloseMenuTransaction method reopen.
private void reopen(final ServerPlayer player, final AbstractContainerMenu container) {
if (container.getSlot(0) == null) {
return;
}
if (!(container instanceof InventoryMenu)) {
// Inventory closed by client, reopen window and send container
player.containerMenu = container;
final Slot slot = container.getSlot(0);
final net.minecraft.world.Container slotInventory = slot.container;
final net.minecraft.network.chat.@Nullable Component title;
// TODO get name from last open
if (slotInventory instanceof MenuProvider) {
title = ((MenuProvider) slotInventory).getDisplayName();
} else {
// expected fallback for unknown types
title = null;
}
slotInventory.startOpen(player);
player.connection.send(new ClientboundOpenScreenPacket(container.containerId, container.getType(), title));
// resync data to client
player.refreshContainer(container);
} else {
// TODO: Maybe print a warning or throw an exception here?
// The player gui cannot be opened from the
// server so allowing this event to be cancellable when the
// GUI has been closed already would result
// in opening the wrong GUI window.
}
}
use of net.minecraft.world.inventory.Slot in project SpongeCommon by SpongePowered.
the class SpongeInventoryMenu method setCurrentInventory.
@Override
public void setCurrentInventory(ViewableInventory inventory) {
if (inventory.getClass().equals(this.inventory.getClass()) && inventory instanceof ViewableCustomInventory && inventory.capacity() == this.inventory.capacity()) {
this.inventory = inventory;
for (Map.Entry<Container, ServerPlayer> entry : this.tracked.entrySet()) {
final net.minecraft.world.inventory.AbstractContainerMenu container = (net.minecraft.world.inventory.AbstractContainerMenu) entry.getKey();
final ServerPlayer player = entry.getValue();
// create a new container for the viewable inventory
final net.minecraft.world.inventory.AbstractContainerMenu newContainer = ((ViewableCustomInventory) inventory).createMenu(-1, ((Player) player).inventory, (Player) player);
for (int i = 0; i < inventory.capacity(); i++) {
// And put its slots into the old container
final Slot slot = newContainer.slots.get(i);
container.slots.set(i, slot);
// Update Container items
((AbstractContainerMenuAccessor) container).accessor$lastSlots().set(i, slot.getItem());
}
// send update to Client
for (ContainerListener listener : ((AbstractContainerMenuAccessor) container).accessor$containerListeners()) {
listener.refreshContainer(container, ((AbstractContainerMenuAccessor) container).accessor$lastSlots());
}
}
} else {
// Get all distinct players and reopen inventory for them
this.inventory = inventory;
this.reopen();
}
}
use of net.minecraft.world.inventory.Slot in project SpongeCommon by SpongePowered.
the class SpongeInventoryMenu method onClick.
public boolean onClick(int slotId, int dragType, ClickType clickTypeIn, Player player, Container container) {
try (CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.pushCause(player);
Cause cause = frame.currentCause();
if (clickTypeIn == ClickType.QUICK_CRAFT) {
return this.onClickDrag(cause, slotId, dragType, container);
}
Optional<org.spongepowered.api.item.inventory.Slot> slot = container.slot(slotId);
if (slot.isPresent()) {
switch(clickTypeIn) {
case SWAP:
if (dragType >= 0 && dragType < 9) {
Optional<org.spongepowered.api.item.inventory.Slot> slot2 = container.slot(dragType);
if (slot2.isPresent() && this.keySwapHandler != null) {
return this.keySwapHandler.handle(cause, container, slot.get(), slotId, ClickTypes.KEY_SWAP.get(), slot2.get());
}
}
break;
case CLONE:
if (this.slotClickHandler != null) {
return this.slotClickHandler.handle(cause, container, slot.get(), slotId, ClickTypes.CLICK_MIDDLE.get());
}
break;
case PICKUP_ALL:
if (this.slotClickHandler != null) {
return this.slotClickHandler.handle(cause, container, slot.get(), slotId, ClickTypes.DOUBLE_CLICK.get());
}
break;
default:
if (this.slotClickHandler != null) {
if (dragType == 0) {
return this.onClickLeft(cause, this.slotClickHandler, clickTypeIn, container, slotId, slot.get());
} else if (dragType == 1) {
return this.onClickRight(cause, this.slotClickHandler, clickTypeIn, container, slotId, slot.get());
}
// else unknown drag-type
}
}
return true;
}
// else no slot present
switch(clickTypeIn) {
case PICKUP:
if (slotId == -999 && this.clickHandler != null) {
if (dragType == 0) {
return this.clickHandler.handle(cause, container, ClickTypes.CLICK_LEFT_OUTSIDE.get());
} else if (dragType == 1) {
return this.clickHandler.handle(cause, container, ClickTypes.CLICK_RIGHT_OUTSIDE.get());
}
}
// else unknown slotId/drag-type
break;
case THROW:
if (slotId == -999) {
// TODO check packets - does THROW with slotid -999 exist or is this actually PICKUP?
// its supposed to be l/r-click with nothing in hand
// TODO check if those exist
// /**
// * Throwing one item on the cursor by clicking outside the inventory window.
// */
// public static final org.spongepowered.api.item.inventory.menu.ClickType
// CLICK_THROW_ONE = DummyObjectProvider.createFor(org.spongepowered.api.item.inventory.menu.ClickType.class, "click_throw_one");
// /**
// * Throwing all items on the cursor by clicking outside the inventory window.
// */
// public static final org.spongepowered.api.item.inventory.menu.ClickType
// CLICK_THROW_ALL = DummyObjectProvider.createFor(org.spongepowered.api.item.inventory.menu.ClickType.class, "click_throw_all");
}
// else unknown slotId/drag-type
break;
}
return true;
}
}
use of net.minecraft.world.inventory.Slot in project SpongeCommon by SpongePowered.
the class PacketPhaseUtil method handleSlotRestore.
public static void handleSlotRestore(@Nullable final Player player, @Nullable final AbstractContainerMenu containerMenu, final List<SlotTransaction> slotTransactions, final boolean eventCancelled) {
try (PhaseContext<@NonNull ?> ignored = BlockPhase.State.RESTORING_BLOCKS.createPhaseContext(PhaseTracker.SERVER).buildAndSwitch()) {
boolean restoredAny = false;
for (final SlotTransaction slotTransaction : slotTransactions) {
if ((!slotTransaction.custom().isPresent() && slotTransaction.isValid()) && !eventCancelled) {
continue;
}
restoredAny = true;
final org.spongepowered.api.item.inventory.Slot slot = slotTransaction.slot();
final ItemStackSnapshot snapshot = eventCancelled || !slotTransaction.isValid() ? slotTransaction.original() : slotTransaction.custom().get();
if (containerMenu == null || slot.viewedSlot() == slot) {
slot.set(snapshot.createStack());
} else {
final int slotNumber = ((SlotAdapter) slot).getOrdinal();
final Slot nmsSlot = containerMenu.getSlot(slotNumber);
if (nmsSlot != null) {
nmsSlot.set(ItemStackUtil.fromSnapshotToNative(snapshot));
}
}
}
if (restoredAny && player instanceof net.minecraft.server.level.ServerPlayer) {
if (containerMenu != null) {
containerMenu.broadcastChanges();
if (player.containerMenu == containerMenu) {
((net.minecraft.server.level.ServerPlayer) player).refreshContainer(containerMenu);
}
} else {
player.inventoryMenu.broadcastChanges();
}
}
}
}
use of net.minecraft.world.inventory.Slot in project SpongeCommon by SpongePowered.
the class PacketPhaseUtil method validateCapturedTransactions.
public static void validateCapturedTransactions(final int slotId, final AbstractContainerMenu containerMenu, final List<SlotTransaction> capturedTransactions) {
if (capturedTransactions.size() == 0 && slotId >= 0 && slotId < containerMenu.slots.size()) {
final Slot slot = containerMenu.getSlot(slotId);
if (slot != null) {
final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(slot.getItem());
final SlotTransaction slotTransaction = new SlotTransaction(((InventoryAdapter) containerMenu).inventoryAdapter$getSlot(slotId).get(), snapshot, snapshot);
capturedTransactions.add(slotTransaction);
}
}
}
Aggregations