use of org.spongepowered.common.interfaces.IMixinContainer in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callCraftEventPost.
public static CraftItemEvent.Craft callCraftEventPost(EntityPlayer player, CraftingInventory inventory, ItemStackSnapshot result, @Nullable CraftingRecipe recipe, Container container, List<SlotTransaction> transactions) {
// Get previous cursor if captured
ItemStack previousCursor = ((IMixinContainer) container).getPreviousCursor();
if (previousCursor == null) {
// or get the current one
previousCursor = player.inventory.getItemStack();
}
Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackUtil.snapshotOf(previousCursor), ItemStackUtil.snapshotOf(player.inventory.getItemStack()));
CraftItemEvent.Craft event = SpongeEventFactory.createCraftItemEventCraft(Sponge.getCauseStackManager().getCurrentCause(), result, inventory, cursorTransaction, Optional.ofNullable(recipe), ((org.spongepowered.api.item.inventory.Container) container), transactions);
SpongeImpl.postEvent(event);
((IMixinContainer) container).setCaptureInventory(false);
// handle slot-transactions
PacketPhaseUtil.handleSlotRestore(player, container, new ArrayList<>(transactions), event.isCancelled());
if (event.isCancelled() || !event.getCursorTransaction().isValid() || event.getCursorTransaction().getCustom().isPresent()) {
// handle cursor-transaction
ItemStackSnapshot newCursor = event.isCancelled() || event.getCursorTransaction().isValid() ? event.getCursorTransaction().getOriginal() : event.getCursorTransaction().getFinal();
player.inventory.setItemStack(ItemStackUtil.fromSnapshotToNative(newCursor));
if (player instanceof EntityPlayerMP) {
((EntityPlayerMP) player).connection.sendPacket(new SPacketSetSlot(-1, -1, player.inventory.getItemStack()));
}
}
transactions.clear();
((IMixinContainer) container).setCaptureInventory(true);
return event;
}
use of org.spongepowered.common.interfaces.IMixinContainer in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method displayContainer.
@Nullable
public static Container displayContainer(EntityPlayerMP player, Inventory inventory) {
net.minecraft.inventory.Container previousContainer = player.openContainer;
net.minecraft.inventory.Container container;
if (inventory instanceof CustomInventory) {
if (!checkValidVanillaCustomInventory(((CustomInventory) inventory))) {
// Invalid size for vanilla inventory ; This is to
return null;
// prevent crashing the client with invalid data
}
}
if (inventory instanceof IInteractionObject) {
final String guiId = ((IInteractionObject) inventory).getGuiID();
switch(guiId) {
case "EntityHorse":
if (inventory instanceof CarriedInventory) {
CarriedInventory<?> cinventory = (CarriedInventory<?>) inventory;
if (cinventory.getCarrier().isPresent() && cinventory.getCarrier().get() instanceof AbstractHorse) {
player.openGuiHorseInventory(((AbstractHorse) cinventory.getCarrier().get()), (IInventory) inventory);
}
}
break;
case "minecraft:chest":
player.displayGUIChest((IInventory) inventory);
break;
case "minecraft:crafting_table":
case "minecraft:anvil":
case "minecraft:enchanting_table":
player.displayGui((IInteractionObject) inventory);
break;
default:
player.displayGUIChest((IInventory) inventory);
break;
}
} else if (inventory instanceof IInventory) {
player.displayGUIChest(((IInventory) inventory));
} else {
return null;
}
container = player.openContainer;
if (previousContainer == container) {
return null;
}
if (!callInteractInventoryOpenEvent(player)) {
return null;
}
if (container instanceof IMixinContainer) {
// This overwrites the normal container behaviour and allows viewing
// inventories that are more than 8 blocks away
// This currently actually only works for the Containers mixed into
// by MixinContainerCanInteract ; but throws no errors for other
// containers
// Allow viewing inventory; except when dead
((IMixinContainer) container).setCanInteractWith(p -> !p.isDead);
}
return container;
}
use of org.spongepowered.common.interfaces.IMixinContainer in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callCreativeClickInventoryEvent.
public static ClickInventoryEvent.Creative callCreativeClickInventoryEvent(EntityPlayerMP player, CPacketCreativeInventoryAction packetIn) {
Sponge.getCauseStackManager().pushCause(player);
// Creative doesn't inform server of cursor status so there is no way of knowing what the final stack is
// Due to this, we can only send the original item that was clicked in slot
Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, ItemStackSnapshot.NONE);
if (((IMixinContainer) player.openContainer).getCapturedTransactions().size() == 0 && packetIn.getSlotId() >= 0 && packetIn.getSlotId() < player.openContainer.inventorySlots.size()) {
Slot slot = player.openContainer.getSlot(packetIn.getSlotId());
if (slot != null) {
ItemStackSnapshot clickedItem = slot.getStack() == null ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) slot.getStack()).createSnapshot();
SlotTransaction slotTransaction = new SlotTransaction(((org.spongepowered.api.item.inventory.Slot) slot), clickedItem, ItemStackSnapshot.NONE);
((IMixinContainer) player.openContainer).getCapturedTransactions().add(slotTransaction);
}
}
ClickInventoryEvent.Creative event = SpongeEventFactory.createClickInventoryEventCreative(Sponge.getCauseStackManager().getCurrentCause(), cursorTransaction, (org.spongepowered.api.item.inventory.Container) player.openContainer, ((IMixinContainer) player.openContainer).getCapturedTransactions());
SpongeImpl.postEvent(event);
Sponge.getCauseStackManager().popCause();
return event;
}
use of org.spongepowered.common.interfaces.IMixinContainer in project SpongeCommon by SpongePowered.
the class DragInventoryStopState method unwindCraftPreview.
public static void unwindCraftPreview(InventoryPacketContext context) {
final EntityPlayerMP player = context.getPacketPlayer();
((IMixinContainer) player.openContainer).setFirePreview(true);
Inventory craftInv = ((Inventory) player.openContainer).query(QueryOperationTypes.INVENTORY_TYPE.of(CraftingInventory.class));
if (craftInv instanceof CraftingInventory) {
List<SlotTransaction> previewTransactions = ((IMixinContainer) player.openContainer).getPreviewTransactions();
if (!previewTransactions.isEmpty()) {
CraftingRecipe recipe = SpongeCraftingRecipeRegistry.getInstance().findMatchingRecipe(((CraftingInventory) craftInv).getCraftingGrid(), ((World) player.world)).orElse(null);
SpongeCommonEventFactory.callCraftEventPre(player, ((CraftingInventory) craftInv), previewTransactions.get(0), recipe, player.openContainer, previewTransactions);
previewTransactions.clear();
}
}
}
Aggregations