Search in sources :

Example 1 with ServerPlayerPacketsProcessor

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

the class PacketInput method process.

public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
    long code = in.readLong();
    boolean pressed = in.readBoolean();
    if (processor instanceof ServerPlayerPacketsProcessor) {
        ServerPlayerPacketsProcessor sppc = (ServerPlayerPacketsProcessor) processor;
        // Look for the controller handling this buisness
        EntityControllable entity = (EntityControllable) sppc.getPlayer().getControlledEntity();
        if (entity != null) {
            // Get input of the client
            input = sppc.getPlayer().getInputsManager().getInputFromHash(code);
            if (input == null)
                throw new NullPointerException("Unknown input hash : " + code);
            // System.out.println(processor.getServerClient().getProfile() +
            // " "+input + " "+pressed);
            // Update it's state
            ((InputVirtual) input).setPressed(pressed);
            // Fire appropriate event
            if (pressed) {
                PlayerInputPressedEvent event = new PlayerInputPressedEvent(sppc.getPlayer(), input);
                entity.getWorld().getGameLogic().getPluginsManager().fireEvent(event);
                if (!event.isCancelled())
                    entity.onControllerInput(input, entity.getControllerComponent().getController());
            } else {
                PlayerInputReleasedEvent event = new PlayerInputReleasedEvent(sppc.getPlayer(), input);
                entity.getWorld().getGameLogic().getPluginsManager().fireEvent(event);
            }
        // TODO why is this disabled and still there ?
        // If we pressed the input, apply game logic
        // if(pressed)
        // entity.handleInteraction(input,
        // entity.getControllerComponent().getController());
        }
    }
}
Also used : PlayerInputReleasedEvent(io.xol.chunkstories.api.events.player.PlayerInputReleasedEvent) ServerPlayerPacketsProcessor(io.xol.chunkstories.api.server.ServerPacketsProcessor.ServerPlayerPacketsProcessor) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) PlayerInputPressedEvent(io.xol.chunkstories.api.events.player.PlayerInputPressedEvent) InputVirtual(io.xol.chunkstories.input.InputVirtual)

Example 2 with ServerPlayerPacketsProcessor

use of io.xol.chunkstories.api.server.ServerPacketsProcessor.ServerPlayerPacketsProcessor in project chunkstories-api by Hugobros3.

the class PacketInventoryMoveItemPile method process.

