chore/feat: cleanup gui logic, extendable inventories / guis

This commit is contained in:
Pierre Maurice Schwang 2022-06-04 16:30:33 +02:00
parent 7cb5df6253
commit dca9ec385f
10 changed files with 325 additions and 196 deletions

View File

@ -41,9 +41,7 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
@ -61,85 +59,45 @@ import java.util.WeakHashMap;
public class BukkitPlotInventory extends PlotInventory<Player, ItemStack> { public class BukkitPlotInventory extends PlotInventory<Player, ItemStack> {
private static Listener INVENTORY_LISTENER; private static Listener INVENTORY_LISTENER;
private final ItemStack[] items;
private final PlotInventoryClickHandler<Player>[] clickHandlers;
private final Map<UUID, Inventory> viewers = new WeakHashMap<>();
private static final Map<UUID, BukkitPlotInventory> INVENTORIES = new WeakHashMap<>(); private static final Map<UUID, BukkitPlotInventory> INVENTORIES = new WeakHashMap<>();
private final ItemStack[] items;
final PlotInventoryClickHandler[] clickHandlers;
Inventory nativeInventory;
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
protected BukkitPlotInventory(final int size, final Caption titleCaption, final TagResolver... titleResolvers) { protected BukkitPlotInventory(
super(size, titleCaption, titleResolvers); PlotPlayer<Player> player, final int size, final Caption titleCaption,
final TagResolver... titleResolvers
) {
super(player, size, titleCaption, titleResolvers);
this.items = new ItemStack[size]; this.items = new ItemStack[size];
this.clickHandlers = new PlotInventoryClickHandler[size]; this.clickHandlers = new PlotInventoryClickHandler[size];
this.nativeInventory = Bukkit.createInventory(player.getPlatformPlayer(), size(),
BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessage.miniMessage().deserialize(
titleCaption().getComponent(player), titleResolvers()
))
);
if (INVENTORY_LISTENER != null) { if (INVENTORY_LISTENER == null) {
return; INVENTORY_LISTENER = new BukkitPlotInventoryListener(INVENTORIES);
BukkitPlatform bukkitPlatform = ((BukkitPlatform) PlotSquared.platform());
bukkitPlatform.getServer().getPluginManager().registerEvents(INVENTORY_LISTENER, bukkitPlatform);
} }
INVENTORY_LISTENER = new Listener() {
@EventHandler
public void onInventoryClick(final org.bukkit.event.inventory.InventoryClickEvent event) {
final PlotPlayer<Player> player = BukkitUtil.adapt((Player) event.getWhoClicked());
BukkitPlotInventory currentInventory = INVENTORIES.get(player.getUUID());
if (currentInventory == null) {
return;
}
if (!Objects.equals(event.getClickedInventory(), currentInventory.viewers.get(player.getUUID()))) {
return;
}
final int slot = event.getRawSlot();
if (slot < 0 || slot >= currentInventory.size()) {
return;
}
final PlotInventoryClickHandler<Player> clickHandler = currentInventory.clickHandlers[slot];
if (clickHandler == null) {
return;
}
event.setCancelled(true);
final ItemStack item = event.getCurrentItem();
if (item == null) {
clickHandler.handle(null, player);
return;
}
clickHandler.handle(new PlotItemStack(
BukkitAdapter.asItemType(item.getType()),
item.getAmount(),
item.getItemMeta().getDisplayName(),
item.getItemMeta().hasLore() ? item.getItemMeta().getLore().toArray(String[]::new) : new String[0]
), player);
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
final PlotPlayer<Player> player = BukkitUtil.adapt((Player) event.getPlayer());
BukkitPlotInventory currentInventory = INVENTORIES.get(player.getUUID());
if (currentInventory == null) {
return;
}
currentInventory.viewers.remove(player.getUUID());
INVENTORIES.remove(player.getUUID());
}
};
BukkitPlatform bukkitPlatform = ((BukkitPlatform) PlotSquared.platform());
bukkitPlatform.getServer().getPluginManager().registerEvents(INVENTORY_LISTENER, bukkitPlatform);
} }
@Override @Override
public void setItem(final int slot, final PlotItemStack item, final PlotInventoryClickHandler<Player> onClick) { public void setItem(final int slot, final PlotItemStack item, final PlotInventoryClickHandler onClick) {
Preconditions.checkElementIndex(slot, size(), "Slot must be in range (0, " + size() + ")"); Preconditions.checkElementIndex(slot, size(), "Slot must be in range (0, " + size() + ")");
this.items[slot] = toPlatformItem(item); this.items[slot] = toPlatformItem(item);
this.clickHandlers[slot] = onClick; this.clickHandlers[slot] = onClick;
for (final Inventory value : viewers.values()) { this.nativeInventory.setItem(slot, this.items[slot]);
value.setItem(slot, this.items[slot]);
}
} }
@Override @Override
public void addItem(final PlotItemStack item, final PlotInventoryClickHandler<Player> onClick) { public void addItem(final PlotItemStack item, final PlotInventoryClickHandler onClick) {
// TODO: probably needs more love (who doesn't) // TODO: probably needs more love (who doesn't)
int slot = -1; int slot = -1;
// try to fill stacks // try to fill stacks
@ -161,31 +119,19 @@ public class BukkitPlotInventory extends PlotInventory<Player, ItemStack> {
Preconditions.checkElementIndex(slot, size()); Preconditions.checkElementIndex(slot, size());
this.items[slot] = toPlatformItem(item); this.items[slot] = toPlatformItem(item);
this.clickHandlers[slot] = onClick; this.clickHandlers[slot] = onClick;
for (final Inventory value : viewers.values()) { this.nativeInventory.setItem(slot, this.items[slot]);
value.setItem(slot, this.items[slot]);
}
} }
@Override @Override
protected void openPlatformPlayer(final PlotPlayer<Player> player) { public void open() {
Component title = MiniMessage.miniMessage().deserialize( INVENTORIES.put(player().getUUID(), this);
titleCaption().getComponent(player), titleResolvers() player().getPlatformPlayer().openInventory(this.nativeInventory);
);
Inventory inventory = Bukkit.createInventory(player.getPlatformPlayer(), size(),
BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(title)
);
for (int i = 0; i < items.length; i++) {
inventory.setItem(i, items[i]);
}
INVENTORIES.put(player.getUUID(), this);
viewers.put(player.getUUID(), inventory);
player.getPlatformPlayer().openInventory(inventory);
} }
@Override @Override
protected void closePlatformPlayer(final PlotPlayer<Player> player) { public void close() {
if (player.getPlatformPlayer().getOpenInventory().getTopInventory().equals(INVENTORIES.get(player.getUUID()))) { if (Objects.equals(player().getPlatformPlayer().getOpenInventory().getTopInventory(), this.nativeInventory)) {
player.getPlatformPlayer().closeInventory(); player().getPlatformPlayer().closeInventory();
} }
} }

View File

@ -0,0 +1,87 @@
package com.plotsquared.bukkit.util.gui;
import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.PlotItemStack;
import com.plotsquared.core.util.gui.PlotInventoryClickHandler;
import com.plotsquared.core.util.gui.PlotInventoryClickType;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.ItemStack;
import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
public class BukkitPlotInventoryListener implements Listener {
private static final EnumMap<ClickType, PlotInventoryClickType> CLICK_MAPPING = new EnumMap<>(ClickType.class);
static {
CLICK_MAPPING.put(ClickType.LEFT, PlotInventoryClickType.LEFT);
CLICK_MAPPING.put(ClickType.RIGHT, PlotInventoryClickType.RIGHT);
CLICK_MAPPING.put(ClickType.SHIFT_LEFT, PlotInventoryClickType.SHIFT_LEFT);
CLICK_MAPPING.put(ClickType.SHIFT_RIGHT, PlotInventoryClickType.SHIFT_RIGHT);
CLICK_MAPPING.put(ClickType.MIDDLE, PlotInventoryClickType.MIDDLE);
}
private final Map<UUID, BukkitPlotInventory> inventories;
public BukkitPlotInventoryListener(final Map<UUID, BukkitPlotInventory> inventories) {
this.inventories = inventories;
}
@EventHandler
public void onInventoryClick(final org.bukkit.event.inventory.InventoryClickEvent event) {
final PlotPlayer<Player> player = BukkitUtil.adapt((Player) event.getWhoClicked());
BukkitPlotInventory currentInventory = inventories.get(player.getUUID());
if (currentInventory == null) {
return;
}
if (!Objects.equals(event.getClickedInventory(), currentInventory.nativeInventory)) {
return;
}
final int slot = event.getRawSlot();
if (slot < 0 || slot >= currentInventory.size()) {
return;
}
PlotInventoryClickType clickType = CLICK_MAPPING.getOrDefault(event.getClick(), PlotInventoryClickType.OTHER);
event.setCancelled(true);
final PlotInventoryClickHandler clickHandler = currentInventory.clickHandlers[slot];
if (clickHandler == null) {
return;
}
final ItemStack item = event.getCurrentItem();
if (item == null) {
clickHandler.handle(null, clickType);
return;
}
clickHandler.handle(new PlotItemStack(
BukkitAdapter.asItemType(item.getType()),
item.getAmount(),
item.getItemMeta().getDisplayName(),
item.getItemMeta().hasLore() ? item.getItemMeta().getLore().toArray(String[]::new) : new String[0]
), clickType);
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
final PlotPlayer<Player> player = BukkitUtil.adapt((Player) event.getPlayer());
BukkitPlotInventory currentInventory = inventories.get(player.getUUID());
if (currentInventory == null) {
return;
}
currentInventory.nativeInventory = null;
inventories.remove(player.getUUID());
}
}

View File

@ -26,6 +26,7 @@
package com.plotsquared.bukkit.util.gui; package com.plotsquared.bukkit.util.gui;
import com.plotsquared.core.configuration.caption.Caption; import com.plotsquared.core.configuration.caption.Caption;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.util.gui.PlotInventory; import com.plotsquared.core.util.gui.PlotInventory;
import com.plotsquared.core.util.gui.PlotInventoryProvider; import com.plotsquared.core.util.gui.PlotInventoryProvider;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
@ -36,11 +37,12 @@ public class BukkitPlotInventoryProvider implements PlotInventoryProvider<Player
@Override @Override
public PlotInventory<Player, ItemStack> createInventory( public PlotInventory<Player, ItemStack> createInventory(
PlotPlayer<?> player,
final int size, final int size,
final Caption titleCaption, final Caption titleCaption,
final TagResolver... titleResolvers final TagResolver... titleResolvers
) { ) {
return new BukkitPlotInventory(size, titleCaption, titleResolvers); return new BukkitPlotInventory((PlotPlayer<Player>) player, size, titleCaption, titleResolvers);
} }
} }

