PlotSquared/Core/src/main/java/com/plotsquared/core/command/Music.java

176 lines
7.8 KiB
Java
Raw Normal View History

/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-04-15 21:26:54 +02:00
package com.plotsquared.core.command;
2016-03-22 18:53:56 +01:00
2020-07-11 17:19:19 +02:00
import com.google.inject.Inject;
2020-07-18 15:06:51 +02:00
import com.plotsquared.core.configuration.caption.TranslatableCaption;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.events.PlotFlagAddEvent;
import com.plotsquared.core.events.PlotFlagRemoveEvent;
import com.plotsquared.core.events.Result;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.permissions.Permission;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.player.PlotPlayer;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotInventory;
import com.plotsquared.core.plot.PlotItemStack;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.plot.flag.PlotFlag;
import com.plotsquared.core.plot.flag.implementations.MusicFlag;
import com.plotsquared.core.util.EventDispatcher;
2020-07-11 17:19:19 +02:00
import com.plotsquared.core.util.InventoryUtil;
import com.sk89q.worldedit.world.item.ItemType;
2019-11-04 20:55:55 +01:00
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 javax.annotation.Nullable;
2018-12-26 19:16:34 +01:00
import java.util.Arrays;
import java.util.Collection;
import java.util.Locale;
2016-03-22 18:53:56 +01:00
Pull/2693 (#2694) * Commit WIP flag work. * More ported flag types, and additions to the flag API. * Make PlotFlag more generic to allow generic flag creation * Pull Captions methods into a Caption interface. * Port MusicFlag * Port flight flag * Port UntrustedVisitFlag * Port DenyExitFlag * Remove paper suggestion * Make ListFlag lists immutable * Update Flag containers. Add javadocs. Add missing methods. * Port description flag * Port greeting and farewell flags * Port weather flag * Move getExample implementation to BooleanFlag * Port reserved flags * Port all boolean flags. * Remove unused flag types * Invert liquid-flow flag * Find the real (legacy) flag name * Change NOITFY -> NOTIFY in Captions * Make IntegerFlag extendable * Port integer flags * Update Flag command to current API state * Begin remaking flag command * Create DoubleFlag + extract common parsing stuff * Supply arguments in flag parse exceptions * Implement missing flag subcommands * Update Flag command to current API state * Implement PriceFlag * Port deny-teleport * Port gamemode flags * Port BreakFlag * Port PlaceFlag * Port UseFlag * Remove old unused flag constants * Port blocked-cmds flag * Fix entity util * Port TimeFlag * Use CaptionUtility for formatting * Port keep flag * Fix imports * Reformat code * Remove unused classes * Fix MainUtil.java * Remove FlagCmd * Add flag info header and footer * Comment out flag related stuff in SchematicHandler * Remove FlagManager * Finalize Plot.java * Finalize PlotArea.java * Finalize PlotListener * Fix API issues * Fix a bunch of compile errors * Fix `/plot flag remove` * Fix initialization of GlobalFlagContainer * Apply API changes to events * Update SQLManager to new API * Invert default value for DenyExitFlag * Replace flag.getValue().toString() with flag.toString() * Make FlagContainer instance in Plot final * Fix various command issues * Clean up PlotSettings * Don't show internal flags in flag list * Fix `/plot flag add` * Remove the info inventory as it's 100% broken * Add plot info entries and fix up the default format * Fix default flag state in Captions * 781c200 part 2 * Fix odd grammar in captions * Fix odd grammar in captions v2 * Add create table statements for plot_flags * Remove old flag references in SQLManager * Use the new plot_flags table * Add tab completion to `/plot flag` * Improve parse error handling * Make flag permission check recognize parse exceptions * Initial progress towards flag conversion * Fix minor issues * Don't validate flags during flag conversion * Allow unrecognized flags to be parsed * Filter out internal flags from command sugguestions * Use the wrong caption when there's no plot description set * Limit command suggestions for boolean flags * Make blocktypelistflags accept blockcategories * Require categories to be prefixed with '#' and fix some minor display issues * Fix plot database conversion * Update PlotFlagEvent.java Updated return description * Fix command annotation wrapping * Add FLAG_UPDATE event for FlagContainer listeners * Make getParentContainer nullable * Document castUnsafe in FlagContainer * Document FlagContainer constructors * Add missing documentation to FlagContainer * Document FlagParseException * Fix wording in FlagContainer javadoc * Document InternalFlag * Document PlotFlag * Minor changes * Remove revisit comments Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> Co-authored-by: NotMyFault <mc.cache@web.de> Co-authored-by: Matt <4009945+MattBDev@users.noreply.github.com>
2020-02-24 18:42:02 +01:00
@CommandDeclaration(command = "music",
permission = "plots.music",
usage = "/plot music",
category = CommandCategory.APPEARANCE,
requiredType = RequiredType.PLAYER)
Pull/2693 (#2694) * Commit WIP flag work. * More ported flag types, and additions to the flag API. * Make PlotFlag more generic to allow generic flag creation * Pull Captions methods into a Caption interface. * Port MusicFlag * Port flight flag * Port UntrustedVisitFlag * Port DenyExitFlag * Remove paper suggestion * Make ListFlag lists immutable * Update Flag containers. Add javadocs. Add missing methods. * Port description flag * Port greeting and farewell flags * Port weather flag * Move getExample implementation to BooleanFlag * Port reserved flags * Port all boolean flags. * Remove unused flag types * Invert liquid-flow flag * Find the real (legacy) flag name * Change NOITFY -> NOTIFY in Captions * Make IntegerFlag extendable * Port integer flags * Update Flag command to current API state * Begin remaking flag command * Create DoubleFlag + extract common parsing stuff * Supply arguments in flag parse exceptions * Implement missing flag subcommands * Update Flag command to current API state * Implement PriceFlag * Port deny-teleport * Port gamemode flags * Port BreakFlag * Port PlaceFlag * Port UseFlag * Remove old unused flag constants * Port blocked-cmds flag * Fix entity util * Port TimeFlag * Use CaptionUtility for formatting * Port keep flag * Fix imports * Reformat code * Remove unused classes * Fix MainUtil.java * Remove FlagCmd * Add flag info header and footer * Comment out flag related stuff in SchematicHandler * Remove FlagManager * Finalize Plot.java * Finalize PlotArea.java * Finalize PlotListener * Fix API issues * Fix a bunch of compile errors * Fix `/plot flag remove` * Fix initialization of GlobalFlagContainer * Apply API changes to events * Update SQLManager to new API * Invert default value for DenyExitFlag * Replace flag.getValue().toString() with flag.toString() * Make FlagContainer instance in Plot final * Fix various command issues * Clean up PlotSettings * Don't show internal flags in flag list * Fix `/plot flag add` * Remove the info inventory as it's 100% broken * Add plot info entries and fix up the default format * Fix default flag state in Captions * 781c200 part 2 * Fix odd grammar in captions * Fix odd grammar in captions v2 * Add create table statements for plot_flags * Remove old flag references in SQLManager * Use the new plot_flags table * Add tab completion to `/plot flag` * Improve parse error handling * Make flag permission check recognize parse exceptions * Initial progress towards flag conversion * Fix minor issues * Don't validate flags during flag conversion * Allow unrecognized flags to be parsed * Filter out internal flags from command sugguestions * Use the wrong caption when there's no plot description set * Limit command suggestions for boolean flags * Make blocktypelistflags accept blockcategories * Require categories to be prefixed with '#' and fix some minor display issues * Fix plot database conversion * Update PlotFlagEvent.java Updated return description * Fix command annotation wrapping * Add FLAG_UPDATE event for FlagContainer listeners * Make getParentContainer nullable * Document castUnsafe in FlagContainer * Document FlagContainer constructors * Add missing documentation to FlagContainer * Document FlagParseException * Fix wording in FlagContainer javadoc * Document InternalFlag * Document PlotFlag * Minor changes * Remove revisit comments Co-authored-by: Hannes Greule <SirYwell@users.noreply.github.com> Co-authored-by: NotMyFault <mc.cache@web.de> Co-authored-by: Matt <4009945+MattBDev@users.noreply.github.com>
2020-02-24 18:42:02 +01:00
public class Music extends SubCommand {
2016-03-23 02:41:37 +01:00
2019-02-04 14:59:11 +01:00
private static final Collection<String> DISCS = Arrays
.asList("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", "music_disc_5", "music_disc_relic"
);
2018-12-26 19:16:34 +01:00
2020-07-11 17:19:19 +02:00
private final InventoryUtil inventoryUtil;
private final EventDispatcher eventDispatcher;
2020-07-11 17:19:19 +02:00
@Inject
public Music(final @Nullable InventoryUtil inventoryUtil, final @NonNull EventDispatcher eventDispatcher) {
2020-07-11 17:19:19 +02:00
this.inventoryUtil = inventoryUtil;
this.eventDispatcher = eventDispatcher;
2020-07-11 17:19:19 +02:00
}
@Override
public boolean onCommand(PlotPlayer<?> player, String[] args) {
2019-09-08 20:02:45 +02:00
Location location = player.getLocation();
final Plot plot = location.getPlotAbs();
2016-03-22 18:53:56 +01:00
if (plot == null) {
2020-07-18 15:06:51 +02:00
player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
return false;
2016-03-22 18:53:56 +01:00
}
if (!plot.hasOwner()) {
player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
return false;
}
if (!plot.isAdded(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_MUSIC_OTHER)) {
player.sendMessage(
TranslatableCaption.of("permission.no_permission"),
TagResolver.resolver(
"node",
Tag.inserting(Permission.PERMISSION_ADMIN_MUSIC_OTHER)
)
);
2016-03-22 18:53:56 +01:00
return true;
}
2021-08-18 11:58:18 +02:00
PlotInventory inv = new PlotInventory(
this.inventoryUtil,
player,
2,
TranslatableCaption.of("plotjukebox.jukebox_header").getComponent(player)
) {
@Override
public boolean onClick(int index) {
2016-03-23 02:41:37 +01:00
PlotItemStack item = getItem(index);
2016-03-22 18:53:56 +01:00
if (item == null) {
return true;
}
2019-11-04 20:55:55 +01:00
if (item.getType() == ItemTypes.BEDROCK) {
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class)
.createFlagInstance(item.getType());
PlotFlagRemoveEvent event = eventDispatcher.callFlagRemove(plotFlag, plot);
if (event.getEventResult() == Result.DENY) {
2020-08-17 15:17:40 +02:00
getPlayer().sendMessage(
TranslatableCaption.of("events.event_denied"),
TagResolver.resolver("value", Tag.inserting(Component.text("Music removal")))
);
return true;
}
plot.removeFlag(event.getFlag());
2021-08-18 11:58:18 +02:00
getPlayer().sendMessage(
TranslatableCaption.of("flag.flag_removed"),
TagResolver.builder()
.tag("flag", Tag.inserting(Component.text("music")))
.tag("value", Tag.inserting(Component.text("music_disc")))
.build()
2021-07-20 22:21:32 +02:00
);
2020-08-17 15:17:40 +02:00
} else if (item.getName().toLowerCase(Locale.ENGLISH).contains("disc")) {
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class)
.createFlagInstance(item.getType());
PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot);
if (event.getEventResult() == Result.DENY) {
2020-08-17 15:17:40 +02:00
getPlayer().sendMessage(
TranslatableCaption.of("events.event_denied"),
TagResolver.resolver("value", Tag.inserting(Component.text("Music addition")))
);
return true;
}
plot.setFlag(event.getFlag());
getPlayer().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()
2021-08-18 11:58:18 +02:00
);
2018-12-26 19:16:34 +01:00
} else {
2020-08-17 15:17:40 +02:00
getPlayer().sendMessage(TranslatableCaption.of("flag.flag_not_added"));
2016-03-22 18:53:56 +01:00
}
return false;
}
};
int index = 0;
2018-12-26 19:16:34 +01:00
for (final String disc : DISCS) {
2020-08-17 13:24:15 +02:00
final String name = String.format("<gold>%s</gold>", disc);
final String[] lore = {TranslatableCaption.of("plotjukebox.click_to_play").getComponent(player)};
ItemType type = ItemTypes.get(disc);
if (type == null) {
continue;
}
final PlotItemStack item = new PlotItemStack(type, 1, name, lore);
if (inv.setItemChecked(index, item)) {
index++;
}
2016-03-22 18:53:56 +01:00
}
2018-12-26 19:16:34 +01:00
// Always add the cancel button
// if (player.getMeta("music") != null) {
String name = TranslatableCaption.of("plotjukebox.cancel_music").getComponent(player);
String[] lore = {TranslatableCaption.of("plotjukebox.reset_music").getComponent(player)};
2018-12-26 19:16:34 +01:00
inv.setItem(index, new PlotItemStack("bedrock", 1, name, lore));
// }
2016-03-22 18:53:56 +01:00
inv.openInventory();
return true;
}
2016-03-22 18:53:56 +01:00
}