Example usage for com.google.common.io ByteArrayDataOutput writeShort

List of usage examples for com.google.common.io ByteArrayDataOutput writeShort

Introduction

In this page you can find the example usage for com.google.common.io ByteArrayDataOutput writeShort.

Prototype

@Override
    void writeShort(int v);

Source Link

Usage

From source file:garmintools.adapters.garmin.LandingFacilityDetailGarminAdapter.java

private void encode(LandingFacilityDetail detail, ByteArrayDataOutput output) {
    BitSet sectionsPresent = new BitSet(16);
    List<ByteArrayDataOutput> sections = new ArrayList<>();
    if (!detail.runways.isEmpty()) {
        sectionsPresent.set(0);/*  www. j av  a  2s. c o m*/
        ByteArrayDataOutput sectionOutput = new LittleEndianByteArrayDataOutput(ByteStreams.newDataOutput());
        sections.add(sectionOutput);
        encodeRunwayInfo(detail.runways, sectionOutput);
    }
    if (!detail.communicationFrequencies.isEmpty()) {
        sectionsPresent.set(1);
        ByteArrayDataOutput sectionOutput = new LittleEndianByteArrayDataOutput(ByteStreams.newDataOutput());
        sections.add(sectionOutput);
        encodeCommunicationInfo(detail.communicationFrequencies, sectionOutput);
    }

    // Temporary; pass through unknown sections.
    for (UnknownLandingFacilityDetailSection unknownSection : detail.protoLandingFacilityDetail
            .getUnknownSectionList()) {
        sectionsPresent.set(unknownSection.getSectionNumber());
        ByteArrayDataOutput sectionOutput = ByteStreams.newDataOutput();
        sectionOutput.write(unknownSection.getData().toByteArray());
        sections.add(sectionOutput);
    }

    output.writeShort(sectionsPresent.length() == 0 ? 0 : (short) sectionsPresent.toLongArray()[0]);
    // write lengths.
    for (ByteArrayDataOutput section : sections) {
        int sectionLength = section.toByteArray().length;
        output.writeShort(sectionLength);
    }
    // write contents.
    for (ByteArrayDataOutput section : sections) {
        output.write(section.toByteArray());
    }
}

From source file:de.minigameslib.mclib.impl.MclibPlugin.java

@Override
public void broadcastServers(CommunicationEndpointId id, DataSection... data) {
    if (data != null) {
        for (final DataSection section : data) {
            // TODO only works if both servers have online players
            // TODO if target servers do not have online players the messages should be queued by bungeecord.
            // TODO ensure that we are really within bungeecord environments, else this will be sent to clients (not good)
            final ByteArrayDataOutput out2 = ByteStreams.newDataOutput();
            final ByteArrayDataOutput out = ByteStreams.newDataOutput();
            out2.writeUTF("Forward"); //$NON-NLS-1$
            out2.writeUTF("ALL"); //$NON-NLS-1$
            out2.writeUTF(MCLIB_SERVER_TO_SERVER_CHANNEL);
            out.writeByte(0);/*  w w w  .ja  v  a  2s.  c  o m*/
            new NetMessage(id, section).toBytes(out);
            byte[] bytes = out.toByteArray();
            if (this.getLogger().isLoggable(Level.FINEST)) {
                this.getLogger().finest("Sending NetMessage to all servers\n" + Arrays.toString(bytes)); //$NON-NLS-1$
            }
            out2.writeShort(bytes.length);
            out2.write(bytes);
            final ObjectServiceInterface osi = ObjectServiceInterface.instance();
            final Optional<? extends Player> player = Bukkit.getOnlinePlayers().stream()
                    .filter(p -> !osi.isHuman(p)).findFirst();
            if (player.isPresent()) {
                // bungee cord ensures this is not send to the client.
                player.get().sendPluginMessage(this, BUNGEECORD_CHANNEL, bytes);
            } else {
                this.bungeeQueue.add(p -> p.sendPluginMessage(this, BUNGEECORD_CHANNEL, bytes));
            }
        }
    }
}

From source file:io.github.runelynx.runicparadise.RunicParadise.java

