Compare commits

..

9 Commits
master ... test

Author SHA1 Message Date
Teriuihi 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
Teriuihi 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
Teriuihi f36bc6fc7f build commit 2024-10-18 23:49:42 +02:00
Teriuihi f3fa8563e6 build commit 2024-10-18 23:46:29 +02:00
Teriuihi 07b5010f74 build commit 2024-10-18 23:42:30 +02:00
Teriuihi 153543fb1f build commit 2024-10-18 23:37:18 +02:00
Teriuihi 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
Teriuihi 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
Teriuihi 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
9 changed files with 58 additions and 238 deletions

View File

@ -6,7 +6,6 @@ import com.alttd.custommobs.config.Messages;
import com.alttd.custommobs.config.MobTypes;
import com.alttd.custommobs.listeners.SpawnerListener;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.NamespacedKey;
import org.bukkit.plugin.java.JavaPlugin;
@Slf4j
@ -16,20 +15,16 @@ public class Main extends JavaPlugin {
public void onEnable() {
log.info("Plugin enabled!");
reloadConfigs();
NamespacedKey mobTypeKey = new NamespacedKey(this, "mob-type");
NamespacedKey entityKey = new NamespacedKey(this, "entity-type");
registerEvents(mobTypeKey, entityKey);
registerCommands(mobTypeKey, entityKey);
registerEvents();
registerCommands();
}
private void registerCommands(NamespacedKey mobTypeKey, NamespacedKey entityKey) {
new CommandManager(this, mobTypeKey, entityKey);
private void registerCommands() {
new CommandManager(this);
}
private void registerEvents(NamespacedKey mobTypeKey, NamespacedKey entityKey) {
getServer().getPluginManager().registerEvents(new SpawnerListener(this, mobTypeKey, entityKey), this);
private void registerEvents() {
getServer().getPluginManager().registerEvents(new SpawnerListener(this), this);
}
public void reloadConfigs() {

View File

@ -24,20 +24,20 @@ public class EntityAttributeModifier implements EntityModifier {
@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;
}
double add = value - attributeInstance.getValue();
log.info("DEBUG: Adding {} to attribute {}", add, attribute.name());
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,
add,
attributeInstance.getValue() - value,
AttributeModifier.Operation.ADD_NUMBER);
attributeInstance.addModifier(attributeModifier);
}

View File

@ -1,12 +1,10 @@
package com.alttd.custommobs.commands;
import com.alttd.custommobs.Main;
import com.alttd.custommobs.commands.subcommands.GiveSpawner;
import com.alttd.custommobs.commands.subcommands.Spawn;
import com.alttd.custommobs.config.Messages;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.NamespacedKey;
import org.bukkit.command.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -21,7 +19,7 @@ import java.util.stream.Collectors;
public class CommandManager implements CommandExecutor, TabExecutor {
private final List<SubCommand> subCommands;
public CommandManager(Main main, @NotNull NamespacedKey mobTypeKey, @NotNull NamespacedKey entityKey) {
public CommandManager(Main main) {
PluginCommand command = main.getCommand("custommobs");
if (command == null) {
subCommands = null;
@ -33,8 +31,7 @@ public class CommandManager implements CommandExecutor, TabExecutor {
command.setAliases(List.of("cm"));
subCommands = Arrays.asList(
new Spawn(main),
new GiveSpawner(main, mobTypeKey, entityKey)
new Spawn(main)
);
}

View File

@ -1,105 +0,0 @@
package com.alttd.custommobs.commands.subcommands;
import com.alttd.custommobs.Main;
import com.alttd.custommobs.abilities.MobType;
import com.alttd.custommobs.commands.SubCommand;
import com.alttd.custommobs.config.Messages;
import com.alttd.custommobs.config.MobTypes;
import com.alttd.custommobs.utility.EntityTypeChecker;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
@Slf4j
public class GiveSpawner extends SubCommand {
private final Main main;
private final NamespacedKey mobTypeKey;
private final NamespacedKey entityKey;
public GiveSpawner(Main main, @NotNull NamespacedKey mobTypeKey, @NotNull NamespacedKey entityKey) {
this.main = main;
this.mobTypeKey = mobTypeKey;
this.entityKey = entityKey;
}
@Override
public boolean onCommand(CommandSender commandSender, String[] args) {
if (!(commandSender instanceof Player player)) {
commandSender.sendRichMessage(Messages.GENERIC.PLAYER_ONLY);
return true;
}
if (args.length != 3) {
return false;
}
Optional<MobType> optionalMobType = MobTypes.MOB_TYPES.get(args[1]);
if (optionalMobType.isEmpty()) {
return false;
}
try {
EntityType entityType = EntityType.valueOf(args[2]);
if (!entityType.isSpawnable() || !EntityTypeChecker.isMob(entityType)) {
return false;
}
} catch (IllegalArgumentException e) {
log.info("DEBUG: Invalid mob type {}", args[2]);
return false;
}
ItemStack spawner = new ItemStack(Material.SPAWNER);
ItemMeta meta = spawner.getItemMeta();
meta.displayName(MiniMessage.miniMessage().deserialize(Messages.GIVE_SPAWNER.SPAWNER_NAME, TagResolver.resolver(
Placeholder.parsed("entity", args[2]),
Placeholder.parsed("mob_type", args[1])
)));
PersistentDataContainer persistentDataContainer = meta.getPersistentDataContainer();
persistentDataContainer.set(mobTypeKey, PersistentDataType.STRING, args[1]);
persistentDataContainer.set(entityKey, PersistentDataType.STRING, args[2]);
spawner.setItemMeta(meta);
if (!player.getInventory().addItem(spawner).isEmpty()) {
player.sendRichMessage(Messages.GIVE_SPAWNER.FULL_INVENTORY);
return true;
}
player.sendRichMessage(Messages.GIVE_SPAWNER.GIVEN, TagResolver.resolver(
Placeholder.parsed("entity", args[2]),
Placeholder.parsed("mob_type", args[1])
));
return true;
}
@Override
public String getName() {
return "give_spawner";
}
@Override
public List<String> getTabComplete(CommandSender commandSender, String[] args) {
return switch (args.length) {
case 2 -> MobTypes.MOB_TYPES.getAllMobTypes();
case 3 -> Arrays.stream(EntityType.values()).map(EntityType::name).toList();
default -> List.of();
};
}
@Override
public String getHelpMessage() {
return Messages.HELP.GIVE_SPAWNER;
}
}

View File

@ -1,12 +1,12 @@
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;
import com.alttd.custommobs.config.Messages;
import com.alttd.custommobs.config.MobTypes;
import com.alttd.custommobs.utility.EntityTypeChecker;
import com.alttd.custommobs.utility.SpawnableTargetBlock;
import lombok.extern.slf4j.Slf4j;
import org.bukkit.Bukkit;
@ -18,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 {
@ -47,7 +48,7 @@ public class Spawn extends SubCommand {
log.info("DEBUG: Invalid mob type {}", args[2]);
return false;
}
if (!entityType.isSpawnable() || !EntityTypeChecker.isMob(entityType)) {
if (!entityType.isSpawnable() || !isMob(entityType)) {
return false;
}
@ -88,17 +89,40 @@ public class Spawn extends SubCommand {
}
}
public boolean isMob(EntityType entityType) {
if (entityType == null) {
return false;
}
try {
Class<? extends Entity> entityClass = entityType.getEntityClass();
if (entityClass == null) {
return false;
}
Class<?> mobEntityClass = Class.forName("org.bukkit.entity." + entityClass.getSimpleName());
return Mob.class.isAssignableFrom(mobEntityClass);
} catch (ClassNotFoundException e) {
return false;
}
}
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

View File

@ -1,7 +1,6 @@
package com.alttd.custommobs.config;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import java.io.File;
@ -30,14 +29,12 @@ public class Messages extends AbstractConfig {
public static String HELP_MESSAGE_WRAPPER = "<gold>Main help:\n<commands></gold>";
public static String HELP_MESSAGE = "<green>Show this menu: <gold>/cm help</gold></green>";
public static String SPAWN = "<green>Spawn a mob of a specific type: <gold>/cm spawn <mob_type> <mob> [<world> <x> <y> <z>]</gold></green>";
public static String GIVE_SPAWNER = "<green>Give a mob spawner for a mob of a specific type: <gold>/cm givespawner <mob_type> <mob></green>";
@SuppressWarnings("unused")
private static void load() {
HELP_MESSAGE_WRAPPER = config.getString(prefix, "help-wrapper", HELP_MESSAGE_WRAPPER);
HELP_MESSAGE = config.getString(prefix, "help", HELP_MESSAGE);
SPAWN = config.getString(prefix, "spawn", SPAWN);
GIVE_SPAWNER = config.getString(prefix, "give-spawner", GIVE_SPAWNER);
}
}
@ -55,31 +52,4 @@ public class Messages extends AbstractConfig {
PLAYER_NOT_FOUND = config.getString(prefix, "player-only", PLAYER_NOT_FOUND);
}
}
public static class GIVE_SPAWNER {
private static final String prefix = "give-spawner.";
public static String FULL_INVENTORY = "<red>You inventory is full so the spawner was not given</red>";
public static String GIVEN = "<green>You have been given a <entity> spawner for type <mob_type></green>";
public static String SPAWNER_NAME = "<gold><entity> <mob_type> spawner</gold>";
@SuppressWarnings("unused")
private static void load() {
FULL_INVENTORY = config.getString(prefix, "full-inventory", FULL_INVENTORY);
GIVEN = config.getString(prefix, "given", GIVEN);
SPAWNER_NAME = config.getString(prefix, "spawner-name", SPAWNER_NAME);
}
}
public static class PLACE_SPAWNER {
private static final String prefix = "place-spawner.";
public static String INVALID_MOB_TYPE = "<red>This spawner has an invalid mob type (<mob-type>)</red>";
public static String INVALID_ENTITY_TYPE = "<red>This spawner has an invalid entity type (<entity-type>)</red>";
@SuppressWarnings("unused")
private static void load() {
INVALID_MOB_TYPE = config.getString(prefix, "invalid-mob-type", INVALID_MOB_TYPE);
INVALID_ENTITY_TYPE = config.getString(prefix, "invalid-entity-type", INVALID_ENTITY_TYPE);
}
}
}

View File

@ -53,36 +53,55 @@ 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 -> {
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;
}
log.info("8");
EntityModifier value = getAttribute(attribute, config.getDouble(String.format("%s%s.modifiers.%s.", prefix, key, modifier), "value", 1));
if (value == null)
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);
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);

View File

@ -3,26 +3,14 @@ package com.alttd.custommobs.listeners;
import com.alttd.custommobs.Main;
import com.alttd.custommobs.abilities.MobType;
import com.alttd.custommobs.abilities.MobTypeApplier;
import com.alttd.custommobs.config.Messages;
import com.alttd.custommobs.config.MobTypes;
import com.alttd.custommobs.utility.EntityTypeChecker;
import io.papermc.paper.persistence.PersistentDataContainerView;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.Sign;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.SpawnerSpawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
@ -31,13 +19,9 @@ import java.util.Optional;
public class SpawnerListener implements Listener {
private final MobTypeApplier mobTypeApplier;
private final NamespacedKey mobTypeKey;
private final NamespacedKey entityKey;
public SpawnerListener(Main main, @NotNull NamespacedKey mobTypeKey, @NotNull NamespacedKey entityKey) {
public SpawnerListener(Main main) {
mobTypeApplier = new MobTypeApplier(main);
this.mobTypeKey = mobTypeKey;
this.entityKey = entityKey;
}
@EventHandler
@ -64,44 +48,4 @@ public class SpawnerListener implements Listener {
mobTypeApplier.apply(livingEntity, mobType);
}
@EventHandler
public void onSpawnerPlace(BlockPlaceEvent event) {
ItemStack itemInHand = event.getItemInHand();
if (!itemInHand.getType().equals(org.bukkit.Material.SPAWNER)) {
return;
}
PersistentDataContainerView persistentDataContainer = itemInHand.getPersistentDataContainer();
if (!persistentDataContainer.has(mobTypeKey) || !persistentDataContainer.has(entityKey)) {
return;
}
String mobType = persistentDataContainer.get(mobTypeKey, PersistentDataType.STRING);
String entityTypeName = persistentDataContainer.get(entityKey, PersistentDataType.STRING);
if (mobType == null || entityTypeName == null) {
return;
}
Optional<MobType> optionalMobType = MobTypes.MOB_TYPES.get(mobType);
if (optionalMobType.isEmpty()) {
event.getPlayer().sendRichMessage(Messages.PLACE_SPAWNER.INVALID_MOB_TYPE, Placeholder.parsed("mob-type", mobType));
return;
}
EntityType entityType;
try {
entityType = EntityType.valueOf(entityTypeName);
if (!entityType.isSpawnable() || !EntityTypeChecker.isMob(entityType)) {
event.getPlayer().sendRichMessage(Messages.PLACE_SPAWNER.INVALID_ENTITY_TYPE, Placeholder.parsed("entity-type", entityTypeName));
return;
}
} catch (IllegalArgumentException e) {
log.info("DEBUG: Invalid mob type {}", entityTypeName);
return;
}
if (event.getBlock().getState() instanceof CreatureSpawner spawner) {
spawner.setSpawnedType(entityType);
} else {
log.info("Actual bock state {}", event.getBlock().getState().getClass().getName());
}
}
}

View File

@ -1,24 +0,0 @@
package com.alttd.custommobs.utility;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Mob;
public class EntityTypeChecker {
public static boolean isMob(EntityType entityType) {
if (entityType == null) {
return false;
}
try {
Class<? extends Entity> entityClass = entityType.getEntityClass();
if (entityClass == null) {
return false;
}
Class<?> mobEntityClass = Class.forName("org.bukkit.entity." + entityClass.getSimpleName());
return Mob.class.isAssignableFrom(mobEntityClass);
} catch (ClassNotFoundException e) {
return false;
}
}
}