Search in sources :

Example 1 with ServerInterface

use of io.xol.chunkstories.api.server.ServerInterface 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)

Example 2 with ServerInterface

use of io.xol.chunkstories.api.server.ServerInterface in project chunkstories by Hugobros3.

the class GiveCommand method handleCommand.

@Override
public boolean handleCommand(CommandEmitter emitter, Command command, String[] arguments) {
    if (!emitter.hasPermission("server.give")) {
        emitter.sendMessage("You don't have the permission.");
        return true;
    }
    if (!(emitter instanceof Player)) {
        emitter.sendMessage("You need to be a player to use this command.");
        return true;
    }
    Content gameContent = server.getContent();
    Player player = (Player) emitter;
    if (arguments.length == 0) {
        player.sendMessage("#FF969BSyntax : /give <item> [amount] [to]");
        return true;
    }
    int amount = 1;
    Player to = player;
    String itemName = arguments[0];
    // Look for the item first
    ItemDefinition type = gameContent.items().getItemDefinition(itemName);
    // If the type was found we are simply trying to spawn an item
    Item item = null;
    if (type != null)
        item = type.newItem();
    else {
        String voxelName = itemName;
        int voxelMeta = 0;
        if (voxelName.contains(":")) {
            voxelMeta = Integer.parseInt(voxelName.split(":")[1]);
            voxelName = voxelName.split(":")[0];
        }
        // Try to find a matching voxel
        Voxel voxel = gameContent.voxels().getVoxel(itemName);
        if (voxel != null) {
            // Spawn new itemPile in his inventory
            ItemVoxel itemVoxel = (ItemVoxel) gameContent.items().getItemDefinition("item_voxel").newItem();
            itemVoxel.voxel = voxel;
            itemVoxel.voxelMeta = voxelMeta;
            item = itemVoxel;
        }
    }
    if (item == null) {
        player.sendMessage("#FF969BItem or voxel \"" + arguments[0] + " can't be found.");
        return true;
    }
    if (arguments.length >= 2) {
        amount = Integer.parseInt(arguments[1]);
    }
    if (arguments.length >= 3) {
        if (gameContent instanceof ServerInterface)
            to = ((ServerInterface) gameContent).getPlayerByName(arguments[2]);
        else {
            player.sendMessage("#FF969BThis is a singleplayer world - there are no other players");
            return true;
        }
    }
    if (to == null) {
        player.sendMessage("#FF969BPlayer \"" + arguments[2] + " can't be found.");
        return true;
    }
    ItemPile itemPile = new ItemPile(item);
    itemPile.setAmount(amount);
    ((EntityWithInventory) to.getControlledEntity()).getInventory().addItemPile(itemPile);
    player.sendMessage("#FF969BGave " + (amount > 1 ? amount + "x " : "") + "#4CFF00" + itemPile.getItem().getName() + " #FF969Bto " + to.getDisplayName());
    return true;
}
Also used : Item(io.xol.chunkstories.api.item.Item) Player(io.xol.chunkstories.api.player.Player) ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) ServerInterface(io.xol.chunkstories.api.server.ServerInterface) Content(io.xol.chunkstories.api.content.Content) ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) Voxel(io.xol.chunkstories.api.voxel.Voxel) ItemDefinition(io.xol.chunkstories.api.item.ItemDefinition) ItemPile(io.xol.chunkstories.api.item.inventory.ItemPile)

Aggregations

Player (io.xol.chunkstories.api.player.Player)2 ServerInterface (io.xol.chunkstories.api.server.ServerInterface)2 GameContext (io.xol.chunkstories.api.GameContext)1 Content (io.xol.chunkstories.api.content.Content)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 Item (io.xol.chunkstories.api.item.Item)1 ItemDefinition (io.xol.chunkstories.api.item.ItemDefinition)1 ItemVoxel (io.xol.chunkstories.api.item.ItemVoxel)1 ItemPile (io.xol.chunkstories.api.item.inventory.ItemPile)1 Voxel (io.xol.chunkstories.api.voxel.Voxel)1 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)1 File (java.io.File)1