Java Code Examples for cpw.mods.fml.common.network.ByteBufUtils

The following examples show how to use cpw.mods.fml.common.network.ByteBufUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ehacks-pro   Source File: ItemCreator.java    License: GNU General Public License v3.0 6 votes vote down vote up
public void giveItem(ItemStack stack) {
    ByteBuf buf = Unpooled.buffer(0);
    buf.writeByte(10);

    ItemStack mail = new ItemStack(Items.stick);
    NBTTagList tagList = new NBTTagList();

    for (int i = 0; i < 6; i++) {
        NBTTagCompound item = new NBTTagCompound();
        item.setByte("Slot", (byte) i);
        stack.writeToNBT(item);
        tagList.appendTag(item);
    }

    NBTTagCompound inv = new NBTTagCompound();
    inv.setTag("Items", tagList);
    inv.setString("UniqueID", UUID.randomUUID().toString());
    mail.stackTagCompound = new NBTTagCompound();
    mail.stackTagCompound.setTag("Package", inv);
    ByteBufUtils.writeItemStack(buf, mail);
    C17PacketCustomPayload packet = new C17PacketCustomPayload("cfm", buf);
    Wrapper.INSTANCE.player().sendQueue.addToSendQueue(packet);
}
 
Example 2
public void writeToBuf(ByteBuf buf){
    ByteBufUtils.writeUTF8String(buf, offeringPlayerName);
    ByteBufUtils.writeUTF8String(buf, offeringPlayerId);
    if(providingPosition != null) {
        buf.writeBoolean(true);
        buf.writeInt(providingPosition.chunkPosX);
        buf.writeInt(providingPosition.chunkPosY);
        buf.writeInt(providingPosition.chunkPosZ);
        buf.writeInt(providingDimensionId);
    } else {
        buf.writeBoolean(false);
    }
    if(returningPosition != null) {
        buf.writeBoolean(true);
        buf.writeInt(returningPosition.chunkPosX);
        buf.writeInt(returningPosition.chunkPosY);
        buf.writeInt(returningPosition.chunkPosZ);
        buf.writeInt(returningDimensionId);
    } else {
        buf.writeBoolean(false);
    }
    buf.writeInt(inStock);
    buf.writeInt(maxTrades);
    buf.writeInt(pendingPayments);
}
 
Example 3
public static Object readField(ByteBuf buf, int type){
    switch(type){
        case 0:
            return buf.readInt();
        case 1:
            return buf.readFloat();
        case 2:
            return buf.readDouble();
        case 3:
            return buf.readBoolean();
        case 4:
            return ByteBufUtils.readUTF8String(buf);
        case 5:
            return buf.readByte();
        case 6:
            return ByteBufUtils.readItemStack(buf);
        case 7:
            if(!buf.readBoolean()) return null;
            return new FluidStack(FluidRegistry.getFluid(ByteBufUtils.readUTF8String(buf)), buf.readInt(), ByteBufUtils.readTag(buf));
    }
    throw new IllegalArgumentException("Invalid sync type! " + type);
}
 
Example 4
Source Project: BigReactors   Source File: ReactantContainer.java    License: MIT License 6 votes vote down vote up
public void deserialize(ByteBuf buffer) {
	capacity = buffer.readInt();
	for(int i = 0; i < tankNames.length; i++) {
		tanks[i] = null;

		boolean hasReactant = buffer.readBoolean();
		if(hasReactant) {
			String reactantName = ByteBufUtils.readUTF8String(buffer);
			int amount = buffer.readInt();
			
			if(!Reactants.isKnown(reactantName)) {
				BRLog.warning("Read an unknown reactant <%s> from a network message; tank %s will remain empty", reactantName, tankNames[i]);
			}
			else {
				tanks[i] = new ReactantStack(reactantName, amount);
				levelAtLastUpdate[i] = amount;
			}
		}
	}
}
 
Example 5
Source Project: ehacks-pro   Source File: Debug.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void sendProxyPacket(String channel, Object... data) {
    ByteBuf buf = Unpooled.buffer();
    for (Object o : data) {
        if (o instanceof Integer) {
            buf.writeInt(((int) o));
            continue;
        }
        if (o instanceof Byte) {
            buf.writeByte(((byte) o));
            continue;
        }
        if (o instanceof Short) {
            buf.writeShort(((short) o));
            continue;
        }
        if (o instanceof String) {
            ByteBufUtils.writeUTF8String(buf, ((String) o));
            continue;
        }
        if (o instanceof ItemStack) {
            ByteBufUtils.writeItemStack(buf, ((ItemStack) o));
            continue;
        }
        if (!(o instanceof NBTTagCompound)) {
            continue;
        }
        ByteBufUtils.writeTag(buf, ((NBTTagCompound) o));
    }
    NetworkDispatcher.get(Wrapper.INSTANCE.mc().getNetHandler().getNetworkManager()).sendProxy(new FMLProxyPacket(buf, channel));
}
 
