Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class ViaBackwardsConfig extends Config implements com.viaversion.viaback
private DialogStyleConfig dialogStyleConfig;
private boolean codeOfConductAsDialog;
private boolean passOriginalItemNameToResourcePacks;
private boolean ignore1_20_5_1_21_4ClickItems;

public ViaBackwardsConfig(File configFile, Logger logger) {
super(configFile, logger);
Expand Down Expand Up @@ -77,6 +78,7 @@ private void loadFields() {
dialogStyleConfig = loadDialogStyleConfig(getSection("dialog-style"));
codeOfConductAsDialog = getBoolean("code-of-conduct-as-dialog", true);
passOriginalItemNameToResourcePacks = getBoolean("pass-original-item-name-to-resource-packs", true);
ignore1_20_5_1_21_4ClickItems = getBoolean("ignore-1_20_5-1_21_4-click-items", true);
}

private DialogStyleConfig loadDialogStyleConfig(final ConfigSection section) {
Expand Down Expand Up @@ -186,6 +188,11 @@ public boolean passOriginalItemNameToResourcePacks() {
return passOriginalItemNameToResourcePacks;
}

@Override
public boolean isIgnore1_20_5_1_21_4ClickItems() {
return ignore1_20_5_1_21_4ClickItems;
}

@Override
public URL getDefaultConfigURL() {
return getClass().getClassLoader().getResource("assets/viabackwards/config.yml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,12 @@ public interface ViaBackwardsConfig extends Config {
* @return true if enabled
*/
boolean passOriginalItemNameToResourcePacks();

/**
* Drops client-sent clicked items and forces inventory resynchronization for 1.20.5-1.21.4 clients on 1.21.5+ servers.
* Works around unfixable Vanilla protocol exploits.
*
* @return true if enabled
*/
boolean isIgnore1_20_5_1_21_4ClickItems();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.LongArrayTag;
import com.viaversion.nbt.tag.Tag;
import com.viaversion.viabackwards.ViaBackwards;
import com.viaversion.viabackwards.api.rewriters.BackwardsStructuredItemRewriter;
import com.viaversion.viabackwards.protocol.v1_21_5to1_21_4.Protocol1_21_5To1_21_4;
import com.viaversion.viabackwards.protocol.v1_21_5to1_21_4.storage.HorseDataStorage;
Expand Down Expand Up @@ -58,6 +59,7 @@
import com.viaversion.viaversion.api.minecraft.item.data.trim.ArmorTrimMaterial1_20_5;
import com.viaversion.viaversion.api.minecraft.item.data.trim.ProvidesTrimMaterial1_20_5;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.rewriter.ComponentRewriter;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types;
Expand Down Expand Up @@ -145,18 +147,40 @@ public void registerPackets() {

protocol.replaceServerbound(ServerboundPackets1_21_4.CONTAINER_CLICK, wrapper -> {
wrapper.passthrough(Types.VAR_INT); // Container id
wrapper.passthrough(Types.VAR_INT); // State id
wrapper.passthrough(Types.SHORT); // Slot
wrapper.passthrough(Types.BYTE); // Button
wrapper.passthrough(Types.VAR_INT); // Mode

// Try our best to get a hashed item out of it - will be wrong for some data component types that don't have their conversion implemented
final int affectedItems = Limit.max(wrapper.passthrough(Types.VAR_INT), 128);
for (int i = 0; i < affectedItems; i++) {
final ProtocolVersion clientVersion = wrapper.user().getProtocolInfo().protocolVersion();
final boolean isTargetVersion = clientVersion.betweenInclusive(ProtocolVersion.v1_20_5, ProtocolVersion.v1_21_4);

if (!isTargetVersion || !ViaBackwards.getConfig().isIgnore1_20_5_1_21_4ClickItems()) {
wrapper.passthrough(Types.VAR_INT); // State id
wrapper.passthrough(Types.SHORT); // Slot
wrapper.passthrough(Types.BYTE); // Button
wrapper.passthrough(Types.VAR_INT); // Mode

// Try our best to get a hashed item out of it - will be wrong for some data component types that don't have their conversion implemented
final int affectedItems = Limit.max(wrapper.passthrough(Types.VAR_INT), 128);
for (int i = 0; i < affectedItems; i++) {
wrapper.passthrough(Types.SHORT); // Slot
itemToHashedItem(wrapper);
}
itemToHashedItem(wrapper);
} else {
final int stateId = wrapper.read(Types.VAR_INT);
wrapper.write(Types.VAR_INT, stateId ^ 1); // Mismatched state ID (forces server resync)

wrapper.passthrough(Types.SHORT); // Slot
wrapper.passthrough(Types.BYTE); // Button
wrapper.passthrough(Types.VAR_INT); // Mode

Limit.max(wrapper.read(Types.VAR_INT), 128); // Affected items

// Discard remaining client-sent item data to bypass item conversion
wrapper.clearInputBuffer();

// Write dummy data
wrapper.write(Types.VAR_INT, 0); // Affected items = 0
wrapper.write(Types.HASHED_ITEM, HashedStructuredItem.empty()); // Carried item empty
}
itemToHashedItem(wrapper);
});

protocol.replaceClientbound(ClientboundPackets1_21_5.SET_EQUIPMENT, wrapper -> {
Expand Down
4 changes: 4 additions & 0 deletions common/src/main/resources/assets/viabackwards/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# If you need help, you can join our Discord - https://viaversion.com/discord
#
# Drops client-sent clicked items and forces inventory resynchronization for 1.20.5-1.21.4 clients on 1.21.5+ servers.
# This works around unfixable Vanilla protocol exploits.
ignore-1_20_5-1_21_4-click-items: true
#
# Passes custom model data into items, as well as the original Vanilla 1.21.4+ item name into custom_model_data strings.
# This allows client resource packs to restore the appearance of newer items entirely missing from this older version.
pass-original-item-name-to-resource-packs: true
Expand Down