View File

@ -27,29 +27,18 @@ package com.plotsquared.core.command;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.events.PlotFlagAddEvent; import com.plotsquared.core.gui.PlotMusicInventory;
import com.plotsquared.core.events.PlotFlagRemoveEvent;
import com.plotsquared.core.events.Result;
import com.plotsquared.core.location.Location; import com.plotsquared.core.location.Location;
import com.plotsquared.core.permissions.Permission; import com.plotsquared.core.permissions.Permission;
import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotItemStack;
import com.plotsquared.core.plot.flag.PlotFlag;
import com.plotsquared.core.plot.flag.implementations.MusicFlag;
import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.Permissions; import com.plotsquared.core.util.Permissions;
import com.plotsquared.core.util.gui.PlotInventory;
import com.plotsquared.core.util.gui.PlotInventoryProvider; import com.plotsquared.core.util.gui.PlotInventoryProvider;
import com.sk89q.worldedit.world.item.ItemTypes;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.Tag; import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.List;
import java.util.stream.Stream;
@CommandDeclaration(command = "music", @CommandDeclaration(command = "music",
permission = "plots.music", permission = "plots.music",
usage = "/plot music", usage = "/plot music",
@ -57,30 +46,13 @@ import java.util.stream.Stream;
requiredType = RequiredType.PLAYER) requiredType = RequiredType.PLAYER)
public class Music extends SubCommand { public class Music extends SubCommand {
private static final List<String> DISCS = Stream.of(
"music_disc_13",
"music_disc_cat",
"music_disc_blocks",
"music_disc_chirp",
"music_disc_far",
"music_disc_mall",
"music_disc_mellohi",
"music_disc_stal",
"music_disc_strad",
"music_disc_ward",
"music_disc_11",
"music_disc_wait",
"music_disc_otherside",
"music_disc_pigstep"
).filter(s -> ItemTypes.get(s) != null).toList();
private final EventDispatcher eventDispatcher; private final EventDispatcher eventDispatcher;
private final PlotInventoryProvider<?, ?> inventoryProvider; private final PlotInventoryProvider<?, ?> inventoryProvider;
@Inject @Inject
public Music( public Music(
final @NonNull EventDispatcher eventDispatcher, final final @NonNull EventDispatcher eventDispatcher,
PlotInventoryProvider<?, ?> inventoryProvider final PlotInventoryProvider<?, ?> inventoryProvider
) { ) {
this.eventDispatcher = eventDispatcher; this.eventDispatcher = eventDispatcher;
this.inventoryProvider = inventoryProvider; this.inventoryProvider = inventoryProvider;
@ -110,66 +82,7 @@ public class Music extends SubCommand {
return true; return true;
} }
final PlotInventory<?, ?> inventory = inventoryProvider.createInventory( new PlotMusicInventory<>(inventoryProvider, player, plot, eventDispatcher).open();
9 * 2,
TranslatableCaption.of("plotjukebox.jukebox_header")
);
for (final String disc : DISCS) {
PlotItemStack itemStack = new PlotItemStack(
disc, 1, String.format("<gold>%s</gold>", disc),
TranslatableCaption.of("plotjukebox.click_to_play").getComponent(player)
);
inventory.addItem(itemStack, (item, clicker) -> {
clicker.closeInventory();
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class)
.createFlagInstance(item.getType());
PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot);
if (event.getEventResult() == Result.DENY) {
clicker.sendMessage(
TranslatableCaption.of("events.event_denied"),
TagResolver.resolver("value", Tag.inserting(Component.text("Music addition")))
);
return;
}
plot.setFlag(event.getFlag());
clicker.sendMessage(
TranslatableCaption.of("flag.flag_added"),
TagResolver.builder()
.tag("flag", Tag.inserting(Component.text("music")))
.tag("value", Tag.inserting(Component.text(event.getFlag().getValue().toString())))
.build()
);
});
}
PlotItemStack cancelItem = new PlotItemStack(
ItemTypes.BEDROCK, 1,
TranslatableCaption.of("plotjukebox.cancel_music").getComponent(player),
TranslatableCaption.of("plotjukebox.reset_music").getComponent(player)
);
inventory.setItem(inventory.size() - 1, cancelItem, (item, clicker) -> {
clicker.closeInventory();
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class)
.createFlagInstance(item.getType());
PlotFlagRemoveEvent event = eventDispatcher.callFlagRemove(plotFlag, plot);
if (event.getEventResult() == Result.DENY) {
clicker.sendMessage(
TranslatableCaption.of("events.event_denied"),
TagResolver.resolver("value", Tag.inserting(Component.text("Music removal")))
);
return;
}
plot.removeFlag(event.getFlag());
clicker.sendMessage(
TranslatableCaption.of("flag.flag_removed"),
TagResolver.builder()
.tag("flag", Tag.inserting(Component.text("music")))
.tag("value", Tag.inserting(Component.text("music_disc")))
.build()
);
});
inventory.open(player);
return true; return true;
} }