Example 6
Source Project: ehacks-pro   Source File: MegaExploit.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void setMagic(int x, int y, int z, ItemStack item) {
    ByteBuf buf = Unpooled.buffer(0);
    buf.writeByte(8);
    buf.writeInt(x);
    buf.writeInt(y);
    buf.writeInt(z);
    ByteBufUtils.writeItemStack(buf, item);
    C17PacketCustomPayload packet = new C17PacketCustomPayload("TConstruct", buf);
    Wrapper.INSTANCE.player().sendQueue.addToSendQueue(packet);
}
 
Example 7
Source Project: ehacks-pro   Source File: TableTop.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void setSlot(int x, int y, int z) throws Exception {
    ByteBuf buf = Unpooled.buffer(0);
    ItemStack stack = Statics.STATIC_ITEMSTACK.copy();
    stack.setStackDisplayName("Mega Super Spell");
    ByteBufUtils.writeItemStack(buf, stack);
    buf.writeInt(x);
    buf.writeInt(y);
    buf.writeInt(z);
    buf.writeInt(1);
    C17PacketCustomPayload packet = new C17PacketCustomPayload("BiblioMCBEdit", buf);
    Wrapper.INSTANCE.player().sendQueue.addToSendQueue(packet);
}
 
Example 8
Source Project: ehacks-pro   Source File: ResearchGod.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void doResearch(String researchId) {
    ByteBuf buf = Unpooled.buffer(0);
    buf.writeByte(14);
    ByteBufUtils.writeUTF8String(buf, researchId);
    buf.writeInt(Wrapper.INSTANCE.player().dimension);
    ByteBufUtils.writeUTF8String(buf, Wrapper.INSTANCE.player().getCommandSenderName());
    buf.writeByte(0);
    C17PacketCustomPayload packet = new C17PacketCustomPayload("thaumcraft", buf);
    Wrapper.INSTANCE.player().sendQueue.addToSendQueue(packet);
}
 
Example 9
Source Project: ehacks-pro   Source File: MagicGod.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void doGive(String a1, String a2) {
    ByteBuf buf = Unpooled.buffer(0);
    buf.writeByte(13);
    buf.writeInt(Wrapper.INSTANCE.player().dimension);
    buf.writeInt(Wrapper.INSTANCE.player().getEntityId());
    buf.writeInt(0);
    buf.writeInt(0);
    buf.writeInt(0);
    ByteBufUtils.writeUTF8String(buf, a1);
    ByteBufUtils.writeUTF8String(buf, a2);
    buf.writeBoolean(true);
    buf.writeBoolean(true);
    C17PacketCustomPayload packet = new C17PacketCustomPayload("thaumcraft", buf);
    Wrapper.INSTANCE.player().sendQueue.addToSendQueue(packet);
}
 
Example 10
public static FMLProxyPacket createPacket(int xPos, int yPos, int zPos, NBTTagCompound nbt, int dim) {
	// This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe.
	ByteBuf data = PacketHandlerBase.createDataBuffer((Class<? extends PacketHandlerBase>) new Object() {}.getClass().getEnclosingClass());

	data.writeInt(dim);
	data.writeInt(xPos);
	data.writeInt(yPos);
	data.writeInt(zPos);
	ByteBufUtils.writeTag(data, nbt);

	return buildPacket(data);
}
 
