Skip to content

Commit

Permalink
Added entity resolver APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSzabo committed Jul 22, 2024
1 parent 158fdfc commit 0c02bd3
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/gg/auroramc/aurora/Aurora.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import gg.auroramc.aurora.config.Config;
import gg.auroramc.aurora.api.expansions.ExpansionManager;
import gg.auroramc.aurora.expansions.economy.EconomyExpansion;
import gg.auroramc.aurora.expansions.entity.EntityExpansion;
import gg.auroramc.aurora.expansions.item.ItemExpansion;
import gg.auroramc.aurora.expansions.leaderboard.LeaderboardExpansion;
import gg.auroramc.aurora.expansions.numberformat.NumberFormatExpansion;
Expand Down Expand Up @@ -78,6 +79,7 @@ private void setupExpansions() {
expansionManager.loadExpansion(EconomyExpansion.class);
expansionManager.loadExpansion(NumberFormatExpansion.class);
expansionManager.loadExpansion(ItemExpansion.class);
expansionManager.loadExpansion(EntityExpansion.class);
expansionManager.loadExpansion(LeaderboardExpansion.class);

if (DependencyManager.hasDep(Dep.WORLDGUARD)) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/gg/auroramc/aurora/api/AuroraAPI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gg.auroramc.aurora.api;

import gg.auroramc.aurora.Aurora;
import gg.auroramc.aurora.api.entity.EntityManager;
import gg.auroramc.aurora.api.expansions.ExpansionManager;
import gg.auroramc.aurora.api.item.ItemManager;
import gg.auroramc.aurora.api.placeholder.PlaceholderHandler;
Expand All @@ -9,6 +10,7 @@
import gg.auroramc.aurora.api.user.UserManager;
import gg.auroramc.aurora.expansions.economy.AuroraEconomy;
import gg.auroramc.aurora.expansions.economy.EconomyExpansion;
import gg.auroramc.aurora.expansions.entity.EntityExpansion;
import gg.auroramc.aurora.expansions.item.ItemExpansion;
import gg.auroramc.aurora.expansions.leaderboard.LeaderboardExpansion;
import gg.auroramc.aurora.expansions.numberformat.NumberFormatExpansion;
Expand Down Expand Up @@ -142,4 +144,8 @@ public static RegionExpansion getRegionManager() {
public static ItemManager getItemManager() {
return Aurora.getExpansionManager().getExpansion(ItemExpansion.class).getItemManager();
}

public static EntityManager getEntityManager() {
return Aurora.getExpansionManager().getExpansion(EntityExpansion.class).getEntityManager();
}
}
57 changes: 57 additions & 0 deletions src/main/java/gg/auroramc/aurora/api/entity/EntityManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package gg.auroramc.aurora.api.entity;


import gg.auroramc.aurora.api.dependency.Dep;
import gg.auroramc.aurora.api.item.TypeId;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

import java.util.LinkedHashMap;
import java.util.Map;

public class EntityManager {
private final VanillaEntityResolver vanillaEntityResolver = new VanillaEntityResolver();
private final Map<String, EntityResolver> resolvers = new LinkedHashMap<>();

public void registerResolver(String plugin, EntityResolver resolver) {
resolvers.put(plugin, resolver);
}

public void registerResolver(Dep plugin, EntityResolver resolver) {
resolvers.put(plugin.getId().toLowerCase(), resolver);
}

public EntityResolver getResolver(String plugin) {
return resolvers.get(plugin);
}

public void unregisterResolver(String plugin) {
resolvers.remove(plugin.toLowerCase());
}

public TypeId resolveId(Entity entity) {
for (EntityResolver resolver : resolvers.values()) {
if (resolver.matches(entity)) {
return resolver.resolveId(entity);
}
}
return TypeId.from(entity.getType());
}

public EntitySpawner resolveEntitySpawner(TypeId typeId, @Nullable Player player) {
if (typeId.namespace().equalsIgnoreCase("minecraft"))
return vanillaEntityResolver.resolveEntitySpawner(typeId.id(), player);

for (var resolver : resolvers.entrySet()) {
if (resolver.getKey().equalsIgnoreCase(typeId.namespace())) {
return resolver.getValue().resolveEntitySpawner(typeId.id(), player);
}
}
return vanillaEntityResolver.resolveEntitySpawner(typeId.id(), player);
}

public EntitySpawner resolveEntitySpawner(TypeId typeId) {
return resolveEntitySpawner(typeId, null);
}
}
18 changes: 18 additions & 0 deletions src/main/java/gg/auroramc/aurora/api/entity/EntityResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package gg.auroramc.aurora.api.entity;

import gg.auroramc.aurora.api.item.TypeId;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

public interface EntityResolver {
boolean matches(Entity entity);

TypeId resolveId(Entity entity);

EntitySpawner resolveEntitySpawner(String id, @Nullable Player player);

default EntitySpawner resolveEntitySpawner(String id) {
return resolveEntitySpawner(id, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gg.auroramc.aurora.api.entity;

import org.bukkit.Location;

import java.util.Map;

public interface EntitySpawner {
void spawn(Location location, Map<String, Object> args);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gg.auroramc.aurora.api.entity;

import gg.auroramc.aurora.api.item.TypeId;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

public class VanillaEntityResolver implements EntityResolver {

@Override
public boolean matches(Entity entity) {
return true;
}

@Override
public TypeId resolveId(Entity entity) {
return TypeId.from(entity.getType());
}

@Override
public EntitySpawner resolveEntitySpawner(String id, @Nullable Player player) {
return new VanillaEntitySpawner(EntityType.valueOf(id.toUpperCase()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gg.auroramc.aurora.api.entity;

import gg.auroramc.aurora.Aurora;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;

import java.util.Map;

public class VanillaEntitySpawner implements EntitySpawner {
private final EntityType entityType;

public VanillaEntitySpawner(EntityType entityType) {
this.entityType = entityType;
}

@Override
public void spawn(Location location, Map<String, Object> args) {
Bukkit.getRegionScheduler().run(Aurora.getInstance(), location, (task) -> {
location.getWorld().spawnEntity(location, entityType);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package gg.auroramc.aurora.expansions.entity;

import gg.auroramc.aurora.Aurora;
import gg.auroramc.aurora.api.dependency.Dep;
import gg.auroramc.aurora.api.dependency.DependencyManager;
import gg.auroramc.aurora.api.entity.EntityManager;
import gg.auroramc.aurora.api.expansions.AuroraExpansion;
import gg.auroramc.aurora.expansions.entity.resolvers.ecomobs.EcoMobsEntityResolver;
import gg.auroramc.aurora.expansions.entity.resolvers.mythicmobs.MythicEntityResolver;
import lombok.Getter;

@Getter
public class EntityExpansion implements AuroraExpansion {
private EntityManager entityManager;

@Override
public void hook() {
entityManager = new EntityManager();

if (DependencyManager.hasDep(Dep.MYTHICMOBS)) {
entityManager.registerResolver(Dep.MYTHICMOBS, new MythicEntityResolver());
Aurora.logger().debug("Hooked into MythicMobs for entity resolvers.");
}

if (DependencyManager.hasEveryDep("Eco", "EcoMobs")) {
entityManager.registerResolver("ecomobs", new EcoMobsEntityResolver());
Aurora.logger().debug("Hooked into EcoMobs for entity resolvers.");
}
}

@Override
public boolean canHook() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gg.auroramc.aurora.expansions.entity.resolvers.ecomobs;

import com.willfp.eco.core.entities.Entities;
import gg.auroramc.aurora.api.entity.EntityResolver;
import gg.auroramc.aurora.api.entity.EntitySpawner;
import gg.auroramc.aurora.api.item.TypeId;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.Nullable;

public class EcoMobsEntityResolver implements EntityResolver {
private final NamespacedKey namespacedKey = new NamespacedKey("ecomobs", "mob");

@Override
public boolean matches(Entity entity) {
return entity.getPersistentDataContainer().has(namespacedKey);
}

@Override
public TypeId resolveId(Entity entity) {
String id = entity.getPersistentDataContainer().get(namespacedKey, PersistentDataType.STRING);
return new TypeId("ecomobs", id);
}

@Override
public EntitySpawner resolveEntitySpawner(String id, @Nullable Player player) {
return new EcoMobsEntitySpawner(Entities.lookup("ecomobs:" + id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gg.auroramc.aurora.expansions.entity.resolvers.ecomobs;

import com.willfp.eco.core.entities.TestableEntity;
import gg.auroramc.aurora.Aurora;
import gg.auroramc.aurora.api.entity.EntitySpawner;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;

import java.util.Map;

public class EcoMobsEntitySpawner implements EntitySpawner {
private final TestableEntity entityType;

public EcoMobsEntitySpawner(TestableEntity entityType) {
this.entityType = entityType;
}

@Override
public void spawn(Location location, Map<String, Object> args) {
if(entityType == null) {
Aurora.logger().warning("Failed to spawn entity, because eco entity is null");
return;
}
Bukkit.getRegionScheduler().run(Aurora.getInstance(), location, (task) -> entityType.spawn(location));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gg.auroramc.aurora.expansions.entity.resolvers.mythicmobs;

import gg.auroramc.aurora.api.entity.EntityResolver;
import gg.auroramc.aurora.api.entity.EntitySpawner;
import gg.auroramc.aurora.api.item.TypeId;
import io.lumine.mythic.bukkit.MythicBukkit;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;

public class MythicEntityResolver implements EntityResolver {

@Override
public boolean matches(Entity entity) {
return MythicBukkit.inst().getMobManager().isMythicMob(entity);
}

@Override
public TypeId resolveId(Entity entity) {
var id = MythicBukkit.inst().getMobManager().getMythicMobInstance(entity).getType().getInternalName();
return new TypeId("mythicmobs", id);
}

@Override
public EntitySpawner resolveEntitySpawner(String id, @Nullable Player player) {
return new MythicEntitySpawner(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gg.auroramc.aurora.expansions.entity.resolvers.mythicmobs;

import gg.auroramc.aurora.Aurora;
import gg.auroramc.aurora.api.entity.EntitySpawner;
import io.lumine.mythic.bukkit.MythicBukkit;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;

import java.util.Map;

public class MythicEntitySpawner implements EntitySpawner {
private final String id;

public MythicEntitySpawner(String id) {
this.id = id;
}

@Override
public void spawn(Location location, Map<String, Object> args) {
Bukkit.getRegionScheduler().run(Aurora.getInstance(), location, (task) -> {
var level = args.containsKey("level") ? (double) args.get("level") : 1.0;
var mob = MythicBukkit.inst().getMobManager().spawnMob(id, location, level);
if (mob == null) {
Aurora.logger().warning("Failed to spawn mythic mob with id " + id);
}
});
}
}

0 comments on commit 0c02bd3

Please sign in to comment.