View File

@ -0,0 +1,120 @@
package com.plotsquared.core.gui;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.events.PlotFlagAddEvent;
import com.plotsquared.core.events.PlotFlagRemoveEvent;
import com.plotsquared.core.events.Result;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotItemStack;
import com.plotsquared.core.plot.flag.PlotFlag;
import com.plotsquared.core.plot.flag.implementations.MusicFlag;
import com.plotsquared.core.util.EventDispatcher;
import com.plotsquared.core.util.gui.ExtendablePlotInventory;
import com.plotsquared.core.util.gui.PlotInventoryProvider;
import com.sk89q.worldedit.world.item.ItemTypes;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import java.util.List;
import java.util.stream.Stream;
public class PlotMusicInventory<P, I> extends ExtendablePlotInventory<P, I> {
private static final List<String> DISCS = Stream.of(
"music_disc_13",
"music_disc_cat",
"music_disc_blocks",
"music_disc_chirp",
"music_disc_far",
"music_disc_mall",
"music_disc_mellohi",
"music_disc_stal",
"music_disc_strad",
"music_disc_ward",
"music_disc_11",
"music_disc_wait",
"music_disc_otherside",
"music_disc_pigstep"
).filter(s -> ItemTypes.get(s) != null).toList();
private final Plot plot;
private final EventDispatcher eventDispatcher;
public PlotMusicInventory(
final PlotInventoryProvider<P, I> provider,
PlotPlayer<?> player,
Plot plot,
EventDispatcher eventDispatcher
) {
super(
provider, player, 2 * 9,
TranslatableCaption.of("plotjukebox.jukebox_header")
);
this.plot = plot;
this.eventDispatcher = eventDispatcher;
setDiscs();
setCancelButton();
}
private void setDiscs() {
for (final String disc : DISCS) {
PlotItemStack itemStack = new PlotItemStack(
disc, 1, String.format("<gold>%s</gold>", disc),
TranslatableCaption.of("plotjukebox.click_to_play").getComponent(player())
);
addItem(itemStack, (item, type) -> {
close();
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class)
.createFlagInstance(item.getType());
PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot);
if (event.getEventResult() == Result.DENY) {
player().sendMessage(
TranslatableCaption.of("events.event_denied"),
TagResolver.resolver("value", Tag.inserting(Component.text("Music addition")))
);
return;
}
plot.setFlag(event.getFlag());
player().sendMessage(
TranslatableCaption.of("flag.flag_added"),
TagResolver.builder()
.tag("flag", Tag.inserting(Component.text("music")))
.tag("value", Tag.inserting(Component.text(event.getFlag().getValue().toString())))
.build()
);
});
}
}
private void setCancelButton() {
PlotItemStack cancelItem = new PlotItemStack(
ItemTypes.BEDROCK, 1,
TranslatableCaption.of("plotjukebox.cancel_music").getComponent(player()),
TranslatableCaption.of("plotjukebox.reset_music").getComponent(player())
);
setItem(size() - 1, cancelItem, (item, type) -> {
close();
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class)
.createFlagInstance(item.getType());
PlotFlagRemoveEvent event = eventDispatcher.callFlagRemove(plotFlag, plot);
if (event.getEventResult() == Result.DENY) {
player().sendMessage(
TranslatableCaption.of("events.event_denied"),
TagResolver.resolver("value", Tag.inserting(Component.text("Music removal")))
);
return;
}
plot.removeFlag(event.getFlag());
player().sendMessage(
TranslatableCaption.of("flag.flag_removed"),
TagResolver.builder()
.tag("flag", Tag.inserting(Component.text("music")))
.tag("value", Tag.inserting(Component.text("music_disc")))
.build()
);
});
}
}

