use of net.minecraft.world.inventory.AbstractContainerMenu in project SpongeCommon by SpongePowered.
the class AbstractContainerMenuMixin_Inventory method impl$sendSlotContents.
private void impl$sendSlotContents(final Integer i, final ItemStack oldStack) {
for (final ContainerListener listener : this.containerListeners) {
boolean isChangingQuantityOnly = true;
if (listener instanceof ServerPlayer) {
isChangingQuantityOnly = ((ServerPlayer) listener).ignoreSlotUpdateHack;
((ServerPlayer) listener).ignoreSlotUpdateHack = false;
}
listener.slotChanged(((AbstractContainerMenu) (Object) this), i, oldStack);
if (listener instanceof ServerPlayer) {
((ServerPlayer) listener).ignoreSlotUpdateHack = isChangingQuantityOnly;
}
}
}
use of net.minecraft.world.inventory.AbstractContainerMenu in project SpongeCommon by SpongePowered.
the class InventoryEventFactory method displayContainer.
@org.checkerframework.checker.nullness.qual.Nullable
public static AbstractContainerMenu displayContainer(final ServerPlayer player, final Inventory inventory, final Component displayName) {
final net.minecraft.world.inventory.AbstractContainerMenu previousContainer = player.containerMenu;
final net.minecraft.world.inventory.AbstractContainerMenu container;
Optional<ViewableInventory> viewable = inventory.asViewable();
if (viewable.isPresent()) {
if (viewable.get() instanceof MenuProvider) {
MenuProvider namedContainerProvider = (MenuProvider) viewable.get();
if (displayName != null) {
namedContainerProvider = new SimpleMenuProvider(namedContainerProvider, SpongeAdventure.asVanilla(displayName));
}
player.openMenu(namedContainerProvider);
} else if (viewable.get() instanceof CarriedInventory) {
Optional carrier = ((CarriedInventory) viewable.get()).carrier();
if (carrier.get() instanceof AbstractHorse) {
player.openHorseInventory(((AbstractHorse) carrier.get()), ((Container) viewable.get()));
}
} else if (viewable.get() instanceof Merchant) {
Merchant merchant = (Merchant) viewable.get();
net.minecraft.network.chat.Component display = null;
int level = 0;
if (merchant instanceof Villager) {
display = ((Villager) merchant).getDisplayName();
level = ((Villager) merchant).getVillagerData().getLevel();
} else if (merchant instanceof WanderingTrader) {
display = ((WanderingTrader) merchant).getDisplayName();
level = 1;
}
if (displayName != null) {
display = SpongeAdventure.asVanilla(displayName);
}
OptionalInt containerId = player.openMenu(new SimpleMenuProvider((id, playerInv, p) -> new MerchantMenu(id, playerInv, merchant), display));
if (containerId.isPresent() && !merchant.getOffers().isEmpty()) {
player.sendMerchantOffers(containerId.getAsInt(), merchant.getOffers(), level, merchant.getVillagerXp(), merchant.showProgressBar(), merchant.canRestock());
}
}
}
container = player.containerMenu;
if (previousContainer == container) {
return null;
}
if (!InventoryEventFactory.callInteractContainerOpenEvent(player)) {
return null;
}
if (container instanceof ContainerBridge) {
// 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 InteractableContainerMixin ; but throws no errors for other
// containers
// Allow viewing inventory; except when dead
((ContainerBridge) container).bridge$setCanInteractWith(p -> !p.removed);
}
return container;
}
use of net.minecraft.world.inventory.AbstractContainerMenu in project SpongeCommon by SpongePowered.
the class ServerPlayerGameModeMixin_Tracker method useItemOn.
/**
* @author Morph
* @reason Fire interact block event.
*/
@Overwrite
public InteractionResult useItemOn(final ServerPlayer playerIn, final Level worldIn, final ItemStack stackIn, final InteractionHand handIn, final BlockHitResult blockRaytraceResultIn) {
final BlockPos blockpos = blockRaytraceResultIn.getBlockPos();
final BlockState blockstate = worldIn.getBlockState(blockpos);
// Sponge start
final BlockSnapshot snapshot = ((ServerWorld) (worldIn)).createSnapshot(VecHelper.toVector3i(blockpos));
final Vector3d hitVec = Vector3d.from(blockRaytraceResultIn.getBlockPos().getX(), blockRaytraceResultIn.getBlockPos().getY(), blockRaytraceResultIn.getBlockPos().getZ());
final org.spongepowered.api.util.Direction direction = DirectionFacingProvider.INSTANCE.getKey(blockRaytraceResultIn.getDirection()).get();
final InteractBlockEvent.Secondary event = SpongeCommonEventFactory.callInteractBlockEventSecondary(playerIn, stackIn, hitVec, snapshot, direction, handIn);
if (event.isCancelled()) {
return InteractionResult.FAIL;
}
final Tristate useItem = event.useItemResult();
final Tristate useBlock = event.useBlockResult();
// Sponge end
if (this.gameModeForPlayer == GameType.SPECTATOR) {
final MenuProvider inamedcontainerprovider = blockstate.getMenuProvider(worldIn, blockpos);
if (inamedcontainerprovider != null) {
playerIn.openMenu(inamedcontainerprovider);
final Vector3i pos = VecHelper.toVector3i(blockRaytraceResultIn.getBlockPos());
final ServerLocation location = ServerLocation.of((ServerWorld) worldIn, pos);
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.pushCause(playerIn);
frame.addContext(EventContextKeys.BLOCK_HIT, ((ServerWorld) (worldIn)).createSnapshot(pos));
((ContainerBridge) playerIn.containerMenu).bridge$setOpenLocation(location);
if (!InventoryEventFactory.callInteractContainerOpenEvent(playerIn)) {
return InteractionResult.SUCCESS;
}
}
return InteractionResult.SUCCESS;
} else {
return InteractionResult.PASS;
}
} else {
final boolean flag = !playerIn.getMainHandItem().isEmpty() || !playerIn.getOffhandItem().isEmpty();
final boolean flag1 = playerIn.isSecondaryUseActive() && flag;
final ItemStack copiedStack = stackIn.copy();
if (useBlock != Tristate.FALSE && !flag1) {
// Sponge check useBlock
final AbstractContainerMenu lastOpenContainer = playerIn.containerMenu;
final InteractionResult result = blockstate.use(worldIn, playerIn, handIn, blockRaytraceResultIn);
if (result.consumesAction() && lastOpenContainer != playerIn.containerMenu) {
final Vector3i pos = VecHelper.toVector3i(blockRaytraceResultIn.getBlockPos());
final ServerLocation location = ServerLocation.of((ServerWorld) worldIn, pos);
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.pushCause(playerIn);
frame.addContext(EventContextKeys.BLOCK_HIT, ((ServerWorld) (worldIn)).createSnapshot(pos));
((ContainerBridge) playerIn.containerMenu).bridge$setOpenLocation(location);
if (!InventoryEventFactory.callInteractContainerOpenEvent(playerIn)) {
return InteractionResult.FAIL;
}
}
}
if (result.consumesAction()) {
CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(playerIn, blockpos, copiedStack);
return result;
}
}
if (!stackIn.isEmpty() && !playerIn.getCooldowns().isOnCooldown(stackIn.getItem())) {
// Sponge start
if (useItem == Tristate.FALSE) {
return InteractionResult.PASS;
}
// Sponge end
final UseOnContext itemusecontext = new UseOnContext(playerIn, handIn, blockRaytraceResultIn);
final InteractionResult result;
if (this.isCreative()) {
final int i = stackIn.getCount();
result = stackIn.useOn(itemusecontext);
stackIn.setCount(i);
} else {
result = stackIn.useOn(itemusecontext);
// Sponge start - log change in hand
final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
final TransactionalCaptureSupplier transactor = context.getTransactor();
transactor.logPlayerInventoryChange(this.player, PlayerInventoryTransaction.EventCreator.STANDARD);
this.player.inventoryMenu.broadcastChanges();
// Sponge end
}
if (result.consumesAction()) {
CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(playerIn, blockpos, copiedStack);
}
return result;
} else {
return InteractionResult.PASS;
}
}
}
use of net.minecraft.world.inventory.AbstractContainerMenu in project SpongeCommon by SpongePowered.
the class LensRegistrar method generateLens.
@SuppressWarnings("unchecked")
private static Lens generateLens(Object inventory, int size, SlotLensProvider slotLensProvider) {
if (size == 0) {
return new DefaultEmptyLens();
}
LensFactory lensFactory = LensRegistrar.lensFactories.get(inventory.getClass());
Lens lens = null;
if (lensFactory != null) {
lens = lensFactory.apply(inventory.getClass(), size, slotLensProvider);
if (lens != null) {
return lens;
}
}
if (inventory instanceof CraftingContainer) {
lens = LensRegistrar.lensCraftingInventory(size, ((CraftingContainer) inventory).getWidth(), ((CraftingContainer) inventory).getHeight(), slotLensProvider);
} else if (inventory instanceof AbstractContainerMenu) {
lens = ContainerUtil.generateLens(((AbstractContainerMenu) inventory), slotLensProvider);
} else if (size == 1) {
return slotLensProvider.getSlotLens(0);
}
if (lens != null) {
return lens;
}
return new SingleIndexedLens(0, size, (Class<? extends Inventory>) inventory.getClass(), slotLensProvider);
}
use of net.minecraft.world.inventory.AbstractContainerMenu in project SpongeCommon by SpongePowered.
the class KeyValueMatcherQuery method search.
private Map<Lens, Integer> search(final Inventory inventory, final Lens lens) {
if (inventory instanceof AbstractContainerMenu) {
final Map<Lens, Integer> matches = new LinkedHashMap<>();
// Search for Container Slot properties
for (final net.minecraft.world.inventory.Slot slot : ((AbstractContainerMenu) inventory).slots) {
if (this.matches(null, null, (Inventory) slot)) {
matches.put(((InventoryAdapter) slot).inventoryAdapter$getRootLens(), 0);
}
}
if (!matches.isEmpty()) {
return matches;
}
// Search for Container Viewed inventory properties
final Set<Inventory> viewedInventories = new HashSet<>();
for (final Slot slot : inventory.slots()) {
viewedInventories.add(slot.viewedSlot().parent());
}
// TODO does this work?
for (final Inventory viewedInventory : viewedInventories) {
if (this.matches(null, null, viewedInventory)) {
matches.put(((InventoryAdapter) viewedInventory).inventoryAdapter$getRootLens(), 0);
}
}
if (!matches.isEmpty()) {
this.delegateOffset(lens, matches);
return matches;
}
}
return this.depthLaterSearch(inventory, lens);
}
Aggregations