use of org.spongepowered.api.item.inventory.type.ViewableInventory in project SpongeCommon by SpongePowered.
the class InventoryTest method doFancyStuff.
private static void doFancyStuff(final PluginContainer plugin, final Player player) {
final GridInventory inv27Grid = player.inventory().query(PrimaryPlayerInventory.class).get().storage();
final Inventory inv27Slots = Inventory.builder().slots(27).completeStructure().plugin(plugin).build();
final Inventory inv27Slots2 = Inventory.builder().slots(27).completeStructure().plugin(plugin).build();
final ViewableInventory doubleMyInventory = ViewableInventory.builder().type(ContainerTypes.GENERIC_9X6.get()).grid(inv27Slots.slots(), Vector2i.from(9, 3), Vector2i.from(0, 0)).grid(inv27Slots2.slots(), Vector2i.from(9, 3), Vector2i.from(0, 3)).completeStructure().carrier(player).plugin(plugin).build();
final InventoryMenu menu = doubleMyInventory.asMenu();
menu.setReadOnly(true);
doubleMyInventory.set(0, ItemStack.of(ItemTypes.IRON_INGOT));
doubleMyInventory.set(8, ItemStack.of(ItemTypes.GOLD_INGOT));
doubleMyInventory.set(45, ItemStack.of(ItemTypes.EMERALD));
doubleMyInventory.set(53, ItemStack.of(ItemTypes.DIAMOND));
menu.registerSlotClick(new MySlotClickHandler(plugin, menu, doubleMyInventory));
Sponge.server().scheduler().submit(Task.builder().plugin(plugin).execute(() -> {
menu.open((ServerPlayer) player);
}).build());
}
use of org.spongepowered.api.item.inventory.type.ViewableInventory 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 org.spongepowered.api.item.inventory.type.ViewableInventory in project SpongeCommon by SpongePowered.
the class SpongeViewableInventoryBuilder method build.
@Override
public ViewableInventory build() {
if (this.plugin == null) {
throw new IllegalStateException("Plugin has not been set on this builder!");
}
if (this.slotDefinitions.isEmpty() && this.info.size != 0) {
Inventory inventory = Inventory.builder().slots(this.info.size).completeStructure().plugin(this.plugin).build();
this.finalInventories = Arrays.asList(inventory);
}
final ViewableCustomInventory inventory = new ViewableCustomInventory(this.plugin, this.type, SpongeViewableInventoryBuilder.containerTypeInfo.get(this.type), this.info.size, this.finalLens, this.finalProvider, this.finalInventories, this.identity, this.carrier);
if (this.slotDefinitions.isEmpty()) {
inventory.vanilla();
}
return ((ViewableInventory) inventory);
}
Aggregations