Add item selection GUI (WIP)

This commit is contained in:
GeorgH93 2020-05-15 22:17:54 +02:00
parent a069874c01
commit 7fa624a0b1
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
13 changed files with 192 additions and 23 deletions

View File

@ -176,17 +176,19 @@
<pattern>at.pcgamingfreaks.Minepacks</pattern>
<shadedPattern>at.pcgamingfreaks.MinepacksStandalone</shadedPattern>
<excludes>
<exclude>at.pcgamingfreaks.Minepacks.Bukkit.API.*</exclude>
<exclude>at.pcgamingfreaks.Minepacks.Bukkit.API.**</exclude>
</excludes>
</relocation>
<relocation>
<pattern>at.pcgamingfreaks</pattern>
<shadedPattern>at.pcgamingfreaks.MinepacksStandalone.libs.at.pcgamingfreaks</shadedPattern>
<excludes>
<exclude>at.pcgamingfreaks.Minepacks.Bukkit.API.Events.**</exclude>
<exclude>at.pcgamingfreaks.Minepacks.Bukkit.API.Backpack</exclude>
<exclude>at.pcgamingfreaks.Minepacks.Bukkit.API.Callback</exclude>
<exclude>at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksPlugin</exclude>
<exclude>at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksCommandManager</exclude>
<exclude>at.pcgamingfreaks.**.IMessage</exclude>
</excludes>
</relocation>
</relocations>

View File

@ -1,3 +1,7 @@
Defaults:
# The item that should be used as a shortcut by default
BackpackItem: BackpackHeadBlue
# The items define the possible backpack items that can be used as shortcuts
Items:
# Example item that uses a player head

View File

@ -166,8 +166,10 @@ ItemShortcut:
# If enabled the players will be given an item they can interact with to open the backpack.
# The item may not be removed from their inventory as long as this option is enabled.
Enabled: true
# The item that should be used as a shortcut, you can create custom ones in the backpacks.yml file
Item: BackpackHeadBlue
# Allows players to chose how the backpack items should look like. They also need the "backpack.choseDesign" permission.
AllowPlayersToChoseItem: true
# Allows players to disable the backpack item. Only effective if AllowPlayersToChoseItem is enabled.
AllowPlayersToDisableItem: true
# Increases compatibility with some death chest plugins. Enable this if the backpack item is added to your death chest!
ImproveDeathChestCompatibility: false
# Prevents the backpack from being used as a hat

View File

@ -91,6 +91,7 @@ Language:
Help: "Zeigt die verfügbaren Befehle an."
Migrate: "Erlaubt das Konvertieren der Datenbank in ein anderes Format."
Shortcut: "Fügt den Rucksack in dein Inventar hinzu."
ShortcutChose: "Ändert das aussehen deines Rucksacks."
Command:
Backpack:
@ -131,6 +132,10 @@ Command:
- clear
- inventoryclear
- clean
Switches:
Set:
- set
- setzen
# Will be shown in the console during startup
LanguageName: "german"

View File

@ -92,6 +92,7 @@ Language:
Help: "Shows all available commands and their description."
Migrate: "Migrates the used database from one type to another."
Shortcut: "Adds the backpack shortcut item to your inventory."
ShortcutChose: "Opens a dialog to chose how your backpack item should look like."
Command:
Backpack:
@ -126,6 +127,9 @@ Command:
- clear
- inventoryclear
- clean
Switches:
Set:
- set
# Will be shown in the console during startup
LanguageName: "english"

View File

@ -24,6 +24,7 @@ permissions:
backpack.size.1: true
backpack.clean: true
backpack.sort: true
backpack.choseDesign: true
backpack.userBig:
description: This permission can be used to give a user the biggest backpack size and allow him to use the backpack.
children:
@ -74,6 +75,9 @@ permissions:
backpack.fullpickup:
description: Allows the player to automatically pick up items when their inventory is full (function needs to be enabled in the config)
defaut: true
backpack.choseDesign:
description: Allows the player to chose the design of the backpack item shortcut.
default: true
backpack.clean.other:
description: Allows the player to clean other players backpacks.
default: op

View File

