Compare commits

...

10 commits

71 changed files with 401 additions and 1245 deletions

View file

@ -2,15 +2,15 @@
```xml ```xml
<repositories> <repositories>
<repository> <repository>
<id>zhdev-repo</id> <id>zhira-repo</id>
<url>https://maven.zhdev.org/repository/</url> <url>https://maven.zhira.net/repository/</url>
</repository> </repository>
</repositories> </repositories>
``` ```
```xml ```xml
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.zhdev.varioutil</groupId> <groupId>net.zhira.varioutil</groupId>
<artifactId>all</artifactId> <artifactId>all</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
@ -22,7 +22,7 @@
```xml ```xml
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.zhdev.varioutil</groupId> <groupId>net.zhira.varioutil</groupId>
<artifactId>common</artifactId> <artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
@ -32,7 +32,7 @@
```java ```java
import java.lang.reflect.Field; import java.lang.reflect.Field;
import org.zhdev.varioutil.ReflectionUtils; import net.zhira.varioutil.ReflectionUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
public class BukkitFields { public class BukkitFields {
@ -53,7 +53,7 @@ public class BukkitFields {
```xml ```xml
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.zhdev.varioutil</groupId> <groupId>net.zhira.varioutil</groupId>
<artifactId>io</artifactId> <artifactId>io</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
@ -62,8 +62,8 @@ public class BukkitFields {
#### Code #### Code
```java ```java
import org.zhdev.varioutil.Config; import net.zhira.varioutil.Config;
import org.zhdev.varioutil.YamlConfig; import net.zhira.varioutil.YamlConfig;
import java.util.List; import java.util.List;

View file

@ -1,9 +1,12 @@
<project> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>parent</artifactId> <artifactId>parent</artifactId>
<groupId>org.zhdev.varioutil</groupId> <groupId>net.zhira.varioutil</groupId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>

View file

@ -1,8 +1,11 @@
<project> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.zhdev.varioutil</groupId> <groupId>net.zhira.varioutil</groupId>
<artifactId>parent</artifactId> <artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>
@ -14,6 +17,10 @@
<id>spigot-repo</id> <id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository> </repository>
<repository>
<id>minecraft-repo</id>
<url>https://libraries.minecraft.net/</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
@ -40,7 +47,7 @@
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.20-R0.1-SNAPSHOT</version> <version>1.21.1-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
@ -50,9 +57,9 @@
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>com.mojang</groupId>
<artifactId>spigot</artifactId> <artifactId>authlib</artifactId>
<version>1.20-R0.1-SNAPSHOT</version> <version>4.0.43</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil; package net.zhira.varioutil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;

View file

@ -1,5 +1,8 @@
package org.zhdev.varioutil; package net.zhira.varioutil;
import net.zhira.varioutil.language.Language;
import net.zhira.varioutil.util.CollectionUtils;
import net.zhira.varioutil.util.ColorUtils;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginIdentifiableCommand; import org.bukkit.command.PluginIdentifiableCommand;
@ -9,16 +12,14 @@ import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPluginLoader; import org.bukkit.plugin.java.JavaPluginLoader;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.zhdev.varioutil.bukkit.command.PreparedPluginCommand; import net.zhira.varioutil.bukkit.command.PreparedPluginCommand;
import org.zhdev.varioutil.config.BukkitYamlConfig; import net.zhira.varioutil.config.BukkitYamlConfig;
import org.zhdev.varioutil.config.Config; import net.zhira.varioutil.config.Config;
import org.zhdev.varioutil.config.ConfigSection; import net.zhira.varioutil.config.ConfigSection;
import org.zhdev.varioutil.language.Language; import net.zhira.varioutil.config.YamlConfig;
import org.zhdev.varioutil.sql.SqlAdapter; import net.zhira.varioutil.sql.SqlAdapter;
import org.zhdev.varioutil.util.BukkitUtils; import net.zhira.varioutil.util.BukkitUtils;
import org.zhdev.varioutil.util.CollectionUtils; import net.zhira.varioutil.util.ConfigUtils;
import org.zhdev.varioutil.util.ColorUtils;
import org.zhdev.varioutil.util.ConfigUtils;
import java.io.File; import java.io.File;
import java.nio.file.Path; import java.nio.file.Path;
@ -124,10 +125,10 @@ public abstract class BukkitPreparedPlugin extends BukkitPlugin implements Liste
protected void loadPhrases() { protected void loadPhrases() {
ConfigSection languageSection = defaultConfig.getOrCreateSection("language"); ConfigSection languageSection = defaultConfig.getOrCreateSection("language");
String locale = languageSection.getString("locale", "default"); String locale = languageSection.getString("locale", "default");
Config languageConfig = new BukkitYamlConfig("language/" + locale + ".yml"); Config languageConfig = new YamlConfig("language/" + locale + ".yml");
loadConfig(languageConfig, "language.yml"); loadConfig(languageConfig, "language.yml", true);
loadConfig(languageConfig, "language/default.yml"); loadConfig(languageConfig, "language/default.yml", true);
loadConfig(languageConfig); loadConfig(languageConfig, true);
ConfigUtils.addPhrases(language, languageConfig, ColorUtils::translateAlternateColorCodes); ConfigUtils.addPhrases(language, languageConfig, ColorUtils::translateAlternateColorCodes);
} }

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.bukkit; package net.zhira.varioutil.bukkit;
import org.bukkit.Material; import org.bukkit.Material;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.bukkit.command; package net.zhira.varioutil.bukkit.command;
import org.bukkit.command.*; import org.bukkit.command.*;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.bukkit.command; package net.zhira.varioutil.bukkit.command;
import org.bukkit.command.*; import org.bukkit.command.*;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.bukkit.gui; package net.zhira.varioutil.bukkit.gui;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.bukkit.gui; package net.zhira.varioutil.bukkit.gui;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;

View file

@ -1,6 +1,6 @@
package org.zhdev.varioutil.bukkit.gui; package net.zhira.varioutil.bukkit.gui;
import org.zhdev.varioutil.config.ConfigSection; import net.zhira.varioutil.config.ConfigSection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.bukkit.gui; package net.zhira.varioutil.bukkit.gui;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;

View file

@ -1,11 +1,11 @@
package org.zhdev.varioutil.bukkit.gui; package net.zhira.varioutil.bukkit.gui;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.inventory.meta.SkullMeta;
import org.zhdev.varioutil.util.BukkitUtils; import net.zhira.varioutil.util.BukkitUtils;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;

View file

@ -1,9 +1,9 @@
package org.zhdev.varioutil.bukkit.gui; package net.zhira.varioutil.bukkit.gui;
import net.zhira.varioutil.config.ConfigSection;
import net.zhira.varioutil.util.StringUtils;
import org.bukkit.Material; import org.bukkit.Material;
import org.zhdev.varioutil.bukkit.LegacyMaterial; import net.zhira.varioutil.bukkit.LegacyMaterial;
import org.zhdev.varioutil.config.ConfigSection;
import org.zhdev.varioutil.util.StringUtils;
public class GuiIconConfig { public class GuiIconConfig {
protected final Material type; protected final Material type;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.bukkit.gui; package net.zhira.varioutil.bukkit.gui;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.bukkit.gui; package net.zhira.varioutil.bukkit.gui;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.config; package net.zhira.varioutil.config;
import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.configuration.serialization.ConfigurationSerialization;

View file

@ -0,0 +1,132 @@
package net.zhira.varioutil.util;
import com.mojang.authlib.GameProfile;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.inventory.meta.SkullMeta;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
class BukkitReflectionUtils {
private static final String OBC_PACKAGE = Bukkit.getServer().getClass().getPackage().getName();
private static final Class<?> __CraftServer__CLASS = ReflectionUtils.searchType(OBC_PACKAGE + ".CraftServer");
private static final Class<?> __CraftMetaSkull__CLASS = ReflectionUtils.searchType(OBC_PACKAGE + ".inventory.CraftMetaSkull");
private static final Field __commandMap__CraftServer__FIELD = ReflectionUtils.fieldSearcher()
.type(__CraftServer__CLASS)
.fieldOf("commandMap")
.fieldType(CommandMap.class)
.search();
private static final Field __knownCommands__SimpleCommandMap__FIELD = ReflectionUtils.fieldSearcher()
.type(SimpleCommandMap.class)
.fieldOf("knownCommands")
.fieldType(Map.class)
.search();
private static final GameProfileConsumer GAME_PROFILE_CONSUMER;
static CommandMap getCommandMap() {
return (CommandMap) ReflectionUtils.getFieldValue(Bukkit.getServer(), __commandMap__CraftServer__FIELD);
}
@SuppressWarnings("unchecked")
static Map<String, Command> getKnownCommands() {
return (Map<String, Command>) ReflectionUtils.getFieldValue(getCommandMap(), __knownCommands__SimpleCommandMap__FIELD);
}
static void setProfile(SkullMeta meta, GameProfile profile) {
GAME_PROFILE_CONSUMER.accept(meta, profile);
}
private interface GameProfileConsumer {
void accept(SkullMeta meta, GameProfile profile);
}
private static class ModernGameProfileConsumer implements GameProfileConsumer {
private final Class<?> __ResolvableProfile_CLASS = ReflectionUtils.getType("net.minecraft.world.item.component.ResolvableProfile");
private final Constructor<?> __ResolvableProfile_CONSTRUCTOR;
protected final Method __setProfile__CraftMetaSkull__METHOD = ReflectionUtils.methodSearcher()
.type(__CraftMetaSkull__CLASS)
.methodOf("setProfile")
.parameters(__ResolvableProfile_CLASS)
.returns(void.class)
.search();
ModernGameProfileConsumer() {
Constructor<?> resolvableConstructor;
try {
resolvableConstructor = __ResolvableProfile_CLASS.getConstructor(GameProfile.class);
} catch (NoSuchMethodException e) {
throw new NoSuchMethodError("Cannot get constructor of " + __ResolvableProfile_CLASS);
}
__ResolvableProfile_CONSTRUCTOR = resolvableConstructor;
}
@Override
public void accept(SkullMeta meta, GameProfile profile) {
try {
Object resolvableProfile = __ResolvableProfile_CONSTRUCTOR.newInstance(profile);
ReflectionUtils.invokeMethod(meta, __setProfile__CraftMetaSkull__METHOD, resolvableProfile);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}
private static class LegacyGameProfileConsumer implements GameProfileConsumer {
protected final Method __setProfile__CraftMetaSkull__METHOD = ReflectionUtils.methodSearcher()
.type(__CraftMetaSkull__CLASS)
.methodOf("setProfile")
.parameters(GameProfile.class)
.returns(void.class)
.search();
@Override
public void accept(SkullMeta meta, GameProfile profile) {
ReflectionUtils.invokeMethod(meta, __setProfile__CraftMetaSkull__METHOD, profile);
}
}
private static class AncientGameProfileConsumer implements GameProfileConsumer {
private final Field __profile__CraftMetaSkull__FIELD = ReflectionUtils.fieldSearcher()
.type(__CraftMetaSkull__CLASS)
.fieldOf("profile")
.fieldType(GameProfile.class)
.search();
@Override
public void accept(SkullMeta meta, GameProfile profile) {
ReflectionUtils.setFieldValue(meta, __profile__CraftMetaSkull__FIELD, profile);
}
}
@SafeVarargs
private static GameProfileConsumer tryCreateGameProfileConsumer(Supplier<GameProfileConsumer>... suppliers) {
List<String> errorMessages = new ArrayList<>();
for (Supplier<GameProfileConsumer> supplier : suppliers) {
try {
return supplier.get();
} catch (Exception | Error e) {
errorMessages.add(e.getClass() + ": " + e.getMessage());
}
}
throw new IllegalStateException("Cannot create GameProfileConsumer: " + String.join("; ", errorMessages));
}
static {
GAME_PROFILE_CONSUMER = tryCreateGameProfileConsumer(
ModernGameProfileConsumer::new,
LegacyGameProfileConsumer::new,
AncientGameProfileConsumer::new
);
}
}

View file

@ -1,7 +1,8 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property; import com.mojang.authlib.properties.Property;
import net.zhira.varioutil.Version;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandMap; import org.bukkit.command.CommandMap;
@ -9,15 +10,11 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.util.io.BukkitObjectInputStream; import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream; import org.bukkit.util.io.BukkitObjectOutputStream;
import org.zhdev.varioutil.Version;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Base64; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Predicate; import java.util.function.Predicate;
public class BukkitUtils { public class BukkitUtils {
@ -28,6 +25,8 @@ public class BukkitUtils {
private static final Map<String, GameProfile> PROFILE_CACHE = new HashMap<>(); private static final Map<String, GameProfile> PROFILE_CACHE = new HashMap<>();
private static final String PROFILE_NAME_CHARACTERS = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890_";
public static Command getCommand(String label) { public static Command getCommand(String label) {
return KNOWN_COMMANDS.get(label); return KNOWN_COMMANDS.get(label);
} }
@ -55,7 +54,12 @@ public class BukkitUtils {
public static void setSkullTexture(SkullMeta meta, String base64) { public static void setSkullTexture(SkullMeta meta, String base64) {
GameProfile profile = PROFILE_CACHE.get(base64); GameProfile profile = PROFILE_CACHE.get(base64);
if (profile == null) { if (profile == null) {
profile = new GameProfile(UUID.randomUUID(), base64); Random random = new Random();
char[] buffer = new char[16];
for (int i = 0; i < 16; i++) {
buffer[i] = PROFILE_NAME_CHARACTERS.charAt(random.nextInt(PROFILE_NAME_CHARACTERS.length()));
}
profile = new GameProfile(UUID.randomUUID(), String.valueOf(buffer));
profile.getProperties().put("textures", new Property("textures", base64)); profile.getProperties().put("textures", new Property("textures", base64));
PROFILE_CACHE.put(base64, profile); PROFILE_CACHE.put(base64, profile);
} }

View file

@ -1,51 +0,0 @@
package org.zhdev.varioutil.util;
import com.mojang.authlib.GameProfile;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.inventory.meta.SkullMeta;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import static org.zhdev.varioutil.util.ReflectionUtils.*;
class BukkitReflectionUtils {
private static final String VERSION = Bukkit.getServer().getClass().getName().split("\\.")[3];
private static final Class<?> __CraftServer__CLASS = searchType("org.bukkit.craftbukkit." + VERSION + ".CraftServer");
private static final Method __setProfile__CraftMetaSkull__METHOD = ReflectionUtils.methodSearcher()
.typeOf("org.bukkit.craftbukkit." + VERSION + ".inventory.CraftMetaSkull")
.methodOf("setProfile")
.parameters(GameProfile.class)
.returns(void.class)
.search();
private static final Field __commandMap__CraftServer__FIELD = fieldSearcher()
.fieldOf("commandMap")
.type(__CraftServer__CLASS)
.fieldType(CommandMap.class)
.search();
private static final Field __knownCommands__SimpleCommandMap__FIELD = fieldSearcher()
.fieldOf("knownCommands")
.type(SimpleCommandMap.class)
.fieldType(Map.class)
.search();
static CommandMap getCommandMap() {
return (CommandMap) getFieldValue(__commandMap__CraftServer__FIELD, Bukkit.getServer());
}
@SuppressWarnings("unchecked")
static Map<String, Command> getKnownCommands() {
return (Map<String, Command>) getFieldValue(__knownCommands__SimpleCommandMap__FIELD, getCommandMap());
}
static void setProfile(SkullMeta meta, GameProfile profile) {
ReflectionUtils.invokeMethod(meta, __setProfile__CraftMetaSkull__METHOD, profile);
}
}

View file

@ -1,9 +1,12 @@
<project> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>parent</artifactId> <artifactId>parent</artifactId>
<groupId>org.zhdev.varioutil</groupId> <groupId>net.zhira.varioutil</groupId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil; package net.zhira.varioutil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;

View file

@ -1,6 +1,6 @@
package org.zhdev.varioutil.language; package net.zhira.varioutil.language;
import org.zhdev.varioutil.util.StringUtils; import net.zhira.varioutil.util.StringUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;

View file

@ -1,6 +1,6 @@
package org.zhdev.varioutil.reflection; package net.zhira.varioutil.reflection;
import org.zhdev.varioutil.util.ReflectionUtils; import net.zhira.varioutil.util.ReflectionUtils;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -74,7 +74,7 @@ public class FieldSearcher {
} }
public Object get() { public Object get() {
return ReflectionUtils.getFieldValue(search(), instance); return ReflectionUtils.getFieldValue(instance, search());
} }
public <T> T get(Class<T> type) { public <T> T get(Class<T> type) {

View file

@ -1,6 +1,6 @@
package org.zhdev.varioutil.reflection; package net.zhira.varioutil.reflection;
import org.zhdev.varioutil.util.ReflectionUtils; import net.zhira.varioutil.util.ReflectionUtils;
import java.lang.reflect.Method; import java.lang.reflect.Method;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
import java.util.Collection; import java.util.Collection;
import java.util.function.Function; import java.util.function.Function;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
import java.time.*; import java.time.*;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
public class EnumUtils { public class EnumUtils {
private static <T extends Enum<T>> T[] getValues(T eNum) { private static <T extends Enum<T>> T[] getValues(T eNum) {

View file

@ -1,7 +1,7 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
import org.zhdev.varioutil.reflection.FieldSearcher; import net.zhira.varioutil.reflection.FieldSearcher;
import org.zhdev.varioutil.reflection.MethodSearcher; import net.zhira.varioutil.reflection.MethodSearcher;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -61,7 +61,8 @@ public class ReflectionUtils {
for (String typeName : typeNames) { for (String typeName : typeNames) {
try { try {
return Class.forName(typeName); return Class.forName(typeName);
} catch (ClassNotFoundException ignored) {} } catch (ClassNotFoundException ignored) {
}
} }
throw new NoClassDefFoundError(String.join(", ", typeNames)); throw new NoClassDefFoundError(String.join(", ", typeNames));
@ -81,11 +82,12 @@ public class ReflectionUtils {
for (String typeName : typeNames) { for (String typeName : typeNames) {
try { try {
return Class.forName(packageName + '.' + typeName); return Class.forName(packageName + '.' + typeName);
} catch (ClassNotFoundException ignored) {} } catch (ClassNotFoundException ignored) {
}
} }
} }
throw new NoClassDefFoundError(String.join(", ", typeNames) + " in " + String.join(", ", packageNames)); throw new NoClassDefFoundError(String.join(", ", typeNames) + " in " + String.join(", ", packageNames));
} }
public static Method getMethod(Class<?> type, String methodName, Class<?>... parameterTypes) throws NoSuchMethodError { public static Method getMethod(Class<?> type, String methodName, Class<?>... parameterTypes) throws NoSuchMethodError {
@ -103,7 +105,8 @@ public class ReflectionUtils {
return checkMethodType(getMethod0(type, methodName, parameterTypes), returnType); return checkMethodType(getMethod0(type, methodName, parameterTypes), returnType);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
exception = e; exception = e;
} catch (NoSuchMethodException ignored) { } } catch (NoSuchMethodException ignored) {
}
} }
if (parameterTypes.length > 0) { if (parameterTypes.length > 0) {
@ -144,7 +147,8 @@ public class ReflectionUtils {
return checkFieldType(getField0(type, fieldName), fieldType); return checkFieldType(getField0(type, fieldName), fieldType);
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
exception = e; exception = e;
} catch (NoSuchFieldException ignored) {} } catch (NoSuchFieldException ignored) {
}
} }
if (exception != null) { if (exception != null) {
@ -154,7 +158,7 @@ public class ReflectionUtils {
throw new NoSuchFieldError(String.join(", ", fieldNames) + " in " + type); throw new NoSuchFieldError(String.join(", ", fieldNames) + " in " + type);
} }
public static Object getFieldValue(Field field, Object instance) throws IllegalStateException { public static Object getFieldValue(Object instance, Field field) throws IllegalStateException {
try { try {
return field.get(instance); return field.get(instance);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
@ -162,7 +166,7 @@ public class ReflectionUtils {
} }
} }
public static void setFieldValue(Field field, Object instance, Object value) throws IllegalStateException { public static void setFieldValue(Object instance, Field field, Object value) throws IllegalStateException {
try { try {
field.set(instance, value); field.set(instance, value);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
@ -186,7 +190,8 @@ public class ReflectionUtils {
if (packagePath.equals(packageName)) { if (packagePath.equals(packageName)) {
try { try {
consumer.accept(loader.loadClass(link)); consumer.accept(loader.loadClass(link));
} catch (ClassNotFoundException ignored) {} } catch (ClassNotFoundException ignored) {
}
} }
} }
} }

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
@ -26,41 +26,44 @@ public class ResourceUtils {
return getResource(path, ResourceUtils.class.getClassLoader()); return getResource(path, ResourceUtils.class.getClassLoader());
} }
public static boolean saveResource(String resourcePath, Path outPath, ClassLoader loader) throws IllegalStateException { public static boolean saveResource(InputStream inputStream, Path outPath) throws IOException {
if (resourcePath == null || resourcePath.equals("")) { Path parent = outPath.getParent();
return false; if (parent != null && Files.notExists(parent)) {
Files.createDirectories(parent);
} }
resourcePath = resourcePath.replace('\\', '/'); if (Files.notExists(outPath) || Files.size(outPath) == 0) {
InputStream in = getResource(resourcePath, loader); OutputStream outputStream = Files.newOutputStream(outPath);
if (in == null) { byte[] buf = new byte[1024];
return false; int len;
} while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
try {
Path parent = outPath.getParent();
if (parent != null && Files.notExists(parent)) {
Files.createDirectories(parent);
} }
outputStream.close();
if (Files.notExists(outPath) || Files.size(outPath) == 0) { inputStream.close();
OutputStream stream = Files.newOutputStream(outPath); return true;
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
stream.write(buf, 0, len);
}
stream.close();
in.close();
return true;
}
} catch (IOException e) {
throw new IllegalStateException("Could not save resource " + resourcePath + " to " + outPath, e);
} }
return false; return false;
} }
public static boolean saveResource(String resourcePath, Path outPath, ClassLoader loader) throws IllegalStateException {
if (resourcePath == null || resourcePath.isEmpty()) {
return false;
}
resourcePath = resourcePath.replace('\\', '/');
InputStream inputStream = getResource(resourcePath, loader);
if (inputStream == null) {
return false;
}
try {
return saveResource(inputStream, outPath);
} catch (IOException e) {
throw new IllegalStateException("Could not save resource " + resourcePath + " to " + outPath, e);
}
}
public static boolean saveResource(String resourcePath, Path outPath) throws IllegalStateException { public static boolean saveResource(String resourcePath, Path outPath) throws IllegalStateException {
return saveResource(resourcePath, outPath, ResourceUtils.class.getClassLoader()); return saveResource(resourcePath, outPath, ResourceUtils.class.getClassLoader());

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;

View file

@ -1,9 +1,12 @@
<project> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>parent</artifactId> <artifactId>parent</artifactId>
<groupId>org.zhdev.varioutil</groupId> <groupId>net.zhira.varioutil</groupId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.sql; package net.zhira.varioutil.sql;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.sql; package net.zhira.varioutil.sql;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;

View file

@ -1,6 +1,6 @@
package org.zhdev.varioutil.sql; package net.zhira.varioutil.sql;
import org.zhdev.varioutil.util.SqlUtils; import net.zhira.varioutil.util.SqlUtils;
public class H2Provider extends AbstractProvider { public class H2Provider extends AbstractProvider {
public H2Provider(String path, String username, String password) { public H2Provider(String path, String username, String password) {

View file

@ -1,6 +1,6 @@
package org.zhdev.varioutil.sql; package net.zhira.varioutil.sql;
import org.zhdev.varioutil.util.SqlUtils; import net.zhira.varioutil.util.SqlUtils;
public class MysqlProvider extends AbstractProvider { public class MysqlProvider extends AbstractProvider {
public MysqlProvider(String address, String dbname, String username, String password, boolean ssl) { public MysqlProvider(String address, String dbname, String username, String password, boolean ssl) {

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.sql; package net.zhira.varioutil.sql;
import java.sql.Connection; import java.sql.Connection;
@ -9,7 +9,6 @@ class NullProvider implements ConnectionProvider {
this.message = message; this.message = message;
} }
@Override @Override
public Connection getConnection() { public Connection getConnection() {
throw new SqlException(message); throw new SqlException(message);

View file

@ -1,6 +1,6 @@
package org.zhdev.varioutil.sql; package net.zhira.varioutil.sql;
import org.zhdev.varioutil.util.CheckedFunction; import net.zhira.varioutil.util.CheckedFunction;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.sql; package net.zhira.varioutil.sql;
public class SqlException extends RuntimeException { public class SqlException extends RuntimeException {
public SqlException() { public SqlException() {

View file

@ -1,6 +1,6 @@
package org.zhdev.varioutil.sql; package net.zhira.varioutil.sql;
import org.zhdev.varioutil.util.SqlUtils; import net.zhira.varioutil.util.SqlUtils;
public class SqliteProvider extends AbstractProvider { public class SqliteProvider extends AbstractProvider {
public SqliteProvider(String path) { public SqliteProvider(String path) {

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
@FunctionalInterface @FunctionalInterface
public interface CheckedFunction<T, R, E extends Exception> { public interface CheckedFunction<T, R, E extends Exception> {

View file

@ -1,8 +1,11 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
import org.zhdev.varioutil.sql.SqlException; import net.zhira.varioutil.sql.SqlException;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -15,13 +18,22 @@ public class SqlUtils {
private static boolean h2; private static boolean h2;
private static boolean mysql; private static boolean mysql;
private static String encodeUrlValue(String value) throws UnsupportedEncodingException {
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
}
public static Connection createMysqlConnection(String address, String dbname, String username, String password, boolean ssl) throws SqlException { public static Connection createMysqlConnection(String address, String dbname, String username, String password, boolean ssl) throws SqlException {
try { try {
if (!address.contains(":")) { String[] addressParts = address.split(":", 2);
address = address + ":3306"; int port;
if (addressParts.length > 1) {
port = Integer.parseInt(encodeUrlValue(addressParts[1]));
address = addressParts[0];
} else {
port = 3306;
} }
return DriverManager.getConnection("jdbc:mysql://" + address + '/' + dbname + "?useSSL=" + ssl, username, password); return DriverManager.getConnection("jdbc:mysql://" + encodeUrlValue(address) + ':' + port + '/' + encodeUrlValue(dbname) + "?useSSL=" + ssl, username, password);
} catch (SQLException e) { } catch (SQLException | UnsupportedEncodingException e) {
throw new SqlException(e); throw new SqlException(e);
} }
} }

View file

@ -1,9 +1,12 @@
<project> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>parent</artifactId> <artifactId>parent</artifactId>
<groupId>org.zhdev.varioutil</groupId> <groupId>net.zhira.varioutil</groupId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.config; package net.zhira.varioutil.config;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.config; package net.zhira.varioutil.config;
public class ConfigException extends RuntimeException { public class ConfigException extends RuntimeException {
public ConfigException() { public ConfigException() {

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.config; package net.zhira.varioutil.config;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.config; package net.zhira.varioutil.config;
final class ConfigSectionNode { final class ConfigSectionNode {
Object value; Object value;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.config; package net.zhira.varioutil.config;
import java.util.*; import java.util.*;
import java.util.function.Function; import java.util.function.Function;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.config; package net.zhira.varioutil.config;
import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.LoaderOptions;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.config; package net.zhira.varioutil.config;
import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.constructor.SafeConstructor;

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.config; package net.zhira.varioutil.config;
import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.representer.Representer; import org.yaml.snakeyaml.representer.Representer;

View file

@ -1,9 +1,12 @@
<project> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>parent</artifactId> <artifactId>parent</artifactId>
<groupId>org.zhdev.varioutil</groupId> <groupId>net.zhira.varioutil</groupId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>

View file

@ -1,15 +1,16 @@
package org.zhdev.varioutil; package net.zhira.varioutil;
import org.zhdev.varioutil.config.Config; import net.zhira.varioutil.config.Config;
import org.zhdev.varioutil.config.ConfigException; import net.zhira.varioutil.config.ConfigException;
import org.zhdev.varioutil.language.Language; import net.zhira.varioutil.language.Language;
import org.zhdev.varioutil.sql.SqlAdapter; import net.zhira.varioutil.util.ResourceUtils;
import org.zhdev.varioutil.util.ResourceUtils; import net.zhira.varioutil.sql.SqlAdapter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Logger; import java.util.logging.Logger;
public interface PreparedPlugin { public interface PreparedPlugin {
@ -23,26 +24,35 @@ public interface PreparedPlugin {
Path getDataDirectory(); Path getDataDirectory();
default Path loadConfig(Config config, String pathname) { default Path loadConfig(Config config, String pathname, boolean resourcePreload) {
ClassLoader classLoader = getClass().getClassLoader(); ClassLoader classLoader = getClass().getClassLoader();
InputStream stream = ResourceUtils.getResource(pathname, classLoader); Path path = Paths.get(getDataDirectory().toString() + '/' + pathname);
if (stream != null) { try (InputStream stream = ResourceUtils.getResource(pathname, classLoader)) {
config.load(stream); if (stream != null) {
} if (resourcePreload) {
config.load(stream);
}
Path path = config.load(getDataDirectory().toString() + '/' + pathname); if (Files.notExists(path) || Files.size(path) == 0) {
try { ResourceUtils.saveResource(stream, path);
if (Files.notExists(path) || Files.size(path) == 0) {
if (ResourceUtils.saveResource(pathname, path, classLoader)) {
config.load(path);
} }
} }
if (Files.exists(path)) config.load(path);
} catch (IOException e) { } catch (IOException e) {
throw new ConfigException(e); throw new ConfigException(e);
} }
return path; return path;
} }
default Path loadConfig(Config config, String pathname) {
return loadConfig(config, pathname, false);
}
default Path loadConfig(Config config, boolean resourcePreload) {
return loadConfig(config, config.getKey(), resourcePreload);
}
default Path loadConfig(Config config) { default Path loadConfig(Config config) {
return loadConfig(config, config.getKey()); return loadConfig(config, config.getKey());
} }

View file

@ -1,13 +1,21 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class ColorUtils { public class ColorUtils {
public static final char COLOR_CHAR = '§'; public static final char COLOR_CHAR = '§';
private static final Map<String, Pattern> COMPILED_PATTERNS = new HashMap<>();
private static Pattern getPattern(String regexp) {
return COMPILED_PATTERNS.computeIfAbsent(regexp, s -> Pattern.compile(regexp));
}
public static void translateAlternateColorCodes(StringBuilder builder, char replaceChar) { public static void translateAlternateColorCodes(StringBuilder builder, char replaceChar) {
Matcher m = Pattern.compile(Pattern.quote(String.valueOf(replaceChar)) + "(#[0-9a-fA-F]{6})").matcher(builder); Matcher m = getPattern(Pattern.quote(String.valueOf(replaceChar)) + "(#[0-9a-fA-F]{6})").matcher(builder);
int start = 0; int start = 0;
while (m.find(start)) { while (m.find(start)) {
String hex = m.group(1); String hex = m.group(1);

View file

@ -1,12 +1,11 @@
package org.zhdev.varioutil.util; package net.zhira.varioutil.util;
import org.zhdev.varioutil.config.ConfigSection; import net.zhira.varioutil.config.ConfigSection;
import org.zhdev.varioutil.language.Language; import net.zhira.varioutil.language.Language;
import org.zhdev.varioutil.sql.*; import net.zhira.varioutil.sql.*;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
@ -50,8 +49,8 @@ public class ConfigUtils {
} }
case "mysql": { case "mysql": {
String address = config.getString("address", "127.0.0.1"); String address = config.getString("address", "127.0.0.1");
String dbname = config.getString("dbname", System.getProperty("user.home")); String dbname = config.getString("dbname", System.getProperty("user.name"));
String username = config.getString("username", System.getProperty("user.home")); String username = config.getString("username", System.getProperty("user.name"));
String password = config.getString("password"); String password = config.getString("password");
boolean ssl = config.getBoolean("ssl", false); boolean ssl = config.getBoolean("ssl", false);
provider = new MysqlProvider(address, dbname, username, password, ssl); provider = new MysqlProvider(address, dbname, username, password, ssl);

View file

@ -1,49 +0,0 @@
package org.zhdev.varioutil.color;
public interface ChatColor {
char CHAR = '§';
NamedChatColor BLACK = new NamedChatColor(0x000000, "black", '0');
NamedChatColor DARK_BLUE = new NamedChatColor(0x0000aa, "dark_blue", '1');
NamedChatColor DARK_GREEN = new NamedChatColor(0x00aa00, "dark_green", '2');
NamedChatColor DARK_AQUA = new NamedChatColor(0x00aaaa, "dark_aqua", '3');
NamedChatColor DARK_RED = new NamedChatColor(0xaa0000, "dark_red", '4');
NamedChatColor DARK_PURPLE = new NamedChatColor(0xaa00aa, "dark_purple", '5');
NamedChatColor GOLD = new NamedChatColor(0xffaa00, "gold", '6');
NamedChatColor GRAY = new NamedChatColor(0xaaaaaa, "gray", '7');
NamedChatColor DARK_GRAY = new NamedChatColor(0x555555, "dark_gray", '8');
NamedChatColor BLUE = new NamedChatColor(0x5555ff, "blue", '9');
NamedChatColor GREEN = new NamedChatColor(0x55ff55, "green", 'a');
NamedChatColor AQUA = new NamedChatColor(0x55ffff, "aqua", 'b');
NamedChatColor RED = new NamedChatColor(0xff5555, "red", 'c');
NamedChatColor LIGHT_PURPLE = new NamedChatColor(0xff55ff, "light_purple", 'd');
NamedChatColor YELLOW = new NamedChatColor(0xffff55, "yellow", 'e');
NamedChatColor WHITE = new NamedChatColor(0xffffff, "white", 'f');
NamedChatColor OBFUSCATED = new NamedChatColor("obfuscated", 'k');
NamedChatColor BOLD = new NamedChatColor("bold", 'l');
NamedChatColor STRIKETHROUGH = new NamedChatColor("strikethrough", 'm');
NamedChatColor UNDERLINED = new NamedChatColor("underlined", 'n');
NamedChatColor ITALIC = new NamedChatColor("italic", 'o');
NamedChatColor RESET = new NamedChatColor("reset", 'r');
int getValue();
String getName();
static ChatColor valueOf(int value) {
return ChatColorCache.BY_VALUE.computeIfAbsent(value, HexChatColor::new);
}
static ChatColor valueOf(char code) {
return ChatColorCache.BY_CODE.get(code);
}
static ChatColor valueOf(String name) {
return ChatColorCache.BY_NAME.get(name);
}
static ChatColor[] values() {
return ChatColorCache.BY_NAME.values().toArray(new ChatColor[0]);
}
}

View file

@ -1,11 +0,0 @@
package org.zhdev.varioutil.color;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
final class ChatColorCache {
static final Map<Integer, ChatColor> BY_VALUE = new ConcurrentHashMap<>();
static final Map<Character, ChatColor> BY_CODE = new LinkedHashMap<>(21);
static final Map<String, ChatColor> BY_NAME = new LinkedHashMap<>(21);
}

View file

@ -1,48 +0,0 @@
package org.zhdev.varioutil.color;
public class HexChatColor implements ChatColor {
final int value;
final String name;
final String string;
HexChatColor(int value, String name, String string) {
this.value = value;
this.name = name;
this.string = string;
}
private HexChatColor(int value, String name) {
this(value, name, hexToCode(name));
}
HexChatColor(int value) {
this(value, "#" + Integer.toHexString(value));
}
public int getValue() {
return value;
}
@Override
public String getName() {
return name;
}
@Override
public String toString() {
return string;
}
public static String hexToCode(String hexValue, char colorChar) {
StringBuilder builder = new StringBuilder().append(colorChar).append('x');
for (int i = 0; i < hexValue.length(); i++) {
builder.append(colorChar).append(hexValue.charAt(i));
}
return builder.toString();
}
public static String hexToCode(String hexValue) {
return hexToCode(hexValue, CHAR);
}
}

View file

@ -1,22 +0,0 @@
package org.zhdev.varioutil.color;
public final class NamedChatColor extends HexChatColor {
private final char code;
NamedChatColor(int value, String name, char code) {
super(value, name, String.valueOf(CHAR) + code);
this.code = code;
if (value != -1) ChatColorCache.BY_VALUE.put(value, this);
ChatColorCache.BY_CODE.put(code, this);
ChatColorCache.BY_NAME.put(name, this);
}
NamedChatColor(String name, char code) {
this(-1, name, code);
}
public char getCode() {
return code;
}
}

View file

@ -1,20 +0,0 @@
package org.zhdev.varioutil.component;
public enum ClickEvent {
OPEN_URL("open_url"),
RUN_COMMAND("run_command"),
SUGGEST_COMMAND("suggest_command"),
CHANGE_PAGE("change_page"),
COPY_TO_CLIPBOARD("copy_to_clipboard");
private final String value;
ClickEvent(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}

View file

@ -1,400 +0,0 @@
package org.zhdev.varioutil.component;
import org.zhdev.varioutil.color.ChatColor;
import java.util.*;
public final class Component {
public static final Component EMPTY = new Component(ComponentType.TEXT, "");
private final ComponentType type;
private final String value;
private final Boolean bold;
private final Boolean italic;
private final Boolean underlined;
private final Boolean strikethrough;
private final Boolean obfuscated;
private final MinecraftFont font;
private final ChatColor color;
private final String insertion;
private final ClickEvent clickEvent;
private final String clickEventValue;
private final HoverEvent hoverEvent;
private final Component[] hoverEventValue;
private final Component[] extra;
private final Component[] with;
public Component(ComponentType type, String value, Boolean bold, Boolean italic, Boolean underlined, Boolean strikethrough, Boolean obfuscated, MinecraftFont font, ChatColor color, String insertion, ClickEvent clickEvent, String clickEventValue, HoverEvent hoverEvent, Component[] hoverEventValue, Component[] extra, Component[] with) {
this.type = type;
this.value = value;
this.bold = bold;
this.italic = italic;
this.underlined = underlined;
this.strikethrough = strikethrough;
this.obfuscated = obfuscated;
this.font = font;
this.color = color;
this.insertion = insertion;
this.clickEvent = clickEvent;
this.clickEventValue = clickEventValue;
this.hoverEvent = hoverEvent;
this.hoverEventValue = hoverEventValue;
this.extra = extra;
this.with = with;
}
public Component(String value, Boolean bold, Boolean italic, Boolean underlined, Boolean strikethrough, Boolean obfuscated, ChatColor color, ClickEvent clickEvent, String clickEventValue) {
this(ComponentType.TEXT, value, bold, italic, underlined, strikethrough, obfuscated, null, color, null, clickEvent, clickEventValue, null, null, null, null);
}
public Component(String value, Boolean bold, Boolean italic, Boolean underlined, Boolean strikethrough, Boolean obfuscated, ChatColor color) {
this(value, bold, italic, underlined, strikethrough, obfuscated, color, null, null);
}
public Component(ComponentType type, String value) {
this(type, value, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
}
public ComponentType getType() {
return type;
}
public String getValue() {
return value;
}
public Boolean getBold() {
return bold;
}
public Boolean getItalic() {
return italic;
}
public Boolean getUnderlined() {
return underlined;
}
public Boolean getStrikethrough() {
return strikethrough;
}
public Boolean getObfuscated() {
return obfuscated;
}
public MinecraftFont getFont() {
return font;
}
public ChatColor getColor() {
return color;
}
public String getInsertion() {
return insertion;
}
public ClickEvent getClickEvent() {
return clickEvent;
}
public String getClickEventValue() {
return clickEventValue;
}
public HoverEvent getHoverEvent() {
return hoverEvent;
}
public Component[] getHoverEventValue() {
return hoverEventValue;
}
public Component[] getExtra() {
return extra;
}
public Component[] getWith() {
return with;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Component component = (Component) o;
if (type != component.type) return false;
if (!Objects.equals(value, component.value)) return false;
if (!Objects.equals(bold, component.bold)) return false;
if (!Objects.equals(italic, component.italic)) return false;
if (!Objects.equals(underlined, component.underlined)) return false;
if (!Objects.equals(strikethrough, component.strikethrough))
return false;
if (!Objects.equals(obfuscated, component.obfuscated)) return false;
if (font != component.font) return false;
if (!Objects.equals(color, component.color)) return false;
if (!Objects.equals(insertion, component.insertion)) return false;
if (clickEvent != component.clickEvent) return false;
if (!Objects.equals(clickEventValue, component.clickEventValue))
return false;
if (hoverEvent != component.hoverEvent) return false;
if (!Arrays.equals(hoverEventValue, component.hoverEventValue)) return false;
if (!Arrays.equals(extra, component.extra)) return false;
return Arrays.equals(with, component.with);
}
@Override
public int hashCode() {
int result = type != null ? type.hashCode() : 0;
result = 31 * result + (value != null ? value.hashCode() : 0);
result = 31 * result + (bold != null ? bold.hashCode() : 0);
result = 31 * result + (italic != null ? italic.hashCode() : 0);
result = 31 * result + (underlined != null ? underlined.hashCode() : 0);
result = 31 * result + (strikethrough != null ? strikethrough.hashCode() : 0);
result = 31 * result + (obfuscated != null ? obfuscated.hashCode() : 0);
result = 31 * result + (font != null ? font.hashCode() : 0);
result = 31 * result + (color != null ? color.hashCode() : 0);
result = 31 * result + (insertion != null ? insertion.hashCode() : 0);
result = 31 * result + (clickEvent != null ? clickEvent.hashCode() : 0);
result = 31 * result + (clickEventValue != null ? clickEventValue.hashCode() : 0);
result = 31 * result + (hoverEvent != null ? hoverEvent.hashCode() : 0);
result = 31 * result + Arrays.hashCode(hoverEventValue);
result = 31 * result + Arrays.hashCode(extra);
result = 31 * result + Arrays.hashCode(with);
return result;
}
@Override
public String toString() {
return "Component{" + "type=" + type + ", value='" + value + "', bold=" + bold + ", italic=" + italic + ", underlined=" + underlined + ", strikethrough=" + strikethrough + ", obfuscated=" + obfuscated + ", font=" + font + ", color=" + color + ", insertion='" + insertion + "', clickEvent=" + clickEvent + ", clickEventValue='" + clickEventValue + "', hoverEvent=" + hoverEvent + ", hoverEventValue=" + Arrays.toString(hoverEventValue) + ", extra=" + Arrays.toString(extra) + ", with=" + Arrays.toString(with) + '}';
}
public static Builder of(String value) {
return new Builder(value);
}
public static Builder textOf(String value) {
return new Builder(value).type(ComponentType.TEXT);
}
public static Builder translateOf(String value) {
return of(value).type(ComponentType.TRANSLATE);
}
public static Builder keybindOf(String value) {
return of(value).type(ComponentType.KEYBIND);
}
public static Builder scoreOf(String value) {
return of(value).type(ComponentType.SCORE);
}
public static Builder selectorOf(String value) {
return of(value).type(ComponentType.SELECTOR);
}
public static final class Builder {
private final List<Component> components;
private ComponentType type = ComponentType.TEXT;
private String value;
private Boolean bold;
private Boolean italic;
private Boolean underlined;
private Boolean strikethrough;
private Boolean obfuscated;
private MinecraftFont font;
private ChatColor color;
private String insertion;
private ClickEvent clickEvent;
private String clickEventValue;
private HoverEvent hoverEvent;
private Component[] hoverEventValue;
private Component[] extra;
private Component[] with;
public Builder(String value, int initialCapacity) {
this.components = new ArrayList<>(initialCapacity);
this.value = value;
}
public Builder(String value) {
this.components = new ArrayList<>();
this.value = value;
}
public Builder type(ComponentType type) {
this.type = type;
return this;
}
public Builder value(String value) {
this.value = value;
return this;
}
public Builder text(String value) {
this.type = ComponentType.TEXT;
this.value = value;
return this;
}
public Builder translate(String value) {
this.type = ComponentType.TRANSLATE;
this.value = value;
return this;
}
public Builder keybind(String value) {
this.type = ComponentType.KEYBIND;
this.value = value;
return this;
}
public Builder score(String value) {
this.type = ComponentType.SCORE;
this.value = value;
return this;
}
public Builder selector(String value) {
this.type = ComponentType.SELECTOR;
this.value = value;
return this;
}
public Builder bold(Boolean bold) {
this.bold = bold;
return this;
}
public Builder italic(Boolean italic) {
this.italic = italic;
return this;
}
public Builder underlined(Boolean underlined) {
this.underlined = underlined;
return this;
}
public Builder strikethrough(Boolean strikethrough) {
this.strikethrough = strikethrough;
return this;
}
public Builder obfuscated(Boolean obfuscated) {
this.obfuscated = obfuscated;
return this;
}
public Builder font(MinecraftFont font) {
this.font = font;
return this;
}
public Builder color(ChatColor color) {
this.color = color;
return this;
}
public Builder insertion(String insertion) {
this.insertion = insertion;
return this;
}
public Builder click(ClickEvent clickEvent, String clickEventValue) {
this.clickEvent = clickEvent;
this.clickEventValue = clickEventValue;
return this;
}
public Builder click(String clickEventValue) {
return click(ClickEvent.OPEN_URL, clickEventValue);
}
public Builder hover(HoverEvent hoverEvent, Component... hoverEventValue) {
this.hoverEvent = hoverEvent;
this.hoverEventValue = hoverEventValue;
return this;
}
public Builder hover(Component... hoverEventValue) {
return hover(HoverEvent.SHOW_TEXT, hoverEventValue);
}
public Builder extra(Component... extra) {
this.extra = extra;
return this;
}
public Builder with(Component... with) {
this.with = with;
return this;
}
public Component create() {
return new Component(type, value, bold, italic, underlined, strikethrough, obfuscated, font, color, insertion, clickEvent, clickEventValue, hoverEvent, hoverEventValue, extra, with);
}
public Builder push() {
Component component = create();
components.add(component);
return this;
}
public void add(Component component) {
this.components.add(component);
}
public void addAll(Collection<Component> components) {
this.components.addAll(components);
}
public void addAll(Component... components) {
Collections.addAll(this.components, components);
}
public Builder append(String value) {
return push().value(value);
}
public Builder appendText(String value) {
return push().type(ComponentType.TEXT).value(value);
}
public Builder appendTranslate(String value) {
return push().type(ComponentType.TRANSLATE).value(value);
}
public Builder appendKeybind(String value) {
return push().type(ComponentType.KEYBIND).value(value);
}
public Builder appendScore(String value) {
return push().type(ComponentType.SCORE).value(value);
}
public Builder appendSelector(String value) {
return push().type(ComponentType.SELECTOR).value(value);
}
public Component[] result() {
return components.toArray(new Component[0]);
}
public Component[] build() {
return push().result();
}
}
}

View file

@ -1,385 +0,0 @@
package org.zhdev.varioutil.component;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.zhdev.varioutil.color.ChatColor;
import org.zhdev.varioutil.color.NamedChatColor;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ComponentSerializer {
private static final Pattern URL_PATTERN = Pattern.compile("^(https?://)?([-\\w_.]{2,}\\.[a-z]{2,63})(/\\S*)?$");
private static String readAsString(JSONObject object, String key) {
Object element = object.get(key);
return element instanceof String ? (String) element : null;
}
private static Boolean readAsBoolean(JSONObject object, String key) {
Object element = object.get(key);
return element instanceof Boolean ? (Boolean) element : null;
}
private static <T extends Enum<T>> T readAsEnum(JSONObject object, Class<T> enumClass) {
Object element = object.get("action");
if (!(element instanceof String)) {
return null;
}
try {
return Enum.valueOf(enumClass, ((String) element).toUpperCase());
} catch (IllegalArgumentException e) {
return null;
}
}
private static ChatColor readAsColor(JSONObject object) {
Object element = object.get("color");
if (!(element instanceof String)) {
return null;
}
String value = (String) element;
try {
return value.startsWith("#") ? ChatColor.valueOf(Integer.decode(value)) : ChatColor.valueOf(value.toLowerCase());
} catch (IllegalArgumentException e) {
return null;
}
}
private static JSONObject readAsObject(JSONObject object, String key) {
Object element = object.get(key);
return element instanceof JSONObject ? (JSONObject) element : null;
}
private static void writeRawProperty(StringBuilder builder, String key, String value) {
builder.append(',').append('"').append(JSONValue.escape(key)).append('"').append(':').append(value);
}
private static void writeRawPropertyWithoutComma(StringBuilder builder, String key, String value) {
builder.append('"').append(JSONValue.escape(key)).append('"').append(':').append(value);
}
private static void writeProperty(StringBuilder builder, String key, Object value) {
if (value != null) writeRawProperty(builder, key, JSONValue.toJSONString(value));
}
private static void writePropertyWithoutComma(StringBuilder builder, String key, Object value) {
if (value != null) writeRawPropertyWithoutComma(builder, key, JSONValue.toJSONString(value));
}
private static void writeProperty(StringBuilder builder, ChatColor value) {
if (value != null) writeRawProperty(builder, "color", JSONValue.toJSONString(value.getName()));
}
private static Component deserialize0(JSONObject object) {
ComponentType type;
Object element;
if ((element = object.get("text")) != null) {
type = ComponentType.TEXT;
} else if ((element = object.get("translate")) != null) {
type = ComponentType.TRANSLATE;
} else if ((element = object.get("keybind")) != null) {
type = ComponentType.KEYBIND;
} else if ((element = object.get("score")) != null) {
type = ComponentType.SCORE;
} else if ((element = object.get("selector")) != null) {
type = ComponentType.SELECTOR;
} else {
return Component.EMPTY;
}
String value = element.toString();
Boolean bold = readAsBoolean(object, "bold");
Boolean italic = readAsBoolean(object, "italic");
Boolean underlined = readAsBoolean(object, "underlined");
Boolean strikethrough = readAsBoolean(object, "strikethrough");
Boolean obfuscated = readAsBoolean(object, "obfuscated");
MinecraftFont font = MinecraftFont.byNamespace(readAsString(object, "font"));
ChatColor color = readAsColor(object);
String insertion = readAsString(object, "insertion");
ClickEvent clickEvent;
String clickEventValue;
JSONObject clickEventObject = readAsObject(object, "clickEvent");
if (clickEventObject != null) {
clickEvent = readAsEnum(clickEventObject, ClickEvent.class);
clickEventValue = readAsString(clickEventObject, "value");
} else {
clickEvent = null;
clickEventValue = null;
}
HoverEvent hoverEvent;
Component[] hoverEventValue;
JSONObject hoverEventObject = readAsObject(object, "hoverEvent");
if (hoverEventObject != null) {
hoverEvent = readAsEnum(hoverEventObject, HoverEvent.class);
hoverEventValue = deserialize(readAsObject(hoverEventObject, "value"));
} else {
hoverEvent = null;
hoverEventValue = null;
}
Component[] extra = null;
Object extraElement = object.get("extra");
if (extraElement != null) {
extra = deserialize(extraElement);
}
Component[] with = null;
Object withElement = object.get("with");
if (withElement != null) {
with = deserialize(withElement);
}
return new Component(type, value, bold, italic, underlined, strikethrough, obfuscated, font, color, insertion,
clickEvent, clickEventValue, hoverEvent, hoverEventValue, extra, with);
}
public static Component[] deserialize(Object json) {
Component[] components;
if (json instanceof JSONArray) {
JSONArray array = (JSONArray) json;
components = new Component[array.size()];
for (int i = 0; i < components.length; i++) {
Object e = array.get(i);
if (!(e instanceof JSONObject)) {
continue;
}
components[i] = deserialize0((JSONObject) e);
}
} else if (json instanceof JSONObject) {
components = new Component[] {deserialize0((JSONObject) json)};
} else {
components = new Component[] {Component.EMPTY};
}
return components;
}
public static Component[] deserialize(String json) {
return deserialize(JSONValue.parse(json));
}
public static Component[] deserializeLegacy(String value, char colorChar, ChatColor resetColor) {
List<Component> components = new ArrayList<>();
StringBuilder valueBuilder = new StringBuilder();
int lastIndex = value.length() - 1;
Matcher urlMatcher = URL_PATTERN.matcher(value);
ChatColor color = null;
Boolean bold = null;
Boolean italic = null;
Boolean underlined = null;
Boolean strikethrough = null;
Boolean obfuscated = null;
for (int i = 0; i < value.length(); i++) {
char currentChar = value.charAt(i);
if (currentChar == colorChar) {
int j = i + 1;
if (j > lastIndex) {
valueBuilder.append(colorChar);
break;
}
ChatColor nextColor;
char nextChar = Character.toLowerCase(value.charAt(j));
if (nextChar == 'x') {
StringBuilder hexValueBuilder = new StringBuilder(7).append('#');
j++;
int n = j + 12;
if (n > lastIndex) {
valueBuilder.append(colorChar);
continue;
}
for (; j < n; j++) {
nextChar = value.charAt(j);
if (nextChar == colorChar) {
continue;
}
hexValueBuilder.append(nextChar);
}
if (hexValueBuilder.length() < 7) {
valueBuilder.append(colorChar);
continue;
}
try {
nextColor = ChatColor.valueOf(Integer.decode(hexValueBuilder.toString()));
if (nextColor == null) {
valueBuilder.append(colorChar);
continue;
}
} catch (NumberFormatException e) {
valueBuilder.append(colorChar);
continue;
}
i = j - 1;
} else {
nextColor = ChatColor.valueOf(nextChar);
if (nextColor == null) {
valueBuilder.append(colorChar);
continue;
}
i++;
}
if (valueBuilder.length() > 0) {
components.add(new Component(valueBuilder.toString(), bold, italic, underlined, strikethrough,
obfuscated, color));
valueBuilder.setLength(0);
}
if (nextColor instanceof NamedChatColor) {
if (nextColor == NamedChatColor.BOLD) {
bold = true;
continue;
} else if (nextColor == NamedChatColor.ITALIC) {
italic = true;
continue;
} else if (nextColor == NamedChatColor.UNDERLINED) {
underlined = true;
continue;
} else if (nextColor == NamedChatColor.STRIKETHROUGH) {
strikethrough = true;
continue;
} else if (nextColor == NamedChatColor.OBFUSCATED) {
obfuscated = true;
continue;
} else if (nextColor == NamedChatColor.RESET) {
nextColor = resetColor;
}
}
bold = null;
italic = null;
underlined = null;
strikethrough = null;
obfuscated = null;
color = nextColor;
continue;
}
int nextIndex = value.indexOf(' ', i);
if (nextIndex == -1) {
nextIndex = value.length();
}
if (urlMatcher.region(i, nextIndex).find()) {
if (valueBuilder.length() > 0) {
components.add(new Component(valueBuilder.toString(), bold, italic, underlined, strikethrough,
obfuscated, color));
valueBuilder.setLength(0);
}
String proto = urlMatcher.group(1);
StringBuilder urlBuilder = new StringBuilder();
if (proto == null || proto.length() == 0) {
urlBuilder.append("http://");
} else {
urlBuilder.append(proto);
}
String domain = urlMatcher.group(2);
urlBuilder.append(domain);
String uri = urlMatcher.group(3);
if (uri != null && uri.length() > 0) {
urlBuilder.append(uri);
}
String url = urlBuilder.toString();
components.add(new Component(urlMatcher.group(), bold, italic, underlined, strikethrough, obfuscated,
color, ClickEvent.OPEN_URL, url));
i += nextIndex - i - 1;
continue;
}
valueBuilder.append(currentChar);
}
components.add(new Component(valueBuilder.toString(), bold, italic, underlined, strikethrough, obfuscated, color));
return components.toArray(new Component[0]);
}
public static Component[] deserializeLegacy(String value, char colorChar) {
return deserializeLegacy(value, colorChar, null);
}
public static Component[] deserializeLegacy(String value) {
return deserializeLegacy(value, ChatColor.CHAR);
}
public static String serialize(Component component) {
StringBuilder builder = new StringBuilder().append('{');
String value = component.getValue();
writePropertyWithoutComma(builder, component.getType().toString(), value);
writeProperty(builder, "bold", component.getBold());
writeProperty(builder, "italic", component.getItalic());
writeProperty(builder, "underlined", component.getUnderlined());
writeProperty(builder, "strikethrough", component.getStrikethrough());
writeProperty(builder, "obfuscated", component.getObfuscated());
writeProperty(builder, "font", component.getFont());
writeProperty(builder, component.getColor());
writeProperty(builder, "insertion", component.getInsertion());
if (component.getClickEvent() != null) {
StringBuilder clickEventBuilder = new StringBuilder().append('{');
writePropertyWithoutComma(clickEventBuilder, "action", component.getClickEvent().toString());
writeProperty(clickEventBuilder, "value", component.getClickEventValue());
writeRawProperty(builder, "clickEvent", clickEventBuilder.append('}').toString());
}
if (component.getHoverEvent() != null) {
StringBuilder hoverEventBuilder = new StringBuilder().append('{');
writePropertyWithoutComma(hoverEventBuilder, "action", component.getHoverEvent().toString());
writeRawProperty(hoverEventBuilder, "value", serialize(component.getHoverEventValue()));
writeRawProperty(builder, "hoverEvent", hoverEventBuilder.append('}').toString());
}
if (component.getExtra() != null) {
writeRawProperty(builder, "extra", serialize(component.getExtra()));
}
if (component.getWith() != null) {
writeRawProperty(builder, "with", serialize(component.getWith()));
}
return builder.append('}').toString();
}
public static String serialize(Component... components) {
if (components.length == 0) {
return "{\"text\":\"\"}";
} else if (components.length == 1) {
return serialize(components[0]);
} else {
StringBuilder builder = new StringBuilder();
for (Component component : components) {
if (builder.length() == 0) {
builder.append('[');
} else {
builder.append(',');
}
builder.append(serialize(component));
}
return builder.append(']').toString();
}
}
}

View file

@ -1,20 +0,0 @@
package org.zhdev.varioutil.component;
public enum ComponentType {
TEXT("text"),
TRANSLATE("translate"),
KEYBIND("keybind"),
SCORE("score"),
SELECTOR("selector");
private final String key;
ComponentType(String key) {
this.key = key;
}
@Override
public String toString() {
return key;
}
}

View file

@ -1,18 +0,0 @@
package org.zhdev.varioutil.component;
public enum HoverEvent {
SHOW_TEXT("show_text"),
SHOW_ITEM("show_item"),
SHOW_ENTITY("show_entity");
private final String value;
HoverEvent(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}

View file

@ -1,32 +0,0 @@
package org.zhdev.varioutil.component;
import java.util.LinkedHashMap;
import java.util.Map;
public enum MinecraftFont {
UNIFORM("minecraft:uniform"),
ALT("minecraft:alt"),
DEFAULT("minecraft:default");
private static final Map<String, MinecraftFont> BY_NAMESPACE = new LinkedHashMap<>();
private final String value;
MinecraftFont(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
public static MinecraftFont byNamespace(String namespace) {
return BY_NAMESPACE.get(namespace);
}
static {
for (MinecraftFont font : values()) {
BY_NAMESPACE.put(font.value, font);
}
}
}

17
pom.xml
View file

@ -1,7 +1,10 @@
<project> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.zhdev.varioutil</groupId> <groupId>net.zhira.varioutil</groupId>
<artifactId>parent</artifactId> <artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
@ -21,6 +24,13 @@
<module>all</module> <module>all</module>
</modules> </modules>
<distributionManagement>
<repository>
<id>zhira-repo</id>
<url>https://maven.zhira.net/repository/</url>
</repository>
</distributionManagement>
<build> <build>
<defaultGoal>install</defaultGoal> <defaultGoal>install</defaultGoal>
<resources> <resources>
@ -107,9 +117,6 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId> <artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version> <version>2.7</version>
<configuration>
<altDeploymentRepository>local::default::file:${project.basedir}/target/deploy</altDeploymentRepository>
</configuration>
<executions> <executions>
<execution> <execution>
<id>deploy</id> <id>deploy</id>

View file

@ -1,14 +1,24 @@
<project> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<artifactId>parent</artifactId> <artifactId>parent</artifactId>
<groupId>org.zhdev.varioutil</groupId> <groupId>net.zhira.varioutil</groupId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</parent> </parent>
<artifactId>velocity</artifactId> <artifactId>velocity</artifactId>
<repositories>
<repository>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
</repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil; package net.zhira.varioutil;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import org.slf4j.Logger; import org.slf4j.Logger;

View file

@ -1,17 +1,17 @@
package org.zhdev.varioutil; package net.zhira.varioutil;
import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; import com.velocitypowered.api.event.proxy.ProxyShutdownEvent;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import net.zhira.varioutil.config.ConfigSection;
import net.zhira.varioutil.language.Language;
import net.zhira.varioutil.util.ColorUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.zhdev.varioutil.config.Config; import net.zhira.varioutil.config.Config;
import org.zhdev.varioutil.config.ConfigSection; import net.zhira.varioutil.config.VelocityTomlConfig;
import org.zhdev.varioutil.config.VelocityTomlConfig; import net.zhira.varioutil.sql.SqlAdapter;
import org.zhdev.varioutil.language.Language; import net.zhira.varioutil.util.ConfigUtils;
import org.zhdev.varioutil.sql.SqlAdapter;
import org.zhdev.varioutil.util.ColorUtils;
import org.zhdev.varioutil.util.ConfigUtils;
import java.nio.file.Path; import java.nio.file.Path;
@ -32,9 +32,9 @@ public abstract class VelocityPreparedPlugin extends VelocityPlugin implements P
ConfigSection languageSection = defaultConfig.getOrCreateSection("language"); ConfigSection languageSection = defaultConfig.getOrCreateSection("language");
String locale = languageSection.getString("locale", "default"); String locale = languageSection.getString("locale", "default");
Config languageConfig = new VelocityTomlConfig("language/" + locale + ".toml"); Config languageConfig = new VelocityTomlConfig("language/" + locale + ".toml");
loadConfig(languageConfig, "language.toml"); loadConfig(languageConfig, "language.toml", true);
loadConfig(languageConfig, "language/default.toml"); loadConfig(languageConfig, "language/default.toml", true);
loadConfig(languageConfig); loadConfig(languageConfig, true);
ConfigUtils.addPhrases(language, languageConfig, ColorUtils::translateAlternateColorCodes); ConfigUtils.addPhrases(language, languageConfig, ColorUtils::translateAlternateColorCodes);
} }

View file

@ -1,4 +1,4 @@
package org.zhdev.varioutil.config; package net.zhira.varioutil.config;
import com.electronwill.nightconfig.core.CommentedConfig; import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.toml.TomlFormat; import com.electronwill.nightconfig.toml.TomlFormat;