Java Code Examples for net.minecraft.network.PacketBuffer#readVarInt()

The following examples show how to use net.minecraft.network.PacketBuffer#readVarInt() . 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 File: GameProfileSerializer.java    From OpenModsLib with MIT License 6 votes vote down vote up
public static GameProfile read(PacketBuffer input) {
	final String uuidStr = input.readString(Short.MAX_VALUE);
	UUID uuid = Strings.isNullOrEmpty(uuidStr)? null : UUID.fromString(uuidStr);
	final String name = input.readString(Short.MAX_VALUE);
	GameProfile result = new GameProfile(uuid, name);
	int propertyCount = input.readVarInt();

	final PropertyMap properties = result.getProperties();
	for (int i = 0; i < propertyCount; ++i) {
		String key = input.readString(Short.MAX_VALUE);
		String value = input.readString(Short.MAX_VALUE);
		if (input.readBoolean()) {
			String signature = input.readString(Short.MAX_VALUE);
			properties.put(key, new Property(key, value, signature));
		} else {
			properties.put(key, new Property(key, value));
		}

	}

	return result;
}
 
Example 2
Source File: CraftingSlotWidget.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void handleClientAction(int id, PacketBuffer buffer) {
    super.handleClientAction(id, buffer);
    if (id == 1) {
        HashMap<Integer, ItemStack> ingredients = new HashMap<>();
        int ingredientAmount = buffer.readVarInt();
        try {
            for (int i = 0; i < ingredientAmount; i++) {
                ingredients.put(buffer.readVarInt(), buffer.readItemStack());
            }
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
        recipeResolver.setCraftingGrid(ingredients);
    }
}
 
Example 3
Source File: WidgetGroupItemFilter.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void readUpdateInfo(int id, PacketBuffer buffer) {
    super.readUpdateInfo(id, buffer);
    if(id == 2) {
        clearAllWidgets();
        if(buffer.readBoolean()) {
            int filterId = buffer.readVarInt();
            this.itemFilter = FilterTypeRegistry.createItemFilterById(filterId);
            this.itemFilter.initUI(this::addWidget);
            this.itemFilter.setMaxStackSize(maxStackSize);
        }
    } else if(id == 3) {
        this.maxStackSize = buffer.readVarInt();
        if (itemFilter != null) {
            itemFilter.setMaxStackSize(maxStackSize);
        }
    }
}
 
Example 4
Source File: LongItemStack.java    From GregTech with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static LongItemStack readItemStack(PacketBuffer packetBuffer) {
    int itemId = packetBuffer.readShort();
    if (itemId < 0) {
        return new LongItemStack(ItemStack.EMPTY);
    } else {
        int stackSize = packetBuffer.readVarInt();
        int metadata = packetBuffer.readShort();
        ItemStack itemStack = new ItemStack(Item.getItemById(itemId), stackSize, metadata);
        try {
            itemStack.getItem().readNBTShareTag(itemStack, packetBuffer.readCompoundTag());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return new LongItemStack(itemStack);
    }
}
 
Example 5
Source File: NullableCollectionSerializer.java    From OpenModsLib with MIT License 6 votes vote down vote up
@Override
public T readFromStream(PacketBuffer input) throws IOException {
	final int length = input.readVarInt();

	T result = createCollection(componentType, length);

	if (length > 0) {
		final int nullBitsSize = StreamUtils.bitsToBytes(length);
		final byte[] nullBits = StreamUtils.readBytes(input, nullBitsSize);
		final InputBitStream nullBitStream = new InputBitStream(StreamAdapters.createSource(nullBits));

		for (int i = 0; i < length; i++) {
			if (nullBitStream.readBit()) {
				final Object value = componentSerializer.readFromStream(input);
				setElement(result, i, value);
			}
		}
	}

	return result;
}
 
Example 6
Source File: CollectionUtils.java    From OpenModsLib with MIT License 5 votes vote down vote up
public static void readSortedIdList(PacketBuffer input, Collection<Integer> output) {
	final int elemCount = input.readVarInt();

	int currentId = 0;
	for (int i = 0; i < elemCount; i++) {
		currentId += input.readVarInt();
		output.add(currentId);
	}
}
 
Example 7
Source File: ChoppingRecipe.java    From Survivalist with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ChoppingRecipe read(ResourceLocation recipeId, PacketBuffer buffer)
{

    String group = buffer.readString(32767);
    Ingredient ingredient = Ingredient.read(buffer);
    ItemStack itemstack = buffer.readItemStack();
    double outputMultiplier = buffer.readDouble();
    double hitCountMultiplier = buffer.readDouble();
    int maxOutput = buffer.readVarInt();
    int sawingTime = buffer.readVarInt();
    return new ChoppingRecipe(recipeId, group, ingredient, itemstack, outputMultiplier, hitCountMultiplier, maxOutput, sawingTime);
}
 
Example 8
Source File: AbstractWidgetGroup.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void handleClientAction(int id, PacketBuffer buffer) {
    if (id == 1) {
        int widgetIndex = buffer.readVarInt();
        int widgetUpdateId = buffer.readVarInt();
        Widget widget = widgets.get(widgetIndex);
        widget.handleClientAction(widgetUpdateId, buffer);
    }
}
 
Example 9
Source File: SyncRpcTarget.java    From OpenModsLib with MIT License 5 votes vote down vote up
@Override
public void readFromStreamStream(Side side, EntityPlayer player, PacketBuffer input) throws IOException {
	syncProvider.readFromStreamStream(side, player, input);

	SyncMap map = getSyncMap();
	objectId = input.readVarInt();
	object = map.getObjectById(objectId);
}
 
Example 10
Source File: CycleButtonWidget.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void readUpdateInfo(int id, PacketBuffer buffer) {
    super.readUpdateInfo(id, buffer);
    if (id == 1) {
        this.currentOption = buffer.readVarInt();
    }
}
 
Example 11
Source File: NetworkEventCodec.java    From OpenModsLib with MIT License 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception {
	final Channel channel = ctx.channel();
	final Side side = channel.attr(NetworkRegistry.CHANNEL_SOURCE).get();

	final PacketBuffer payload = new PacketBuffer(msg.payload());
	final int typeId = payload.readVarInt();
	final NetworkEventEntry type = CommonRegistryCallbacks.getEntryIdMap(registry).inverse().get(typeId);

	final EventDirection validator = type.getDirection();
	Preconditions.checkState(validator != null && validator.validateReceive(side),
			"Invalid direction: receiving packet %s on side %s", msg.getClass(), side);

	final NetworkEvent event = type.createPacket();
	event.readFromStream(payload);
	event.dispatcher = msg.getDispatcher();

	event.side = side;

	final INetHandler handler = msg.handler();
	if (handler != null) event.sender = OpenMods.proxy.getPlayerFromHandler(handler);

	final int bufferJunkSize = payload.readableBytes();
	if (bufferJunkSize > 0) Log.warn("%s junk bytes left in buffer, event %s", bufferJunkSize, event);

	out.add(event);
	payload.release();
}
 
Example 12
Source File: ByteBufUtils.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static List<BlockPos> readRelativeBlockList(PacketBuffer buf, BlockPos origin) {
    ArrayList<BlockPos> resultList = new ArrayList<>();
    int amount = buf.readVarInt();
    for (int i = 0; i < amount; i++) {
        int posX = origin.getX() + buf.readVarInt();
        int posY = origin.getY() + buf.readVarInt();
        int posZ = origin.getZ() + buf.readVarInt();
        resultList.add(new BlockPos(posX, posY, posZ));
    }
    return resultList;
}
 
Example 13
Source File: MetaTileEntityHolder.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void receiveInitialSyncData(PacketBuffer buf) {
    if (buf.readBoolean()) {
        int metaTileEntityId = buf.readVarInt();
        setMetaTileEntity(GregTechAPI.META_TILE_ENTITY_REGISTRY.getObjectById(metaTileEntityId));
        this.metaTileEntity.receiveInitialSyncData(buf);
        scheduleChunkForRenderUpdate();
        this.needToUpdateLightning = true;
    }
}
 
Example 14
Source File: MetaTileEntityChest.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void receiveCustomData(int dataId, PacketBuffer buf) {
    super.receiveCustomData(dataId, buf);
    if (dataId == 100) {
        this.numPlayersUsing = buf.readVarInt();
    }
}
 
Example 15
Source File: TileEntityPipeBase.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void receiveInitialSyncData(PacketBuffer buf) {
    readPipeProperties(buf);
    this.blockedConnections = buf.readVarInt();
    this.insulationColor = buf.readInt();
    this.coverableImplementation.readInitialSyncData(buf);
}
 
Example 16
Source File: PipeCoverableImplementation.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void readInitialSyncData(PacketBuffer buf) {
    for (EnumFacing coverSide : EnumFacing.VALUES) {
        int coverId = buf.readVarInt();
        if (coverId != -1) {
            CoverDefinition coverDefinition = CoverDefinition.getCoverByNetworkId(coverId);
            CoverBehavior coverBehavior = coverDefinition.createCoverBehavior(this, coverSide);
            coverBehavior.readInitialSyncData(buf);
            this.coverBehaviors[coverSide.getIndex()] = coverBehavior;
        }
    }
}
 
Example 17
Source File: SyncableVarInt.java    From OpenModsLib with MIT License 4 votes vote down vote up
@Override
public void readFromStream(PacketBuffer stream) {
	value = stream.readVarInt();
}
 
Example 18
Source File: SyncableEnum.java    From OpenModsLib with MIT License 4 votes vote down vote up
@Override
public void readFromStream(PacketBuffer buf) {
	buf.readVarInt();
}
 
Example 19
Source File: SyncableEnum.java    From OpenModsLib with MIT License 4 votes vote down vote up
@Override
public void readFromStream(PacketBuffer stream) {
	int ordinal = stream.readVarInt();
	value = values[ordinal];
}
 
Example 20
Source File: MetaTileEntityPump.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void receiveInitialSyncData(PacketBuffer buf) {
    super.receiveInitialSyncData(buf);
    this.pumpHeadY = buf.readVarInt();
}