@ -17,40 +17,159 @@
package at.pcgamingfreaks.Minepacks.Bukkit.Command;
import at.pcgamingfreaks.Bukkit.GUI.GuiBuilder;
import at.pcgamingfreaks.Bukkit.GUI.GuiButton;
import at.pcgamingfreaks.Bukkit.GUI.IGui;
import at.pcgamingfreaks.Bukkit.Message.Message;
import at.pcgamingfreaks.Command.HelpData;
import at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksCommand;
import at.pcgamingfreaks.Minepacks.Bukkit.Item.ItemConfig;
import at.pcgamingfreaks.Minepacks.Bukkit.Listener.ItemShortcut;
import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks;
import at.pcgamingfreaks.Minepacks.Bukkit.Permissions;
import at.pcgamingfreaks.StringUtils;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/*if[STANDALONE]
import at.pcgamingfreaks.Bukkit.GUI.GuiListener;
end[STANDALONE]*/
public class ShortcutCommand extends MinepacksCommand
{
private final Message messageItemGiven;
private final String[] setSwitch;
private final String descriptionChose;
private final Message messageItemGiven, messageShortcutSet, messageUnknownShortcutStyle;
private final ItemShortcut itemShortcut;
private final Set<String> validShortcutStyles;
private final IGui gui;
private final boolean playerChoice, allowPlayerDisable;
public ShortcutCommand(Minepacks plugin, final @NotNull ItemShortcut itemShortcut)
public ShortcutCommand(final @NotNull Minepacks plugin, final @NotNull ItemShortcut itemShortcut)
{
super(plugin, "shortcut", plugin.getLanguage().getTranslated("Commands.Description.Shortcut"), Permissions.USE, true, plugin.getLanguage().getCommandAliases("Shortcut"));
this.itemShortcut = itemShortcut;
descriptionChose = plugin.getLanguage().getTranslated("Commands.Description.ShortcutChose");
messageItemGiven = plugin.getLanguage().getMessage("Ingame.Shortcut.Given");
messageShortcutSet = plugin.getLanguage().getMessage("Ingame.Shortcut.Set");
messageUnknownShortcutStyle = plugin.getLanguage().getMessage("Ingame.Shortcut.UnknownShortcutStyle").replaceAll("\\{ShortcutStyle}", "%s");
setSwitch = plugin.getLanguage().getSwitch("Set", "set");
playerChoice = plugin.getConfiguration().isItemShortcutPlayerChoiceEnabled();
allowPlayerDisable = plugin.getConfiguration().isItemShortcutPlayerDisableItemEnabled();
/*if[STANDALONE]
plugin.getServer().getPluginManager().registerEvents(new GuiListener(), plugin);
end[STANDALONE]*/
if(playerChoice)
{
validShortcutStyles = plugin.getBackpacksConfig().getBackpackItems().stream().map(ItemConfig::getName).collect(Collectors.toSet());
validShortcutStyles.add("default");
if(allowPlayerDisable) validShortcutStyles.add("none");
gui = buildGui(plugin);
}
else
{
validShortcutStyles = null;
gui = null;
}
}
private @NotNull IGui buildGui(final @NotNull Minepacks plugin)
{
final String setCommandBase = plugin.getLanguage().getCommandAliases("Backpack", "backpack")[0] + ' ' + plugin.getLanguage().getCommandAliases("Shortcut", "shortcut")[0] + ' ' + setSwitch[0] + ' ';
final List<ItemConfig> backpackItems = plugin.getBackpacksConfig().getBackpackItems();
final int buttonCount = backpackItems.size() + (allowPlayerDisable ? 2 : 1);
final int buttonCountAligned = ((buttonCount / 9) + 1) * 9; // Aligns it to a multiple of 9
final GuiBuilder guiBuilder = new GuiBuilder(plugin.getLanguage().getTranslated("Ingame.Shortcut.GUI.Title"));
//region add item buttons
for(ItemConfig itemConfig : backpackItems)
{
GuiButton button = new GuiButton(itemConfig.make(1), (player, clickType, cursor) -> { player.performCommand(setCommandBase + itemConfig.getName()); player.closeInventory(); });
guiBuilder.addButton(button);
}
//endregion
//region add empty slots to place special buttons at the end of the line
for(int i = buttonCount; i < buttonCountAligned; i++)
{
guiBuilder.addButton(GuiButton.EMPTY_BUTTON);
}
//endregion
//region set default button
ItemStack item = new ItemConfig("default", "BARRIER", 1, "default", null, -1, null).make();
guiBuilder.addButton(new GuiButton(item, (player, clickType, cursor) -> { player.performCommand(setCommandBase + "default"); player.closeInventory(); }));
//endregion
//region set disable button
if(allowPlayerDisable)
{
item = new ItemStack(Material.BARRIER);
guiBuilder.addButton(new GuiButton(item, (player, clickType, cursor) -> { player.performCommand(setCommandBase + "none"); player.closeInventory(); }));
}
//endregion
return guiBuilder.build();
}
@Override
public void execute(@NotNull CommandSender sender, @NotNull String mainCommandAlias, @NotNull String alias, @NotNull String[] args)
{
itemShortcut.addItem((Player) sender);
messageItemGiven.send(sender);
if(playerChoice && args.length >= 1 && sender.hasPermission(Permissions.CHOSE_DESIGN) && StringUtils.arrayContainsIgnoreCase(setSwitch, args[0]))
{
if(args.length == 1) gui.show((Player) sender);
else
{
if(validShortcutStyles.contains(args[1])) //TODO make test case insensitive
{
//TODO set shortcut
messageShortcutSet.send(sender); //TODO add more information
}
else
{
messageUnknownShortcutStyle.send(sender, args[1]);
}
}
}
else
{
itemShortcut.addItem((Player) sender);
messageItemGiven.send(sender);
}
}
@Override
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String mainCommandAlias, @NotNull String alias, @NotNull String[] args)
{
if(playerChoice && sender.hasPermission(Permissions.CHOSE_DESIGN) && args.length > 0)
{
final String arg = args[args.length - 1].toLowerCase(Locale.ENGLISH);
if(args.length == 1)
return Arrays.stream(setSwitch).filter(s -> s.toLowerCase(Locale.ENGLISH).startsWith(arg)).collect(Collectors.toList());
else if(args.length == 2)
return validShortcutStyles.stream().filter(style -> style.toLowerCase(Locale.ENGLISH).startsWith(arg)).collect(Collectors.toList());
}
return null;
}
@Override
public @Nullable List<HelpData> getHelp(final @NotNull CommandSender sender)
{
List<HelpData> help = new ArrayList<>(2);
if(playerChoice && sender.hasPermission(Permissions.CHOSE_DESIGN))
{
help.add(new HelpData(getTranslatedName(), "set", descriptionChose));
}
//noinspection ConstantConditions
help.addAll(super.getHelp(sender));
return help;
}
}

