Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Fix another batch of issues
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniFoldi committed Jan 20, 2022
1 parent 5bccea4 commit 15b88c7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 25 deletions.
25 changes: 11 additions & 14 deletions src/main/java/com/danifoldi/protogui/command/PluginCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.danifoldi.protogui.command;

import com.danifoldi.protogui.gui.GuiGrid;
import com.danifoldi.protogui.main.ProtoGuiAPI;
import com.danifoldi.protogui.platform.PlatformInteraction;
import com.danifoldi.protogui.util.Message;
Expand Down Expand Up @@ -90,18 +91,14 @@ public void onLogCommand(@Source PlatformInteraction.ProtoSender sender, @Greedy
}

@CommandDefinition(route = "bgui|bungeegui|pgui|protogui open", permission = "protogui.command.open", runAsync = true)
public void onOpenCommand(@Source PlatformInteraction.ProtoSender sender, Collection<PlatformInteraction.ProtoPlayer> targets, String guiName, @OptParam String target) {
public void onOpenCommand(@Source PlatformInteraction.ProtoSender sender, Collection<PlatformInteraction.ProtoPlayer> targets, GuiGrid gui, @OptParam String target) {

if (ProtoGuiAPI.getInstance().getGui(guiName) == null) {
sender.send(Message.GUI_NOT_FOUND.process(null, Pair.of("name", guiName)));
return;
}
if (target == null && ProtoGuiAPI.getInstance().getGui(guiName).isTargeted()) {
if (target == null && gui.isTargeted()) {
sender.send(Message.GUI_TARGET_REQUIRED.process(null));
return;
}

targets.forEach(p -> ProtoGuiAPI.getInstance().openGui(p.uniqueId(), guiName, target == null ? "" : target));
targets.forEach(p -> ProtoGuiAPI.getInstance().openGui(p.uniqueId(), gui, target == null ? "" : target));
sender.send(Message.ACTION_COMPLETE.process(null, Pair.of("count", String.valueOf(targets.size()))));
}

Expand Down Expand Up @@ -141,6 +138,13 @@ public void onSoundCommand(@Source PlatformInteraction.ProtoSender sender, Colle
sender.send(Message.ACTION_COMPLETE.process(null, Pair.of("count", String.valueOf(targets.size()))));
}

@CommandDefinition(route = "bgui|bungeegui|pgui|protogui subtitle", permission = "protogui.command.subtitle", runAsync = true)
public void onSubtitleCommand(@Source PlatformInteraction.ProtoSender sender, Collection<PlatformInteraction.ProtoPlayer> targets, int fadeIn, int stay, int fadeOut, String message) {

targets.forEach(p -> p.subtitle(Message.process(p, message), fadeIn, stay, fadeOut));
sender.send(Message.ACTION_COMPLETE.process(null, Pair.of("count", String.valueOf(targets.size()))));
}

@CommandDefinition(route = "bgui|bungeegui|pgui|protogui sudo", permission = "protogui.command.sudo", runAsync = true)
public void onSudoCommand(@Source PlatformInteraction.ProtoSender sender, Collection<PlatformInteraction.ProtoPlayer> targets, @Greedy String command) {

Expand All @@ -154,11 +158,4 @@ public void onTitleCommand(@Source PlatformInteraction.ProtoSender sender, Colle
targets.forEach(p -> p.title(Message.process(p, message), fadeIn, stay, fadeOut));
sender.send(Message.ACTION_COMPLETE.process(null, Pair.of("count", String.valueOf(targets.size()))));
}

@CommandDefinition(route = "bgui|bungeegui|pgui|protogui subtitle", permission = "protogui.command.subtitle", runAsync = true)
public void onSubtitleCommand(@Source PlatformInteraction.ProtoSender sender, Collection<PlatformInteraction.ProtoPlayer> targets, int fadeIn, int stay, int fadeOut, String message) {

targets.forEach(p -> p.subtitle(Message.process(p, message), fadeIn, stay, fadeOut));
sender.send(Message.ACTION_COMPLETE.process(null, Pair.of("count", String.valueOf(targets.size()))));
}
}
23 changes: 14 additions & 9 deletions src/main/java/com/danifoldi/protogui/main/GuiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -152,10 +153,10 @@ GuiGrid loadGuiGrid(Config gui, String name) {
return GuiGrid.builder()
.items(loadItemMap(gui.getOrElse("items", Config.inMemory()), size))
.targeted(gui.getOrElse("targeted", false))
.commands(gui.getOrElse("aliases", List.of(name.toLowerCase(Locale.ROOT))).stream().map(String::toLowerCase).collect(Collectors.toList()))
.permission(gui.getOrElse("permission", "protogui.gui." + name.toLowerCase(Locale.ROOT).replace("{", "").replace("}", "").replace(" ", "")))
.commands(gui.getOrElse("aliases", Collections.singletonList("<<#TEMPLATE#>>")))
.permission(gui.getOrElse("permission", "<<#TEMPLATE#>>"))
.size(size)
.title(gui.getOrElse("title", "GUI " + name.toLowerCase(Locale.ROOT)))
.title(gui.getOrElse("title", "<<#TEMPLATE#>>"))
.selfTarget(gui.getOrElse("selfTarget", true))
.ignoreVanished(gui.getOrElse("ignoreVanished", true))
.requireOnlineTarget(gui.getOrElse("requireOnlineTarget", false))
Expand All @@ -175,10 +176,10 @@ GuiGrid loadGuiGrid(Config gui, String name, GuiGrid template) {
return GuiGrid.builder()
.items(loadItemMap(gui.get("items"), size, template.getItems()))
.targeted(gui.getOrElse("targeted", template.isTargeted()))
.commands(gui.getOrElse("aliases", template.getCommandAliases()))
.permission(gui.getOrElse("permission", template.getPermission()))
.commands(gui.getOrElse("aliases", template.getCommandAliases().size() > 0 && Objects.equals(template.getCommandAliases().get(0), "<<#TEMPLATE#>>") ? Collections.singletonList(name.toLowerCase(Locale.ROOT)) : template.getCommandAliases()))
.permission(gui.getOrElse("permission", template.getPermission().equals("<<#TEMPLATE#>>") ? "protogui.gui." + name.toLowerCase(Locale.ROOT).replace("{", "").replace("}", "").replace(" ", "") : template.getPermission()))
.size(size)
.title(gui.getOrElse("title", template.getTitle()))
.title(gui.getOrElse("title", template.getTitle().equals("<<#TEMPLATE#>>") ? "GUI " + name.toLowerCase(Locale.ROOT) : template.getTitle()))
.selfTarget(gui.getOrElse("selfTarget", template.isSelfTarget()))
.ignoreVanished(gui.getOrElse("ignoreVanished", template.isIgnoreVanished()))
.requireOnlineTarget(gui.getOrElse("requireOnlineTarget", template.isRequireOnlineTarget()))
Expand Down Expand Up @@ -293,17 +294,17 @@ void handleActions(final @NotNull UUID uuid, final int slot) {
if (guiAction.getGuiItem().getClickSound() != null) {
guiAction.getGuiItem().getClickSound().playFor(uuid);
}
open(guiAction.getGui(), uuid, null);
open(guiAction.getGui(), uuid, "");
});
}

void open(final @NotNull String name, final @NotNull UUID uuid, final @NotNull String target) {
void open(final @NotNull GuiGrid gui, final @NotNull UUID uuid, final @NotNull String target) {
String name = getGuiName(gui);
logger.info("Opening gui " + name + " for player " + uuid + " (target: " + target + ")");

final PlatformInteraction.ProtoPlayer player = ProtoGuiAPI.getInstance().getPlatform().getPlayer(uuid);
final PlatformInteraction.ProtoPlayer targetPlayer = ProtoGuiAPI.getInstance().getPlatform().getPlayer(target);
final PlatformInteraction.ProtoPlayer placeholderPlayer = menus.get(name).isRequireOnlineTarget() && menus.get(name).isPlaceholdersTarget() && targetPlayer != null ? targetPlayer : player;
final GuiGrid gui = menus.get(name);
final Inventory inventory = new Inventory(SlotUtil.getInventoryType(gui.getGuiSize())).title(Message.process(placeholderPlayer, gui.getTitle(), Pair.of("player", player.name()), Pair.of("target", targetPlayer != null ? targetPlayer.name() : target)));

if (gui.getOpenSound() != null) {
Expand Down Expand Up @@ -376,6 +377,10 @@ void open(final @NotNull String name, final @NotNull UUID uuid, final @NotNull S
openGuis.put(uuid, Pair.of(name, target));
}

void open(final @NotNull String name, final @NotNull UUID uuid, final @NotNull String target) {
open(getGui(name), uuid, target);
}

void runCommand(final @NotNull UUID uuid, final @NotNull GuiGrid openGui, final int slot, final @NotNull String target, final @NotNull ClickType clickType) {
logger.info("Running " + clickType.name() + " commands for player " + uuid + " slot " + slot + " with target " + target);

Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/danifoldi/protogui/main/ProtoGuiAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
public class ProtoGuiAPI {

private static @Nullable ProtoGuiAPI apiInstance;
public static @Nullable ProtoGuiAPI getInstance() {
public static @NotNull ProtoGuiAPI getInstance() {
if (apiInstance == null) {
throw new RuntimeException("Cannot access API while plugin is not loaded");
}

return apiInstance;
}
static void setInstance(final @Nullable ProtoGuiAPI apiInstance) {
Expand Down Expand Up @@ -62,6 +66,17 @@ public void openGui(final @NotNull UUID uuid, final @NotNull String guiName, fin
guiHandler.open(guiName, uuid, target);
}

/**
* Opens a GUI for a player
*
* @param uuid - the user that the GUI will open for
* @param gui - the name GUI to open
* @param target - target value for the GUI, pass any {@link String} for untargeted GUIs
*/
public void openGui(final @NotNull UUID uuid, final @NotNull GuiGrid gui, final @NotNull String target) {
guiHandler.open(gui, uuid, target);
}

/**
* Closes the open GUI for a player
* @param uuid - the user to force close the GUI for
Expand Down
18 changes: 17 additions & 1 deletion src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,20 @@ serverNotFound: '&cServer {name} not found'
emptyMessage: '&cMessage can''t be empty'
noPermission: '&cYou don''t have permission to execute that command'
guiListTop: '&a{count} GUIs are loaded:'
guiListItem: '&0- &6{name}'
guiListItem: '&0- &6{name}'
conditionFailed: '&cCondition {id} failed'
dispatcherFailedToExecuteCommand: '&cFailed to execute command {commandline}'
dispatcherNoSuchCommand: '&cCommand {name} not found'
dispatcherTooFewArguments: '&cToo few arguments, syntax: {syntax}'
dispatcherTooManyArguments: '&cToo many arguments, syntax: {syntax}'
dispatcherIllegalCommandSource: '&cInvalid source, found {found} instead of {required}'
dispatcherInvalidBooleanValue: '&cInvalid boolean: {input}'
dispatcherInvalidNumberValue: '&cInvalid number: {input}'
dispatcherInvalidCharacterValue: '&cInvalid character: {input}'
dispatcherNumberOutOfRange: '&cNumber {input} is out of range {min} - {max}'
parameterQuotedStringInvalidTrailingCharacter: '&cQuoted string {input} has invalid trailing character'
parameterStringRegexError: '&cString {input} does not match pattern {regex}'
parameterMissingFlagValue: '&cMissing flag value: {input}'
parameterMissingFlag: '&cMissing flag: {syntax}'
parameterDuplicateFlag: '&cDuplicate flag: {flag}'
parameterUnrecognizedCommandFlag: '&cUnrecognized flag: {input}'

0 comments on commit 15b88c7

Please sign in to comment.