Search in sources :

Example 1 with EntityDefinition

use of io.xol.chunkstories.api.entity.EntityDefinition in project chunkstories by Hugobros3.

the class SpawnEntityCommand method handleCommand.

@Override
public boolean handleCommand(CommandEmitter emitter, Command command, String[] arguments) {
    if (!(emitter instanceof Player)) {
        emitter.sendMessage("You need to be a player to use this command.");
        return true;
    }
    Player player = (Player) emitter;
    if (!emitter.hasPermission("world.spawnEntity")) {
        emitter.sendMessage("You don't have the permission.");
        return true;
    }
    if (arguments.length == 0) {
        emitter.sendMessage("Syntax: /spawnEntity <entityId> [x y z]");
        return false;
    }
    Location loc = player.getLocation();
    if (arguments.length >= 4) {
        loc = new Location(player.getWorld(), Double.parseDouble(arguments[1]), Double.parseDouble(arguments[2]), Double.parseDouble(arguments[3]));
    }
    EntityDefinition entityType;
    String entityName = arguments[0];
    entityType = server.getContent().entities().getEntityDefinition(entityName);
    if (entityType == null) {
        emitter.sendMessage("Entity type : " + arguments[0] + " not found in loaded content.");
        return true;
    }
    Entity entity = entityType.create(loc);
    entity.setLocation(loc);
    loc.getWorld().addEntity(entity);
    emitter.sendMessage("#00FFD0" + "Spawned " + entity.getClass().getSimpleName() + " at " + (arguments.length >= 4 ? loc.toString() : player.getName()));
    return true;
}
Also used : EntityDefinition(io.xol.chunkstories.api.entity.EntityDefinition) Entity(io.xol.chunkstories.api.entity.Entity) Player(io.xol.chunkstories.api.player.Player) Location(io.xol.chunkstories.api.Location)

Example 2 with EntityDefinition

use of io.xol.chunkstories.api.entity.EntityDefinition in project chunkstories by Hugobros3.

the class CulledEntitiesRenderer method renderEntities.

public int renderEntities(RenderingInterface renderer) {
    ((WorldImplementation) world).entitiesLock.readLock().lock();
    // Sort them by type
    Map<EntityDefinition, List<EntityRenderable>> renderableEntitiesTypes = new HashMap<EntityDefinition, List<EntityRenderable>>();
    for (Entity entity : world.getAllLoadedEntities()) {
        if (entity instanceof EntityRenderable) {
            EntityRenderable entityRenderable = (EntityRenderable) entity;
            List<EntityRenderable> entitiesOfThisType = renderableEntitiesTypes.get(entityRenderable.getDefinition());
            if (entitiesOfThisType == null) {
                renderableEntitiesTypes.put(entityRenderable.getDefinition(), new ArrayList<EntityRenderable>());
                entitiesOfThisType = renderableEntitiesTypes.get(entityRenderable.getDefinition());
            }
            entitiesOfThisType.add(entityRenderable);
        }
    }
    int entitiesRendered = 0;
    for (Entry<EntityDefinition, List<EntityRenderable>> entry : renderableEntitiesTypes.entrySet()) {
        List<EntityRenderable> entities = entry.getValue();
        // Caches entity renderers until we f12
        if (!entityRenderers.containsKey(entry.getKey()))
            entityRenderers.put(entry.getKey(), entities.get(0).getEntityRenderer());
        EntityRenderer<? extends EntityRenderable> entityRenderer = entityRenderers.get(entry.getKey());
        if (entityRenderer == null)
            continue;
        try {
            int e = entityRenderer.renderEntities(renderer, new EntitiesRendererIterator<>(renderer, entities));
            entitiesRendered += e;
        } catch (Throwable e) {
            System.out.println("Exception rendering entities " + entities.get(0).getClass().getSimpleName() + " using " + entityRenderer.getClass().getSimpleName());
            e.printStackTrace();
        }
    }
    ((WorldImplementation) world).entitiesLock.readLock().unlock();
    return entitiesRendered;
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) EntityRenderable(io.xol.chunkstories.api.rendering.entity.EntityRenderable) HashMap(java.util.HashMap) WorldImplementation(io.xol.chunkstories.world.WorldImplementation) EntityDefinition(io.xol.chunkstories.api.entity.EntityDefinition) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Entity (io.xol.chunkstories.api.entity.Entity)2 EntityDefinition (io.xol.chunkstories.api.entity.EntityDefinition)2 Location (io.xol.chunkstories.api.Location)1 Player (io.xol.chunkstories.api.player.Player)1 EntityRenderable (io.xol.chunkstories.api.rendering.entity.EntityRenderable)1 WorldImplementation (io.xol.chunkstories.world.WorldImplementation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1