View File

@ -27,10 +27,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Getter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -39,14 +38,19 @@ public class BackpacksConfig extends Configuration
{
private static final int CONFIG_VERSION = 1;
private static final Pattern ITEM_TEXT_PLACEHOLDER_PATTERN = Pattern.compile("\\{(?<placeholder>[\\w-.]+)}");
@Getter private static BackpacksConfig instance;
private final @NotNull Minepacks plugin;
private final Map<String, ItemConfig> itemConfigs = new HashMap<>();
@Getter private final Set<String> validShortcutStyles = new HashSet<>();
@Getter private String defaultBackpackItem = "";
@Getter private boolean allowItemShortcut = true;
public BackpacksConfig(final @NotNull Minepacks plugin)
{
super(plugin, CONFIG_VERSION, CONFIG_VERSION, "backpacks.yml");
this.plugin = plugin;
instance = this;
}
@Override
@ -61,15 +65,29 @@ protected boolean newConfigCreated()
}
@Override
protected void doUpgrade(@NotNull YamlFileManager oldConfig)
protected void doUpgrade(final @NotNull YamlFileManager oldConfig)
{
doUpgrade(oldConfig, new HashMap<>(), getYamlE().getKeysFiltered("Items\\..*"));
}
public void loadData()
{
allowItemShortcut = true;
itemConfigs.clear();
loadItemConfigs("Items");
if(itemConfigs.isEmpty())
{
logger.warning("There musst be at least one item defined to use the items feature!");
allowItemShortcut = false;
}
validShortcutStyles.addAll(getBackpackItems().stream().map(ItemConfig::getName).collect(Collectors.toList()));
defaultBackpackItem = getString("Defaults.BackpackItem", "unknown");
if(!validShortcutStyles.contains(defaultBackpackItem))
{
String tmp = validShortcutStyles.iterator().next();
logger.warning("Unknown default backpack item '" + defaultBackpackItem + "'. Using '" + tmp + "' instead.");
defaultBackpackItem = tmp;
}
}
private void loadItemConfigs(final @NotNull String parentKey)

