globalEntityMap = new ConcurrentHashMap<>();
protected AbstractEntityLibAPI(Platform platform, APIConfig settings) {
this.platform = platform;
@@ -27,6 +32,22 @@ public abstract class AbstractEntityLibAPI
implements EntityLibAPI() : Collections.emptyList();
}
+ @Override
+ public @Nullable WrapperEntity findEntity(int entityId) {
+ return globalEntityMap.get(entityId);
+ }
+
+ @Override
+ public void globalRegisterEntity(WrapperEntity entity) {
+ if (globalEntityMap.containsKey(entity.getEntityId())) throw new IllegalArgumentException("Entity with that Id is already registered and present");
+ globalEntityMap.put(entity.getEntityId(), entity);
+ }
+
+ @Override
+ public void globalUnregisterEntity(@NotNull WrapperEntity entity) {
+ globalEntityMap.remove(entity.getEntityId());
+ }
+
@NotNull
@Override
public APIConfig getSettings() {
diff --git a/common/src/main/java/me/tofaa/entitylib/common/AbstractWorldWrapper.java b/common/src/main/java/me/tofaa/entitylib/common/AbstractWorldWrapper.java
index b7a29fd..311c176 100644
--- a/common/src/main/java/me/tofaa/entitylib/common/AbstractWorldWrapper.java
+++ b/common/src/main/java/me/tofaa/entitylib/common/AbstractWorldWrapper.java
@@ -54,6 +54,7 @@ public abstract class AbstractWorldWrapper implements WorldWrapper {
player.spawn(this, location);
entities.put(player.getUuid(), player);
entitiesById.put(player.getEntityId(), player);
+ EntityLib.getApi().globalRegisterEntity(player);
return player;
}
@@ -62,6 +63,7 @@ public abstract class AbstractWorldWrapper implements WorldWrapper {
entity.spawn(this, location);
entities.put(entity.getUuid(), entity);
entitiesById.put(entity.getEntityId(), entity);
+ EntityLib.getApi().globalRegisterEntity(entity);
return entity;
}
@@ -70,6 +72,7 @@ public abstract class AbstractWorldWrapper implements WorldWrapper {
entity.despawn();
this.entities.remove(entity.getUuid());
this.entitiesById.remove(entity.getEntityId());
+ EntityLib.getApi().globalUnregisterEntity(entity);
}
@Override
diff --git a/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java b/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java
index 919fa12..fd509b0 100644
--- a/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java
+++ b/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java
@@ -25,7 +25,7 @@ public class SpigotEntityLibPlatform extends AbstractPlatform {
@Override
- public EntityLibAPI, ?> getAPI() {
+ public SpigotEntityLibAPI getAPI() {
return api;
}
diff --git a/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java b/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java
index 849ec69..b583bb0 100644
--- a/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java
+++ b/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java
@@ -8,20 +8,25 @@ import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.EntityLibAPI;
import me.tofaa.entitylib.WorldWrapper;
import me.tofaa.entitylib.meta.mobs.passive.ChickenMeta;
+import me.tofaa.entitylib.spigot.SpigotEntityLibAPI;
import me.tofaa.entitylib.spigot.SpigotEntityLibPlatform;
import me.tofaa.entitylib.wrapper.WrapperEntity;
import org.bukkit.World;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandExecutor;
+import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
+import org.jetbrains.annotations.NotNull;
-public class TestEntityLibPlugin extends JavaPlugin implements Listener {
+public class TestEntityLibPlugin extends JavaPlugin implements CommandExecutor {
- private EntityLibAPI api;
+ private SpigotEntityLibAPI api;
private WrapperEntity e;
private WorldWrapper world;
@@ -35,24 +40,27 @@ public class TestEntityLibPlugin extends JavaPlugin implements Listener {
.usePlatformLogger();
EntityLib.init(platform, settings);
- api = EntityLib.getApi();
- getServer().getPluginManager().registerEvents(this, this);
+ api = platform.getAPI();
+ getCommand("testapi").setExecutor(this);
}
- @EventHandler
- public void onCrouch(PlayerToggleSneakEvent event) {
- Player player = event.getPlayer();
+ @Override
+ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
+ if (!(sender instanceof Player)) return false;
+ Player player = (Player) sender;
if (e == null) {
world = api.wrapWorld(player.getWorld());
e = world.spawnEntity(EntityTypes.CHICKEN, SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
+ e.addViewer(player.getUniqueId());
+ player.sendMessage("Spawned");
}
- world = api.wrapWorld(player.getWorld());
- e = world.spawnEntity(EntityTypes.CHICKEN, SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
ChickenMeta meta = (ChickenMeta) e.getEntityMeta();
meta.setBaby(!meta.isBaby());
meta.setHasGlowingEffect(!meta.hasGlowingEffect());
+ meta.setHasNoGravity(!meta.hasNoGravity());
- e.addViewer(player.getUniqueId());
+ player.sendMessage("Updated");
+ return true;
}
}
diff --git a/test-plugin/src/main/resources/plugin.yml b/test-plugin/src/main/resources/plugin.yml
index df284e5..8c23a0e 100644
--- a/test-plugin/src/main/resources/plugin.yml
+++ b/test-plugin/src/main/resources/plugin.yml
@@ -6,15 +6,5 @@ main: me.tofaa.testentitylib.TestEntityLibPlugin
api-version: "1.19"
commands:
testapi:
- description: Test PEEntityMeta API
- usage: /
-
- testentity:
- description: Test PEEntity API
- usage: /
- testdisplay:
- description: Test PEDisplay API
- usage: /
- spawnclickablefrog:
- description: Spawn a clickable frog
+ description: Test EntityLib API
usage: /
\ No newline at end of file