Example 11
@Override
public void handle(ByteBuf data, EntityPlayer player) {
	int dimension = data.readInt();
	int xPos = data.readInt();
	int yPos = data.readInt();
	int zPos = data.readInt();
	NBTTagCompound nbt = ByteBufUtils.readTag(data);

	WorldClient proxyworld = ProxyWorldManager.getProxyworld(dimension);
	if (proxyworld == null) return;
	if (proxyworld.provider.dimensionId != dimension) return;
	if (proxyworld.blockExists(xPos, yPos, zPos)) {
		TileEntity tileentity = proxyworld.getTileEntity(xPos, yPos, zPos);

		if (tileentity != null) {
			tileentity.readFromNBT(nbt);
		} else {
			//Create tile entity from data
			tileentity = TileEntity.createAndLoadEntity(nbt);
			if (tileentity != null) {
				proxyworld.addTileEntity(tileentity);
			}
		}
		proxyworld.markTileEntityChunkModified(xPos, yPos, zPos, tileentity);
		proxyworld.setTileEntity(xPos, yPos, zPos, tileentity);
		proxyworld.markBlockForUpdate(xPos, yPos, zPos);
	}
}
 
Example 12
@Override
public void toBytes( ByteBuf pBuffer )
{
  pBuffer.writeInt( _mFrame );
  pBuffer.writeInt( _mNumFrames );
  ByteBufUtils.writeUTF8String( pBuffer, _mPayload );
}
 
Example 13
Source Project: Framez   Source File: PacketSingleBlockSync.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void toBytes(ByteBuf buf) {

    super.toBytes(buf);

    buf.writeInt(block.getX());
    buf.writeInt(block.getY());
    buf.writeInt(block.getZ());
    NBTTagCompound t = FrameMovementRegistry.instance().writeInfo(block);
    ByteBufUtils.writeTag(buf, t);
}
 
Example 14
Source Project: Framez   Source File: PacketSingleBlockSync.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void fromBytes(ByteBuf buf) {

    super.fromBytes(buf);

    v = new Vec3i(buf.readInt(), buf.readInt(), buf.readInt());
    t = ByteBufUtils.readTag(buf);
}
 
Example 15
Source Project: Framez   Source File: PacketBlockSync.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void toBytes(ByteBuf buf) {

    super.toBytes(buf);

    List<IMovingBlock> l = structure.getBlocks();
    buf.writeInt(l.size());
    for (IMovingBlock b : l) {
        buf.writeInt(b.getX());
        buf.writeInt(b.getY());
        buf.writeInt(b.getZ());
        NBTTagCompound t = FrameMovementRegistry.instance().writeInfo(b);
        ByteBufUtils.writeTag(buf, t);
    }
}
 
Example 16
Source Project: Framez   Source File: PacketBlockSync.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void fromBytes(ByteBuf buf) {

    super.fromBytes(buf);

    int count = buf.readInt();
    for (int i = 0; i < count; i++) {
        Vec3i v = new Vec3i(buf.readInt(), buf.readInt(), buf.readInt());
        NBTTagCompound t = ByteBufUtils.readTag(buf);
        data.put(v, t);
    }
}
 
Example 17
Source Project: Chisel-2   Source File: MessageAutoChisel.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void fromBytes(ByteBuf buf) {
	super.fromBytes(buf);
	this.playSound = buf.readBoolean();
	this.breakChisel = buf.readBoolean();
	this.chiseled = buf.readInt();
	this.base = ByteBufUtils.readItemStack(buf);
}
 
Example 18
public static void writeField(ByteBuf buf, Object value, int type){
    switch(type){
        case 0:
            buf.writeInt((Integer)value);
            break;
        case 1:
            buf.writeFloat((Float)value);
            break;
        case 2:
            buf.writeDouble((Double)value);
            break;
        case 3:
            buf.writeBoolean((Boolean)value);
            break;
        case 4:
            ByteBufUtils.writeUTF8String(buf, (String)value);
            break;
        case 5:
            buf.writeByte((Byte)value);
            break;
        case 6:
            ByteBufUtils.writeItemStack(buf, (ItemStack)value);
            break;
        case 7:
            buf.writeBoolean(value != null);
            if(value != null) {
                FluidStack stack = (FluidStack)value;
                ByteBufUtils.writeUTF8String(buf, stack.getFluid().getName());
                buf.writeInt(stack.amount);
                ByteBufUtils.writeTag(buf, stack.tag);
            }
            break;
    }
}
 
Example 19
@Override
public void toBytes(ByteBuf buffer){
    ByteBufUtils.writeUTF8String(buffer, message);
    if(replacements != null) {
        buffer.writeInt(replacements.length);
        for(String replacement : replacements) {
            ByteBufUtils.writeUTF8String(buffer, replacement);
        }
    } else {
        buffer.writeInt(0);
    }
}
 