View File

@ -376,12 +376,6 @@ public boolean isItemShortcutEnabled()
return MCVersion.isNewerOrEqualThan(MCVersion.MC_1_8) && getConfigE().getBoolean("ItemShortcut.Enabled", true);
}
public @NotNull String getItemShortcutItemName()
{
return getConfigE().getString("ItemShortcut.Item", "BackpackHeadBlue");
}
public boolean isItemShortcutImproveDeathChestCompatibilityEnabled()
{
return getConfigE().getBoolean("ItemShortcut.ImproveDeathChestCompatibility", false);
@ -401,6 +395,16 @@ public int getItemShortcutPreferredSlotId()
{
return getConfigE().getInt("ItemShortcut.PreferredSlotId", -1);
}
public boolean isItemShortcutPlayerChoiceEnabled()
{
return getConfigE().getBoolean("ItemShortcut.AllowPlayersToChoseItem", true);
}
public boolean isItemShortcutPlayerDisableItemEnabled()
{
return getConfigE().getBoolean("ItemShortcut.AllowPlayersToDisableItem", true);
}
//endregion
//region Sound settings

View File

@ -61,4 +61,11 @@ public String[] getCommandAliases(final String command, final @NotNull String...
List<String> aliases = getLangE().getStringList("Command." + command, new ArrayList<>(0));
return (aliases.size() > 0) ? aliases.toArray(new String[0]) : defaults;
}
public @NotNull String[] getSwitch(final @NotNull String key, final @NotNull String defaultSwitch)
{
List<String> switches = getLangE().getStringList("Command.Switches." + key, new ArrayList<>(1));
if(!switches.contains(defaultSwitch)) switches.add(defaultSwitch);
return switches.toArray(new String[0]);
}
}

View File

@ -67,10 +67,10 @@ public ItemShortcut(Minepacks plugin)
openCommand = plugin.getLanguage().getCommandAliases("Backpack", "backpack")[0] + ' ' + plugin.getLanguage().getCommandAliases("Open", "open")[0];
messageDoNotRemoveItem = plugin.getLanguage().getMessage("Ingame.DontRemoveShortcut");
itemConfig = plugin.getBackpacksConfig().getItemConfig("Items." + plugin.getConfiguration().getItemShortcutItemName());
itemConfig = plugin.getBackpacksConfig().getItemConfig("Items." + plugin.getBackpacksConfig().getDefaultBackpackItem());
if(itemConfig == null)
{
plugin.getLogger().severe("Item '" + plugin.getConfiguration().getItemShortcutItemName() + "' is not defined in the backpacks.yml file! Item shortcut will be disabled!");
plugin.getLogger().severe("Item '" + plugin.getBackpacksConfig().getDefaultBackpackItem() + "' is not defined in the backpacks.yml file! Item shortcut will be disabled!");
throw new IllegalArgumentException("The item is not defined.");
}

View File

@ -210,7 +210,7 @@ private void load()
pluginManager.registerEvents(itemFilter, this);
}
if(config.isShulkerboxesDisable()) pluginManager.registerEvents(new DisableShulkerboxes(this), this);
if(config.isItemShortcutEnabled())
if(config.isItemShortcutEnabled() && backpacksConfig.isAllowItemShortcut())
{
try
{
@ -218,7 +218,6 @@ private void load()
pluginManager.registerEvents(itemShortcut, this);
commandManager.registerSubCommand(new ShortcutCommand(this, itemShortcut));
}
catch(IllegalArgumentException ignored) {}
catch(Exception e)
{
e.printStackTrace();

View File

@ -31,6 +31,7 @@ public class Permissions
public static final String NO_COOLDOWN = BASE + "noCooldown";
public static final String IGNORE_GAME_MODE = BASE + "ignoreGameMode";
public static final String IGNORE_WORLD_BLACKLIST = BASE + "ignoreWorldBlacklist";
public static final String CHOSE_DESIGN = BASE + "choseDesign";
public static final String UPDATE = BASE + "update";
public static final String RELOAD = BASE + "reload";
public static final String MIGRATE = BASE + "migrate";