Search in sources :

Example 1 with Message

use of com.flowpowered.networking.Message in project Dragonet-Legacy by DragonetMC.

the class DragonetSession 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.
 */
@Override
public void setPlayer(PlayerProfile profile) {
    if (this.getPlayer() != null) {
        throw new IllegalStateException("Cannot set player twice");
    }
    // initialize the player
    PlayerDataService.PlayerReader reader = this.getServer().getPlayerDataService().beginReadingData(profile.getUniqueId());
    // this.player = new GlowPlayer(this, profile, reader);
    this.player = new DragonetPlayer(this, profile, reader);
    // login event
    PlayerLoginEvent event = EventFactory.onPlayerLogin(player, this.getHostname());
    if (event.getResult() != PlayerLoginEvent.Result.ALLOWED) {
        disconnect(event.getKickMessage(), true);
        return;
    }
    // Dragonet-Add: or same username
    for (GlowPlayer other : getServer().getOnlinePlayers()) {
        if ((other != player && other.getUniqueId().equals(player.getUniqueId())) || other.getName().equalsIgnoreCase(player.getName())) {
            other.getSession().disconnect("You logged in from another location.", true);
            break;
        }
    }
    player.join(this, reader);
    WindowItemsPacket creativeInv = null;
    // Send the StartGamePacket
    StartGamePacket pkStartGame = new StartGamePacket();
    pkStartGame.seed = 0;
    pkStartGame.generator = 1;
    if (this.player.getGameMode().equals(GameMode.CREATIVE)) {
        pkStartGame.gamemode = 1;
        creativeInv = WindowItemsPacket.CREATIVE_INVENTORY;
    } else if (this.player.getGameMode().equals(GameMode.SURVIVAL) && this.player.getGameMode().equals(GameMode.ADVENTURE)) {
        pkStartGame.gamemode = 0;
    } else {
        creativeInv = new WindowItemsPacket();
        creativeInv.windowID = PEWindowConstantID.PLAYER_CREATIVE;
    }
    pkStartGame.eid = (long) this.player.getEntityId();
    pkStartGame.spawnX = this.player.getWorld().getSpawnLocation().getBlockX();
    pkStartGame.spawnY = this.player.getWorld().getSpawnLocation().getBlockY();
    pkStartGame.spawnZ = this.player.getWorld().getSpawnLocation().getBlockZ();
    pkStartGame.x = (float) this.player.getLocation().getX();
    pkStartGame.y = (float) this.player.getLocation().getY();
    pkStartGame.z = (float) this.player.getLocation().getZ();
    this.send(pkStartGame);
    if (creativeInv != null) {
        this.send(creativeInv);
    }
    // Send crafting recipie list
    sendRecipies();
    // Send Time
    SetTimePacket pkTime = new SetTimePacket((int) (this.getPlayer().getWorld().getTime() & 0xFFFFFFFF), false);
    this.send(pkTime);
    // Send Spawn Position
    SetSpawnPositionPacket pkSpawnPos = new SetSpawnPositionPacket();
    pkSpawnPos.x = this.player.getLocation().getBlockX();
    pkSpawnPos.y = this.player.getLocation().getBlockY();
    pkSpawnPos.z = this.player.getLocation().getBlockZ();
    this.send(pkSpawnPos);
    // Send Health
    SetHealthPacket pkHealth = new SetHealthPacket((int) Math.floor(this.getPlayer().getHealth()));
    this.send(pkHealth);
    // Send Difficulty Packet
    SetDifficultyPacket pkDifficulty = new SetDifficultyPacket();
    pkDifficulty.difficulty = this.getServer().getDifficulty().getValue();
    this.send(pkDifficulty);
    player.getWorld().getRawPlayers().add(player);
    GlowServer.logger.log(Level.INFO, "{0} [{1}] connected from Minecraft PE, UUID: {2}", new Object[] { player.getName(), this.getAddress(), player.getUniqueId() });
    // Preprare chunks
    this.chunkManager.prepareLoginChunks();
    // message and user list
    String message = EventFactory.onPlayerJoin(player).getJoinMessage();
    if (message != null && !message.isEmpty()) {
        this.getServer().broadcastMessage(message);
    }
    // todo: display names are included in the outgoing messages here, but
    // don't show up on the client. A workaround or proper fix is needed.
    Message addMessage = new UserListItemMessage(UserListItemMessage.Action.ADD_PLAYER, player.getUserListEntry());
    List<UserListItemMessage.Entry> entries = new ArrayList<>();
    for (GlowPlayer other : this.getServer().getOnlinePlayers()) {
        if (other != player && other.canSee(player)) {
            other.getSession().send(addMessage);
        }
        if (player.canSee(other)) {
            entries.add(other.getUserListEntry());
        }
    }
    send(new UserListItemMessage(UserListItemMessage.Action.ADD_PLAYER, entries));
    RakNetInterface.sendName();
}
Also used : DragonetPlayer(org.dragonet.entity.DragonetPlayer) Message(com.flowpowered.networking.Message) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage) GlowPlayer(net.glowstone.entity.GlowPlayer) SetHealthPacket(org.dragonet.net.packet.minecraft.SetHealthPacket) UserListItemMessage(net.glowstone.net.message.play.game.UserListItemMessage) WindowItemsPacket(org.dragonet.net.packet.minecraft.WindowItemsPacket) ArrayList(java.util.ArrayList) StartGamePacket(org.dragonet.net.packet.minecraft.StartGamePacket) SetTimePacket(org.dragonet.net.packet.minecraft.SetTimePacket) PlayerDataService(net.glowstone.io.PlayerDataService) SetDifficultyPacket(org.dragonet.net.packet.minecraft.SetDifficultyPacket) SetSpawnPositionPacket(org.dragonet.net.packet.minecraft.SetSpawnPositionPacket) PlayerLoginEvent(org.bukkit.event.player.PlayerLoginEvent)

