Search in sources :

Example 1 with EntityDeathEvent

use of io.xol.chunkstories.api.events.entity.EntityDeathEvent in project chunkstories-core by Hugobros3.

the class EntityComponentHealth method handleDeath.

private void handleDeath() {
    EntityDeathEvent entityDeathEvent = new EntityDeathEvent(entityLiving);
    entity.getWorld().getGameLogic().getPluginsManager().fireEvent(entityDeathEvent);
    // Handles cases of controlled player death
    if (entity instanceof EntityControllable) {
        Controller controller = ((EntityControllable) entity).getControllerComponent().getController();
        if (controller != null) {
            controller.setControlledEntity(null);
            // Serverside stuff
            if (controller instanceof Player && entity.getWorld() instanceof WorldMaster) {
                Player player = (Player) controller;
                PlayerDeathEvent event = new PlayerDeathEvent(player);
                entity.getWorld().getGameLogic().getPluginsManager().fireEvent(event);
                // When a player dies, delete his save as well
                File playerSavefile = new File(((WorldMaster) entity.getWorld()).getFolderPath() + "/players/" + player.getName().toLowerCase() + ".csf");
                if (playerSavefile.exists()) {
                    // Player save file is deleted upon death
                    playerSavefile.delete();
                }
                if (event.getDeathMessage() != null) {
                    GameContext gc = player.getContext();
                    if (gc instanceof ServerInterface)
                        ((ServerInterface) gc).broadcastMessage(event.getDeathMessage());
                }
            } else {
            // Weird, undefined cases ( controller wasn't a player, maybe some weird mod logic here
            }
        }
    }
}
Also used : Player(io.xol.chunkstories.api.player.Player) ServerInterface(io.xol.chunkstories.api.server.ServerInterface) EntityDeathEvent(io.xol.chunkstories.api.events.entity.EntityDeathEvent) GameContext(io.xol.chunkstories.api.GameContext) PlayerDeathEvent(io.xol.chunkstories.api.events.player.PlayerDeathEvent) Controller(io.xol.chunkstories.api.entity.Controller) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) File(java.io.File) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Aggregations

GameContext (io.xol.chunkstories.api.GameContext)1 Controller (io.xol.chunkstories.api.entity.Controller)1 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)1 EntityDeathEvent (io.xol.chunkstories.api.events.entity.EntityDeathEvent)1 PlayerDeathEvent (io.xol.chunkstories.api.events.player.PlayerDeathEvent)1 Player (io.xol.chunkstories.api.player.Player)1 ServerInterface (io.xol.chunkstories.api.server.ServerInterface)1 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)1 File (java.io.File)1