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 all 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
9 changes: 9 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.entities.ScaleManager;
import dev.aurelium.auraskills.api.AuraSkillsApi;
import dev.aurelium.auraskills.api.skill.Skill;
import dev.aurelium.auraskills.api.skill.Skills;
Expand Down Expand Up @@ -43,6 +44,7 @@ public class AuraMobs extends JavaPlugin implements PolyglotProvider {
private ConfigManager configManager;
private Polyglot polyglot;
private Locale language;
private ScaleManager scaleManager;
private List<String> enabledWorlds;
private boolean worldWhitelist;
private boolean placeholderAPIEnabled;
Expand Down Expand Up @@ -79,6 +81,9 @@ public void onEnable() {
language = new Locale(optionString("language"));
mobKey = new NamespacedKey(this, "isAureliumMob");
namesEnabled = optionBoolean("custom_name.enabled");
scaleManager = new ScaleManager(this);
scaleManager.loadConfiguration();

this.getServer().getPluginManager().registerEvents(new MobSpawn(this), this);
this.getServer().getPluginManager().registerEvents(new EntityXpGainListener(this), this);
if (namesEnabled) {
Expand Down Expand Up @@ -130,6 +135,10 @@ public Polyglot getPolyglot() {
return polyglot;
}

public ScaleManager getScaleManager() {
return scaleManager;
}

public boolean isNamesEnabled() {
return namesEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void onReload(CommandSender sender) {
plugin.getConfigManager().loadConfig();
plugin.getPolyglot().getMessageManager().loadMessages();
plugin.setLanguage(new Locale(plugin.optionString("language")));
plugin.getScaleManager().loadConfiguration();
sender.sendMessage(ColorUtils.colorMessage(plugin.getMsg("commands.reload")));
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/aurelium/auramobs/entities/AureliumMob.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public AureliumMob(LivingEntity mob, int level, AuraMobs plugin) {
));
mob.setCustomNameVisible(false);
}

if (plugin.getScaleManager().hasScaleAttribute()) {
plugin.getScaleManager().applyScale(mob, level1);
}
}

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

import org.bukkit.entity.EntityType;

import java.util.List;

public class EntityScale {

private final int levelStart;
private final int levelEnd;
private final double[] fixed;
private final double intervalStart;
private final double intervalEnd;
private final double chance;
private final List<EntityType> types;

public EntityScale(int levelStart, int levelEnd, double[] fixed, double intervalStart, double intervalEnd, double chance, List<EntityType> types) {
this.levelStart = levelStart;
this.levelEnd = levelEnd;
this.fixed = fixed;
this.intervalStart = intervalStart;
this.intervalEnd = intervalEnd;
this.chance = chance;
this.types = types;
}

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 List<EntityType> getTypes() {
return types;
}
}
122 changes: 122 additions & 0 deletions src/main/java/dev/aurelium/auramobs/entities/ScaleManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package dev.aurelium.auramobs.entities;

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class ScaleManager {

private final List<EntityScale> entries = new ArrayList<>();
private Attribute scaleAttribute;
private final AuraMobs plugin;

public ScaleManager(AuraMobs plugin) {
this.plugin = plugin;
}

public void loadConfiguration() {
try {
entries.clear();

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

if (scaleAttribute == null) return;

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

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

if (entry.split("-").length < 2) {
plugin.getLogger().warning("Scale entry key must be in a range format (e.g. 1-20)");
continue;
}

int levelStart = Integer.parseInt(entry.split("-")[0]);
int levelEnd = Integer.parseInt(entry.split("-")[1]);
double chance = entrySection.getDouble("chance", 1.0);
double intervalStart = 0;
double intervalEnd = 0;
double[] fixed = new double[0];

String scale = entrySection.getString("scale");

if (scale == null) {
plugin.getLogger().warning("Scale entry " + entry + " is missing the scale value!");
continue;
}

if (scale.contains("-")) {
if (scale.split("-").length != 2) {
plugin.getLogger().warning("Scale entry " + entry + " has an invalid scale value!");
continue;
}

intervalStart = Double.parseDouble(scale.split("-")[0]);
intervalEnd = Double.parseDouble(scale.split("-")[1]);

} else {
fixed = Arrays.stream(scale.replace(" ", "").split(",")).mapToDouble(Double::parseDouble).toArray();
}

List<EntityType> types = new ArrayList<>();
if (entrySection.contains("types")) {
for (String type : entrySection.getStringList("types")) {
try {
types.add(EntityType.valueOf(type.toUpperCase()));
} catch (IllegalArgumentException e) {
plugin.getLogger().warning("Invalid entity type in scale entry " + entry + ": " + type);
}
}
}

entries.add(new EntityScale(levelStart, levelEnd, fixed, intervalStart, intervalEnd, chance, types));
}
} catch (Exception e) {
plugin.getLogger().warning("Failed to load scale configuration: " + e.getMessage());
}
}

public void applyScale(LivingEntity entity, int level) {
AttributeInstance ai = entity.getAttribute(scaleAttribute);
if (ai == null) return;

for (EntityScale entry : entries) {
if (level < entry.getLevelStart() || level > entry.getLevelEnd()) {
continue;
}
if (!entry.getTypes().isEmpty() && !entry.getTypes().contains(entity.getType())) {
continue;
}

if (Math.random() < entry.getChance()) {
if (entry.getFixed().length > 0) {
double random = entry.getFixed()[ThreadLocalRandom.current().nextInt(entry.getFixed().length)];
ai.setBaseValue(Math.max(.00625, Math.min(16, random)));
} else {
double random = entry.getIntervalStart() + (entry.getIntervalEnd() - entry.getIntervalStart()) * Math.random();
ai.setBaseValue(Math.max(.00625, Math.min(16, random)));
}
}
}
}

public boolean hasScaleAttribute() {
return scaleAttribute != null;
}
}
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