View File

@ -0,0 +1,49 @@
package com.plotsquared.core.util.gui;
import com.plotsquared.core.configuration.caption.Caption;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.PlotItemStack;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
public class ExtendablePlotInventory<P, I> extends PlotInventory<P, I> {
private final PlotInventory<P, I> delegate;
public ExtendablePlotInventory(PlotInventory<P, I> delegate) {
super(delegate.player(), delegate.size(), delegate.titleCaption(), delegate.titleResolvers());
this.delegate = delegate;
}
public ExtendablePlotInventory(
PlotInventoryProvider<P, I> provider, PlotPlayer<?> player, int size, Caption title,
TagResolver... titleResolver
) {
this(provider.createInventory(player, size, title, titleResolver));
}
@Override
public void setItem(final int slot, final PlotItemStack item, final PlotInventoryClickHandler onClick) {
delegate.setItem(slot, item, onClick);
}
@Override
public void addItem(final PlotItemStack item, final PlotInventoryClickHandler onClick) {
delegate.addItem(item, onClick);
}
@Override
public void open() {
delegate.open();
}
@Override
public void close() {
delegate.close();
}
@Override
public I toPlatformItem(final PlotItemStack item) {
return delegate.toPlatformItem(item);
}
}

View File

@ -37,9 +37,10 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
*/ */
public abstract class PlotInventory<P, I> { public abstract class PlotInventory<P, I> {
private final PlotInventoryClickHandler<P> NOOP_CLICK_HANDLER = (x, y) -> { private final PlotInventoryClickHandler NOOP_CLICK_HANDLER = (x, y) -> {
}; };
private final PlotPlayer<P> player;
private final int size; private final int size;
private final Caption titleCaption; private final Caption titleCaption;
private final TagResolver[] titleResolvers; private final TagResolver[] titleResolvers;
@ -52,7 +53,8 @@ public abstract class PlotInventory<P, I> {
* @param titleResolvers The tag resolvers to use for the title * @param titleResolvers The tag resolvers to use for the title
* @since 7.0.0 * @since 7.0.0
*/ */
protected PlotInventory(int size, Caption titleCaption, TagResolver... titleResolvers) { protected PlotInventory(PlotPlayer<P> player, int size, Caption titleCaption, TagResolver... titleResolvers) {
this.player = player;
this.size = size; this.size = size;
this.titleCaption = titleCaption; this.titleCaption = titleCaption;
this.titleResolvers = titleResolvers; this.titleResolvers = titleResolvers;
@ -67,7 +69,7 @@ public abstract class PlotInventory<P, I> {
* @param onClick The handler to call when clicking this item * @param onClick The handler to call when clicking this item
* @since TODO * @since TODO
*/ */
public abstract void setItem(int slot, PlotItemStack item, PlotInventoryClickHandler<P> onClick); public abstract void setItem(int slot, PlotItemStack item, PlotInventoryClickHandler onClick);
/** /**
* Set an item in this inventory at a specific slot / index. * Set an item in this inventory at a specific slot / index.
@ -88,7 +90,7 @@ public abstract class PlotInventory<P, I> {
* @param onClick The handler to call when clicking this item * @param onClick The handler to call when clicking this item
* @since TODO * @since TODO
*/ */
public abstract void addItem(PlotItemStack item, PlotInventoryClickHandler<P> onClick); public abstract void addItem(PlotItemStack item, PlotInventoryClickHandler onClick);
/** /**
* Add an item to this inventory, at the first slot possible (first empty slot, or first slot with the exact same item) * Add an item to this inventory, at the first slot possible (first empty slot, or first slot with the exact same item)
@ -101,28 +103,18 @@ public abstract class PlotInventory<P, I> {
} }
/** /**
* Opens this inventory for a specific {@link PlotPlayer} * Opens this inventory
* *
* @param player The player to open this inventory for
* @since TODO * @since TODO
*/ */
public void open(PlotPlayer<?> player) { public abstract void open();
this.openPlatformPlayer((PlotPlayer<P>) player);
}
protected abstract void openPlatformPlayer(PlotPlayer<P> player);
/** /**
* Close this inventory for a specific {@link PlotPlayer} * Close this inventory
* *
* @param player The player to close this inventory for
* @since TODO * @since TODO
*/ */
public void close(PlotPlayer<?> player) { public abstract void close();
this.closePlatformPlayer((PlotPlayer<P>) player);
}
protected abstract void closePlatformPlayer(PlotPlayer<P> player);
public abstract I toPlatformItem(PlotItemStack item); public abstract I toPlatformItem(PlotItemStack item);
@ -134,6 +126,14 @@ public abstract class PlotInventory<P, I> {
return size; return size;
} }
/**
* Get the associated player of this inventory
* @return {@link PlotPlayer}
*/
public PlotPlayer<P> player() {
return player;
}
protected Caption titleCaption() { protected Caption titleCaption() {
return titleCaption; return titleCaption;
} }

View File

@ -25,11 +25,10 @@
*/ */
package com.plotsquared.core.util.gui; package com.plotsquared.core.util.gui;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.PlotItemStack; import com.plotsquared.core.plot.PlotItemStack;
public interface PlotInventoryClickHandler<P> { public interface PlotInventoryClickHandler {
void handle(PlotItemStack itemStack, PlotPlayer<P> plotPlayer); void handle(PlotItemStack itemStack, PlotInventoryClickType clickType);
} }

View File

@ -0,0 +1,12 @@
package com.plotsquared.core.util.gui;
public enum PlotInventoryClickType {
LEFT,
SHIFT_LEFT,
RIGHT,
SHIFT_RIGHT,
MIDDLE,
OTHER
}

View File

@ -26,6 +26,7 @@
package com.plotsquared.core.util.gui; package com.plotsquared.core.util.gui;
import com.plotsquared.core.configuration.caption.Caption; import com.plotsquared.core.configuration.caption.Caption;
import com.plotsquared.core.player.PlotPlayer;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
/** /**
@ -44,6 +45,6 @@ public interface PlotInventoryProvider<P, I> {
* @param titleResolvers The (optional) placeholder resolvers for the inventory * @param titleResolvers The (optional) placeholder resolvers for the inventory
* @return The platform inventory * @return The platform inventory
*/ */
PlotInventory<P, I> createInventory(int size, Caption titleCaption, TagResolver... titleResolvers); PlotInventory<P, I> createInventory(PlotPlayer<?> player, int size, Caption titleCaption, TagResolver... titleResolvers);
} }