mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2025-06-30 12:44:44 +02:00
chore/feat: cleanup gui logic, extendable inventories / guis
This commit is contained in:
@ -27,29 +27,18 @@ package com.plotsquared.core.command;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
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.gui.PlotMusicInventory;
|
||||
import com.plotsquared.core.location.Location;
|
||||
import com.plotsquared.core.permissions.Permission;
|
||||
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.Permissions;
|
||||
import com.plotsquared.core.util.gui.PlotInventory;
|
||||
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 org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@CommandDeclaration(command = "music",
|
||||
permission = "plots.music",
|
||||
usage = "/plot music",
|
||||
@ -57,30 +46,13 @@ import java.util.stream.Stream;
|
||||
requiredType = RequiredType.PLAYER)
|
||||
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 PlotInventoryProvider<?, ?> inventoryProvider;
|
||||
|
||||
@Inject
|
||||
public Music(
|
||||
final @NonNull EventDispatcher eventDispatcher, final
|
||||
PlotInventoryProvider<?, ?> inventoryProvider
|
||||
final @NonNull EventDispatcher eventDispatcher,
|
||||
final PlotInventoryProvider<?, ?> inventoryProvider
|
||||
) {
|
||||
this.eventDispatcher = eventDispatcher;
|
||||
this.inventoryProvider = inventoryProvider;
|
||||
@ -110,66 +82,7 @@ public class Music extends SubCommand {
|
||||
return true;
|
||||
}
|
||||
|
||||
final PlotInventory<?, ?> inventory = inventoryProvider.createInventory(
|
||||
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);
|
||||
new PlotMusicInventory<>(inventoryProvider, player, plot, eventDispatcher).open();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -37,9 +37,10 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
*/
|
||||
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 Caption titleCaption;
|
||||
private final TagResolver[] titleResolvers;
|
||||
@ -52,7 +53,8 @@ public abstract class PlotInventory<P, I> {
|
||||
* @param titleResolvers The tag resolvers to use for the title
|
||||
* @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.titleCaption = titleCaption;
|
||||
this.titleResolvers = titleResolvers;
|
||||
@ -67,7 +69,7 @@ public abstract class PlotInventory<P, I> {
|
||||
* @param onClick The handler to call when clicking this item
|
||||
* @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.
|
||||
@ -88,7 +90,7 @@ public abstract class PlotInventory<P, I> {
|
||||
* @param onClick The handler to call when clicking this item
|
||||
* @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)
|
||||
@ -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
|
||||
*/
|
||||
public void open(PlotPlayer<?> player) {
|
||||
this.openPlatformPlayer((PlotPlayer<P>) player);
|
||||
}
|
||||
|
||||
protected abstract void openPlatformPlayer(PlotPlayer<P> player);
|
||||
public abstract void open();
|
||||
|
||||
/**
|
||||
* Close this inventory for a specific {@link PlotPlayer}
|
||||
* Close this inventory
|
||||
*
|
||||
* @param player The player to close this inventory for
|
||||
* @since TODO
|
||||
*/
|
||||
public void close(PlotPlayer<?> player) {
|
||||
this.closePlatformPlayer((PlotPlayer<P>) player);
|
||||
}
|
||||
|
||||
protected abstract void closePlatformPlayer(PlotPlayer<P> player);
|
||||
public abstract void close();
|
||||
|
||||
public abstract I toPlatformItem(PlotItemStack item);
|
||||
|
||||
@ -134,6 +126,14 @@ public abstract class PlotInventory<P, I> {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the associated player of this inventory
|
||||
* @return {@link PlotPlayer}
|
||||
*/
|
||||
public PlotPlayer<P> player() {
|
||||
return player;
|
||||
}
|
||||
|
||||
protected Caption titleCaption() {
|
||||
return titleCaption;
|
||||
}
|
||||
|
@ -25,11 +25,10 @@
|
||||
*/
|
||||
package com.plotsquared.core.util.gui;
|
||||
|
||||
import com.plotsquared.core.player.PlotPlayer;
|
||||
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);
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
package com.plotsquared.core.util.gui;
|
||||
|
||||
public enum PlotInventoryClickType {
|
||||
|
||||
LEFT,
|
||||
SHIFT_LEFT,
|
||||
RIGHT,
|
||||
SHIFT_RIGHT,
|
||||
MIDDLE,
|
||||
OTHER
|
||||
|
||||
}
|
@ -26,6 +26,7 @@
|
||||
package com.plotsquared.core.util.gui;
|
||||
|
||||
import com.plotsquared.core.configuration.caption.Caption;
|
||||
import com.plotsquared.core.player.PlotPlayer;
|
||||
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
|
||||
* @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);
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user