Example 2 with Message

use of com.flowpowered.networking.Message in project Dragonet-Legacy by DragonetMC.

the class MovePlayerPacketTranslator method handleSpecific.

@Override
public Message[] handleSpecific(MovePlayerPacket packet) {
    Location loc = new Location(this.getSession().getPlayer().getWorld(), packet.x, packet.y, packet.z);
    if (!this.getSession().validateMovement(loc)) {
        // Revert
        this.getSession().sendPosition();
        System.out.println("Reverted movement! ");
        return null;
    }
    // Hack ;P
    ((DragonetPlayer) this.getSession().getPlayer()).setLocation(new Location(((DragonetPlayer) this.getSession().getPlayer()).getWorld(), packet.x, packet.y, packet.z, packet.yaw, packet.pitch));
    return new Message[] { new PlayerPositionLookMessage(false, (double) packet.x, (double) packet.y, (double) packet.z, packet.yaw, packet.pitch) };
}
Also used : DragonetPlayer(org.dragonet.entity.DragonetPlayer) PlayerPositionLookMessage(net.glowstone.net.message.play.player.PlayerPositionLookMessage) Message(com.flowpowered.networking.Message) PlayerPositionLookMessage(net.glowstone.net.message.play.player.PlayerPositionLookMessage) Location(org.bukkit.Location)

Example 3 with Message

use of com.flowpowered.networking.Message in project Dragonet-Legacy by DragonetMC.

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());
        }
        assertEquals("Asymmetry for " + reg.getOpcode() + "/" + message.getClass().getName(), message, decoded);
    } 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.networking.Message) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Example 4 with Message

use of com.flowpowered.networking.Message in project Dragonet-Legacy by DragonetMC.

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;
        }
        assertTrue("Codec missing for: " + message, any);
    }
    // special case: HeldItemMessage is excluded from tests
    inboundSet.remove(HeldItemMessage.class);
    outboundSet.remove(HeldItemMessage.class);
    assertTrue("Did not test inbound classes: " + inboundSet, inboundSet.isEmpty());
    // todo: enable the outbound check for PlayProtocol
    if (!(protocol instanceof PlayProtocol)) {
        assertTrue("Did not test outbound classes: " + outboundSet, outboundSet.isEmpty());
    }
}
Also used : Codec(com.flowpowered.networking.Codec) HeldItemMessage(net.glowstone.net.message.play.inv.HeldItemMessage) Message(com.flowpowered.networking.Message) PlayProtocol(net.glowstone.net.protocol.PlayProtocol) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with Message

use of com.flowpowered.networking.Message in project Dragonet-Legacy by DragonetMC.

the class UseItemPacketTranslator method handleSpecific.

