Search in sources :

Example 16 with Message

use of com.flowpowered.network.Message in project Glowstone by GlowstoneMC.

the class GlowEvokerFangs method createSpawnMessage.

@Override
public List<Message> createSpawnMessage() {
    List<Message> result = new LinkedList<>();
    double x = location.getX();
    double y = location.getY();
    double z = location.getZ();
    int yaw = Position.getIntYaw(location);
    int pitch = Position.getIntPitch(location);
    result.add(new SpawnObjectMessage(id, UUID.randomUUID(), 79, x, y, z, pitch, yaw, 0, 0, 0, 0));
    return result;
}
Also used : SpawnObjectMessage(net.glowstone.net.message.play.entity.SpawnObjectMessage) Message(com.flowpowered.network.Message) SpawnObjectMessage(net.glowstone.net.message.play.entity.SpawnObjectMessage) LinkedList(java.util.LinkedList)

Example 17 with Message

use of com.flowpowered.network.Message in project Glowstone by GlowstoneMC.

the class PlayerSwingArmHandler method handle.

@Override
public void handle(GlowSession session, PlayerSwingArmMessage message) {
    GlowPlayer player = session.getPlayer();
    Block block;
    try {
        block = player.getTargetBlock((Set<Material>) null, 6);
    } catch (IllegalStateException ex) {
        // getTargetBlock failed to find any block at all
        block = null;
    }
    if (block == null || block.isEmpty()) {
        if (EventFactory.onPlayerInteract(player, Action.LEFT_CLICK_AIR).useItemInHand() == Result.DENY)
            return;
    // todo: item interactions with air
    }
    if (!EventFactory.callEvent(new PlayerAnimationEvent(player)).isCancelled()) {
        // play the animation to others
        Message toSend = new AnimateEntityMessage(player.getEntityId(), message.getHand() == 1 ? AnimateEntityMessage.SWING_OFF_HAND : AnimateEntityMessage.SWING_MAIN_HAND);
        player.getWorld().getRawPlayers().stream().filter(observer -> observer != player && observer.canSeeEntity(player)).forEach(observer -> observer.getSession().send(toSend));
    }
}
Also used : EventFactory(net.glowstone.EventFactory) PlayerAnimationEvent(org.bukkit.event.player.PlayerAnimationEvent) Message(com.flowpowered.network.Message) Set(java.util.Set) GlowPlayer(net.glowstone.entity.GlowPlayer) Result(org.bukkit.event.Event.Result) Action(org.bukkit.event.block.Action) MessageHandler(com.flowpowered.network.MessageHandler) Block(org.bukkit.block.Block) GlowSession(net.glowstone.net.GlowSession) PlayerSwingArmMessage(net.glowstone.net.message.play.player.PlayerSwingArmMessage) Material(org.bukkit.Material) AnimateEntityMessage(net.glowstone.net.message.play.entity.AnimateEntityMessage) AnimateEntityMessage(net.glowstone.net.message.play.entity.AnimateEntityMessage) Set(java.util.Set) Message(com.flowpowered.network.Message) PlayerSwingArmMessage(net.glowstone.net.message.play.player.PlayerSwingArmMessage) AnimateEntityMessage(net.glowstone.net.message.play.entity.AnimateEntityMessage) GlowPlayer(net.glowstone.entity.GlowPlayer) Block(org.bukkit.block.Block) PlayerAnimationEvent(org.bukkit.event.player.PlayerAnimationEvent)

Example 18 with Message

use of com.flowpowered.network.Message in project Glowstone by GlowstoneMC.

the class BaseProtocolTest method checkCodec.

private void checkCodec(Codec.CodecRegistration reg, Message message) {
    // check a message with its codec
    try {
        Codec<Message> codec = reg.getCodec();
        ByteBuf buffer = codec.encode(Unpooled.buffer(), message);
        Message decoded = codec.decode(buffer);
        if (buffer.refCnt() > 0) {
            buffer.release(buffer.refCnt());
        }
        assertThat("Asymmetry for " + reg.getOpcode() + "/" + message.getClass().getName(), decoded, is(message));
    } catch (IOException e) {
        throw new AssertionError("Error in I/O for " + reg.getOpcode() + "/" + message.getClass().getName(), e);
    }
}
Also used : HeldItemMessage(net.glowstone.net.message.play.inv.HeldItemMessage) Message(com.flowpowered.network.Message) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Example 19 with Message

use of com.flowpowered.network.Message in project Glowstone by GlowstoneMC.

the class BaseProtocolTest method testProtocol.

