Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Scale Feature for Mobs with Levels #11

Merged
merged 8 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ repositories {
maven("https://repo.extendedclip.com/content/repositories/placeholderapi/")
maven("https://maven.enginehub.org/repo/")
maven("https://repo.aikar.co/content/groups/aikar/")
maven("https://repo.helpch.at/releases/")
}

dependencies {
Expand All @@ -28,7 +29,7 @@ dependencies {
implementation("org.bstats:bstats-bukkit:3.0.2")
compileOnly("dev.aurelium:auraskills-api-bukkit:2.2.0")
compileOnly("org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT")
compileOnly("me.clip:placeholderapi:2.11.2")
compileOnly("me.clip:placeholderapi:2.11.6")
compileOnly("com.sk89q.worldguard:worldguard-bukkit:7.0.7-SNAPSHOT")
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/aurelium/auramobs/AuraMobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.aurelium.auramobs.config.OptionValue;
import dev.aurelium.auramobs.listeners.*;
import dev.aurelium.auramobs.util.Formatter;
import dev.aurelium.auramobs.util.ScaleUtil;
import dev.aurelium.auraskills.api.AuraSkillsApi;
import dev.aurelium.auraskills.api.skill.Skill;
import dev.aurelium.auraskills.api.skill.Skills;
Expand Down Expand Up @@ -79,6 +80,9 @@ public void onEnable() {
language = new Locale(optionString("language"));
mobKey = new NamespacedKey(this, "isAureliumMob");
namesEnabled = optionBoolean("custom_name.enabled");

ScaleUtil.initialize(this);
Archy-X marked this conversation as resolved.
Show resolved Hide resolved

this.getServer().getPluginManager().registerEvents(new MobSpawn(this), this);
this.getServer().getPluginManager().registerEvents(new EntityXpGainListener(this), this);
if (namesEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import co.aikar.commands.annotation.Subcommand;
import dev.aurelium.auramobs.AuraMobs;
import dev.aurelium.auramobs.util.ColorUtils;
import dev.aurelium.auramobs.util.ScaleUtil;
import org.bukkit.command.CommandSender;

import java.util.Locale;
Expand All @@ -26,6 +27,7 @@ public void onReload(CommandSender sender) {
plugin.getConfigManager().loadConfig();
plugin.getPolyglot().getMessageManager().loadMessages();
plugin.setLanguage(new Locale(plugin.optionString("language")));
ScaleUtil.loadConfiguration();
Archy-X marked this conversation as resolved.
Show resolved Hide resolved
sender.sendMessage(ColorUtils.colorMessage(plugin.getMsg("commands.reload")));
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dev/aurelium/auramobs/entities/AureliumMob.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.aurelium.auramobs.AuraMobs;
import dev.aurelium.auramobs.util.ColorUtils;
import dev.aurelium.auramobs.util.MessageUtils;
import dev.aurelium.auramobs.util.ScaleUtil;
import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;
import org.bukkit.Location;
Expand Down Expand Up @@ -79,6 +80,8 @@ public AureliumMob(LivingEntity mob, int level, AuraMobs plugin) {
));
mob.setCustomNameVisible(false);
}

ScaleUtil.applyScale(mob, level1);
Archy-X marked this conversation as resolved.
Show resolved Hide resolved
}

}
114 changes: 114 additions & 0 deletions src/main/java/dev/aurelium/auramobs/util/ScaleUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package dev.aurelium.auramobs.util;

import dev.aurelium.auramobs.AuraMobs;
import org.bukkit.attribute.Attribute;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.LivingEntity;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.concurrent.ThreadLocalRandom;

public class ScaleUtil {

private static final LinkedList<ScaleEntry> entries = new LinkedList<>();
Archy-X marked this conversation as resolved.
Show resolved Hide resolved
private static Attribute scaleAttribute;
private static AuraMobs plugin;

private static class ScaleEntry {
Archy-X marked this conversation as resolved.
Show resolved Hide resolved

private final int levelStart;
private final int levelEnd;
private final double[] fixed;
private final double intervalStart;
private final double intervalEnd;
private final double chance;

public ScaleEntry(int levelStart, int levelEnd, double[] fixed, double intervalStart, double intervalEnd, double chance) {
this.levelStart = levelStart;
this.levelEnd = levelEnd;
this.fixed = fixed;
this.intervalStart = intervalStart;
this.intervalEnd = intervalEnd;
this.chance = chance;
}

public int getLevelStart() {
return levelStart;
}

public int getLevelEnd() {
return levelEnd;
}

public double[] getFixed() {
return fixed;
}

public double getIntervalStart() {
return intervalStart;
}

public double getIntervalEnd() {
return intervalEnd;
}

public double getChance() {
return chance;
}
}

public static void initialize(AuraMobs plugin) {
Archy-X marked this conversation as resolved.
Show resolved Hide resolved
ScaleUtil.plugin = plugin;
loadConfiguration();
}

public static void loadConfiguration() {

Archy-X marked this conversation as resolved.
Show resolved Hide resolved
entries.clear();

for (Attribute attribute : Attribute.values()) {
if (attribute.name().toLowerCase().contains("scale")) {
scaleAttribute = attribute;
break;
}
}

if(scaleAttribute == null) return;
Archy-X marked this conversation as resolved.
Show resolved Hide resolved

ConfigurationSection section = plugin.getConfig().getConfigurationSection("scales");

for (String entry : section.getKeys(false)) {
ConfigurationSection entrySection = section.getConfigurationSection(entry);

int levelStart = Integer.parseInt(entry.split("-")[0]);
int levelEnd = Integer.parseInt(entry.split("-")[1]);
Archy-X marked this conversation as resolved.
Show resolved Hide resolved
double chance = entrySection.getDouble("chance");

String scale = entrySection.getString("scale");
Archy-X marked this conversation as resolved.
Show resolved Hide resolved
if(scale.contains("-")) {
Archy-X marked this conversation as resolved.
Show resolved Hide resolved
double intervalStart = Double.parseDouble(scale.split("-")[0]);
double intervalEnd = Double.parseDouble(scale.split("-")[1]);
entries.add(new ScaleEntry(levelStart, levelEnd, new double[0], intervalStart, intervalEnd, chance));
} else {
double[] fixed = Arrays.stream(scale.replace(" ", "").split(",")).mapToDouble(Double::parseDouble).toArray();
entries.add(new ScaleEntry(levelStart, levelEnd, fixed, 0, 0, chance));
}
}
}

public static void applyScale(LivingEntity entity, int level) {
for (ScaleEntry entry : entries) {
if (level >= entry.getLevelStart() && level <= entry.getLevelEnd()) {
if (Math.random() < entry.getChance()) {
if (entry.getFixed().length > 0) {
entity.getAttribute(scaleAttribute).setBaseValue(Math.max(.00625, Math.min(16, entry.getFixed()[ThreadLocalRandom.current().nextInt(entry.getFixed().length)])));
Archy-X marked this conversation as resolved.
Show resolved Hide resolved
} else {
double random = entry.getIntervalStart() + (entry.getIntervalEnd() - entry.getIntervalStart()) * Math.random();
entity.getAttribute(scaleAttribute).setBaseValue(Math.max(.00625, Math.min(16, random)));
}
}
}
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ spawn_reasons:
- 'PIGLIN_ZOMBIFIED'
- 'COMMAND'
- 'CUSTOM'
- 'DEFAULT'
- 'DEFAULT'
scales:
Loading