@Override
public Message[] handleSpecific(UseItemPacket packet) {
    UseItemPacket pkUseItem = (UseItemPacket) packet;
    System.out.println("FACE=" + (pkUseItem.face & 0xFF) + ", ITEM=" + packet.item);
    if (pkUseItem.face == 0xFF) {
        // Air touch
        // Left click air
        PlayerSwingArmMessage msg = new PlayerSwingArmMessage();
        return new Message[] { msg };
    }
    if (!(pkUseItem.face >= 0 && pkUseItem.face < 6)) {
        return null;
    }
    // Check the slot
    ItemStack test_holding = this.getSession().getPlayer().getInventory().getItemInHand();
    if (packet.item.id != this.getTranslator().getItemTranslator().translateToPE(test_holding.getTypeId()) || packet.item.meta != test_holding.getDurability()) {
        // Not same, resend slot
        PlayerEquipmentPacket pkRet = new PlayerEquipmentPacket();
        pkRet.eid = this.getSession().getPlayer().getEntityId();
        pkRet.item = new PEInventorySlot((short) 0, (byte) 0, (short) 0);
        pkRet.item.id = (short) (this.getTranslator().getItemTranslator().translateToPE(test_holding.getTypeId()) & 0xFFFF);
        pkRet.item.count = (byte) (test_holding.getAmount() & 0xFF);
        pkRet.item.meta = test_holding.getDurability();
        pkRet.selectedSlot = this.getSession().getPlayer().getInventory().getHeldItemSlot();
        // Resend block
        UpdateBlockPacket pkUpdateBlock = new UpdateBlockPacket();
        UpdateBlockPacket.UpdateBlockRecord blockRec = new UpdateBlockPacket.UpdateBlockRecord();
        blockRec.x = packet.x;
        blockRec.z = packet.z;
        blockRec.y = (byte) (packet.y & 0xFF);
        blockRec.block = (byte) (this.getSession().getPlayer().getWorld().getBlockAt(pkUseItem.x, pkUseItem.y, pkUseItem.z).getTypeId() & 0xFF);
        blockRec.meta = (byte) (this.getSession().getPlayer().getWorld().getBlockAt(pkUseItem.x, pkUseItem.y, pkUseItem.z).getData() & 0xFF);
        pkUpdateBlock.records = new UpdateBlockPacket.UpdateBlockRecord[] { blockRec };
        getSession().send(pkRet);
        getSession().send(pkUpdateBlock);
        return null;
    }
    // Copied from Glowstone class BlockPlacementHandler
    new BlockPlacementHandler().handle(getSession(), new BlockPlacementMessage(packet.x, packet.y, packet.z, packet.face, test_holding, 0, 0, 0));
    return null;
}
Also used : BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) UseItemPacket(org.dragonet.net.packet.minecraft.UseItemPacket) Message(com.flowpowered.networking.Message) PlayerSwingArmMessage(net.glowstone.net.message.play.player.PlayerSwingArmMessage) BlockPlacementMessage(net.glowstone.net.message.play.player.BlockPlacementMessage) ItemStack(org.bukkit.inventory.ItemStack) PlayerSwingArmMessage(net.glowstone.net.message.play.player.PlayerSwingArmMessage) PlayerEquipmentPacket(org.dragonet.net.packet.minecraft.PlayerEquipmentPacket) BlockPlacementHandler(net.glowstone.net.handler.play.player.BlockPlacementHandler) PEInventorySlot(org.dragonet.inventory.PEInventorySlot) UpdateBlockPacket(org.dragonet.net.packet.minecraft.UpdateBlockPacket)

Aggregations

Message (com.flowpowered.networking.Message)7 GlowPlayer (net.glowstone.entity.GlowPlayer)2 HeldItemMessage (net.glowstone.net.message.play.inv.HeldItemMessage)2 DiggingMessage (net.glowstone.net.message.play.player.DiggingMessage)2 DragonetPlayer (org.dragonet.entity.DragonetPlayer)2 Codec (com.flowpowered.networking.Codec)1 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 GlowWorld (net.glowstone.GlowWorld)1 GlowBlock (net.glowstone.block.GlowBlock)1 PlayerDataService (net.glowstone.io.PlayerDataService)1 BlockPlacementHandler (net.glowstone.net.handler.play.player.BlockPlacementHandler)1 UserListItemMessage (net.glowstone.net.message.play.game.UserListItemMessage)1 BlockPlacementMessage (net.glowstone.net.message.play.player.BlockPlacementMessage)1 PlayerPositionLookMessage (net.glowstone.net.message.play.player.PlayerPositionLookMessage)1 PlayerSwingArmMessage (net.glowstone.net.message.play.player.PlayerSwingArmMessage)1 PlayProtocol (net.glowstone.net.protocol.PlayProtocol)1 Location (org.bukkit.Location)1