From c3ff403602530acdff1a5940cf8c79b78754673a Mon Sep 17 00:00:00 2001 From: Hxncus Date: Sat, 3 Aug 2024 23:46:45 +0500 Subject: [PATCH] 1.0.0 Testing setting scale attribute to living entities and players --- .github/ISSUE_TEMPLATE/bug-report.yml | 2 +- .github/workflows/build.yml | 68 +++++ .github/workflows/pre_build_jars.yml | 2 - .github/workflows/release.yml | 52 ++++ build.gradle.kts | 18 +- .../mc/entitygenetics/EntityGenetics.java | 90 +++++-- .../mc/entitygenetics/color/ColorManager.java | 184 ------------- .../color/caching/LruCache.java | 90 ------- .../color/caching/LruElement.java | 51 ---- .../color/pattern/GradientPattern.java | 27 -- .../entitygenetics/color/pattern/Pattern.java | 5 - .../color/pattern/RainbowPattern.java | 25 -- .../color/pattern/SolidPattern.java | 27 -- .../entitygenetics/config/ConfigManager.java | 120 --------- .../mc/entitygenetics/config/Messages.java | 10 +- .../mc/entitygenetics/config/Settings.java | 19 +- .../listener/EntityListener.java | 27 -- .../listener/PlayerListener.java | 7 - .../entitygenetics/util/ReflectionUtil.java | 254 ------------------ .../mc/entitygenetics/util/VersionUtil.java | 92 ------- bukkit/src/main/resources/plugin.yml | 6 +- bukkit/src/main/resources/settings.yml | 24 ++ gradle.properties | 5 +- 23 files changed, 251 insertions(+), 954 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/release.yml delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/ColorManager.java delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/caching/LruCache.java delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/caching/LruElement.java delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/GradientPattern.java delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/Pattern.java delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/RainbowPattern.java delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/SolidPattern.java delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/ConfigManager.java delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/listener/EntityListener.java delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/listener/PlayerListener.java delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/util/ReflectionUtil.java delete mode 100644 bukkit/src/main/java/kz/hxncus/mc/entitygenetics/util/VersionUtil.java diff --git a/.github/ISSUE_TEMPLATE/bug-report.yml b/.github/ISSUE_TEMPLATE/bug-report.yml index 6d939dd..53590e5 100644 --- a/.github/ISSUE_TEMPLATE/bug-report.yml +++ b/.github/ISSUE_TEMPLATE/bug-report.yml @@ -68,7 +68,7 @@ body: label: Server logs and config description: | If you think it will be helpful, please include any server logs. - Also please provide the config.yml file from `plugins/EntityGenetics/config.yml`. + Also please provide the settings.yml file from `plugins/EntityGenetics/settings.yml`. placeholder: Logs and configs (if you think it'll help!) validations: required: false diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..48036de --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,68 @@ +name: Java CI with Gradle +on: + push: + branches: + - development + pull_request: + branches: + - development +jobs: + build: + permissions: write-all + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4.1.7 + - name: Set up JDK + uses: actions/setup-java@v4.2.1 + with: + distribution: adopt + java-version: 21 + - name: Make build.gradle executable + run: chmod +x gradlew + - name: Build plugin + run: ./gradlew build + - name: Upload plugin + uses: actions/upload-artifact@v4.3.5 + with: + name: EntityGenetics + path: "*/build/libs/*.jar" + - uses: dev-drprasad/delete-tag-and-release@v1.1 + if: ${{ github.event_name == 'push' }} + with: + delete_release: true + tag_name: dev-build + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + - name: Find git version + id: git-version + run: echo "id=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT + - name: Find correct JAR + if: ${{ github.event_name == 'push' }} + id: find-jar + run: | + output="$(find build/libs/ ! -name "*-javadoc.jar" ! -name "*-sources.jar" -type f -printf "%f\n")" + echo "::set-output name=jarname::$output" + - name: Release the build + if: ${{ github.event_name == 'push' }} + uses: ncipollo/release-action@v1 + with: + artifacts: build/libs/${{ steps.find-jar.outputs.jarname }} + body: ${{ join(github.event.commits.*.message, '\n') }} + prerelease: true + name: Dev-build ${{ steps.git-version.outputs.id }} + tag: dev-build + - name: Upload to Modrinth + if: ${{ github.event_name == 'push' }} + uses: RubixDev/modrinth-upload@v1.0.0 + with: + token: ${{ secrets.MODRINTH_TOKEN }} + file_path: build/libs/${{ steps.find-jar.outputs.jarname }} + name: Dev-build ${{ steps.git-version.outputs.id }} + version: ${{ steps.git-version.outputs.id }} + changelog: ${{ join(github.event.commits.*.message, '\n') }} + game_versions: 1.21,1.20.6,1.20.5,1.20.4,1.20.3,1.20.2,1.20.1,1.20,1.19.4,1.19.3,1.19.2,1.19.1,1.19,1.18.2,1.18.1,1.18,1.17.1,1.17,1.16.5,1.16.4,1.16.3,1.16.2,1.16.1,1.16,1.15.2,1.15.1,1.15,1.14.4,1.14.3,1.14.2,1.14.1,1.14,1.13.2,1.13.1,1.13,1.12.2,1.12.1,1.12,1.11.2,1.11.1,1.11,1.10.2,1.10.1,1.10,1.9.4,1.9.3,1.9.2,1.9.1,1.9,1.8.9,1.8.8,1.8.7,1.8.6,1.8.5,1.8.4,1.8.3,1.8.2,1.8.1,1.8 + release_type: beta + loaders: bukkit,spigot,paper,purpur,folia + featured: false + project_id: Th1vTBns diff --git a/.github/workflows/pre_build_jars.yml b/.github/workflows/pre_build_jars.yml index 2a8f0e4..d1a64ee 100644 --- a/.github/workflows/pre_build_jars.yml +++ b/.github/workflows/pre_build_jars.yml @@ -1,5 +1,4 @@ name: Test compiling against Java 8, 11 and 16 - on: pull_request: branches: @@ -7,7 +6,6 @@ on: paths: - "src/**" - "build.gradle" - jobs: testBuilds: strategy: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..c2ad5b4 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,52 @@ +name: Java CI with Gradle +on: + release: + types: [published] +jobs: + build: + permissions: write-all + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4.1.7 + - name: Set up JDK + uses: actions/setup-java@v4.2.1 + with: + distribution: adopt + java-version: 21 + - name: Make build.gradle executable + run: chmod +x gradlew + - name: Build plugin + run: ./gradlew build + - name: Upload plugin + uses: actions/upload-artifact@v4.3.5 + with: + name: EntityGenetics + path: "*/build/libs/*.jar" + - name: Find correct JAR + id: find-jar + run: | + output="$(find build/libs/ ! -name "*-javadoc.jar" ! -name "*-sources.jar" -type f -printf "%f\n")" + echo "::set-output name=jarname::$output" + - name: Upload to the GitHub release + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + with: + upload_url: ${{ github.event.release.upload_url }} + asset_path: build/libs/${{ steps.find-jar.outputs.jarname }} + asset_name: ${{ steps.find-jar.outputs.jarname }} + asset_content_type: application/java-archive + - name: Upload to Modrinth + uses: RubixDev/modrinth-upload@v1.0.0 + with: + token: ${{ secrets.MODRINTH_TOKEN }} + file_path: build/libs/${{ steps.find-jar.outputs.jarname }} + name: Release ${{ github.event.release.tag_name }} + version: ${{ github.event.release.tag_name }} + changelog: ${{ github.event.release.body }} + game_versions: 1.21,1.20.6,1.20.5 + release_type: release + loaders: bukkit,spigot,paper,purpur,folia + featured: true + project_id: Th1vTBns diff --git a/build.gradle.kts b/build.gradle.kts index 16da348..256584b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,6 +17,15 @@ version = property("projectVersion") as String allprojects { apply(plugin = "java") + tasks { + processResources { + filesMatching("**/plugin.yml") { + expand("version" to rootProject.version, "name" to rootProject.name) + } + duplicatesStrategy = DuplicatesStrategy.INCLUDE + filteringCharset = Charsets.UTF_8.name() + } + } repositories { mavenCentral() maven("https://papermc.io/repo/repository/maven-public/") // Paper @@ -29,8 +38,8 @@ allprojects { } dependencies { compileOnly("org.spigotmc:spigot-api:$spigot") - compileOnly(fileTree("../libs/compileOnly/")) compileOnly("org.projectlombok:lombok:$lombok") + compileOnly(fileTree("../libs/compileOnly/")) implementation("org.bstats:bstats-bukkit:$bstats") implementation(fileTree("../libs/implementation/")) @@ -53,13 +62,6 @@ tasks { compileJava.get().options.encoding = Charsets.UTF_8.name() javadoc.get().options.encoding = Charsets.UTF_8.name() processResources { - inputs.properties( - "version" to version, - "name" to rootProject.name - ) - filesMatching("**/plugin.yml") { - expand(mapOf("version" to version, "name" to rootProject.name)) - } duplicatesStrategy = DuplicatesStrategy.INCLUDE filteringCharset = Charsets.UTF_8.name() } diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/EntityGenetics.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/EntityGenetics.java index 3eab6af..3161d9d 100644 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/EntityGenetics.java +++ b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/EntityGenetics.java @@ -1,18 +1,20 @@ package kz.hxncus.mc.entitygenetics; -import kz.hxncus.mc.entitygenetics.color.ColorManager; -import kz.hxncus.mc.entitygenetics.config.ConfigManager; -import kz.hxncus.mc.entitygenetics.listener.EntityListener; -import kz.hxncus.mc.entitygenetics.listener.PlayerListener; +import kz.hxncus.mc.minesonapi.MinesonAPI; +import kz.hxncus.mc.minesonapi.bukkit.event.EventManager; +import kz.hxncus.mc.minesonapi.random.SimpleRandom; +import kz.hxncus.mc.minesonapi.util.AttributeUtil; import lombok.Getter; -import org.bukkit.plugin.PluginManager; +import org.bukkit.attribute.Attribute; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.entity.EntitySpawnEvent; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.plugin.java.JavaPlugin; @Getter public final class EntityGenetics extends JavaPlugin { private static EntityGenetics instance; - private ColorManager colorManager; - private ConfigManager configManager; public EntityGenetics() { instance = this; @@ -24,17 +26,71 @@ public static EntityGenetics get() { @Override public void onEnable() { - registerManagers(); - registerListeners(getServer().getPluginManager()); + registerEvents(); } - private void registerManagers() { - this.colorManager = new ColorManager(this); - this.configManager = new ConfigManager(this); - } - - private void registerListeners(PluginManager pluginManager) { - pluginManager.registerEvents(new EntityListener(), this); - pluginManager.registerEvents(new PlayerListener(), this); + private void registerEvents() { + EventManager eventManager = MinesonAPI.get().getEventManager(); + eventManager.register(EntitySpawnEvent.class, event -> { + if (!(event.getEntity() instanceof LivingEntity livingEntity)) { + return; + } + double randomValue = SimpleRandom.get().nextDouble(-0.5, 0.5); + AttributeUtil.acceptIfExists(livingEntity.getAttribute(Attribute.GENERIC_SCALE), scaleAttribute -> { + double multiplier = scaleAttribute.getBaseValue() + randomValue; + scaleAttribute.setBaseValue(multiplier); + AttributeUtil.acceptIfExists(livingEntity.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_ATTACK_DAMAGE)); + AttributeUtil.acceptIfExists(livingEntity.getAttribute(Attribute.GENERIC_ATTACK_SPEED), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_ATTACK_SPEED)); + AttributeUtil.acceptIfExists(livingEntity.getAttribute(Attribute.GENERIC_FALL_DAMAGE_MULTIPLIER), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_FALL_DAMAGE_MULTIPLIER)); + AttributeUtil.acceptIfExists(livingEntity.getAttribute(Attribute.GENERIC_JUMP_STRENGTH), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_JUMP_STRENGTH)); + AttributeUtil.acceptIfExists(livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_MAX_HEALTH)); + AttributeUtil.acceptIfExists(livingEntity.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_MOVEMENT_SPEED)); + AttributeUtil.acceptIfExists(livingEntity.getAttribute(Attribute.GENERIC_SAFE_FALL_DISTANCE), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_SAFE_FALL_DISTANCE)); + AttributeUtil.acceptIfExists(livingEntity.getAttribute(Attribute.GENERIC_STEP_HEIGHT), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_STEP_HEIGHT)); + AttributeUtil.acceptIfExists(livingEntity.getAttribute(Attribute.ZOMBIE_SPAWN_REINFORCEMENTS), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.ZOMBIE_SPAWN_REINFORCEMENTS)); + }); + }); + eventManager.register(PlayerJoinEvent.class, event -> { + Player player = event.getPlayer(); + if (player.hasPlayedBefore()) { + return; + } + double randomValue = SimpleRandom.get().nextDouble(-0.5, 0.5); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.GENERIC_SCALE), scaleAttribute -> { + double multiplier = scaleAttribute.getBaseValue() + randomValue; + scaleAttribute.setBaseValue(multiplier); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_ATTACK_DAMAGE)); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.GENERIC_ATTACK_SPEED), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_ATTACK_SPEED)); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.GENERIC_FALL_DAMAGE_MULTIPLIER), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_FALL_DAMAGE_MULTIPLIER)); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.GENERIC_JUMP_STRENGTH), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_JUMP_STRENGTH)); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.GENERIC_MAX_HEALTH), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_MAX_HEALTH)); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_MOVEMENT_SPEED)); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.GENERIC_SAFE_FALL_DISTANCE), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_SAFE_FALL_DISTANCE)); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.GENERIC_STEP_HEIGHT), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.GENERIC_STEP_HEIGHT)); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.PLAYER_BLOCK_BREAK_SPEED), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.PLAYER_BLOCK_BREAK_SPEED)); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.PLAYER_BLOCK_INTERACTION_RANGE), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.PLAYER_BLOCK_INTERACTION_RANGE)); + AttributeUtil.acceptIfExists(player.getAttribute(Attribute.PLAYER_ENTITY_INTERACTION_RANGE), + maxHealthAttribute -> maxHealthAttribute.setBaseValue(multiplier * AttributeUtil.PLAYER_ENTITY_INTERACTION_RANGE)); + }); + }); } } diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/ColorManager.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/ColorManager.java deleted file mode 100644 index 51c12e7..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/ColorManager.java +++ /dev/null @@ -1,184 +0,0 @@ -package kz.hxncus.mc.entitygenetics.color; - -import com.google.common.collect.ImmutableMap; -import kz.hxncus.mc.entitygenetics.EntityGenetics; -import kz.hxncus.mc.entitygenetics.color.caching.LruCache; -import kz.hxncus.mc.entitygenetics.color.pattern.GradientPattern; -import kz.hxncus.mc.entitygenetics.color.pattern.Pattern; -import kz.hxncus.mc.entitygenetics.color.pattern.RainbowPattern; -import kz.hxncus.mc.entitygenetics.color.pattern.SolidPattern; -import kz.hxncus.mc.entitygenetics.util.ReflectionUtil; -import kz.hxncus.mc.entitygenetics.util.VersionUtil; -import lombok.NonNull; -import net.md_5.bungee.api.ChatColor; - -import java.awt.*; -import java.lang.reflect.InvocationTargetException; -import java.util.Arrays; -import java.util.List; - -public class ColorManager { - private final List SPECIAL_COLORS = Arrays.asList("&l", "&n", "&o", "&k", "&m"); - private final LruCache LRU_CACHE = new LruCache(1); - private final ImmutableMap COLORS = new ImmutableMap.Builder() - .put(new Color(0), ChatColor.getByChar('0')) - .put(new Color(170), ChatColor.getByChar('1')) - .put(new Color(43520), ChatColor.getByChar('2')) - .put(new Color(43690), ChatColor.getByChar('3')) - .put(new Color(11141120), ChatColor.getByChar('4')) - .put(new Color(11141290), ChatColor.getByChar('5')) - .put(new Color(16755200), ChatColor.getByChar('6')) - .put(new Color(11184810), ChatColor.getByChar('7')) - .put(new Color(5592405), ChatColor.getByChar('8')) - .put(new Color(5592575), ChatColor.getByChar('9')) - .put(new Color(5635925), ChatColor.getByChar('a')) - .put(new Color(5636095), ChatColor.getByChar('b')) - .put(new Color(16733525), ChatColor.getByChar('c')) - .put(new Color(16733695), ChatColor.getByChar('d')) - .put(new Color(16777045), ChatColor.getByChar('e')) - .put(new Color(16777215), ChatColor.getByChar('f')) - .build(); - private static EntityGenetics plugin; - private final List PATTERNS = Arrays.asList(new GradientPattern(plugin), new SolidPattern(plugin), new RainbowPattern(plugin)); - - public ColorManager(EntityGenetics plugin) { - ColorManager.plugin = plugin; - } - - @NonNull - public String process(@NonNull String string) { - String result = LRU_CACHE.getResult(string); - if (result == null) { - String input = string; - for (Pattern pattern : PATTERNS) { - string = pattern.process(string); - } - string = ChatColor.translateAlternateColorCodes('&', string); - LRU_CACHE.put(input, string); - return string; - } - return result; - } - - @NonNull - public List process(@NonNull List strings) { - strings.replaceAll(this::process); - return strings; - } - - @NonNull - public String color(@NonNull String string, @NonNull Color color) throws InvocationTargetException, IllegalAccessException { - return (VersionUtil.IS_HEX_VERSION ? (String) ReflectionUtil.getMethod(ChatColor.class, "of", Color.class).invoke(color) : getClosestColor(color)) + string; - } - - @NonNull - public String color(@NonNull String string, @NonNull Color start, @NonNull Color end) { - StringBuilder specialColors = new StringBuilder(); - for (String color : SPECIAL_COLORS) { - if (string.contains(color)) { - specialColors.append(color); - string = string.replace(color, ""); - } - } - StringBuilder stringBuilder = new StringBuilder(); - ChatColor[] colors = createGradient(start, end, string.length()); - String[] characters = string.split(""); - for (int i = 0; i < string.length(); i++) - stringBuilder.append(colors[i]) - .append(specialColors) - .append(characters[i]); - return stringBuilder.toString(); - } - - @NonNull - public String rainbow(@NonNull String string, float saturation) { - StringBuilder specialColors = new StringBuilder(); - for (String color : SPECIAL_COLORS) { - if (string.contains(color)) { - specialColors.append(color); - string = string.replace(color, ""); - } - } - StringBuilder stringBuilder = new StringBuilder(); - ChatColor[] colors = createRainbow(string.length(), saturation); - String[] characters = string.split(""); - for (int i = 0; i < string.length(); i++) - stringBuilder.append(colors[i]) - .append(specialColors) - .append(characters[i]); - return stringBuilder.toString(); - } - - @NonNull - public ChatColor getColor(@NonNull String string) { - try { - return VersionUtil.IS_HEX_VERSION ? (ChatColor) ReflectionUtil.getMethod(ChatColor.class, "of", Color.class) - .invoke(new Color(Integer.parseInt(string, 16))) : getClosestColor(new Color(Integer.parseInt(string, 16))); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); - } - } - - @NonNull - public String stripColorFormatting(@NonNull String string) { - return string.replaceAll("&\\w{5,8}(:[0-9A-F]{6})?>", ""); - } - - @NonNull - private ChatColor[] createRainbow(int step, float saturation) { - ChatColor[] colors = new ChatColor[step]; - double colorStep = 1.0D / step; - for (int i = 0; i < step; i++) { - Color color = Color.getHSBColor((float) (colorStep * i), saturation, saturation); - if (VersionUtil.IS_HEX_VERSION) { - try { - colors[i] = (ChatColor) ReflectionUtil.getMethod(ChatColor.class, "of", Color.class) - .invoke(color); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); - } - } else { - colors[i] = getClosestColor(color); - } - } - return colors; - } - - @NonNull - private ChatColor[] createGradient(@NonNull Color start, @NonNull Color end, int step) { - if (step <= 1) - return new ChatColor[]{ChatColor.WHITE, ChatColor.WHITE, ChatColor.WHITE}; - ChatColor[] colors = new ChatColor[step]; - int stepR = Math.abs(start.getRed() - end.getRed()) / (step - 1); - int stepG = Math.abs(start.getGreen() - end.getGreen()) / (step - 1); - int stepB = Math.abs(start.getBlue() - end.getBlue()) / (step - 1); - int[] direction = {(start.getRed() < end.getRed()) ? 1 : -1, (start.getGreen() < end.getGreen()) ? 1 : -1, (start.getBlue() < end.getBlue()) ? 1 : -1}; - for (int i = 0; i < step; i++) { - Color color = new Color(start.getRed() + stepR * i * direction[0], start.getGreen() + stepG * i * direction[1], start.getBlue() + stepB * i * direction[2]); - if (VersionUtil.IS_HEX_VERSION) { - try { - colors[i] = (ChatColor) ReflectionUtil.getMethod(ChatColor.class, "of", Color.class) - .invoke(color); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); - } - } else { - colors[i] = getClosestColor(color); - } - } - return colors; - } - - private ChatColor getClosestColor(@NonNull Color color) { - Color nearestColor = null; - double nearestDistance = 2.147483647E9D; - for (Color colors : COLORS.keySet()) { - double distance = Math.pow((color.getRed() - colors.getRed()), 2.0D) + Math.pow((color.getGreen() - colors.getGreen()), 2.0D) + Math.pow((color.getBlue() - colors.getBlue()), 2.0D); - if (nearestDistance > distance) { - nearestColor = colors; - nearestDistance = distance; - } - } - return COLORS.get(nearestColor); - } -} diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/caching/LruCache.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/caching/LruCache.java deleted file mode 100644 index 25fc53f..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/caching/LruCache.java +++ /dev/null @@ -1,90 +0,0 @@ -package kz.hxncus.mc.entitygenetics.color.caching; - -import lombok.Getter; - -import java.util.Deque; -import java.util.LinkedList; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; - -@Getter -public class LruCache { - public boolean equals(Object o) { - if (o == this) - return true; - if (!(o instanceof LruCache)) - return false; - LruCache other = (LruCache) o; - if (!other.canEqual(this)) - return false; - if (getMaxSize() != other.getMaxSize()) - return false; - Object thisQUE = getQUE(); - Object otherQUE = other.getQUE(); - if (!Objects.equals(thisQUE, otherQUE)) - return false; - Object thisMAP = getMAP(); - Object otherMAP = other.getMAP(); - return Objects.equals(thisMAP, otherMAP); - } - - protected boolean canEqual(Object other) { - return other instanceof LruCache; - } - - public int hashCode() { - int PRIME = 59; - int result = 1; - result = result * 59 + getMaxSize(); - Object QUE = getQUE(); - result = result * 59 + ((QUE == null) ? 43 : QUE.hashCode()); - Object MAP = getMAP(); - return result * 59 + ((MAP == null) ? 43 : MAP.hashCode()); - } - - public String toString() { - return "LruCache(QUE=" + getQUE() + ", MAP=" + getMAP() + ", maxSize=" + getMaxSize() + ")"; - } - - private final Deque QUE = new LinkedList<>(); - - private final Map MAP = new ConcurrentHashMap<>(); - - private final int maxSize; - - public LruCache(int maxSize) { - this.maxSize = maxSize; - } - - public String getResult(String input) { - if (input != null && this.MAP.containsKey(input)) { - LruElement curr = this.MAP.get(input); - synchronized (this.QUE) { - this.QUE.remove(input); - this.QUE.addFirst(input); - } - return curr.getResult(); - } - return null; - } - - public void put(String input, String result) { - if (input == null || result == null) - return; - synchronized (this.QUE) { - if (this.MAP.containsKey(input)) { - this.QUE.remove(input); - } else { - int size = this.QUE.size(); - if (size == this.maxSize && size > 0) { - String temp = this.QUE.removeLast(); - this.MAP.remove(temp); - } - } - LruElement newObj = new LruElement(input, result); - this.QUE.addFirst(input); - this.MAP.put(input, newObj); - } - } -} diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/caching/LruElement.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/caching/LruElement.java deleted file mode 100644 index 6a53732..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/caching/LruElement.java +++ /dev/null @@ -1,51 +0,0 @@ -package kz.hxncus.mc.entitygenetics.color.caching; - -import lombok.Getter; - -import java.util.Objects; - -@Getter -public class LruElement { - private final String input; - - private final String result; - - public boolean equals(Object o) { - if (o == this) - return true; - if (!(o instanceof LruElement)) - return false; - LruElement other = (LruElement) o; - if (!other.canEqual(this)) - return false; - Object thisInput = getInput(); - Object otherInput = other.getInput(); - if (!Objects.equals(thisInput, otherInput)) - return false; - Object thisResult = getResult(); - Object otherResult = other.getResult(); - return Objects.equals(thisResult, otherResult); - } - - protected boolean canEqual(Object other) { - return other instanceof LruElement; - } - - public int hashCode() { - int PRIME = 59; - int result = 1; - Object input = getInput(); - result = result * 59 + ((input == null) ? 43 : input.hashCode()); - Object $result = getResult(); - return result * 59 + (($result == null) ? 43 : $result.hashCode()); - } - - public String toString() { - return "LruElement(input=" + getInput() + ", result=" + getResult() + ")"; - } - - public LruElement(String input, String result) { - this.input = input; - this.result = result; - } -} diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/GradientPattern.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/GradientPattern.java deleted file mode 100644 index 78da283..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/GradientPattern.java +++ /dev/null @@ -1,27 +0,0 @@ -package kz.hxncus.mc.entitygenetics.color.pattern; - -import kz.hxncus.mc.entitygenetics.EntityGenetics; - -import java.awt.*; -import java.util.regex.Matcher; - -public class GradientPattern implements Pattern { - private static EntityGenetics plugin; - - public GradientPattern(EntityGenetics plugin) { - GradientPattern.plugin = plugin; - } - - private static final java.util.regex.Pattern PATTERN = java.util.regex.Pattern.compile("[<{]#([A-Fa-f0-9]{6})[}>](((?![<{]#[A-Fa-f0-9]{6}[}>]).)*)[<{]/#([A-Fa-f0-9]{6})[}>]"); - - public String process(String string) { - Matcher matcher = PATTERN.matcher(string); - while (matcher.find()) { - String start = matcher.group(1); - String content = matcher.group(2); - String end = matcher.group(4); - string = string.replace(matcher.group(), plugin.getColorManager().color(content, new Color(Integer.parseInt(start, 16)), new Color(Integer.parseInt(end, 16)))); - } - return string; - } -} diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/Pattern.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/Pattern.java deleted file mode 100644 index 2de7c1c..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/Pattern.java +++ /dev/null @@ -1,5 +0,0 @@ -package kz.hxncus.mc.entitygenetics.color.pattern; - -public interface Pattern { - String process(String paramString); -} diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/RainbowPattern.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/RainbowPattern.java deleted file mode 100644 index 5cea9d0..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/RainbowPattern.java +++ /dev/null @@ -1,25 +0,0 @@ -package kz.hxncus.mc.entitygenetics.color.pattern; - -import kz.hxncus.mc.entitygenetics.EntityGenetics; - -import java.util.regex.Matcher; - -public class RainbowPattern implements Pattern { - private static EntityGenetics plugin; - - public RainbowPattern(EntityGenetics plugin) { - RainbowPattern.plugin = plugin; - } - - private static final java.util.regex.Pattern PATTERN = java.util.regex.Pattern.compile("(.*?)"); - - public String process(String string) { - Matcher matcher = PATTERN.matcher(string); - while (matcher.find()) { - String saturation = matcher.group(1); - String content = matcher.group(2); - string = string.replace(matcher.group(), plugin.getColorManager().rainbow(content, Float.parseFloat(saturation))); - } - return string; - } -} diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/SolidPattern.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/SolidPattern.java deleted file mode 100644 index f664bfe..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/color/pattern/SolidPattern.java +++ /dev/null @@ -1,27 +0,0 @@ -package kz.hxncus.mc.entitygenetics.color.pattern; - - -import kz.hxncus.mc.entitygenetics.EntityGenetics; - -import java.util.regex.Matcher; - -public class SolidPattern implements Pattern { - private static EntityGenetics plugin; - - public SolidPattern(EntityGenetics plugin) { - SolidPattern.plugin = plugin; - } - - public static final java.util.regex.Pattern PATTERN = java.util.regex.Pattern.compile("[<{]#([A-Fa-f0-9]{6})[}>]|&?#([A-Fa-f0-9]{6})"); - - public String process(String string) { - Matcher matcher = PATTERN.matcher(string); - while (matcher.find()) { - String color = matcher.group(1); - if (color == null) - color = matcher.group(2); - string = string.replace(matcher.group(), String.valueOf(plugin.getColorManager().getColor(color))); - } - return string; - } -} diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/ConfigManager.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/ConfigManager.java deleted file mode 100644 index 47ea17c..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/ConfigManager.java +++ /dev/null @@ -1,120 +0,0 @@ -package kz.hxncus.mc.entitygenetics.config; - -import kz.hxncus.mc.entitygenetics.EntityGenetics; -import lombok.EqualsAndHashCode; -import org.bukkit.configuration.file.YamlConfiguration; - -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; - -@EqualsAndHashCode -public class ConfigManager { - private final EntityGenetics plugin; - private final YamlConfiguration defaultSettings; - private final YamlConfiguration defaultLanguage; - private YamlConfiguration settings; - private YamlConfiguration languages; - - public ConfigManager(EntityGenetics plugin) { - this.plugin = plugin; - this.defaultSettings = extractDefault("settings.yml"); - this.defaultLanguage = extractDefault("languages/" + Settings.LANGUAGE + ".yml"); - this.validateConfigs(); - } - - public YamlConfiguration getSettings() { - return settings == null ? defaultSettings : settings; - } - - public File getSettingsFile() { - return new File(plugin.getDataFolder(), "settings.yml"); - } - - public YamlConfiguration getLanguages() { - return languages == null ? defaultLanguage : languages; - } - - public File getLanguagesFile() { - return new File(plugin.getDataFolder(), "language.yml"); - } - - public File getDataFile() { - return new File(plugin.getDataFolder(), "data.yml"); - } - private YamlConfiguration extractDefault(String source) { - try (InputStreamReader inputStreamReader = new InputStreamReader(plugin.getResource(source))) { - return YamlConfiguration.loadConfiguration(inputStreamReader); - } catch (IOException e) { - plugin.getLogger().severe("Failed to extract default file: " + source); - if (Settings.DEBUG.toBool()) { - e.printStackTrace(); - } - throw new RuntimeException(); - } - } - - public void validateConfigs() { - settings = validate("settings.yml", defaultSettings); - languages = validate(plugin.getDataFolder().toPath().resolve("settings.yml").toString(), defaultLanguage); - } - - private YamlConfiguration validate(String configName, YamlConfiguration defaultConfiguration) { - File file = extractConfiguration(configName); - YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file); - boolean updated = false; - for (String key : defaultConfiguration.getKeys(true)) { - if (configuration.get(key) == null) { - updated = true; - plugin.getServer().getConsoleSender().sendMessage(getLanguages().getString(Messages.UPDATING_CONFIG_KEY.toPath(), "Languages file not found")); - configuration.set(key, defaultConfiguration.get(key)); - } - } - for (String key : configuration.getKeys(false)) { - if (defaultConfiguration.get(key) == null) { - updated = true; - plugin.getServer().getConsoleSender().sendMessage(getLanguages().getString(Messages.REMOVING_CONFIG_KEY.toPath(), "Languages file not found")); - configuration.set(key, null); - } - } - if (updated) { - try { - configuration.save(file); - } catch (IOException e) { - plugin.getLogger().severe("Failed to save updated configuration file: " + file.getName()); - if (Settings.DEBUG.toBool()) { - e.printStackTrace(); - } - } - } - return configuration; - } - - public File extractConfiguration(String fileName) { - File file = new File(this.plugin.getDataFolder(), fileName); - if (!file.exists()) { - this.plugin.saveResource(fileName, false); - } - return file; - } - - public void saveSettingsConfig() { - try { - getSettings().save(getSettingsFile()); - } catch (IOException e) { - if (Settings.DEBUG.toBool()) { - e.printStackTrace(); - } - } - } - - public void saveLanguageConfig() { - try { - getLanguages().save(getLanguagesFile()); - } catch (IOException e) { - if (Settings.DEBUG.toBool()) { - e.printStackTrace(); - } - } - } -} diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/Messages.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/Messages.java index abffc34..56b5274 100644 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/Messages.java +++ b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/Messages.java @@ -1,6 +1,6 @@ package kz.hxncus.mc.entitygenetics.config; -import kz.hxncus.mc.entitygenetics.EntityGenetics; +import kz.hxncus.mc.minesonapi.MinesonAPI; import lombok.NonNull; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -11,8 +11,8 @@ public enum Messages { PREFIX("general.prefix"), UPDATING_CONFIG_KEY("general.updating_config"), REMOVING_CONFIG_KEY("general.removing_config"), NOT_ENOUGH_PERMISSION("command.not_enough_permission"), INCORRECT_SENDER("command.incorrect_sender"), - PLAYER_HELP("command.player_help"), ADMIN_HELP("command.admin_help"), INVENTORY_FULL("general.inventory_full"), - NUMBER_FORMAT("command.number_format"), MATERIAL_NOT_FOUND("command.material_not_found"),; + PLAYER_HELP("command.player_help"), ADMIN_HELP("command.admin_help"), + NUMBER_FORMAT("command.number_format"); private final String path; @@ -64,7 +64,7 @@ public String process(String message, Object... args) { } private String colorize(String message) { - return EntityGenetics.get().getColorManager().process(message); + return MinesonAPI.get().getColorManager().process(message); } private String format(String message, final Object... args) { @@ -105,6 +105,6 @@ public void log(final Object... args) { } public YamlConfiguration getLanguages() { - return kz.hxncus.mc.entitygenetics.EntityGenetics.get().getConfigManager().getLanguages(); + return MinesonAPI.get().getConfigManager().getOrCreateConfig("languages/" + Settings.LANGUAGE + ".yml"); } } diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/Settings.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/Settings.java index 3d42e91..735a6db 100644 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/Settings.java +++ b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/config/Settings.java @@ -1,13 +1,12 @@ package kz.hxncus.mc.entitygenetics.config; import kz.hxncus.mc.entitygenetics.EntityGenetics; -import lombok.Getter; +import kz.hxncus.mc.minesonapi.MinesonAPI; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import java.util.List; -@Getter public enum Settings { VERSION("version"), DEBUG("debug"), LANGUAGE("language"); private final String path; @@ -17,11 +16,11 @@ public enum Settings { } public Object getValue() { - return getSettings().get(path); + return getSettings().get(this.path); } public Object getValue(Object def) { - return getSettings().get(path, def); + return getSettings().get(this.path, def); } public void setValue(Object value) { @@ -29,7 +28,7 @@ public void setValue(Object value) { } public void setValue(Object value, boolean save) { - getSettings().set(path, value); + getSettings().set(this.path, value); if (save) { EntityGenetics plugin = EntityGenetics.get(); try { @@ -49,8 +48,12 @@ public String toString(Object def) { return colorize((String) getValue(def)); } + public String toPath() { + return this.path; + } + public String colorize(String input) { - return EntityGenetics.get().getColorManager().process(input); + return MinesonAPI.get().getColorManager().process(input); } public boolean toBool() { @@ -62,7 +65,7 @@ public Number toNumber() { } public List toStringList() { - List stringList = getSettings().getStringList(path); + List stringList = getSettings().getStringList(this.path); stringList.replaceAll(this::colorize); return stringList; } @@ -72,6 +75,6 @@ public ConfigurationSection toConfigSection() { } public YamlConfiguration getSettings() { - return EntityGenetics.get().getConfigManager().getSettings(); + return MinesonAPI.get().getConfigManager().getOrCreateConfig("settings.yml"); } } diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/listener/EntityListener.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/listener/EntityListener.java deleted file mode 100644 index 94ab6ea..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/listener/EntityListener.java +++ /dev/null @@ -1,27 +0,0 @@ -package kz.hxncus.mc.entitygenetics.listener; - -import org.bukkit.attribute.Attribute; -import org.bukkit.attribute.AttributeInstance; -import org.bukkit.entity.Entity; -import org.bukkit.entity.LivingEntity; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.EntitySpawnEvent; - -import java.util.SplittableRandom; - -public class EntityListener implements Listener { - @EventHandler - public void onEntitySpawn(EntitySpawnEvent event) { - Entity entity = event.getEntity(); - if (!(entity instanceof LivingEntity livingEntity)) { - return; - } - SplittableRandom random = new SplittableRandom(); - double randomValue = random.nextDouble(-0.5, 0.5); - AttributeInstance attribute = livingEntity.getAttribute(Attribute.GENERIC_SCALE); - if (attribute != null) { - attribute.setBaseValue(attribute.getBaseValue() + randomValue); - } - } -} diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/listener/PlayerListener.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/listener/PlayerListener.java deleted file mode 100644 index ce5371c..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/listener/PlayerListener.java +++ /dev/null @@ -1,7 +0,0 @@ -package kz.hxncus.mc.entitygenetics.listener; - -import org.bukkit.event.Listener; - -public class PlayerListener implements Listener { - -} diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/util/ReflectionUtil.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/util/ReflectionUtil.java deleted file mode 100644 index 5dd3c62..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/util/ReflectionUtil.java +++ /dev/null @@ -1,254 +0,0 @@ -package kz.hxncus.mc.entitygenetics.util; - -import kz.hxncus.mc.entitygenetics.EntityGenetics; -import lombok.Getter; -import lombok.experimental.UtilityClass; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; - -@UtilityClass -public class ReflectionUtil { - public Constructor getConstructor(Class clazz, Class... parameterTypes) throws NoSuchMethodException { - Class[] primitiveTypes = DataType.getPrimitive(parameterTypes); - byte b; - int i; - Constructor[] arrayOfConstructor = clazz.getConstructors(); - for (i = arrayOfConstructor.length, b = 0; b < i; ) { - Constructor constructor = arrayOfConstructor[b]; - if (!DataType.compare(DataType.getPrimitive(constructor.getParameterTypes()), primitiveTypes)) { - b++; - continue; - } - return constructor; - } - throw new NoSuchMethodException("There is no such constructor in this class with the specified parameter types"); - } - - public Constructor getConstructor(String className, PackageType packageType, Class... parameterTypes) throws NoSuchMethodException, ClassNotFoundException { - return getConstructor(packageType.getClass(className), parameterTypes); - } - - public Object instantiateObject(Class clazz, Object... arguments) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException { - return getConstructor(clazz, DataType.getPrimitive(arguments)).newInstance(arguments); - } - - public Object instantiateObject(String className, PackageType packageType, Object... arguments) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException { - return instantiateObject(packageType.getClass(className), arguments); - } - - public Method getMethod(Class clazz, String methodName, Class... parameterTypes) { - Class[] primitiveTypes = DataType.getPrimitive(parameterTypes); - byte b; - int i; - Method[] arrayOfMethod; - for (i = (arrayOfMethod = clazz.getMethods()).length, b = 0; b < i; ) { - Method method = arrayOfMethod[b]; - if (!method.getName() - .equals(methodName) || !DataType.compare(DataType.getPrimitive(method.getParameterTypes()), primitiveTypes)) { - b++; - continue; - } - return method; - } - throw new RuntimeException("There is no such method in this class with the specified name and parameter types"); - } - - public Method getMethod(String className, PackageType packageType, String methodName, Class... parameterTypes) throws ClassNotFoundException { - return getMethod(packageType.getClass(className), methodName, parameterTypes); - } - - public Object invokeMethod(Object instance, String methodName, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { - return getMethod(instance.getClass(), methodName, DataType.getPrimitive(arguments)).invoke(instance, arguments); - } - - public Object invokeMethod(Object instance, Class clazz, String methodName, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { - return getMethod(clazz, methodName, DataType.getPrimitive(arguments)).invoke(instance, arguments); - } - - public Object invokeMethod(Object instance, String className, PackageType packageType, String methodName, Object... arguments) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException { - return invokeMethod(instance, packageType.getClass(className), methodName, arguments); - } - - public Field getField(Class clazz, boolean declared, String fieldName) { - Field field; - try { - field = declared ? clazz.getDeclaredField(fieldName) : clazz.getField(fieldName); - } catch (NoSuchFieldException e) { - throw new RuntimeException(e); - } - field.setAccessible(true); - return field; - } - - public Field getField(String className, PackageType packageType, boolean declared, String fieldName) throws NoSuchFieldException, SecurityException, ClassNotFoundException { - return getField(packageType.getClass(className), declared, fieldName); - } - - public Object getValue(Object instance, Class clazz, boolean declared, String fieldName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { - return getField(clazz, declared, fieldName).get(instance); - } - - public Object getValue(Object instance, String className, PackageType packageType, boolean declared, String fieldName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException { - return getValue(instance, packageType.getClass(className), declared, fieldName); - } - - public Object getValue(Object instance, boolean declared, String fieldName) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { - return getValue(instance, instance.getClass(), declared, fieldName); - } - - public void setValue(Object instance, Class clazz, boolean declared, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { - getField(clazz, declared, fieldName).set(instance, value); - } - - public void setValue(Object instance, String className, PackageType packageType, boolean declared, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException { - setValue(instance, packageType.getClass(className), declared, fieldName, value); - } - - public void setValue(Object instance, boolean declared, String fieldName, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { - setValue(instance, instance.getClass(), declared, fieldName, value); - } - - @Getter - public enum PackageType { - MINECRAFT_SERVER("net.minecraft.server." + getServerVersion()), - CRAFTBUKKIT("org.bukkit.craftbukkit." + getServerVersion()), - CRAFTBUKKIT_BLOCK(CRAFTBUKKIT + "block"), - CRAFTBUKKIT_CHUNKIO(CRAFTBUKKIT + "chunkio"), - CRAFTBUKKIT_COMMAND(CRAFTBUKKIT + "command"), - CRAFTBUKKIT_CONVERSATIONS(CRAFTBUKKIT + "conversations"), - CRAFTBUKKIT_ENCHANTMENS(CRAFTBUKKIT + "enchantments"), - CRAFTBUKKIT_ENTITY(CRAFTBUKKIT + "entity"), - CRAFTBUKKIT_EVENT(CRAFTBUKKIT + "event"), - CRAFTBUKKIT_GENERATOR(CRAFTBUKKIT + "generator"), - CRAFTBUKKIT_HELP(CRAFTBUKKIT + "help"), - CRAFTBUKKIT_INVENTORY(CRAFTBUKKIT + "inventory"), - CRAFTBUKKIT_MAP(CRAFTBUKKIT + "map"), - CRAFTBUKKIT_METADATA(CRAFTBUKKIT + "metadata"), - CRAFTBUKKIT_POTION(CRAFTBUKKIT + "potion"), - CRAFTBUKKIT_PROJECTILES(CRAFTBUKKIT + "projectiles"), - CRAFTBUKKIT_SCHEDULER(CRAFTBUKKIT + "scheduler"), - CRAFTBUKKIT_SCOREBOARD(CRAFTBUKKIT + "scoreboard"), - CRAFTBUKKIT_UPDATER(CRAFTBUKKIT + "updater"), - CRAFTBUKKIT_UTIL(CRAFTBUKKIT + "util"); - - private final String path; - - PackageType(String path) { - this.path = path; - } - - public Class getClass(String className) throws ClassNotFoundException { - return Class.forName(this + "." + className); - } - - @Override - public String toString() { - return this.path; - } - - public static String getServerVersion() { - return EntityGenetics.get().getServer().getClass().getPackage().getName().substring(23); - } - } - - @Getter - public enum DataType { - BYTE(byte.class, Byte.class), - SHORT(short.class, Short.class), - INTEGER(int.class, Integer.class), - LONG(long.class, Long.class), - CHARACTER(char.class, Character.class), - FLOAT(float.class, Float.class), - DOUBLE(double.class, Double.class), - BOOLEAN(boolean.class, Boolean.class); - - private static final Map, DataType> CLASS_MAP = new HashMap<>(); - - private final Class primitive; - - private final Class reference; - - static { - byte b; - int i; - DataType[] arrayOfDataType; - for (i = (arrayOfDataType = values()).length, b = 0; b < i; ) { - DataType type = arrayOfDataType[b]; - CLASS_MAP.put(type.primitive, type); - CLASS_MAP.put(type.reference, type); - b++; - } - } - - DataType(Class primitive, Class reference) { - this.primitive = primitive; - this.reference = reference; - } - - public static DataType fromClass(Class clazz) { - return CLASS_MAP.get(clazz); - } - - public static Class getPrimitive(Class clazz) { - DataType type = fromClass(clazz); - return (type == null) ? clazz : type.getPrimitive(); - } - - public Class getReference(Class clazz) { - DataType type = fromClass(clazz); - return (type == null) ? clazz : type.getReference(); - } - - public static Class[] getPrimitive(Class[] classes) { - int length = (classes == null) ? 0 : classes.length; - Class[] types = new Class[length]; - for (int index = 0; index < length; index++) - types[index] = getPrimitive(classes[index]); - return types; - } - - public Class[] getReference(Class[] classes) { - int length = (classes == null) ? 0 : classes.length; - Class[] types = new Class[length]; - for (int index = 0; index < length; index++) - types[index] = getReference(classes[index]); - return types; - } - - public static Class[] getPrimitive(Object[] objects) { - int length = (objects == null) ? 0 : objects.length; - Class[] types = new Class[length]; - for (int index = 0; index < length; index++) - types[index] = getPrimitive(objects[index].getClass()); - return types; - } - - public Class[] getReference(Object[] objects) { - int length = (objects == null) ? 0 : objects.length; - Class[] types = new Class[length]; - for (int index = 0; index < length; index++) - types[index] = getReference(objects[index].getClass()); - return types; - } - - public static boolean compare(Class[] primary, Class[] secondary) { - if (primary == null || secondary == null || primary.length != secondary.length) - return false; - for (int index = 0; index < primary.length; ) { - Class primaryClass = primary[index]; - Class secondaryClass = secondary[index]; - if (primaryClass.equals(secondaryClass) || primaryClass.isAssignableFrom(secondaryClass)) { - index++; - continue; - } - return false; - } - return true; - } - } -} \ No newline at end of file diff --git a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/util/VersionUtil.java b/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/util/VersionUtil.java deleted file mode 100644 index 59db202..0000000 --- a/bukkit/src/main/java/kz/hxncus/mc/entitygenetics/util/VersionUtil.java +++ /dev/null @@ -1,92 +0,0 @@ -package kz.hxncus.mc.entitygenetics.util; - -import kz.hxncus.mc.entitygenetics.EntityGenetics; -import lombok.experimental.UtilityClass; -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.FallingBlock; -import org.bukkit.entity.Player; -import org.bukkit.util.BlockIterator; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -@UtilityClass -public class VersionUtil { - public final int CURRENT_VERSION = getCurrentVersion(); - public final boolean IS_PDC_VERSION = CURRENT_VERSION >= 1140; - public final boolean IS_HEX_VERSION = CURRENT_VERSION >= 1160; - public final boolean IS_TARGET_BLOCK_VERSION = CURRENT_VERSION >= 1140; - public final boolean IS_NAMESPACED_KEY_VERSION = CURRENT_VERSION >= 1120; - public final boolean IS_SPAWN_EGG_META_VERSION = CURRENT_VERSION >= 1110; - public final boolean IS_POTION_COLOR_VERSION = CURRENT_VERSION >= 1110; - public final boolean IS_POTION_DATA_VERSION = CURRENT_VERSION >= 190; - public final Material SIGN = getSign(); - - private int getCurrentVersion() { - Matcher matcher = Pattern.compile("(?\\d+\\.\\d+)(?\\.\\d+)?").matcher(Bukkit.getBukkitVersion()); - StringBuilder stringBuilder = new StringBuilder(); - if (matcher.find()) { - stringBuilder.append(matcher.group("version") - .replace(".", "")); - String patch = matcher.group("patch"); - if (patch == null) { - stringBuilder.append('0'); - } else { - stringBuilder.append(patch.replace(".", "")); - } - } - try { - return Integer.parseInt(stringBuilder.toString()); - } catch (NumberFormatException ignored) { - throw new RuntimeException("Could not retrieve server version!"); - } - } - - private Material getSign() { - if (VersionUtil.CURRENT_VERSION < 1140) { - return Material.valueOf("SIGN"); - } else { - return Material.valueOf("OAK_SIGN"); - } - } - - public Block getTargetBlock(Player player, int distance) { - if (IS_TARGET_BLOCK_VERSION) { - Block targetBlock = player.getTargetBlockExact(distance); - if (targetBlock != null) { - return targetBlock; - } - } else { - BlockIterator iterator = new BlockIterator(player, distance); - while (iterator.hasNext()) { - Block block = iterator.next(); - if (block.getType() != Material.AIR) { - return block; - } - } - } - return player.getLocation().getBlock(); - } - - public String getEnchantmentName(Enchantment enchantment) { - if (IS_NAMESPACED_KEY_VERSION) { - return enchantment.getKey().getKey(); - } else { - return enchantment.getName(); - } - } - - // TODO Check when FallingBlock#setCancelDrop(boolean cancel) is created - public void removeFallingBlockAfterLand(FallingBlock fallingBlock) { - EntityGenetics plugin = EntityGenetics.get(); - plugin.getServer().getScheduler().runTaskTimer(plugin, task -> { - if (!fallingBlock.isValid()) { - fallingBlock.getLocation().getBlock().setType(Material.AIR); - task.cancel(); - } - }, 0L, 10L); - } -} \ No newline at end of file diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index 70a3f25..e6f0d65 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -1,7 +1,9 @@ -name: "${name}" -version: "${version}" +name: "$name" +version: "$version" main: kz.hxncus.mc.entitygenetics.EntityGenetics api-version: '1.20' +depend: + - MinesonAPI commands: entitygenetics: aliases: diff --git a/bukkit/src/main/resources/settings.yml b/bukkit/src/main/resources/settings.yml index 25226c9..0bd1184 100644 --- a/bukkit/src/main/resources/settings.yml +++ b/bukkit/src/main/resources/settings.yml @@ -1,3 +1,27 @@ version: 1 debug: false language: "en" +food: + BEEF: + per_eat_scale: 0.01 + MILK: + per_eat_scale: 0.02 + SPIDER_EYE: + per_eat_scale: -0.05 +entity: + # Apply to any entity if not writed + start_scale: 0.5 + min_scale: 1.0 + max_scale: 1.5 + PLAYER: + start_scale: 0.5-0.75 + min_scale: 0.5-1.0 + max_scale: 1.0-1.5 + COW: + start_scale: 0.75 + min_scale: 1.0 + max_scale: 1.5 + PIG: + start_scale: 0.75 + min_scale: 0.75 + max_scale: 1.0-1.5 diff --git a/gradle.properties b/gradle.properties index 9678fa7..f2b45a9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,8 +8,9 @@ junit=5.10.2 lombok=1.18.30 # Gradle properties -org.gradle.jvmargs=-Xmx2G -XX:+UseParallelGC -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 org.gradle.daemon=true org.gradle.cache=true org.gradle.caching=true -org.gradle.parallel=true \ No newline at end of file +org.gradle.parallel=true +org.gradle.jvmargs=-Xmx2048M -XX:MaxPermSize=512m -XX:+UseParallelGC -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 +org.gradle.configureondemand=true