Add IdAndData util and replace conversions (#3786)

This commit is contained in:
EnZaXD 2024-04-16 15:55:55 +02:00 committed by GitHub
parent 2586788bf6
commit ea5cf3e594
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 125 additions and 9 deletions

View File

@ -0,0 +1,113 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2024 ViaVersion and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.viaversion.viaversion.util;
import com.google.common.base.Preconditions;
import java.util.Objects;
public class IdAndData {
private int id;
private byte data;
public IdAndData(int id) {
this(id, -1);
}
public IdAndData(int id, int data) {
Preconditions.checkArgument(data >= 0 && data <= 15, "Data has to be between 0 and 15: (id: " + id + " data: " + data + ")");
this.id = id;
this.data = (byte) data;
}
public static int getId(final int rawData) {
return rawData >> 4;
}
public static int getData(final int rawData) {
return rawData & 15;
}
public static int toRawData(final int id) {
return id << 4;
}
public static int removeData(final int data) {
return data & ~15;
}
public static IdAndData fromRawData(final int rawData) {
return new IdAndData(rawData >> 4, rawData & 15);
}
public static int toRawData(final int id, final int data) {
return (id << 4) | (data & 15);
}
public int toRawData() {
return toRawData(id, data);
}
public IdAndData withData(int data) {
return new IdAndData(this.id, data);
}
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public byte getData() {
return data;
}
public void setData(final byte data) {
this.data = data;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
IdAndData idAndData = (IdAndData) o;
return id == idAndData.id && data == idAndData.data;
}
@Override
public int hashCode() {
return Objects.hash(id, data);
}
@Override
public String toString() {
return "IdAndData{" +
"id=" + id +
", data=" + data +
'}';
}
}

View File

@ -65,6 +65,7 @@ import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.util.ChatColorUtil;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.GsonUtil;
import com.viaversion.viaversion.util.IdAndData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -317,7 +318,7 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
}
} else {
for (int i = 0; i < 16; i++) {
int newItem = MAPPINGS.getItemMappings().getNewId(item << 4 | i);
int newItem = MAPPINGS.getItemMappings().getNewId(IdAndData.toRawData(item, i));
if (newItem != -1) {
PacketWrapper packet = wrapper.create(ClientboundPackets1_13.COOLDOWN);
packet.write(Type.VAR_INT, newItem);
@ -342,11 +343,11 @@ public class Protocol1_13To1_12_2 extends AbstractProtocol<ClientboundPackets1_1
int id = wrapper.get(Type.INT, 0);
int data = wrapper.get(Type.INT, 1);
if (id == 1010) { // Play record
wrapper.set(Type.INT, 1, getMappingData().getItemMappings().getNewId(data << 4));
wrapper.set(Type.INT, 1, getMappingData().getItemMappings().getNewId(IdAndData.toRawData(data)));
} else if (id == 2001) { // Block break + block break sound
int blockId = data & 0xFFF;
int blockData = data >> 12;
wrapper.set(Type.INT, 1, WorldPackets.toNewId(blockId << 4 | blockData));
wrapper.set(Type.INT, 1, WorldPackets.toNewId(IdAndData.toRawData(blockId, blockData)));
}
});
}

View File

@ -39,6 +39,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SoundSource
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.SpawnEggRewriter;
import com.viaversion.viaversion.rewriter.ItemRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.IdAndData;
import com.viaversion.viaversion.util.Key;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
@ -270,7 +271,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
// Save original id
int originalId = (item.identifier() << 16 | item.data() & 0xFFFF);
int rawId = (item.identifier() << 4 | item.data() & 0xF);
int rawId = IdAndData.toRawData(item.identifier(), item.data());
// NBT Additions
if (isDamageable(item.identifier())) {
@ -458,9 +459,9 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
tag.put(nbtTagName(), new IntTag(originalId)); // Data will be lost, saving original id
}
if (item.identifier() == 31 && item.data() == 0) { // Shrub was removed
rawId = 32 << 4; // Dead Bush
} else if (Protocol1_13To1_12_2.MAPPINGS.getItemMappings().getNewId(rawId & ~0xF) != -1) {
rawId &= ~0xF; // Remove data
rawId = IdAndData.toRawData(32); // Dead Bush
} else if (Protocol1_13To1_12_2.MAPPINGS.getItemMappings().getNewId(IdAndData.removeData(rawId)) != -1) {
rawId = IdAndData.removeData(rawId);
} else {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Failed to get 1.13 item for " + item.identifier());
@ -538,7 +539,7 @@ public class InventoryPackets extends ItemRewriter<ClientboundPackets1_12_1, Ser
tag.put("EntityTag", entityTag);
}
} else {
rawId = (oldId >> 4) << 16 | oldId & 0xF;
rawId = IdAndData.getId(oldId) << 16 | oldId & 0xF;
}
}
}

View File

@ -45,6 +45,7 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data.ParticleRew
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.PaintingProvider;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage;
import com.viaversion.viaversion.util.IdAndData;
import com.viaversion.viaversion.util.Key;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
@ -569,7 +570,7 @@ public class WorldPackets {
if (newId != -1) {
return newId;
}
newId = Protocol1_13To1_12_2.MAPPINGS.getBlockMappings().getNewId(oldId & ~0xF); // Remove data
newId = Protocol1_13To1_12_2.MAPPINGS.getBlockMappings().getNewId(IdAndData.removeData(oldId)); // Remove data
if (newId != -1) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().warning("Missing block " + oldId);