Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
Patches
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Nov 26, 2023
1 parent b7d7ff3 commit c301ea4
Show file tree
Hide file tree
Showing 33 changed files with 666 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import su.nexmedia.engine.api.placeholder.PlaceholderMap;
import su.nexmedia.engine.lang.EngineLang;
import su.nexmedia.engine.utils.Placeholders;
import su.nexmedia.engine.utils.regex.RegexUtil;
import su.nexmedia.engine.utils.regex.TimedMatcher;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

Expand Down Expand Up @@ -94,10 +93,11 @@ public final void execute(@NotNull CommandSender sender, @NotNull String label,
String name = flag.getName();
Pattern pattern = flag.getPattern();

Matcher matcher = RegexUtil.getMatcher(pattern, argLine);
if (RegexUtil.matcherFind(matcher)) {
result.getFlags().put(flag, matcher.group(2).trim());
argLine = argLine.replace(matcher.group(0), "");
TimedMatcher matcher = TimedMatcher.create(pattern, argLine, 100);
matcher.setDebug(true);
if (matcher.find()) {
result.getFlags().put(flag, matcher.getMatcher().group(2).trim());
argLine = argLine.replace(matcher.getMatcher().group(0), "");
}
}
result.setArgs(argLine.isEmpty() ? new String[0] : argLine.trim().split(" "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public class CommandFlag<T> {

public CommandFlag(@NotNull String name, @NotNull Function<String, T> parser) {
this.name = name;
//this.pattern = Pattern.compile("-" + name + "(\\s|$)((?=-)|\\S*)"); valid
this.pattern = Pattern.compile("-" + name + "(\\s|$)([^-]*)"); // experimantal
this.pattern = Pattern.compile("-" + name + "(\\s|$)([^-]*)");
this.parser = parser;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Comman
AbstractCommand<P> command = this.findChildren(args);
if (!command.hasPermission(sender)) return Collections.emptyList();

List<String> list = new ArrayList<>();
if (!command.getChildrens().isEmpty()) {
List<String> list = new ArrayList<>();
command.getChildrens().stream().filter(child -> child.hasPermission(sender))
.forEach(child -> list.addAll(Arrays.asList(child.getAliases())));
return StringUtil.getByPartialMatches(list, args[args.length - 1], 2);
}

List<String> list = command.getTab(player, command.equals(this) ? (args.length) : (args.length - 1), args);
else {
list.addAll(command.getTab(player, command.equals(this) ? (args.length) : (args.length - 1), args));
}
return StringUtil.getByPartialMatches(list, args[args.length - 1], 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package su.nexmedia.engine.api.config;

import org.jetbrains.annotations.NotNull;

public class ConfigKey<T> {

private final String path;
private final JOption.Writer<T> writer;
private final JOption.Reader<T> reader;

public ConfigKey(@NotNull String path, JOption.Writer<T> writer, JOption.Reader<T> reader) {
this.path = path;
this.writer = writer;
this.reader = reader;
}

public static ConfigKey<Integer> asInt(@NotNull String path) {
return new ConfigKey<>(path, JYML::set, JOption.READER_INT);
}

public T read(@NotNull JYML cfg, @NotNull T defaultValue) {
return this.getReader().read(cfg, this.getPath(), defaultValue);
}

public void write(@NotNull JYML cfg, @NotNull T obj) {
this.getWriter().write(cfg, this.getPath(), obj);
}

@NotNull
public String getPath() {
return path;
}

@NotNull
public JOption.Reader<T> getReader() {
return reader;
}

@NotNull
public JOption.Writer<T> getWriter() {
return writer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@ public JOption(@NotNull String path, @NotNull Reader<T> reader, @NotNull Supplie
}

public JOption(@NotNull String path, @NotNull Reader<T> reader, @NotNull T defaultValue, @Nullable String... description) {
this(path, null, reader, defaultValue, description);
}

public JOption(@NotNull String path, @Nullable Writer<T> writer, @NotNull Reader<T> reader, @NotNull T defaultValue, @Nullable String... description) {
this.path = path;
this.description = description == null ? new String[0] : description;
this.reader = reader;
this.defaultValue = defaultValue;
}

@NotNull
public static <T> JOption<T> create(@NotNull ConfigKey<T> key, @NotNull T defaultValue, @Nullable String... description) {
return new JOption<>(key.getPath(), key.getWriter(), key.getReader(), defaultValue, description);
}

@NotNull
public static JOption<Boolean> create(@NotNull String path, boolean defaultValue, @Nullable String... description) {
return new JOption<>(path, READER_BOOLEAN, defaultValue, description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected void onShutdown() {
this.saveTask.stop();
this.saveTask = null;
}
this.onSynchronize();
//this.onSynchronize();
this.onSave();
this.getConnector().close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ public void onLoad() {
}

public void onUnload() {
Player player = this.getPlayer();
if (player != null) {
this.name = player.getName();
this.setLastOnline(System.currentTimeMillis());
}

}

@SuppressWarnings("unchecked")
Expand All @@ -62,6 +58,10 @@ public final String getName() {
return this.name;
}

void setName(String name) {
this.name = name;
}

public final long getDateCreated() {
return dateCreated;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,12 @@ protected void onShutdown() {

@Override
public void onSave() {
//int off = 0;
for (U userLoaded : this.dataHolder.getUserManager().getUsersLoaded()) {
if (!userLoaded.isOnline()) {
this.dataHolder.getUserManager().getUsersLoadedMap().remove(userLoaded.getId());
//off++;
}
this.saveUser(userLoaded);
}

//int on = this.dataHolder.getUserManager().getUsersLoadedMap().size();
//this.plugin.info("Auto-save: Saved " + on + " online users | " + off + " offline users.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

public abstract class AbstractUserManager<P extends NexPlugin<P>, U extends AbstractUser<P>> extends AbstractManager<P> {

Expand All @@ -24,7 +25,7 @@ public abstract class AbstractUserManager<P extends NexPlugin<P>, U extends Abst
public AbstractUserManager(@NotNull P plugin, @NotNull UserDataHolder<P, U> dataHolder) {
super(plugin);
this.dataHolder = dataHolder;
this.usersLoaded = new HashMap<>();
this.usersLoaded = new ConcurrentHashMap<>();
}

@Override
Expand Down Expand Up @@ -126,15 +127,26 @@ public final void unloadUser(@NotNull Player player) {
}

public final void unloadUser(@NotNull UUID uuid) {
U user = this.getUsersLoadedMap().remove(uuid);
U user = this.getUsersLoadedMap().get(uuid);
if (user == null) return;

this.unloadUser(user);
}

public void unloadUser(@NotNull U user) {
user.onUnload();
Player player = user.getPlayer();
if (player != null) {
user.setName(player.getName());
user.setLastOnline(System.currentTimeMillis());
}
this.saveUser(user);

this.plugin.runTaskLaterAsync(task -> {
if (!user.isOnline()) {
this.getUsersLoadedMap().remove(user.getId());
user.onUnload();
}
}, 40L);
}

public void saveUser(@NotNull U user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public Builder action(@NotNull String click, @NotNull String action) {

@NotNull
public Builder click(@NotNull String click, @NotNull String action) {
return this.addLore(GRAY + "(" + WHITE + click + GRAY + " to " + action + GRAY + ")");
return this.addLore("", GRAY + "(" + WHITE + click + GRAY + " to " + action + GRAY + ")");
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Pattern getPattern() {
}

private final NexPlugin<?> plugin;
private final PlaceholderMap placeholderMap;
@Deprecated private final PlaceholderMap placeholderMap;

private String msgRaw;
private String msgLocalized;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,54 +29,58 @@ public ConfigMenu(@NotNull P plugin, @NotNull JYML cfg) {
}

public void load() {
if (!this.isCodeCreation() || this.cfg.contains(this.itemSection)) {
this.loadConfig();
}
else {
this.loadDefaults();
this.write();
}

List<String> comments = new ArrayList<>();
comments.add("=".repeat(20) + " GUI CONTENT " + "=".repeat(20));
comments.add("You can freely edit items in this section as you wish (add, remove, modify items).");
comments.add("Get some tips in documentation: " + Placeholders.WIKI_MENU_URL);
comments.add("The following values are available for button types:");
this.handlers.forEach((clazz, handler) -> {
comments.addAll(handler.getClicks().keySet().stream().map(e -> "- " + e.name()).toList());
});
comments.add("=".repeat(50));
this.cfg.setComments(this.itemSection, comments);
this.cfg.saveChanges();
this.loadConfig();
}

public boolean isCodeCreation() {
return false;
}

@Deprecated
public void loadDefaults() {

}

@NotNull
protected MenuOptions createDefaultOptions() {
return new MenuOptions("", 27, InventoryType.CHEST);
}

@NotNull
protected List<MenuItem> createDefaultItems() {
return new ArrayList<>();
}

public void loadConfig() {
String oldTitle = cfg.getString("Title");
int oldSize = cfg.getInt("Size", 0);
InventoryType oldType = cfg.getEnum("Inventory_Type", InventoryType.class);

String title = JOption.create("Settings.Title", oldTitle != null ? oldTitle : "",
boolean isCodeCreation = this.isCodeCreation() && !this.cfg.contains(this.itemSection);
MenuOptions options;

if (isCodeCreation) {
options = this.createDefaultOptions();
}
else {
options = new MenuOptions(oldTitle != null ? oldTitle : "", oldSize != 0 ? oldSize : 27, oldType != null ? oldType : InventoryType.CHEST);
}

String title = JOption.create("Settings.Title", options.getTitle(),
"Sets the GUI title."
).mapReader(Colorizer::apply).read(cfg);

int size = JOption.create("Settings.Size", oldSize != 0 ? oldSize : 27,
"Sets the GUI size. Must be multiply of 9."
int size = JOption.create("Settings.Size", options.getSize(),
"Sets the GUI size. Must be multiply of 9.",
"Useful for '" + InventoryType.CHEST.name() + "' Inventory Type only."
).read(cfg);

InventoryType type = JOption.create("Settings.Inventory_Type", InventoryType.class, oldType != null ? oldType : InventoryType.CHEST,
InventoryType type = JOption.create("Settings.Inventory_Type", InventoryType.class, options.getType(),
"Sets the GUI type.",
"https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/inventory/InventoryType.html"
).read(cfg);

int autoRefresh = JOption.create("Settings.Auto_Refresh", 0,
int autoRefresh = JOption.create("Settings.Auto_Refresh", options.getAutoRefresh(),
"Sets the GUI auto-refresh interval (in seconds). Set this to 0 to disable."
).read(cfg);

Expand All @@ -85,16 +89,44 @@ public void loadConfig() {
this.getOptions().setType(type);
this.getOptions().setAutoRefresh(autoRefresh);

this.loadAdditional();

this.cfg.remove("Title");
this.cfg.remove("Size");
this.cfg.remove("Inventory_Type");

if (isCodeCreation) {
AtomicInteger count = new AtomicInteger();
this.createDefaultItems().forEach(menuItem -> {
String name = StringUtil.lowerCaseUnderscore(ItemUtil.getItemName(menuItem.getItem()));

this.writeItem(menuItem, this.itemSection + "." + name + "_" + count.incrementAndGet());
});
}

this.cfg.getSection(this.itemSection).forEach(sId -> {
MenuItem menuItem = this.readItem(this.itemSection + "." + sId);
this.addItem(menuItem);
});

List<String> comments = new ArrayList<>();
comments.add("=".repeat(20) + " GUI CONTENT " + "=".repeat(20));
comments.add("You can freely edit items in this section as you wish (add, remove, modify items).");
comments.add("Get some tips in documentation: " + Placeholders.WIKI_MENU_URL);
comments.add("The following values are available for button types:");
this.handlers.forEach((clazz, handler) -> {
comments.addAll(handler.getClicks().keySet().stream().map(e -> "- " + e.name()).sorted(String::compareTo).toList());
});
comments.add("=".repeat(50));
this.cfg.setComments(this.itemSection, comments);
this.cfg.saveChanges();
}

protected void loadAdditional() {

}

/*@Deprecated
protected void write() {
this.cfg.set("Settings.Title", this.getOptions().getTitle());
this.cfg.set("Settings.Size", this.getOptions().getSize());
Expand All @@ -107,7 +139,7 @@ protected void write() {
this.writeItem(menuItem, this.itemSection + "." + name + "_" + count.incrementAndGet());
});
}
}*/

@Override
public void clear() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ public MenuItem addItem(@NotNull ItemStack item, @NotNull EditorLocale locale, i
return menuItem;
}

protected void handleInput(@NotNull MenuViewer viewer, @NotNull LangKey prompt, @NotNull InputHandler handler) {
public void handleInput(@NotNull MenuViewer viewer, @NotNull LangKey prompt, @NotNull InputHandler handler) {
this.handleInput(viewer.getPlayer(), prompt, handler);
}

protected void handleInput(@NotNull Player player, @NotNull LangKey prompt, @NotNull InputHandler handler) {
public void handleInput(@NotNull Player player, @NotNull LangKey prompt, @NotNull InputHandler handler) {
this.handleInput(player, this.plugin.getMessage(prompt), handler);
}

protected void handleInput(@NotNull Player player, @NotNull LangMessage prompt, @NotNull InputHandler handler) {
public void handleInput(@NotNull Player player, @NotNull LangMessage prompt, @NotNull InputHandler handler) {
EditorManager.prompt(player, prompt.getLocalized());
EditorManager.startEdit(player, handler);
this.plugin.runTask(task -> player.closeInventory());
Expand Down
Loading

0 comments on commit c301ea4

Please sign in to comment.