Skip to content

Commit

Permalink
feat: add command descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
titivermeesch committed Nov 10, 2024
1 parent a21d435 commit 533b4a5
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 55 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ java {


group = 'me.playbosswar.com'
version = '8.10.0'
version = '8.11.0'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -68,7 +68,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer-java21'
version = '8.10.0'
version = '8.11.0'
from components.java
}
}
Expand Down
4 changes: 2 additions & 2 deletions java17-build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ java {


group = 'me.playbosswar.com'
version = '8.10.0'
version = '8.11.0'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -63,7 +63,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer-java17'
version = '8.10.0'
version = '8.11.0'

from components.java
}
Expand Down
4 changes: 2 additions & 2 deletions java8-build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ java {
}

group = 'me.playbosswar.com'
version = '8.10.0'
version = '8.11.0'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -70,7 +70,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer-java8'
version = '8.10.0'
version = '8.11.0'

from components.java
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void init(Player player, InventoryContents contents) {

contents.set(5, 8, ClickableItem.of(Items.getBackItem(), e -> new EditTaskMenu(task).INVENTORY.open(player)));

String[] addItemLore = languageManager.getList(LanguageKey.ADD_COMMAND_LORE).toArray(new String[]{});
String[] addItemLore = languageManager.getList(LanguageKey.ADD_COMMAND_LORE).toArray(new String[] {});
ItemStack addItem = Items.generateItem(LanguageKey.ADD_COMMAND, XMaterial.ANVIL, addItemLore);
ClickableItem clickableAddItem = ClickableItem.of(addItem, e -> {
TaskCommand taskCommand = new TaskCommand("say This is my command",
Expand All @@ -64,25 +64,26 @@ public void init(Player player, InventoryContents contents) {
List<String> selectModeLore = languageManager.getList(LanguageKey.GENDER_LORE);
selectModeLore.add("");
selectModeLore.add(languageManager.get(LanguageKey.GUI_CURRENT, task.getCommandExecutionMode().toString()));
if(task.getCommandExecutionMode().equals(CommandExecutionMode.INTERVAL)) {
if (task.getCommandExecutionMode().equals(CommandExecutionMode.INTERVAL)) {
selectModeLore.add(
languageManager.get(LanguageKey.CURRENT_INTERVAL,
task.getCommandExecutionMode().equals(CommandExecutionMode.INTERVAL) ?
task.getCommandExecutionInterval().toString() : ""));
task.getCommandExecutionMode().equals(CommandExecutionMode.INTERVAL)
? task.getCommandExecutionInterval().toString()
: ""));
}

selectModeLore.add("");
selectModeLore.add(languageManager.get(LanguageKey.LEFT_CLICK_SWITCH));
selectModeLore.add(task.getCommandExecutionMode().equals(CommandExecutionMode.INTERVAL) ?
languageManager.get(LanguageKey.RIGHT_CLICK_CHANGE_INTERVAL) : "");

selectModeLore.add(task.getCommandExecutionMode().equals(CommandExecutionMode.INTERVAL)
? languageManager.get(LanguageKey.RIGHT_CLICK_CHANGE_INTERVAL)
: "");

ItemStack selectModeItem = Items.generateItem(LanguageKey.EXECUTION_MODE, XMaterial.DIAMOND_SHOVEL,
selectModeLore.toArray(new String[]{}));
selectModeLore.toArray(new String[] {}));
ItemMeta selectedModeItemMeta = selectModeItem.getItemMeta();
selectedModeItemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
ClickableItem clickableSelectModeItem = ClickableItem.of(selectModeItem, e -> {
if(e.getClick().equals(ClickType.LEFT)) {
if (e.getClick().equals(ClickType.LEFT)) {
task.switchCommandExecutionMode();
this.INVENTORY.open(player);
return;
Expand All @@ -97,7 +98,7 @@ public void init(Player player, InventoryContents contents) {
commandSettingsLore.add("");
commandSettingsLore.add(languageManager.get(LanguageKey.LEFT_CLICK_EDIT));
ItemStack commandSettingsItem = Items.generateItem(LanguageKey.OPEN_COMMANDS_SETTINGS, XMaterial.REPEATER,
commandSettingsLore.toArray(new String[]{}));
commandSettingsLore.toArray(new String[] {}));
ClickableItem clickableCommandSettingsItem = ClickableItem.of(commandSettingsItem,
e -> new CommandSettingsMenu(task).INVENTORY.open(player));
contents.set(5, 0, clickableCommandSettingsItem);
Expand All @@ -111,31 +112,32 @@ public void update(Player player, InventoryContents contents) {
private ClickableItem[] getAllCommands(Player p) {
List<TaskCommand> commands = task.getCommands();

if(commands == null) {
if (commands == null) {
return new ClickableItem[0];
}

ClickableItem[] items = new ClickableItem[commands.size()];

for(int i = 0; i < items.length; i++) {
for (int i = 0; i < items.length; i++) {
TaskCommand taskCommand = commands.get(i);
String command = taskCommand.getCommand();
String[] lore = new String[]{
String[] lore = new String[] {
"",
languageManager.get(LanguageKey.GENDER, taskCommand.getGender().toString()),
languageManager.get(LanguageKey.ITEM_DESCRIPTION_LORE, taskCommand.getDescription()),
"",
languageManager.get(LanguageKey.LEFT_CLICK_EDIT),
languageManager.get(LanguageKey.RIGHT_CLICK_DELETE)
};
ItemStack item = Items.generateItem("§b" + command, XMaterial.COMMAND_BLOCK_MINECART, lore);

items[i] = ClickableItem.of(item, e -> {
if(e.getClick().equals(ClickType.LEFT)) {
if (e.getClick().equals(ClickType.LEFT)) {
new EditCommandMenu(task, taskCommand).INVENTORY.open(p);
return;
}

if(e.getClick().equals(ClickType.RIGHT)) {
if (e.getClick().equals(ClickType.RIGHT)) {
task.removeCommand(taskCommand);
this.INVENTORY.open(p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public EditCommandMenu(Task task, TaskCommand taskCommand) {
public void init(Player player, InventoryContents contents) {
contents.fillBorders(ClickableItem.empty(XMaterial.BLUE_STAINED_GLASS_PANE.parseItem()));

String[] editCommandLore = new String[]{"",
String[] editCommandLore = new String[] { "",
languageManager.get(LanguageKey.EDIT_COMMAND_DESCRIPTION),
"",
languageManager.get(LanguageKey.GUI_CURRENT, taskCommand.getCommand())
Expand All @@ -62,37 +62,59 @@ public void init(Player player, InventoryContents contents) {
});
contents.set(1, 1, clickableCommandItem);


ItemStack genderItem = Items.generateItem(LanguageKey.GENDER_ITEM, XMaterial.CHAINMAIL_HELMET,
languageManager.getList(LanguageKey.GENDER_SELECTOR_LORE, taskCommand.getGender().toString()).toArray(new String[]{}));
languageManager.getList(LanguageKey.GENDER_SELECTOR_LORE, taskCommand.getGender().toString())
.toArray(new String[] {}));
ClickableItem clickableGenderItem = ClickableItem.of(genderItem, e -> {
taskCommand.toggleGender();
task.storeInstance();
this.INVENTORY.open(player);
});
contents.set(1, 2, clickableGenderItem);

if(!taskCommand.getGender().equals(Gender.CONSOLE)) {
ItemStack descriptionItem = Items.generateItem(LanguageKey.DESCRIPTION_ITEM, XMaterial.BOOK,
languageManager.getList(LanguageKey.DESCRIPTION_LORE, taskCommand.getDescription())
.toArray(new String[] {}));

ConversationFactory descriptionConversationHistory = new ConversationFactory(CommandTimerPlugin.getPlugin())
.withModality(true)
.withFirstPrompt(new TextInputConversationPrompt(LanguageKey.EDIT_COMMAND_DESCRIPTION, text -> {
taskCommand.setDescription(text);
task.storeInstance();
new EditCommandMenu(task, taskCommand).INVENTORY.open(player);
}));

ClickableItem clickableDescriptionItem = ClickableItem.of(descriptionItem, e -> {
descriptionConversationHistory.buildConversation(player).begin();
player.closeInventory();
});
contents.set(1, 3, clickableDescriptionItem);

if (!taskCommand.getGender().equals(Gender.CONSOLE)) {
ItemStack intervalItem = Items.generateItem(LanguageKey.TASK_INTERVAL_ITEM_TITLE, XMaterial.CLOCK,
languageManager.getList(LanguageKey.COMMAND_INTERVAL_LORE, taskCommand.getInterval().toString()).toArray(new String[]{}));
languageManager.getList(LanguageKey.COMMAND_INTERVAL_LORE, taskCommand.getInterval().toString())
.toArray(new String[] {}));
ClickableItem clickableIntervalItem = ClickableItem.of(intervalItem,
e -> new EditIntervalMenu(task, taskCommand.getInterval(),
ev -> new EditCommandMenu(task, taskCommand).INVENTORY.open(player)).INVENTORY.open(player));
contents.set(1, 3, clickableIntervalItem);
ev -> new EditCommandMenu(task, taskCommand).INVENTORY.open(player)).INVENTORY
.open(player));
contents.set(1, 4, clickableIntervalItem);
}

if(task.getCommandExecutionMode().equals(CommandExecutionMode.ORDERED)) {
if (task.getCommandExecutionMode().equals(CommandExecutionMode.ORDERED)) {
ItemStack delayItem = Items.generateItem(LanguageKey.COMMAND_DELAY_TITLE, XMaterial.CLOCK,
languageManager.getList(LanguageKey.COMMAND_DELAY_LORE, taskCommand.getDelay().toString()).toArray(new String[]{}));
languageManager.getList(LanguageKey.COMMAND_DELAY_LORE, taskCommand.getDelay().toString())
.toArray(new String[] {}));
ClickableItem clickableDelayItem = ClickableItem.of(delayItem,
e -> new EditIntervalMenu(task, taskCommand.getDelay(),
ev -> new EditCommandMenu(task, taskCommand).INVENTORY.open(player)).INVENTORY.open(player));
ev -> new EditCommandMenu(task, taskCommand).INVENTORY.open(player)).INVENTORY
.open(player));

// Calculate position for item
if(!taskCommand.getGender().equals(Gender.CONSOLE)) {
contents.set(1, 4, clickableDelayItem);
if (!taskCommand.getGender().equals(Gender.CONSOLE)) {
contents.set(1, 5, clickableDelayItem);
} else {
contents.set(1, 3, clickableDelayItem);
contents.set(1, 4, clickableDelayItem);
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/me/playbosswar/com/language/LanguageKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public enum LanguageKey {
TASK_COMMANDS_GUI_TITLE,
EDIT_COMMAND_GUI_TITLE,
EDIT_COMMAND_DESCRIPTION,
ITEM_DESCRIPTION_LORE,
DESCRIPTION_ITEM,
DESCRIPTION_LORE,
DESCRIPTION_CONVERSATION_INPUT,
ENTER_COMMAND_INPUT,
OPEN_COMMANDS_SETTINGS,
OPEN_COMMANDS_SETTINGS_LORE,
Expand Down Expand Up @@ -151,6 +155,7 @@ public enum LanguageKey {
MINECRAFT_TIME;

public static LanguageKey getByTag(String tag) {
return Arrays.stream(values()).filter(value -> value.toString().equals(tag.toUpperCase())).findFirst().orElse(null);
return Arrays.stream(values()).filter(value -> value.toString().equals(tag.toUpperCase())).findFirst()
.orElse(null);
}
}
26 changes: 15 additions & 11 deletions src/main/java/me/playbosswar/com/language/LanguageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;

/**
* Take note that this in the only class where we cannot use our integrated logging system since LanguageManager may
* Take note that this in the only class where we cannot use our integrated
* logging system since LanguageManager may
* not be fully loaded yet
*/
public class LanguageManager {
Expand All @@ -34,7 +36,7 @@ public LanguageManager(Plugin plugin, String language) {
try {
validateConfiguration();
loadLanguage();
} catch(Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}
Expand All @@ -43,24 +45,25 @@ private void validateConfiguration() throws Exception {
boolean fileSaveRequired = false;
JSONObject selectedLanguageObject = getLanguageObject(selectedLanguage);
JSONObject defaultLanguageObject = getLanguageObject("default");
for(LanguageKey languageKey : LanguageKey.values()) {
if(selectedLanguageObject.get(languageKey.toString().toLowerCase()) == null) {
for (LanguageKey languageKey : LanguageKey.values()) {
if (selectedLanguageObject.get(languageKey.toString().toLowerCase()) == null) {
Bukkit.getLogger().log(Level.WARNING,
"Translation file " + selectedLanguage + " is missing the key " + languageKey.toString().toLowerCase() + ", adding default value");
"Translation file " + selectedLanguage + " is missing the key "
+ languageKey.toString().toLowerCase() + ", adding default value");
selectedLanguageObject.put(
languageKey.toString().toLowerCase(),
defaultLanguageObject.get(languageKey.toString().toLowerCase()));
fileSaveRequired = true;
}
}

if(fileSaveRequired) {
if (fileSaveRequired) {
try {
String filePath = getLanguageFilePath(selectedLanguage);
FileWriter jsonFile = new FileWriter(filePath);
jsonFile.write(selectedLanguageObject.toJSONString());
jsonFile.flush();
} catch(IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
Expand All @@ -72,10 +75,11 @@ private void loadLanguage() throws Exception {

Arrays.stream(LanguageKey.values()).forEach(languageKey -> {
boolean fileHasKey = obj.containsKey(languageKey.toString().toLowerCase());
if(!fileHasKey) {
if (!fileHasKey) {
Bukkit.getPluginManager().disablePlugin(CommandTimerPlugin.getPlugin());
throw new RuntimeException(
"Could not find translation for " + languageKey.toString().toLowerCase() + " for language " + selectedLanguage);
"Could not find translation for " + languageKey.toString().toLowerCase() + " for language "
+ selectedLanguage);
}

translations.put(languageKey, (String) obj.get(languageKey.toString().toLowerCase()));
Expand All @@ -94,7 +98,7 @@ private String getLanguageFilePath(String language) {

public String get(LanguageKey key) {
String text = translations.get(key);
if(text == null) {
if (text == null) {
Bukkit.getLogger().log(Level.WARNING, "could not load key " + key.name());
return "";
}
Expand All @@ -104,7 +108,7 @@ public String get(LanguageKey key) {
public String get(LanguageKey key, String... replacers) {
String translation = Messages.colorize(translations.get(key));

for(int i = 1; i <= replacers.length; i++) {
for (int i = 1; i <= replacers.length; i++) {
String placeholder = "$" + i;
translation = translation.replace(placeholder, replacers[i - 1]);
}
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/me/playbosswar/com/tasks/TaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class TaskCommand {
private Gender gender;
private TaskInterval interval;
private TaskInterval delay;
private String description = "";

public TaskCommand(String command, Gender gender) {
this.command = command;
Expand All @@ -32,32 +33,32 @@ public void setGender(Gender gender) {
}

public void toggleGender() {
if(gender.equals(Gender.OPERATOR)) {
if (gender.equals(Gender.OPERATOR)) {
setGender(Gender.PLAYER);
return;
}

if(gender.equals(Gender.PLAYER)) {
if (gender.equals(Gender.PLAYER)) {
setGender(Gender.CONSOLE);
return;
}

if(gender.equals(Gender.CONSOLE)) {
if (gender.equals(Gender.CONSOLE)) {
setGender(Gender.CONSOLE_PER_USER);
return;
}

if(gender.equals(Gender.CONSOLE_PER_USER)) {
if (gender.equals(Gender.CONSOLE_PER_USER)) {
setGender(Gender.CONSOLE_PER_USER_OFFLINE);
return;
}

if(gender.equals(Gender.CONSOLE_PER_USER_OFFLINE)) {
if (gender.equals(Gender.CONSOLE_PER_USER_OFFLINE)) {
setGender(Gender.CONSOLE_PROXY);
return;
}

if(gender.equals(Gender.CONSOLE_PROXY)) {
if (gender.equals(Gender.CONSOLE_PROXY)) {
setGender(Gender.OPERATOR);
}
}
Expand All @@ -77,4 +78,12 @@ public TaskInterval getDelay() {
public void setDelay(TaskInterval delay) {
this.delay = delay;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}
Loading

0 comments on commit 533b4a5

Please sign in to comment.