-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/gg/auroramc/aurora/api/entity/EntityManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/main/java/gg/auroramc/aurora/api/entity/EntityResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/gg/auroramc/aurora/api/entity/EntitySpawner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/gg/auroramc/aurora/api/entity/VanillaEntityResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/gg/auroramc/aurora/api/entity/VanillaEntitySpawner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/gg/auroramc/aurora/expansions/entity/EntityExpansion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...in/java/gg/auroramc/aurora/expansions/entity/resolvers/ecomobs/EcoMobsEntityResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ain/java/gg/auroramc/aurora/expansions/entity/resolvers/ecomobs/EcoMobsEntitySpawner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
.../java/gg/auroramc/aurora/expansions/entity/resolvers/mythicmobs/MythicEntityResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...n/java/gg/auroramc/aurora/expansions/entity/resolvers/mythicmobs/MythicEntitySpawner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |