Skip to content

Commit

Permalink
Fix config NPE and default chance to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed Sep 21, 2024
1 parent baa87ed commit a7c3642
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/main/java/dev/aurelium/auramobs/entities/ScaleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand Down Expand Up @@ -35,20 +36,27 @@ public void loadConfiguration() {
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");
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) {
if (scale == null) {
plugin.getLogger().warning("Scale entry " + entry + " is missing the scale value!");
continue;
}
Expand Down Expand Up @@ -85,21 +93,24 @@ public void loadConfiguration() {
}

public void applyScale(LivingEntity entity, int level) {
for (EntityScale entry : entries) {
if (level >= entry.getLevelStart() && level <= entry.getLevelEnd()) {
AttributeInstance ai = entity.getAttribute(scaleAttribute);
if (ai == null) return;

if (!entry.getTypes().isEmpty() && !entry.getTypes().contains(entity.getType())) {
continue;
}
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)];
entity.getAttribute(scaleAttribute).setBaseValue(Math.max(.00625, Math.min(16, random)));
} else {
double random = entry.getIntervalStart() + (entry.getIntervalEnd() - entry.getIntervalStart()) * Math.random();
entity.getAttribute(scaleAttribute).setBaseValue(Math.max(.00625, Math.min(16, random)));
}
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)));
}
}
}
Expand Down

0 comments on commit a7c3642

Please sign in to comment.