Example 20
public static AmadronOfferCustom loadFromBuf(ByteBuf buf){
    AmadronOfferCustom offer = new AmadronOfferCustom(PacketSyncAmadronOffers.getFluidOrItemStack(buf), PacketSyncAmadronOffers.getFluidOrItemStack(buf), ByteBufUtils.readUTF8String(buf), ByteBufUtils.readUTF8String(buf));
    if(buf.readBoolean()) {
        offer.setProvidingPosition(new ChunkPosition(buf.readInt(), buf.readInt(), buf.readInt()), buf.readInt());
    }
    if(buf.readBoolean()) {
        offer.setReturningPosition(new ChunkPosition(buf.readInt(), buf.readInt(), buf.readInt()), buf.readInt());
    }
    offer.inStock = buf.readInt();
    offer.maxTrades = buf.readInt();
    offer.pendingPayments = buf.readInt();
    return offer;
}
 
Example 21
@Override
public void toBytes(ByteBuf buf){
    super.toBytes(buf);
    buf.writeByte(type.ordinal());
    buf.writeInt(values.length);
    for(int i = 0; i < types.length; i++) {
        buf.writeByte(types[i]);
        PacketUpdateGui.writeField(buf, values[i], types[i]);
    }
    ByteBufUtils.writeTag(buf, extraData);
}
 
Example 22
@Override
public void fromBytes(ByteBuf buf){
    super.fromBytes(buf);
    type = IDescSynced.Type.values()[buf.readByte()];
    int dataAmount = buf.readInt();
    types = new byte[dataAmount];
    values = new Object[dataAmount];
    for(int i = 0; i < dataAmount; i++) {
        types[i] = buf.readByte();
        values[i] = PacketUpdateGui.readField(buf, types[i]);
    }
    extraData = ByteBufUtils.readTag(buf);
}
 
Example 23
@Override
public void toBytes(ByteBuf buf){
    super.toBytes(buf);
    buf.writeBoolean(settingStack != null);
    if(settingStack != null) {
        ByteBufUtils.writeUTF8String(buf, settingStack.getFluid().getName());
        buf.writeInt(settingStack.amount);
        ByteBufUtils.writeTag(buf, settingStack.tag);
    }
    buf.writeInt(settingIndex);
}
 
Example 24
Source Project: BigReactors   Source File: ReactantContainer.java    License: MIT License 5 votes vote down vote up
public void serialize(ByteBuf buffer) {
	buffer.writeInt(capacity);
	for(int i = 0; i < tankNames.length; i++) {
		boolean hasReactant = getReactantAmount(i) > 0;
		buffer.writeBoolean(hasReactant);
		if(hasReactant) {
			ByteBufUtils.writeUTF8String(buffer, tanks[i].getName());
			buffer.writeInt(tanks[i].amount);
		}
	}
}
 
Example 25
@Override
public void toBytes(ByteBuf buffer){
    super.toBytes(buffer);
    ByteBufUtils.writeUTF8String(buffer, particleName);
    buffer.writeDouble(dx);
    buffer.writeDouble(dy);
    buffer.writeDouble(dz);
}
 
Example 26
@Override
public void fromBytes(ByteBuf buffer){
    super.fromBytes(buffer);
    particleName = ByteBufUtils.readUTF8String(buffer);
    dx = buffer.readDouble();
    dy = buffer.readDouble();
    dz = buffer.readDouble();
}
 
Example 27
@Override
public void toBytes(ByteBuf buffer){
    super.toBytes(buffer);
    buffer.writeInt(text.length);
    for(String line : text) {
        ByteBufUtils.writeUTF8String(buffer, line);
    }
}
 
Example 28
@Override
public void fromBytes(ByteBuf buffer){
    super.fromBytes(buffer);
    int lines = buffer.readInt();
    text = new String[lines];
    for(int i = 0; i < lines; i++) {
        text[i] = ByteBufUtils.readUTF8String(buffer);
    }
}
 
Example 29
@Override
public void toBytes(ByteBuf buf){
    ByteBufUtils.writeUTF8String(buf, varName);
    buf.writeInt(value.chunkPosX);
    buf.writeInt(value.chunkPosY);
    buf.writeInt(value.chunkPosZ);
}
 
Example 30
public static Object getFluidOrItemStack(ByteBuf buf){
    if(buf.readByte() == 0) {
        return ByteBufUtils.readItemStack(buf);
    } else {
        return new FluidStack(FluidRegistry.getFluid(ByteBufUtils.readUTF8String(buf)), buf.readInt(), ByteBufUtils.readTag(buf));
    }
}