Skip to content

Commit

Permalink
Add item equipment support for entity loot
Browse files Browse the repository at this point in the history
- The keys hand, off_hand, feet, legs, chest, and head are used to define equipment on the spawned entity
- The item format is the same as regular item loot
  • Loading branch information
Archy-X committed Jul 7, 2024
1 parent ff5e888 commit 071eecc
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public MythicMobsHook(AuraSkills plugin, ConfigurationNode config) {

// Wait for loot manager to be created, but add parser before it is loaded
plugin.getScheduler().executeSync(() ->
plugin.getLootTableManager().getLootManager().registerCustomEntityParser(new MythicEntityLootParser()));
plugin.getLootTableManager().getLootManager().registerCustomEntityParser(new MythicEntityLootParser(plugin)));
}

@EventHandler(priority = EventPriority.HIGH)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package dev.aurelium.auraskills.bukkit.hooks.mythicmobs.loot;

import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.bukkit.loot.entity.EntityProperties;
import dev.aurelium.auraskills.bukkit.loot.entity.EntitySupplier;
import dev.aurelium.auraskills.bukkit.loot.parser.CustomEntityParser;
import org.spongepowered.configurate.ConfigurationNode;

public class MythicEntityLootParser implements CustomEntityParser {

private final AuraSkills plugin;

public MythicEntityLootParser(AuraSkills plugin) {
this.plugin = plugin;
}

@Override
public EntitySupplier getEntitySupplier(ConfigurationNode config) {
return new MythicEntitySupplier(EntityProperties.fromConfig(config));
return new MythicEntitySupplier(EntityProperties.fromConfig(config, plugin));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void initLootManager() {
lootManager.registerContextProvider(new SourceContextProvider(plugin));
lootManager.registerContextProvider(new MobContextProvider());
lootManager.registerCustomItemParser(new ItemKeyParser(plugin));
lootManager.registerCustomEntityParser(new VanillaEntityParser());
lootManager.registerCustomEntityParser(new VanillaEntityParser(plugin));
lootManager.addLootOptionKeys("xp");
lootManager.addPoolOptionKeys("chance_per_luck", "require_open_water");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
package dev.aurelium.auraskills.bukkit.loot.entity;

import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.bukkit.util.ConfigurateItemParser;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.spongepowered.configurate.ConfigurationNode;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public record EntityProperties(String entityId,
String name,
Integer level,
Double health,
Double damage,
Float horizontalVelocity,
Float verticalVelocity) {
Float verticalVelocity,
Map<EquipmentSlot, ItemStack> equipment) {

public static EntityProperties fromConfig(ConfigurationNode config) {
public static EntityProperties fromConfig(ConfigurationNode config, AuraSkills plugin) {
String[] id = config.node("entity").getString("").split(":");

// Parse equipment items for each slot
var itemParser = new ConfigurateItemParser(plugin);

Map<EquipmentSlot, ItemStack> equipment = new HashMap<>();
for (EquipmentSlot slot : EquipmentSlot.values()) {
ConfigurationNode itemNode = config.node(slot.toString().toLowerCase(Locale.ROOT));
if (itemNode.empty()) continue;

ItemStack item = itemParser.parseItem(itemNode);
equipment.put(slot, item);
}

return new EntityProperties(
id.length > 1 ? id[1] : id[0],
config.node("name").getString(),
config.node("level").empty() ? null : config.node("level").getInt(),
config.node("health").empty() ? null : config.node("health").getDouble(),
config.node("damage").empty() ? null : config.node("damage").getDouble(),
config.node("velocity", "horizontal").empty() ? null : config.node("velocity", "horizontal").getFloat(),
config.node("velocity", "vertical").empty() ? null : config.node("velocity", "vertical").getFloat()
config.node("velocity", "vertical").empty() ? null : config.node("velocity", "vertical").getFloat(),
equipment
);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package dev.aurelium.auraskills.bukkit.loot.entity;

import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.bukkit.loot.parser.CustomEntityParser;
import org.spongepowered.configurate.ConfigurationNode;

public class VanillaEntityParser implements CustomEntityParser {

private final AuraSkills plugin;

public VanillaEntityParser(AuraSkills plugin) {
this.plugin = plugin;
}

@Override
public EntitySupplier getEntitySupplier(ConfigurationNode config) {
return new VanillaEntitySupplier(EntityProperties.fromConfig(config));
return new VanillaEntitySupplier(EntityProperties.fromConfig(config, plugin));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;

import java.util.Map.Entry;

public class VanillaEntitySupplier extends EntitySupplier {

public VanillaEntitySupplier(EntityProperties entityProperties) {
Expand All @@ -21,7 +26,9 @@ public Entity spawnEntity(AuraSkills plugin, Location location) {
World world = location.getWorld();
if (world == null) return null;

Entity entity = world.spawnEntity(location, EntityType.valueOf(getEntityProperties().entityId().toUpperCase()));
EntityProperties properties = getEntityProperties();

Entity entity = world.spawnEntity(location, EntityType.valueOf(properties.entityId().toUpperCase()));

if (entity instanceof LivingEntity livingEntity) {
if (getEntityProperties().health() != null) {
Expand All @@ -31,20 +38,29 @@ public Entity spawnEntity(AuraSkills plugin, Location location) {
livingEntity.setHealth(Math.min(getEntityProperties().health(), attribute.getValue()));
}
}
if (getEntityProperties().damage() != null) {
if (properties.damage() != null) {
AttributeInstance attribute = livingEntity.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE);
if (attribute != null) {
attribute.setBaseValue(getEntityProperties().damage());
}
}
// Add equipment
EntityEquipment equipment = livingEntity.getEquipment();
if (equipment != null) {
for (Entry<EquipmentSlot, ItemStack> entry : properties.equipment().entrySet()) {
EquipmentSlot slot = entry.getKey();
ItemStack item = entry.getValue();
equipment.setItem(slot, item, true);
}
}
}

if (getEntityProperties().name() != null) {
if (properties.name() != null) {
entity.setCustomName(plugin.getMessageProvider().applyFormatting(getEntityProperties().name()));
entity.setCustomNameVisible(true);
}

if (getEntityProperties().level() != null) {
if (properties.level() != null) {
entity.setMetadata("auraskills_level", new FixedMetadataValue(plugin, getEntityProperties().level()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import dev.aurelium.auraskills.api.ability.Ability;
import dev.aurelium.auraskills.api.event.loot.LootDropEvent;
import dev.aurelium.auraskills.api.loot.Loot;
import dev.aurelium.auraskills.api.loot.LootContext;
import dev.aurelium.auraskills.api.loot.LootPool;
import dev.aurelium.auraskills.api.loot.LootTable;
import dev.aurelium.auraskills.api.skill.Skill;
import dev.aurelium.auraskills.api.source.XpSource;
import dev.aurelium.auraskills.api.stat.Stats;
import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.bukkit.hooks.WorldGuardFlags.FlagKey;
import dev.aurelium.auraskills.bukkit.hooks.WorldGuardHook;
import dev.aurelium.auraskills.api.loot.Loot;
import dev.aurelium.auraskills.api.loot.LootPool;
import dev.aurelium.auraskills.api.loot.LootTable;
import dev.aurelium.auraskills.api.loot.LootContext;
import dev.aurelium.auraskills.bukkit.loot.context.MobContext;
import dev.aurelium.auraskills.bukkit.loot.context.SourceContext;
import dev.aurelium.auraskills.bukkit.loot.type.CommandLoot;
Expand All @@ -32,14 +32,17 @@
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.HashSet;
import java.util.Locale;
import java.util.Random;
import java.util.Set;

public abstract class LootHandler {

Expand Down

0 comments on commit 071eecc

Please sign in to comment.