@SuppressWarnings("deprecation")
public void updatePlayerInfoOnJoin(String name, UUID pUUID) {
    final Date now = new Date();
    final String playerName = name;
    final UUID playerUUID = pUUID;

    MySQL MySQL = new MySQL(instance, instance.getConfig().getString("dbHost"),
            instance.getConfig().getString("dbPort"), instance.getConfig().getString("dbDatabase"),
            instance.getConfig().getString("dbUser"), instance.getConfig().getString("dbPassword"));
    final Connection dbConn = MySQL.openConnection();
    int rowCount = -1;
    int rowCountnameMatch = -1;
    String previousName = "";

    try {/*w  ww. ja  v a  2 s.  co  m*/
        PreparedStatement dStmt = dbConn
                .prepareStatement("SELECT COUNT(*) as Total, PlayerName FROM rp_PlayerInfo WHERE UUID = ?;");
        dStmt.setString(1, playerUUID.toString());
        ResultSet dbResult = dStmt.executeQuery();
        while (dbResult.next()) {
            rowCount = dbResult.getInt("Total");
            previousName = dbResult.getString("PlayerName");
        }
        dStmt.close();

        PreparedStatement zStmt = dbConn
                .prepareStatement("SELECT COUNT(*) as Total FROM rp_PlayerInfo WHERE PlayerName = ?;");
        zStmt.setString(1, playerName);
        ResultSet zResult = zStmt.executeQuery();
        while (zResult.next()) {
            rowCountnameMatch = zResult.getInt("Total");
        }
        zStmt.close();

    } catch (SQLException e) {
        getLogger().log(Level.SEVERE, "Cant check for row count in updatePlayerInfoOnJoin for " + playerName
                + " because: " + e.getMessage());
    }

    if (rowCount != rowCountnameMatch) {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                "sc Name change detected for " + playerName + " (" + previousName + ")");
        Bukkit.getLogger().log(Level.INFO,
                "[RP] Name change detected for " + playerName + " (" + previousName + ")");
    }

    try {

        // if this player has no rows in the table yet

        if (rowCount == 0) {

            Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(instance, new Runnable() {
                public void run() {
                    // tell the other server this one is reconnected
                    // to the universe
                    ByteArrayDataOutput out = ByteStreams.newDataOutput();
                    out.writeUTF("Forward"); // So BungeeCord knows
                    // to forward it
                    out.writeUTF("ONLINE");
                    out.writeUTF("NewPlayer"); // The channel name
                    // to check if this
                    // your data

                    ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
                    DataOutputStream msgout = new DataOutputStream(msgbytes);

                    try {
                        msgout.writeUTF(Bukkit.getPlayer(playerUUID).getDisplayName()); // You
                        // can
                        // do
                        // anything
                        // msgout
                        msgout.writeShort(123);
                    } catch (IOException e) {
                    }

                    out.writeShort(msgbytes.toByteArray().length);
                    out.write(msgbytes.toByteArray());

                    // If you don't care about the player
                    // Player player =
                    // Iterables.getFirst(Bukkit.getOnlinePlayers(),
                    // null);
                    // Else, specify them

                    Bukkit.getPlayer(playerUUID).sendPluginMessage(instance, "BungeeCord", out.toByteArray());
                }
            }, 140);

            // /////////////////////
            PreparedStatement dStmt = dbConn.prepareStatement(
                    "INSERT INTO rp_PlayerInfo (`PlayerName`, `UUID`, `ActiveFaith`, `LastIP`, `FirstSeen`, `LastSeen`) VALUES "
                            + "(?, ?, ?, ?, ?, ?);");
            dStmt.setString(1, playerName);
            dStmt.setString(2, playerUUID.toString());
            dStmt.setString(3, "Sun");
            dStmt.setString(4, Bukkit.getPlayer(playerUUID).getAddress().getAddress().getHostAddress());
            dStmt.setLong(5, now.getTime());
            dStmt.setLong(6, now.getTime());

            dStmt.executeUpdate();

            PreparedStatement pStmt = dbConn
                    .prepareStatement("INSERT INTO rp_PlayerMobKills (`UUID`) VALUES " + "(?);");
            pStmt.setString(1, playerUUID.toString());
            pStmt.executeUpdate();

            pStmt.close();
            dStmt.close();

            // if this player has 1 row in the table
        } else if (rowCount == 1) {
            PreparedStatement dStmt = dbConn.prepareStatement(
                    "UPDATE `rp_PlayerInfo` SET LastSeen=?, PlayerName=?, LastIP=? WHERE UUID=?;");
            dStmt.setLong(1, now.getTime());
            dStmt.setString(2, playerName);
            dStmt.setString(3, Bukkit.getPlayer(playerUUID).getAddress().getAddress().getHostAddress());
            dStmt.setString(4, playerUUID.toString());
            dStmt.executeUpdate();
            dStmt.close();
            Bukkit.getLogger().log(Level.INFO, "[RP] PlayerInfo data updated for " + playerName);

            // if this player has MORE than 1 row in the
            // table
        } else if (rowCount > 1) {
            int counter = 1;
            PreparedStatement zStmt = dbConn
                    .prepareStatement("SELECT * FROM rp_PlayerInfo WHERE UUID = ? ORDER BY ID ASC;");
            zStmt.setString(1, playerUUID.toString());
            ResultSet zResult = zStmt.executeQuery();
            while (zResult.next()) {
                // The first row is our valid one - update
                // it!
                if (counter == 1) {
                    PreparedStatement dStmt = dbConn.prepareStatement(
                            "UPDATE `rp_PlayerInfo` SET LastSeen=?, PlayerName=? WHERE UUID=?;");
                    dStmt.setLong(1, now.getTime());
                    dStmt.setString(2, playerName);
                    dStmt.setString(3, playerUUID.toString());
                    dStmt.executeUpdate();
                    dStmt.close();

                    Bukkit.getLogger().log(Level.INFO, "[RP] PlayerInfo data [row " + zResult.getInt("ID")
                            + "] updated for " + playerName);
                    // All further rows are invalid, delete
                    // them!
                } else if (counter > 1) {
                    PreparedStatement dStmt = dbConn
                            .prepareStatement("DELETE FROM `rp_PlayerInfo` WHERE ID = ? LIMIT 1;");
                    dStmt.setInt(1, zResult.getInt("ID"));
                    dStmt.executeUpdate();
                    dStmt.close();
                    Bukkit.getLogger().log(Level.INFO,
                            "[RP] PlayerInfo dupe row cleanup (name change?)! Deleted row "
                                    + zResult.getInt("ID"));
                }

                counter++;
            }
            zStmt.close();

        }

        dbConn.close();

    } catch (SQLException e) {
        getLogger().log(Level.SEVERE,
                "Cant work with DB updatePlayerInfoOnJoin for " + playerName + " because: " + e.getMessage());
    }

}