@Test
public void testProtocol() throws Exception {
    Map<Class<? extends Message>, Codec.CodecRegistration> inboundMap = getField(inboundCodecs, CodecLookupService.class, "messages");
    Map<Class<? extends Message>, Codec.CodecRegistration> outboundMap = getField(outboundCodecs, CodecLookupService.class, "messages");
    Set<Class<? extends Message>> inboundSet = new HashSet<>(inboundMap.keySet());
    Set<Class<? extends Message>> outboundSet = new HashSet<>(outboundMap.keySet());
    for (Message message : testMessages) {
        boolean any = false;
        Class<? extends Message> clazz = message.getClass();
        // test inbound
        Codec.CodecRegistration registration = inboundCodecs.find(clazz);
        if (registration != null) {
            inboundSet.remove(clazz);
            checkCodec(registration, message);
            any = true;
        }
        // test outbound
        registration = outboundCodecs.find(clazz);
        if (registration != null) {
            outboundSet.remove(clazz);
            checkCodec(registration, message);
            any = true;
        }
        assertThat("Codec missing for: " + message, any, is(true));
    }
    // special case: HeldItemMessage is excluded from tests
    inboundSet.remove(HeldItemMessage.class);
    outboundSet.remove(HeldItemMessage.class);
    assertThat("Did not test inbound classes: " + inboundSet, inboundSet.isEmpty(), is(true));
    // todo: enable the outbound check for PlayProtocol
    if (!(protocol instanceof PlayProtocol)) {
        assertThat("Did not test outbound classes: " + outboundSet, outboundSet.isEmpty(), is(true));
    }
}
Also used : Codec(com.flowpowered.network.Codec) HeldItemMessage(net.glowstone.net.message.play.inv.HeldItemMessage) Message(com.flowpowered.network.Message) PlayProtocol(net.glowstone.net.protocol.PlayProtocol) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with Message

use of com.flowpowered.network.Message in project Glowstone by GlowstoneMC.

the class GlowSession method setPlayer.

/**
     * Sets the player associated with this session.
     *
     * @param profile The player's profile with name and UUID information.
     * @throws IllegalStateException if there is already a player associated
     *                               with this session.
     */
public void setPlayer(PlayerProfile profile) {
    if (player != null) {
        throw new IllegalStateException("Cannot set player twice");
    }
    // isActive check here in case player disconnected during authentication
    if (!isActive()) {
        // no need to call onDisconnect() since it only does anything if there's a player set
        return;
    }
    // initialize the player
    PlayerReader reader = server.getPlayerDataService().beginReadingData(profile.getUniqueId());
    player = new GlowPlayer(this, profile, reader);
    finalizeLogin(profile);
    // but before the GlowPlayer initialization was completed
    if (!isActive()) {
        reader.close();
        onDisconnect();
        return;
    }
    // Kick other players with the same UUID
    for (GlowPlayer other : getServer().getRawOnlinePlayers()) {
        if (other != player && other.getUniqueId().equals(player.getUniqueId())) {
            other.getSession().disconnect("You logged in from another location.", true);
            break;
        }
    }
    // login event
    PlayerLoginEvent event = EventFactory.onPlayerLogin(player, hostname);
    if (event.getResult() != Result.ALLOWED) {
        disconnect(event.getKickMessage(), true);
        return;
    }
    //joins the player
    player.join(this, reader);
    player.getWorld().getRawPlayers().add(player);
    online = true;
    GlowServer.logger.info(player.getName() + " [" + address + "] connected, UUID: " + player.getUniqueId());
    // message and user list
    String message = EventFactory.onPlayerJoin(player).getJoinMessage();
    if (message != null && !message.isEmpty()) {
        server.broadcastMessage(message);
    }
    Message addMessage = new UserListItemMessage(Action.ADD_PLAYER, player.getUserListEntry());
    List<Entry> entries = new ArrayList<>();
    for (GlowPlayer other : server.getRawOnlinePlayers()) {
        if (other != player && other.canSee(player)) {
            other.getSession().send(addMessage);
        }
        if (player.canSee(other)) {
            entries.add(other.getUserListEntry());
        }
    }
    send(new UserListItemMessage(Action.ADD_PLAYER, entries));
}
Also used : Entry(net.glowstone.net.message.play.game.UserListItemMessage.Entry) KickMessage(net.glowstone.net.message.KickMessage) Message(com.flowpowered.network.Message) PingMessage(net.glowstone.net.message.play.game.PingMessage) LoginSuccessMessage(net.glowstone.net.message.login.LoginSuccessMessage) DestroyEntitiesMessage(net.glowstone.net.message.play.entity.DestroyEntitiesMessage) SetCompressionMessage(net.glowstone.net.message.SetCompressionMessage) BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage) AsyncableMessage(com.flowpowered.network.AsyncableMessage) PlayerReader(net.glowstone.io.PlayerDataService.PlayerReader) GlowPlayer(net.glowstone.entity.GlowPlayer) PlayerLoginEvent(org.bukkit.event.player.PlayerLoginEvent) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage)

Aggregations

Message (com.flowpowered.network.Message)20 LinkedList (java.util.LinkedList)4 GlowPlayer (net.glowstone.entity.GlowPlayer)4 MetadataMap (net.glowstone.entity.meta.MetadataMap)4 EntityHeadRotationMessage (net.glowstone.net.message.play.entity.EntityHeadRotationMessage)4 EntityMetadataMessage (net.glowstone.net.message.play.entity.EntityMetadataMessage)3 AsyncableMessage (com.flowpowered.network.AsyncableMessage)2 IOException (java.io.IOException)2 GlowBlock (net.glowstone.block.GlowBlock)2 Key (net.glowstone.chunk.GlowChunk.Key)2 SpawnObjectMessage (net.glowstone.net.message.play.entity.SpawnObjectMessage)2 HeldItemMessage (net.glowstone.net.message.play.inv.HeldItemMessage)2 ItemStack (org.bukkit.inventory.ItemStack)2 Vector (org.bukkit.util.Vector)2 Codec (com.flowpowered.network.Codec)1 MessageHandler (com.flowpowered.network.MessageHandler)1 ByteBuf (io.netty.buffer.ByteBuf)1 File (java.io.File)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1