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

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

Introduction

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

Prototype

@Override
    void writeInt(int v);

Source Link

Usage

From source file:de.paleocrafter.pmfw.tileentity.TileEntityWithData.java

private void writePacketFromClass(ByteArrayDataOutput out, Class<?> clazz, int iteration) {
    int fieldCount = 0;
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);/*from   w ww .ja  v  a  2 s.  c o m*/
        Annotation anno = field.getAnnotation(TileData.class);
        if (anno != null)
            fieldCount++;
    }
    out.writeInt(fieldCount);
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        Annotation anno = field.getAnnotation(TileData.class);
        if (anno != null) {
            if (((TileData) anno).storeToPacket()) {
                try {
                    String className = null;
                    className = field.getType().getSimpleName();
                    className = Character.toUpperCase(className.charAt(0)) + className.substring(1);
                    if (field.getType().isAssignableFrom(String.class))
                        className = "UTF";

                    if (className != null) {
                        Object value = field.get(this);
                        Class<?> type = field.getType();
                        if (Primitives.wrap(type) != null || value instanceof String
                                || value instanceof IDataObject) {
                            if (value instanceof Byte || value.getClass() == byte.class) {
                                type = int.class;
                                value = (byte) value;
                            }
                            if (value instanceof IDataObject) {
                                out.writeUTF(field.getName());
                                ((IDataObject) value).writeToPacket(out);
                            } else {
                                Method method = out.getClass().getMethod("write" + className, type);
                                out.writeUTF(field.getName());
                                method.setAccessible(true);
                                method.invoke(out, value);
                            }
                        }
                    }

                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:codecrafter47.bungeetablistplus.bridge.BukkitBridge.java

@EventHandler
public void onPluginMessage(PluginMessageEvent event) {
    if (event.getReceiver() instanceof ProxiedPlayer && event.getSender() instanceof Server) {

        ProxiedPlayer player = (ProxiedPlayer) event.getReceiver();
        Server server = (Server) event.getSender();

        if (event.getTag().equals(BridgeProtocolConstants.CHANNEL)) {
            event.setCancelled(true);//from   w ww.  j  av  a 2s . c om

            ConnectedPlayer connectedPlayer = plugin.getConnectedPlayerManager().getPlayerIfPresent(player);
            if (connectedPlayer != null) {

                PlayerBridgeDataCache cache = connectedPlayer.getBridgeDataCache();

                DataInput input = new DataInputStream(new ByteArrayInputStream(event.getData()));

                try {
                    int messageId = input.readUnsignedByte();

                    switch (messageId) {
                    case BridgeProtocolConstants.MESSAGE_ID_SERVER_HANDSHAKE:

                        if (cache.connection != null) {
                            disableConnection(cache);
                        }
                        cache.connection = server;
                        getServerDataCache(server.getInfo().getName()).addConnection(server);
                        ArrayList<DataKey<?>> keys = Lists.newArrayList(cache.getQueriedKeys());

                        ByteArrayDataOutput data = ByteStreams.newDataOutput();
                        data.writeByte(BridgeProtocolConstants.MESSAGE_ID_PROXY_REQUEST_DATA);
                        data.writeInt(keys.size());
                        for (DataKey<?> key : keys) {
                            DataStreamUtils.writeDataKey(data, key);
                            data.writeInt(idMap.getNetId(key));
                        }
                        server.sendData(BridgeProtocolConstants.CHANNEL, data.toByteArray());
                        break;
                    case BridgeProtocolConstants.MESSAGE_ID_SERVER_UPDATE_DATA:

                        if (cache.connection == server) {
                            onDataReceived(cache, input, input.readInt());
                        }

                        break;
                    case BridgeProtocolConstants.MESSAGE_ID_SERVER_UPDATE_SERVER_DATA:

                        if (cache.connection == server) {
                            ServerBridgeDataCache serverDataCache = getServerDataCache(
                                    server.getInfo().getName());
                            int revision = input.readInt();
                            int size = input.readInt();
                            if (size > 0 && revision == serverDataCache.lastRevision + 1) {
                                onDataReceived(serverDataCache, input, size);
                                serverDataCache.lastRevision = revision;
                            } else if (size > 0 || revision > serverDataCache.lastRevision) {
                                Server connection = serverDataCache.getConnection();
                                if (connection != null) {
                                    ByteArrayDataOutput output = ByteStreams.newDataOutput();
                                    output.writeByte(
                                            BridgeProtocolConstants.MESSAGE_ID_PROXY_REQUEST_RESET_SERVER_DATA);
                                    connection.sendData(BridgeProtocolConstants.CHANNEL, output.toByteArray());
                                    serverDataCache.lastRevision = 0;
                                }
                            }
                        }

                        break;
                    case BridgeProtocolConstants.MESSAGE_ID_SERVER_DISABLE_CONNECTION:

                        disableConnection(cache);
                        serverInformation.get(server.getInfo().getName()).reset();
                        break;
                    case BridgeProtocolConstants.MESSAGE_ID_SERVER_ENABLE_CONNECTION:

                        UUID serverId = DataStreamUtils.readUUID(input);
                        ServerBridgeDataCache serverData = getServerDataCache(server.getInfo().getName());
                        if (!serverData.serverId.equals(serverId)) {
                            serverData.serverId = serverId;
                            serverData.reset();
                        }

                        initializeHandshake(server);
                        break;
                    case BridgeProtocolConstants.MESSAGE_ID_SERVER_OUTDATED:

                        rlExecutor.execute(() -> plugin.getLogger().warning(
                                "Bridge plugin on server " + server.getInfo().getName() + " is outdated."));
                        break;
                    }
                } catch (IOException ex) {
                    plugin.getLogger().log(Level.SEVERE,
                            "An unexpected error occurred while processing bridge data.", ex);
                }
            }
        }
    }
}

From source file:net.minecraftforge.gradle.patcher.TaskGenBinPatches.java

private void createBinPatches(HashMap<String, byte[]> patches, String root, File base, File target)
        throws Exception {
    JarFile cleanJ = new JarFile(base);
    JarFile dirtyJ = new JarFile(target);

    for (Map.Entry<String, String> entry : obfMapping.entrySet()) {
        String obf = entry.getKey();
        String srg = entry.getValue();

        if (!patchedFiles.contains(obf)) // Not in the list of patch files.. we didn't edit it.
        {/* w ww  .  j a  va  2  s  .  co m*/
            continue;
        }

        JarEntry cleanE = cleanJ.getJarEntry(obf + ".class");
        JarEntry dirtyE = dirtyJ.getJarEntry(obf + ".class");

        if (dirtyE == null) //Something odd happened.. a base MC class wasn't in the obfed jar?
        {
            continue;
        }

        byte[] clean = (cleanE != null ? ByteStreams.toByteArray(cleanJ.getInputStream(cleanE)) : new byte[0]);
        byte[] dirty = ByteStreams.toByteArray(dirtyJ.getInputStream(dirtyE));

        byte[] diff = delta.compute(clean, dirty);

        ByteArrayDataOutput out = ByteStreams.newDataOutput(diff.length + 50);
        out.writeUTF(obf); // Clean name
        out.writeUTF(obf.replace('/', '.')); // Source Notch name
        out.writeUTF(srg.replace('/', '.')); // Source SRG Name
        out.writeBoolean(cleanE != null); // Exists in Clean
        if (cleanE != null) {
            out.writeInt(adlerHash(clean)); // Hash of Clean file
        }
        out.writeInt(diff.length); // Patch length
        out.write(diff); // Patch

        patches.put(root + srg.replace('/', '.') + ".binpatch", out.toByteArray());
    }

    cleanJ.close();
    dirtyJ.close();
}

From source file:arrowsplus.entity.EntityArrowBase.java

@Override
public void writeSpawnData(ByteArrayDataOutput data) {
    data.writeInt(arrowType);
    data.writeInt(bowType);
}

From source file:smpships.entity.EntityShipBlock.java

@Override
protected void writeControlBlockSpawnData(ByteArrayDataOutput data) { // runs on server

    data.writeUTF(name);/*from w  w  w  .jav a2  s  .c o  m*/
    data.writeUTF(captain);

    // write mates
    data.writeInt(mates.size());
    for (String mate : mates)
        data.writeUTF(mate);

    data.writeFloat(draft);

    data.writeInt(children.size());
    for (int i = 0; i < children.size(); i++) { // write child data

        data.writeInt(children.get(i).xOff);
        data.writeInt(children.get(i).yOff);
        data.writeInt(children.get(i).zOff);
        data.writeInt(children.get(i).blockID);
        data.writeInt(children.get(i).metadata);

    }

}

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

private void encodeRunwayInfo(List<Runway> runways, ByteArrayDataOutput output) {
    for (Runway runway : runways) {
        Proto.Runway proto = runway.protoRunway;

        int data = ((runway.runwayNumberSuffix.getIndex() & 7) << 29) | ((proto.getRunwayNumber() & 0x1f) << 24)
                | ((proto.getUnknown(0) & 1) << 23) | ((runway.lighting.getIndex() & 7) << 20)
                | ((runway.surface.getIndex() & 0xf) << 16) | ((proto.getRunwayLengthFeet() & 0xffff));
        output.writeInt(data);

        data = ((proto.getRunwayWidthFeet() & 0x7ff) << 13)
                | ((proto.hasUnknownAdditionalInfo1() ? 1 : 0) << 12)
                | ((proto.hasUnknownAdditionalInfo2() ? 1 : 0) << 11)
                | ((proto.hasUnknownAdditionalInfo3() ? 1 : 0) << 10) | ((proto.getUnknown(1) & 1) << 9)
                | ((proto.getUnknown(2) & 1) << 8) | ((proto.getUnknown(3) & 0xff));
        output.writeShort(data & 0xffff);
        output.write((data >> 16) & 0xff);

        if (proto.hasUnknownAdditionalInfo1()) {
            output.write(proto.getUnknownAdditionalInfo1().toByteArray());
        }//w  w w  .ja  va2s  .c  om
        if (proto.hasUnknownAdditionalInfo2()) {
            output.write(proto.getUnknownAdditionalInfo2().toByteArray());
        }
        if (proto.hasUnknownAdditionalInfo3()) {
            output.write(proto.getUnknownAdditionalInfo3().toByteArray());
        }
    }
}

From source file:shadowmage.ancient_warfare.common.npcs.NpcBase.java

@Override
public void writeSpawnData(ByteArrayDataOutput data) {
    data.writeInt(teamNum);
    data.writeInt(rank);//w w w.  ja  v a  2  s  .  com
    data.writeInt(this.npcType.getGlobalNpcType());
}

From source file:shadowmage.ancient_warfare.common.vehicles.VehicleBase.java

@Override
public void writeSpawnData(ByteArrayDataOutput data) {
    data.writeFloat(this.getHealth());
    data.writeInt(this.vehicleType.getGlobalVehicleType());
    data.writeInt(this.vehicleMaterialLevel);
    ByteTools.writeNBTTagCompound(upgradeHelper.getNBTTag(), data);
    ByteTools.writeNBTTagCompound(ammoHelper.getNBTTag(), data);
    ByteTools.writeNBTTagCompound(moveHelper.getNBTTag(), data);
    ByteTools.writeNBTTagCompound(firingHelper.getNBTTag(), data);
    ByteTools.writeNBTTagCompound(firingVarsHelper.getNBTTag(), data);
    data.writeFloat(localLaunchPower);/*from ww w . ja  va 2s .c o  m*/
    data.writeFloat(localTurretPitch);
    data.writeFloat(localTurretRotation);
    data.writeFloat(localTurretDestPitch);
    data.writeFloat(localTurretDestRot);
    data.writeInt(teamNum);
    data.writeFloat(localTurretRotationHome);
    data.writeBoolean(this.isSettingUp);
    if (this.isSettingUp) {
        data.writeInt(this.setupTicks);
    }
}

From source file:net.portalblock.rbbridge.MessageChannelListener.java

@EventHandler
public void onPluginMessage(final PluginMessageEvent event) {
    if (event.getTag().equals("RedisBungee") && event.getSender() instanceof Server) {
        final byte[] data = Arrays.copyOf(event.getData(), event.getData().length);
        plugin.getProxy().getScheduler().runAsync(plugin, new Runnable() {
            @Override/*from w w w .  j  a  va2s  .c  o m*/
            public void run() {
                ByteArrayDataInput in = ByteStreams.newDataInput(data);

                String subchannel = in.readUTF();
                ByteArrayDataOutput out = ByteStreams.newDataOutput();
                String type;

                switch (subchannel) {
                case "PlayerList":
                    out.writeUTF("PlayerList");
                    Set<UUID> original = Collections.emptySet();
                    type = in.readUTF();
                    if (type.equals("ALL")) {
                        out.writeUTF("ALL");
                        original = RedisBungee.getApi().getPlayersOnline();
                    } else {
                        try {
                            original = RedisBungee.getApi().getPlayersOnServer(type);
                        } catch (IllegalArgumentException ignored) {
                        }
                    }
                    Set<String> players = new HashSet<String>();
                    for (UUID uuid : original)
                        players.add(RedisBungee.getApi().getNameFromUuid(uuid, false));
                    out.writeUTF(Joiner.on(',').join(players));
                    break;
                case "PlayerCount":
                    out.writeUTF("PlayerCount");
                    type = in.readUTF();
                    if (type.equals("ALL")) {
                        out.writeUTF("ALL");
                        out.writeInt(RedisBungee.getApi().getPlayerCount());
                    } else {
                        out.writeUTF(type);
                        try {
                            out.writeInt(RedisBungee.getApi().getPlayersOnServer(type).size());
                        } catch (IllegalArgumentException e) {
                            out.writeInt(0);
                        }
                    }
                    break;
                case "LastOnline":
                    String user = in.readUTF();
                    out.writeUTF("LastOnline");
                    out.writeUTF(user);
                    out.writeLong(RedisBungee.getApi()
                            .getLastOnline(RedisBungee.getApi().getUuidFromName(user, true)));
                    break;
                default:
                    break;
                }

                ((Server) event.getSender()).sendData("RedisBungee", out.toByteArray());
            }
        });
    }
}

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

private void encodeCommunicationInfo(List<CommunicationFrequency> freqs, ByteArrayDataOutput output) {
    for (CommunicationFrequency freq : freqs) {
        Proto.CommunicationFrequency proto = freq.protoCommunicationFrequency;

        int additionalDataBitmap = (proto.hasUnknownAdditionalInfo1() ? 1 : 0)
                | ((proto.hasUnknownAdditionalInfo2() ? 1 : 0) << 1)
                | ((proto.hasUnknownAdditionalInfo3() ? 1 : 0) << 2)
                | ((proto.hasUnknownAdditionalInfo4() ? 1 : 0) << 3) | ((proto.hasNarrative() ? 1 : 0) << 4)
                | ((proto.getUnknownAdditionalInfo5() ? 1 : 0) << 5)
                | ((proto.getUnknownAdditionalInfo6() ? 1 : 0) << 6)
                | ((proto.getUnknownAdditionalInfo7() ? 1 : 0) << 7);

        int data = ((encodeFrequency(proto.getFrequencyGhz()) & 0xfff) | ((proto.getUnknown(0) & 3) << 12)
                | ((proto.getUnknown(1) & 1) << 14) | ((proto.getUnknown(2) & 1) << 15)
                | ((freq.frequencyType.getIndex()) & 0x3f) << 16) | ((proto.getUnknown(3) & 1) << 22)
                | (((additionalDataBitmap > 0) ? 1 : 0) << 23) | ((proto.getUnknown(4) & 7) << 24)
                | ((proto.getUnknown(5) & 7) << 27) | ((proto.getUnknown(6) & 1) << 30)
                | ((proto.getUnknown(7) & 1) << 31);
        output.writeInt(data);

        if (additionalDataBitmap > 0) {
            output.write(additionalDataBitmap & 0xff);
            if (proto.hasUnknownAdditionalInfo1()) {
                output.write(proto.getUnknownAdditionalInfo1().toByteArray());
            }/*from  ww w  .java 2s .co  m*/
            if (proto.hasUnknownAdditionalInfo2()) {
                output.write(proto.getUnknownAdditionalInfo2().toByteArray());
            }
            if (proto.hasUnknownAdditionalInfo3()) {
                output.write(proto.getUnknownAdditionalInfo3().toByteArray());
            }
            if (proto.hasUnknownAdditionalInfo4()) {
                output.write(proto.getUnknownAdditionalInfo4().toByteArray());
            }
            if (proto.hasNarrative()) {
                BitListOutputStream bitListOutputStream = new BitListOutputStream();
                for (int i = 0; i < NUM_NARRATIVE_LEADING_BITS; ++i) {
                    bitListOutputStream.writeBits(ImmutableList.of(false));
                }
                VariableLengthAsciiEncoding encoding = new VariableLengthAsciiEncoding(bitListOutputStream);
                encoding.encodeExtended(proto.getNarrative());
                byte b[] = bitListOutputStream.toByteArray();
                output.write(b.length & 0xff);
                output.write(b);
            }
        }
    }
}