Skip to content

Instantly share code, notes, and snippets.

@SocraticPhoenix
Last active January 12, 2017 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SocraticPhoenix/c7be9f432683270fc3c79aa55f35d3a5 to your computer and use it in GitHub Desktop.
Save SocraticPhoenix/c7be9f432683270fc3c79aa55f35d3a5 to your computer and use it in GitHub Desktop.
public class GridFinalizer implements Finalizer<InventoryButtonPage, GridTarget> {
@Override
public void display(Player player, GridTarget target, InventoryButtonPage page, PluginContainer owner) {
CustomInventoryBuilder builder = new CustomInventoryBuilder();
builder.of(InventoryArchetypes.CHEST).property(new InventoryDimension(9, target.height()))
.property(new InventoryTitle(page.title()));
int offset = (9 - target.length()) / 2;
offset = offset < 0 ? 0 : offset;
Inventory inventory = builder.build(owner.getInstance().get());
for (int x = 0; x < target.length(); x++) {
for (int y = 0; y < target.height(); y++) {
Inventory slot = inventory.query(new SlotPos(x + offset, y)).query(Slot.class);
if(slot instanceof Slot) {
target.get(x, y).ifPresent(stack -> ((Slot) slot).set(stack));
}
}
}
player.closeInventory(Cause.of(NamedCause.source(MenuPlugin.container()), NamedCause.of("reason", InventoryReason.NEW_MENU)));
player.openInventory(inventory, Cause.of(NamedCause.source(MenuPlugin.container()), NamedCause.of("reason", InventoryReason.OPEN_MENU)));
}
@Override
public Class<InventoryButtonPage> page() {
return InventoryButtonPage.class;
}
@Override
public Class<GridTarget> target() {
return GridTarget.class;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment