use of net.minecraft.world.inventory.MerchantMenu 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.MerchantMenu in project SpongeCommon by SpongePowered.
the class SelectTradeTransaction method createInventoryEvent.
@Override
Optional<ClickContainerEvent> createInventoryEvent(final List<SlotTransaction> slotTransactions, final List<Entity> entities, final PhaseContext<@NonNull ?> context, final Cause cause) {
final ItemStackSnapshot cursorItem = ItemStackUtil.snapshotOf(this.player.inventory.getCarried());
final Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(cursorItem, cursorItem);
if (this.menu instanceof MerchantMenu) {
final MerchantOffer offer = ((MerchantMenu) this.menu).getOffers().get(this.tradeItem);
final ClickContainerEvent.SelectTrade event = SpongeEventFactory.createClickContainerEventSelectTrade(cause, (Container) this.menu, cursorTransaction, Optional.empty(), (TradeOffer) offer, slotTransactions, this.tradeItem);
return Optional.of(event);
}
SpongeCommon.logger().warn("SelectTradeTransaction without MerchantMenu");
return Optional.empty();
}
use of net.minecraft.world.inventory.MerchantMenu in project SpongeCommon by SpongePowered.
the class ServerGamePacketListenerImplMixin_Inventory method impl$onHandleSelectTrade.
@Inject(method = "handleSelectTrade", at = @At("RETURN"))
private void impl$onHandleSelectTrade(final ServerboundSelectTradePacket param0, final CallbackInfo ci) {
if (this.player.containerMenu instanceof MerchantMenu) {
final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
final TransactionalCaptureSupplier transactor = context.getTransactor();
transactor.logSelectTrade(this.player, param0.getItem());
// capture
this.player.containerMenu.broadcastChanges();
}
}
Aggregations