public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
    if (!(processor instanceof ServerPlayerPacketsProcessor)) {
        processor.logger().warn("Received a " + this.getClass().getSimpleName() + " but this GameContext isn't providen with a packet processor made to deal with it");
        return;
    }
    ServerPlayerPacketsProcessor sppc = (ServerPlayerPacketsProcessor) processor;
    Player player = sppc.getPlayer();
    EntityControllable playerEntity = player.getControlledEntity();
    oldX = in.readInt();
    oldY = in.readInt();
    newX = in.readInt();
    newY = in.readInt();
    amount = in.readInt();
    from = InventoryTranslator.obtainInventoryHandle(in, processor);
    to = InventoryTranslator.obtainInventoryHandle(in, processor);
    // If this pile is spawned from the void
    if (// || from == InventoryTranslator.INVENTORY_CREATIVE_TRASH)
    from == null) {
        try {
            itemPile = ItemPile.obtainItemPileFromStream(player.getWorld().getContentTranslator(), in);
        } catch (NullItemException e) {
            // This ... isn't supposed to happen
            processor.logger().info("User " + sender + " is trying to spawn a null ItemPile for some reason.");
        } catch (UndefinedItemTypeException e) {
            // This is slightly more problematic
            processor.logger().warn(e.getMessage());
        // e.printStackTrace(processor.getLogger().getPrintWriter());
        }
    } else {
        itemPile = from.getItemPileAt(oldX, oldY);
    }
    // Check access
    if (to != null && playerEntity != null) {
        if (!to.isAccessibleTo(playerEntity)) {
            player.sendMessage("You don't have access to this.");
            return;
        }
    }
    // Check using event
    PlayerMoveItemEvent moveItemEvent = new PlayerMoveItemEvent(player, itemPile, from, to, oldX, oldY, newX, newY, amount);
    player.getContext().getPluginManager().fireEvent(moveItemEvent);
    if (!moveItemEvent.isCancelled()) {
        // Restrict item spawning
        if (// || from instanceof InventoryLocalCreativeMenu)
        from == null) {
            if (player.hasPermission("items.spawn") || (player.getControlledEntity() != null && player.getControlledEntity() instanceof EntityCreative && ((EntityCreative) player.getControlledEntity()).getCreativeModeComponent().get())) {
            // Let it happen when in creative mode or owns items.spawn perm
            } else {
                player.sendMessage("#C00000You are neither in creative mode nor have the items.spawn permission.");
                return;
            }
        }
        // If target inventory is null, this means the item was dropped
        if (to == null) {
            if (playerEntity == null) {
                System.out.println("Dropping items isn't possible if the player doesn't control any entity.");
                return;
            }
            // If we're pulling this out of an inventory ( and not /dev/null ), we need to remove it from that
            Inventory sourceInventory = itemPile.getInventory();
            Location loc = playerEntity.getLocation();
            EventItemDroppedToWorld dropItemEvent = new EventItemDroppedToWorld(loc, sourceInventory, itemPile);
            player.getContext().getPluginManager().fireEvent(dropItemEvent);
            if (!dropItemEvent.isCancelled()) {
                if (sourceInventory != null)
                    sourceInventory.setItemPileAt(itemPile.getX(), itemPile.getY(), null);
                if (dropItemEvent.getItemEntity() != null)
                    loc.getWorld().addEntity(dropItemEvent.getItemEntity());
            }
            return;
        }
        itemPile.moveItemPileTo(to, newX, newY, amount);
    }
}
Also used : UndefinedItemTypeException(io.xol.chunkstories.api.exceptions.UndefinedItemTypeException) Player(io.xol.chunkstories.api.player.Player) EventItemDroppedToWorld(io.xol.chunkstories.api.events.item.EventItemDroppedToWorld) PlayerMoveItemEvent(io.xol.chunkstories.api.events.player.PlayerMoveItemEvent) EntityCreative(io.xol.chunkstories.api.entity.interfaces.EntityCreative) ServerPlayerPacketsProcessor(io.xol.chunkstories.api.server.ServerPacketsProcessor.ServerPlayerPacketsProcessor) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) NullItemException(io.xol.chunkstories.api.exceptions.NullItemException) Inventory(io.xol.chunkstories.api.item.inventory.Inventory) Location(io.xol.chunkstories.api.Location)

Aggregations

EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)2 ServerPlayerPacketsProcessor (io.xol.chunkstories.api.server.ServerPacketsProcessor.ServerPlayerPacketsProcessor)2 Location (io.xol.chunkstories.api.Location)1 EntityCreative (io.xol.chunkstories.api.entity.interfaces.EntityCreative)1 EventItemDroppedToWorld (io.xol.chunkstories.api.events.item.EventItemDroppedToWorld)1 PlayerInputPressedEvent (io.xol.chunkstories.api.events.player.PlayerInputPressedEvent)1 PlayerInputReleasedEvent (io.xol.chunkstories.api.events.player.PlayerInputReleasedEvent)1 PlayerMoveItemEvent (io.xol.chunkstories.api.events.player.PlayerMoveItemEvent)1 NullItemException (io.xol.chunkstories.api.exceptions.NullItemException)1 UndefinedItemTypeException (io.xol.chunkstories.api.exceptions.UndefinedItemTypeException)1 Inventory (io.xol.chunkstories.api.item.inventory.Inventory)1 Player (io.xol.chunkstories.api.player.Player)1 InputVirtual (io.xol.chunkstories.input.InputVirtual)1