This commit is contained in:
nulli0n committed 2024-11-04 16:27:09 +05:00
1 parent 556bc98ddd
commit 77541c59d8
9 files changed
+123 -69

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>su.nightexpress.nightcore</groupId> <groupId>su.nightexpress.nightcore</groupId>
<artifactId>nightcore</artifactId> <artifactId>nightcore</artifactId>
<version>2.7.0</version> <version>2.7.1</version>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
@@ -3,8 +3,7 @@ package su.nightexpress.nightcore.language;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import su.nightexpress.nightcore.NightCorePlugin; import su.nightexpress.nightcore.NightCorePlugin;
import su.nightexpress.nightcore.config.FileConfig; import su.nightexpress.nightcore.config.FileConfig;
import su.nightexpress.nightcore.language.entry.LangEntry; import su.nightexpress.nightcore.language.entry.LangElement;
import su.nightexpress.nightcore.language.entry.LangEnum;
import su.nightexpress.nightcore.manager.SimpleManager; import su.nightexpress.nightcore.manager.SimpleManager;
import su.nightexpress.nightcore.util.Plugins; import su.nightexpress.nightcore.util.Plugins;
import su.nightexpress.nightcore.util.Reflex; import su.nightexpress.nightcore.util.Reflex;
@@ -67,12 +66,15 @@ public class LangManager extends SimpleManager<NightCorePlugin> {
} }
public void loadEntries(@NotNull Class<?> clazz) { public void loadEntries(@NotNull Class<?> clazz) {
Reflex.getFields(clazz, LangEntry.class).forEach(entry -> { Reflex.getFields(clazz, LangElement.class).forEach(entry -> {
entry.load(this.plugin); entry.load(this.plugin);
}); });
Reflex.getFields(clazz, LangEnum.class).forEach(langEnum -> { // Reflex.getFields(clazz, LangEntry.class).forEach(entry -> {
langEnum.load(this.plugin); // entry.load(this.plugin);
}); // });
// Reflex.getFields(clazz, LangEnum.class).forEach(langEnum -> {
// langEnum.load(this.plugin);
// });
} }
@Deprecated @Deprecated
@@ -0,0 +1,12 @@
package su.nightexpress.nightcore.language.entry;
import org.jetbrains.annotations.NotNull;
import su.nightexpress.nightcore.NightCorePlugin;
import su.nightexpress.nightcore.config.FileConfig;
public interface LangElement {
void write(@NotNull FileConfig config);
void load(@NotNull NightCorePlugin plugin);
}
@@ -1,10 +1,8 @@
package su.nightexpress.nightcore.language.entry; package su.nightexpress.nightcore.language.entry;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import su.nightexpress.nightcore.NightCorePlugin;
import su.nightexpress.nightcore.config.FileConfig;
public abstract class LangEntry/*<T>*/ { public abstract class LangEntry implements LangElement {
protected final String path; protected final String path;
protected final String defaultText; protected final String defaultText;
@@ -14,18 +12,13 @@ public abstract class LangEntry/*<T>*/ {
this.defaultText = defaultText; this.defaultText = defaultText;
} }
public abstract boolean write(@NotNull FileConfig config);
//@NotNull
public abstract /*T*/ void load(@NotNull NightCorePlugin plugin);
@NotNull @NotNull
public String getPath() { public String getPath() {
return path; return this.path;
} }
@NotNull @NotNull
public String getDefaultText() { public String getDefaultText() {
return defaultText; return this.defaultText;
} }
} }
@@ -10,7 +10,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
public class LangEnum<E extends Enum<E>> { public class LangEnum<E extends Enum<E>> implements LangElement {
private final String path; private final String path;
private final Class<E> clazz; private final Class<E> clazz;
@@ -27,6 +27,11 @@ public class LangEnum<E extends Enum<E>> {
return new LangEnum<>(path, clazz); return new LangEnum<>(path, clazz);
} }
@Override
public void write(@NotNull FileConfig config) {
}
public void load(@NotNull NightCorePlugin plugin) { public void load(@NotNull NightCorePlugin plugin) {
FileConfig config = plugin.getLang(); FileConfig config = plugin.getLang();
@@ -15,14 +15,14 @@ import java.util.List;
import static su.nightexpress.nightcore.util.text.tag.Tags.*; import static su.nightexpress.nightcore.util.text.tag.Tags.*;
public class LangItem extends LangEntry/*<Pair<String, List<String>>>*/ { public class LangItem extends LangEntry {
private final List<String> defaultLore; private final List<String> defaultLore;
private String localizedName; private String localizedName;
private List<String> localizedLore; private List<String> localizedLore;
private TextRoot wrappedName; private TextRoot wrappedName;
private List<TextRoot> wrappedLore; private List<TextRoot> wrappedLore;
public LangItem(@NotNull String key, @NotNull String defaultName, @NotNull List<String> defaultLore) { public LangItem(@NotNull String key, @NotNull String defaultName, @NotNull List<String> defaultLore) {
@@ -36,47 +36,43 @@ public class LangItem extends LangEntry/*<Pair<String, List<String>>>*/ {
} }
@Override @Override
public boolean write(@NotNull FileConfig config) { public void write(@NotNull FileConfig config) {
if (!config.contains(this.getPath())) { config.set(this.path + ".Name", this.defaultText);
config.set(this.getPath() + ".Name", this.getDefaultName()); config.set(this.path + ".Lore", this.defaultLore);
config.set(this.getPath() + ".Lore", this.getDefaultLore());
return true;
}
return false;
} }
@Override @Override
public void load(@NotNull NightCorePlugin plugin) { public void load(@NotNull NightCorePlugin plugin) {
FileConfig config = plugin.getLang(); FileConfig config = plugin.getLang();
this.write(config); if (!config.contains(this.path)) {
this.write(config);
}
this.setLocalizedName(config.getString(this.getPath() + ".Name", this.getDefaultName())); this.setLocalizedName(config.getString(this.path + ".Name", this.defaultText));
this.setLocalizedLore(config.getStringList(this.getPath() + ".Lore")); this.setLocalizedLore(config.getStringList(this.path + ".Lore"));
//return Pair.of(this.getLocalizedName(), this.getLocalizedLore());
} }
public void apply(@NotNull ItemStack item) { public void apply(@NotNull ItemStack item) {
ItemUtil.editMeta(item, meta -> { ItemUtil.editMeta(item, meta -> {
meta.setDisplayName(this.getWrappedName().toLegacy()); meta.setDisplayName(this.wrappedName.toLegacy());
meta.setLore(this.getWrappedLore().stream().map(TextRoot::toLegacy).toList()); meta.setLore(this.wrappedLore.stream().map(TextRoot::toLegacy).toList());
}); });
} }
@NotNull @NotNull
public String getDefaultName() { public String getDefaultName() {
return this.getDefaultText(); return this.defaultText;
} }
@NotNull @NotNull
public List<String> getDefaultLore() { public List<String> getDefaultLore() {
return defaultLore; return this.defaultLore;
} }
@NotNull @NotNull
public String getLocalizedName() { public String getLocalizedName() {
return localizedName; return this.localizedName;
} }
public void setLocalizedName(@NotNull String localizedName) { public void setLocalizedName(@NotNull String localizedName) {
@@ -86,7 +82,7 @@ public class LangItem extends LangEntry/*<Pair<String, List<String>>>*/ {
@NotNull @NotNull
public List<String> getLocalizedLore() { public List<String> getLocalizedLore() {
return localizedLore; return this.localizedLore;
} }
public void setLocalizedLore(@NotNull List<String> localizedLore) { public void setLocalizedLore(@NotNull List<String> localizedLore) {
@@ -97,12 +93,12 @@ public class LangItem extends LangEntry/*<Pair<String, List<String>>>*/ {
@NotNull @NotNull
public TextRoot getWrappedName() { public TextRoot getWrappedName() {
return wrappedName; return this.wrappedName;
} }
@NotNull @NotNull
public List<TextRoot> getWrappedLore() { public List<TextRoot> getWrappedLore() {
return wrappedLore; return this.wrappedLore;
} }
public static final String CLICK = "Click"; public static final String CLICK = "Click";
@@ -0,0 +1,55 @@
package su.nightexpress.nightcore.language.entry;
import org.bukkit.Keyed;
import org.bukkit.Registry;
import org.jetbrains.annotations.NotNull;
import su.nightexpress.nightcore.NightCorePlugin;
import su.nightexpress.nightcore.config.ConfigValue;
import su.nightexpress.nightcore.config.FileConfig;
import su.nightexpress.nightcore.util.BukkitThing;
import su.nightexpress.nightcore.util.StringUtil;
import java.util.HashMap;
import java.util.Map;
public class LangKeyed<E extends Keyed> implements LangElement {
private final String path;
private final Registry<E> registry;
private final Map<String, String> localeMap;
public LangKeyed(@NotNull String path, @NotNull Registry<E> registry) {
this.path = path;
this.registry = registry;
this.localeMap = new HashMap<>();
}
@NotNull
public static <E extends Keyed> LangKeyed<E> of(@NotNull String path, @NotNull Registry<E> registry) {
return new LangKeyed<>(path, registry);
}
@Override
public void write(@NotNull FileConfig config) {
}
public void load(@NotNull NightCorePlugin plugin) {
FileConfig config = plugin.getLang();
BukkitThing.allFromRegistry(this.registry).forEach(keyed -> {
String namespace = BukkitThing.toString(keyed);
String localized = StringUtil.capitalizeUnderscored(namespace);
String text = ConfigValue.create(this.path + "." + namespace, localized).read(config);
this.localeMap.put(namespace, text);
});
}
@NotNull
public String getLocalized(@NotNull E keyed) {
String namespace = BukkitThing.toString(keyed);
return this.localeMap.getOrDefault(namespace, namespace);
}
}
@@ -21,29 +21,25 @@ public class LangString extends LangEntry {
} }
@Override @Override
public boolean write(@NotNull FileConfig config) { public void write(@NotNull FileConfig config) {
if (!config.contains(this.getPath())) { config.set(this.path, this.defaultText);
String textDefault = this.getDefaultText();
config.set(this.getPath(), textDefault);
return true;
}
return false;
} }
@Override @Override
public void load(@NotNull NightCorePlugin plugin) { public void load(@NotNull NightCorePlugin plugin) {
FileConfig config = plugin.getLang(); FileConfig config = plugin.getLang();
this.write(config); if (!config.contains(this.path)) {
String text = config.getString(this.getPath(), this.getPath()); this.write(config);
this.setString(text); }
//return this.getString(); String text = config.getString(this.path, this.path);
this.setString(text);
} }
@NotNull @NotNull
public String getString() { public String getString() {
return string; return this.string;
} }
public void setString(@NotNull String string) { public void setString(@NotNull String string) {
@@ -58,6 +54,6 @@ public class LangString extends LangEntry {
@NotNull @NotNull
public String getLegacy() { public String getLegacy() {
return this.getMessage().toLegacy(); return this.message.toLegacy();
} }
} }
@@ -29,40 +29,35 @@ public class LangText extends LangEntry {
} }
@Override @Override
public boolean write(@NotNull FileConfig config) { public void write(@NotNull FileConfig config) {
if (!config.contains(this.getPath())) { String[] textSplit = this.defaultText.split("\n");
String textDefault = this.getDefaultText(); config.set(this.path, textSplit.length > 1 ? Arrays.asList(textSplit) : this.defaultText);
String[] textSplit = textDefault.split("\n");
config.set(this.getPath(), textSplit.length > 1 ? Arrays.asList(textSplit) : textDefault);
return true;
}
return false;
} }
@Override @Override
public void load(@NotNull NightCorePlugin plugin) { public void load(@NotNull NightCorePlugin plugin) {
FileConfig config = plugin.getLang(); FileConfig config = plugin.getLang();
this.write(config); if (!config.contains(this.path)) {
this.write(config);
}
List<String> text = new ArrayList<>(config.getStringList(this.getPath())); List<String> text = new ArrayList<>(config.getStringList(this.path));
if (text.isEmpty()) { if (text.isEmpty()) {
text.add(config.getString(this.getPath(), this.getPath())); text.add(config.getString(this.path, this.path));
} }
this.setMessage(LangMessage.parse(plugin, String.join(Placeholders.TAG_LINE_BREAK, text))); this.setMessage(LangMessage.parse(plugin, String.join(Placeholders.TAG_LINE_BREAK, text)));
//return this.getMessage();
} }
@NotNull @NotNull
public LangMessage getMessage() { public LangMessage getMessage() {
return message; return this.message;
} }
@NotNull @NotNull
public LangMessage getMessage(@NotNull NightCorePlugin plugin) { public LangMessage getMessage(@NotNull NightCorePlugin plugin) {
return message.setPrefix(plugin.getPrefix()); return this.message.setPrefix(plugin.getPrefix());
} }
public void setMessage(@NotNull LangMessage message) { public void setMessage(@NotNull LangMessage message) {