Compare commits

...
10 Commits
Author SHA1 Message Date
auto 3da2b37aed Fix config string format in MobTypes.java
Replaced concatenation with String.format for clarity and consistency. This change ensures that the configuration strings are constructed correctly, preventing potential runtime errors.
2024-10-18 23:53:59 +02:00
auto e8b926ab41 Fix config string format in MobTypes.java
Replaced concatenation with String.format for clarity and consistency. This change ensures that the configuration strings are constructed correctly, preventing potential runtime errors.
2024-10-18 23:51:54 +02:00
auto f36bc6fc7f build commit 2024-10-18 23:49:42 +02:00
auto f3fa8563e6 build commit 2024-10-18 23:46:29 +02:00
auto 07b5010f74 build commit 2024-10-18 23:42:30 +02:00
auto 153543fb1f build commit 2024-10-18 23:37:18 +02:00
auto d29c3eb3c3 Add debug print statement to config load method
Inserted a temporary debug print statement for diagnostic purposes in the `Config.load` method. This will help in identifying issues during the configuration loading process.
2024-10-18 23:30:21 +02:00
auto 4937b6e337 Log config data for debugging
Added debug statements to log configuration details including the attribute names and modifier values being processed. This will help in identifying and troubleshooting issues with entity modifiers setup.
2024-10-18 23:27:29 +02:00
auto 77f9cfe0a6 Add logging to debug entity attributes
Inserted debug logs to track attribute setting and entity modifiers. This will aid in troubleshooting issues related to entity spawning and attribute application.
2024-10-18 23:21:49 +02:00
auto c8cb2309f6 Rename and enhance AttributeModifier
Renamed AttributeModifier to EntityAttributeModifier for clarity. Added Main dependency and improved attribute setting logic with namespaced keys for better error handling and debugging. Updated MobTypes to pass Main instance accordingly.
2024-10-18 22:59:47 +02:00
5 changed files with 89 additions and 43 deletions
+1 -1
View File
@@ -30,7 +30,7 @@ public class Main extends JavaPlugin {
public void reloadConfigs() {
Config.reload();
Messages.reload();
MobTypes.reload();
MobTypes.reload(this);
}
@Override
@@ -1,34 +0,0 @@
package com.alttd.custommobs.abilities.modifiers;
import com.alttd.custommobs.abilities.EntityModifier;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.LivingEntity;
@Slf4j
public class AttributeModifier implements EntityModifier {
private final double value;
private final Attribute attribute;
public AttributeModifier(Attribute attribute, double value) {
this.value = value;
this.attribute = attribute;
}
@Override
public void apply(LivingEntity entity) {
AttributeInstance attributeInstance = entity.getAttribute(attribute);
if (attributeInstance == null) {
log.warn("Entity {} has no {} attribute", entity.getName(), attribute.name());
return;
}
attributeInstance.setBaseValue(value);
}
@Override
public String getName() {
return attribute.name();
}
}
@@ -0,0 +1,49 @@
package com.alttd.custommobs.abilities.modifiers;
import com.alttd.custommobs.Main;
import com.alttd.custommobs.abilities.EntityModifier;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.entity.LivingEntity;
@Slf4j
public class EntityAttributeModifier implements EntityModifier {
private final double value;
private final Attribute attribute;
private final Main main;
public EntityAttributeModifier(Main main, Attribute attribute, double value) {
this.main = main;
this.value = value;
this.attribute = attribute;
}
@Override
public void apply(LivingEntity entity) {
log.info("DEBUG: Setting attribute {} to {}", attribute.name(), value);
AttributeInstance attributeInstance = entity.getAttribute(attribute);
if (attributeInstance == null) {
log.warn("Entity {} has no {} attribute", entity.getName(), attribute.name());
return;
}
log.info("DEBUG: Setting attribute {} to {}", attribute.name(), value);
NamespacedKey namespacedKey = NamespacedKey.fromString("cm", main);
if (namespacedKey == null) {
log.error("Namespaced key was null when applying attribute modifier");
return;
}
AttributeModifier attributeModifier = new AttributeModifier(namespacedKey,
attributeInstance.getValue() - value,
AttributeModifier.Operation.ADD_NUMBER);
attributeInstance.addModifier(attributeModifier);
}
@Override
public String getName() {
return attribute.name();
}
}
@@ -1,6 +1,7 @@
package com.alttd.custommobs.commands.subcommands;
import com.alttd.custommobs.Main;
import com.alttd.custommobs.abilities.EntityModifier;
import com.alttd.custommobs.abilities.MobType;
import com.alttd.custommobs.abilities.MobTypeApplier;
import com.alttd.custommobs.commands.SubCommand;
@@ -17,6 +18,7 @@ import org.bukkit.entity.*;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Slf4j
public class Spawn extends SubCommand {
@@ -104,16 +106,23 @@ public class Spawn extends SubCommand {
}
private void spawnMob(World world, Location location, EntityType entityType, MobType mobType) {
log.info("one");
log.info(mobType.toString());
log.info(mobType.entityModifiers().stream().map(EntityModifier::getName).collect(Collectors.joining(", ")));
if (entityType.getEntityClass() == null) {
log.warn("Tried to spawn entity with null entity class {}", entityType.name());
return;
}
log.info("two");
Entity entity = world.spawn(location, entityType.getEntityClass());
log.info("three");
if (!(entity instanceof LivingEntity livingEntity)) {
log.warn("Spawned entity is not a living entity? {}", entityType.name());
return;
}
log.info("four");
mobTypeApplier.apply(livingEntity, mobType);
log.info("five");
}
@Override
@@ -1,7 +1,8 @@
package com.alttd.custommobs.config;
import com.alttd.custommobs.Main;
import com.alttd.custommobs.abilities.Ability;
import com.alttd.custommobs.abilities.modifiers.AttributeModifier;
import com.alttd.custommobs.abilities.modifiers.EntityAttributeModifier;
import com.alttd.custommobs.abilities.EntityModifier;
import com.alttd.custommobs.abilities.MobType;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
@@ -16,19 +17,21 @@ import java.util.*;
@Slf4j
public class MobTypes extends AbstractConfig {
static MobTypes config;
private final Main main;
MobTypes() {
MobTypes(Main main) {
super(
new File(File.separator
+ "mnt" + File.separator
+ "configs" + File.separator
+ "custommobs"),
"mob-types.yml");
this.main = main;
}
public static void reload() {
public static void reload(Main main) {
log.info("Reloading mob types");
config = new MobTypes();
config = new MobTypes(main);
config.readConfig(MobTypes.class, null);
}
@@ -50,37 +53,56 @@ public class MobTypes extends AbstractConfig {
@SuppressWarnings("unused")
private static void load() {
MOB_TYPES.clear();
log.info("Loading mob types");
ConfigurationSection configurationSection = config.getConfigurationSection(prefix.substring(0, prefix.length() - 1));
if (configurationSection == null) {
log.warn("No config section found for {}", prefix.substring(0, prefix.length() - 1));
return;
}
log.info("1");
Set<String> keys = configurationSection.getKeys(false);
log.info("2");
keys.forEach(key -> {
List<Ability> abilities = config.getStringList(prefix + key, ".abilities", List.of()).stream()
.map(MobTypes.MOB_TYPES::getAbility)
.filter(Optional::isPresent)
.map(Optional::get)
.toList();
log.info("3");
List<EntityModifier> entityModifiers = new ArrayList<>();
log.info("4");
config.getConfigurationSection(prefix + key + ".modifiers").getKeys(false).forEach(modifier -> {
String attribute = config.getString(prefix + key + ".modifiers." + modifier, "attribute", null);
log.info("{}{}.modifiers.{}", prefix, key, modifier);
log.info("5 {}", modifier);
String attribute = config.getString(String.format("%s%s.modifiers.%s.", prefix, key, modifier), "attribute", null);
log.info("6");
if (attribute == null) {
log.info("RETURN (7)");
return;
}
EntityModifier value = getAttribute(attribute, config.getDouble(prefix + key + ".modifiers." + modifier, "value", 1));
if (value == null)
log.info("8");
EntityModifier value = getAttribute(attribute, config.getDouble(String.format("%s%s.modifiers.%s.", prefix, key, modifier), "value", 1));
log.info("9");
if (value == null) {
log.info("RETURN (10)");
return;
}
log.info("11");
entityModifiers.add(value);
});
log.info("12");
MOB_TYPES.put(key, new MobType(abilities, entityModifiers));
log.info("13");
});
}
private static EntityModifier getAttribute(String attributeName, double value) {
try {
Attribute attribute = Attribute.valueOf(attributeName);
return new AttributeModifier(attribute, value);
log.info("DEBUG in case replacements break");
log.info("DEBUG: {}", attributeName);
log.info("DEBUG: {}", attribute.name());
return new EntityAttributeModifier(config.main, attribute, value);
} catch (IllegalArgumentException e) {
log.error("Invalid attribute {}", attributeName, e);
return null;