Skip to content

Commit

Permalink
Properly track minecart object entity types in <= 1.12.2
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Jan 3, 2025
1 parent 284810f commit a291f88
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@

package com.viaversion.viabackwards.api.rewriters;

import com.viaversion.nbt.tag.ByteTag;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.IntTag;
import com.viaversion.nbt.tag.NumberTag;
import com.viaversion.nbt.tag.ShortTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.nbt.tag.Tag;
import com.viaversion.viabackwards.api.BackwardsProtocol;
import com.viaversion.viabackwards.api.data.MappedLegacyBlockItem;
import com.viaversion.viabackwards.api.data.BackwardsMappingDataLoader;
import com.viaversion.viabackwards.api.data.MappedLegacyBlockItem;
import com.viaversion.viabackwards.protocol.v1_12to1_11_1.data.BlockColors1_11_1;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.BlockChangeRecord;
Expand All @@ -41,17 +48,10 @@
import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.libs.gson.JsonPrimitive;
import com.viaversion.nbt.tag.ByteTag;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.IntTag;
import com.viaversion.nbt.tag.NumberTag;
import com.viaversion.nbt.tag.ShortTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.nbt.tag.Tag;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.IdAndData;
import java.util.HashMap;
import java.util.Map;
import com.viaversion.viaversion.util.IdAndData;
import org.checkerframework.checker.nullness.qual.Nullable;

public abstract class LegacyBlockItemRewriter<C extends ClientboundPacketType, S extends ServerboundPacketType,
Expand Down Expand Up @@ -226,10 +226,9 @@ public void register() {

public PacketHandler getFallingBlockHandler() {
return wrapper -> {
final EntityTypes1_12.ObjectType type = EntityTypes1_12.ObjectType.findById(wrapper.get(Types.BYTE, 0));
final int objectData = wrapper.get(Types.INT, 0);
final EntityTypes1_12.ObjectType type = EntityTypes1_12.ObjectType.findById(wrapper.get(Types.BYTE, 0), objectData);
if (type == EntityTypes1_12.ObjectType.FALLING_BLOCK) {
final int objectData = wrapper.get(Types.INT, 0);

final IdAndData block = handleBlock(objectData & 4095, objectData >> 12 & 15);
if (block == null) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.checkerframework.checker.nullness.qual.Nullable;

public abstract class LegacyEntityRewriter<C extends ClientboundPacketType, T extends BackwardsProtocol<C, ?, ?, ?>> extends EntityRewriterBase<C, T> {
Expand Down Expand Up @@ -125,7 +124,9 @@ public PacketHandler getMobSpawnRewriter1_11(Type<List<EntityData>> dataType) {

protected PacketHandler getObjectTrackerHandler() {
return wrapper -> {
EntityType type = objectTypeFromId(wrapper.get(Types.BYTE, 0));
int id = wrapper.get(Types.BYTE, 0);
int data = wrapper.get(Types.INT, 0);
EntityType type = objectTypeFromId(id, data);
if (type == null) {
return;
}
Expand All @@ -142,18 +143,20 @@ protected PacketHandler getTrackerAndDataHandler(Type<List<EntityData>> dataType
};
}

protected PacketHandler getObjectRewriter(Function<Byte, ObjectType> objectGetter) {
protected PacketHandler getObjectRewriter(ObjectTypeGetter objectGetter) {
return wrapper -> {
ObjectType type = objectGetter.apply(wrapper.get(Types.BYTE, 0));
int id = wrapper.get(Types.BYTE, 0);
int data = wrapper.get(Types.INT, 0);
ObjectType type = objectGetter.get(id, data);
if (type == null) {
return;
}

EntityReplacement data = getObjectData(type);
if (data != null) {
wrapper.set(Types.BYTE, 0, (byte) data.replacementId());
if (data.objectData() != -1) {
wrapper.set(Types.INT, 0, data.objectData());
EntityReplacement replacement = getObjectData(type);
if (replacement != null) {
wrapper.set(Types.BYTE, 0, (byte) replacement.replacementId());
if (replacement.objectData() != -1) {
wrapper.set(Types.INT, 0, replacement.objectData());
}
}
};
Expand All @@ -169,4 +172,10 @@ protected interface IdSetter {

void setId(PacketWrapper wrapper, int id);
}

@FunctionalInterface
protected interface ObjectTypeGetter {

ObjectType get(int id, int data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ protected void registerRewrites() {

@Override
public EntityType typeFromId(int typeId) {
return EntityTypes1_10.getTypeFromId(typeId, false);
return EntityTypes1_10.EntityType.findById(typeId);
}

@Override
public EntityType objectTypeFromId(int typeId) {
return EntityTypes1_10.getTypeFromId(typeId, true);
public EntityType objectTypeFromId(int typeId, int data) {
return EntityTypes1_10.ObjectType.getEntityType(typeId, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ protected void registerRewrites() {

@Override
public EntityType typeFromId(int typeId) {
return EntityTypes1_11.getTypeFromId(typeId, false);
return EntityTypes1_11.EntityType.findById(typeId);
}

@Override
public EntityType objectTypeFromId(int typeId) {
return EntityTypes1_11.getTypeFromId(typeId, true);
public EntityType objectTypeFromId(int typeId, int data) {
return EntityTypes1_11.ObjectType.getEntityType(typeId, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,11 @@ private EntityData getHorseDataType(int type) {

@Override
public EntityType typeFromId(int typeId) {
return EntityTypes1_11.getTypeFromId(typeId, false);
return EntityTypes1_11.EntityType.findById(typeId);
}

@Override
public EntityType objectTypeFromId(int typeId) {
return EntityTypes1_11.getTypeFromId(typeId, true);
public EntityType objectTypeFromId(int typeId, int data) {
return EntityTypes1_11.ObjectType.getEntityType(typeId, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.viaversion.viabackwards.protocol.v1_12to1_11_1.rewriter;

import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter;
import com.viaversion.viabackwards.protocol.v1_12to1_11_1.Protocol1_12To1_11_1;
import com.viaversion.viabackwards.protocol.v1_12to1_11_1.storage.ParrotStorage;
Expand All @@ -35,7 +36,6 @@
import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.protocols.v1_11_1to1_12.packet.ClientboundPackets1_12;
import com.viaversion.viaversion.protocols.v1_9_1to1_9_3.packet.ClientboundPackets1_9_3;
import com.viaversion.nbt.tag.CompoundTag;

public class EntityPacketRewriter1_12 extends LegacyEntityRewriter<ClientboundPackets1_12, Protocol1_12To1_11_1> {

Expand Down Expand Up @@ -285,11 +285,11 @@ protected void registerRewrites() {

@Override
public EntityType typeFromId(int typeId) {
return EntityTypes1_12.getTypeFromId(typeId, false);
return EntityTypes1_12.EntityType.findById(typeId);
}

@Override
public EntityType objectTypeFromId(int typeId) {
return EntityTypes1_12.getTypeFromId(typeId, true);
public EntityType objectTypeFromId(int typeId, int data) {
return EntityTypes1_12.ObjectType.getEntityType(typeId, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public void register() {
handler(wrapper -> {
int entityId = wrapper.get(Types.VAR_INT, 0);
byte type = wrapper.get(Types.BYTE, 0);
EntityTypes1_13.EntityType entType = EntityTypes1_13.getTypeFromId(type, true);
int data = wrapper.get(Types.INT, 0);
EntityTypes1_13.EntityType entType = EntityTypes1_13.ObjectType.getEntityType(type, data);
if (entType == null) {
return;
}

// Rewrite falling block
if (entType.is(EntityTypes1_13.EntityType.FALLING_BLOCK)) {
int data = wrapper.get(Types.INT, 0);
wrapper.set(Types.INT, 0, protocol.getMappingData().getNewBlockStateId(data));
}

Expand Down Expand Up @@ -159,11 +159,11 @@ protected void registerRewrites() {

@Override
public EntityType typeFromId(int typeId) {
return EntityTypes1_13.getTypeFromId(typeId, false);
return EntityTypes1_13.EntityType.findById(typeId);
}

@Override
public EntityType objectTypeFromId(int typeId) {
return EntityTypes1_13.getTypeFromId(typeId, true);
public EntityType objectTypeFromId(int typeId, int data) {
return EntityTypes1_13.ObjectType.getEntityType(typeId, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ public void register() {
handler(getObjectTrackerHandler());

handler(wrapper -> {
EntityTypes1_13.ObjectType type = EntityTypes1_13.ObjectType.findById(wrapper.get(Types.BYTE, 0));
int id = wrapper.get(Types.BYTE, 0);
int data = wrapper.get(Types.INT, 0);
EntityTypes1_13.ObjectType type = EntityTypes1_13.ObjectType.findById(id, data);
if (type == EntityTypes1_13.ObjectType.FALLING_BLOCK) {
int blockState = wrapper.get(Types.INT, 0);
int combined = Protocol1_13To1_12_2.MAPPINGS.getNewBlockStateId(blockState);
int combined = Protocol1_13To1_12_2.MAPPINGS.getNewBlockStateId(data);
combined = ((combined >> 4) & 0xFFF) | ((combined & 0xF) << 12);
wrapper.set(Types.INT, 0, combined);
} else if (type == EntityTypes1_13.ObjectType.ITEM_FRAME) {
int data = wrapper.get(Types.INT, 0);
data = switch (data) {
case 3 -> 0;
case 4 -> 1;
Expand Down Expand Up @@ -134,7 +134,7 @@ public void register() {

handler(wrapper -> {
int type = wrapper.get(Types.VAR_INT, 1);
EntityType entityType = EntityTypes1_13.getTypeFromId(type, false);
EntityType entityType = EntityTypes1_13.EntityType.findById(type);
if (entityType == null) {
return;
}
Expand Down Expand Up @@ -358,12 +358,12 @@ protected void registerRewrites() {

@Override
public EntityType typeFromId(int typeId) {
return EntityTypes1_13.getTypeFromId(typeId, false);
return EntityTypes1_13.EntityType.findById(typeId);
}

@Override
public EntityType objectTypeFromId(int typeId) {
return EntityTypes1_13.getTypeFromId(typeId, true);
public EntityType objectTypeFromId(int typeId, int data) {
return EntityTypes1_13.ObjectType.getEntityType(typeId, data);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void register() {
handler(wrapper -> {
int id = wrapper.get(Types.BYTE, 0);
int mappedId = newEntityId(id);
EntityTypes1_13.EntityType entityType = EntityTypes1_13.getTypeFromId(mappedId, false);
EntityTypes1_13.EntityType entityType = EntityTypes1_13.EntityType.findById(mappedId);
if (entityType == null) {
// Would be EntityType.PIG on a 1.14 client, but later discarded anyway since not an object type
return;
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
projectVersion=5.2.1
projectVersion=5.2.2-SNAPSHOT

# Smile emoji
mcVersions=1.21.4,1.21.3,1.21.2,1.21.1,1.21,1.20.6,1.20.5,1.20.4, 1.20.3, 1.20.2, 1.20.1, 1.20, 1.19.4, 1.19.3, 1.19.2, 1.19.1, 1.19, 1.18.2, 1.18.1, 1.18, 1.17.1, 1.17, 1.16.5, 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.16, 1.15.2, 1.15.1, 1.15, 1.14.4, 1.14.3, 1.14.2, 1.14.1, 1.14, 1.13.2, 1.13.1, 1.13, 1.12.2, 1.12.1, 1.12, 1.11.2, 1.11.1, 1.11, 1.10.2, 1.10.1, 1.10
mcVersions=1.21.4, 1.21.3, 1.21.2, 1.21.1, 1.21, 1.20.6, 1.20.5, 1.20.4, 1.20.3, 1.20.2, 1.20.1, 1.20, 1.19.4, 1.19.3, 1.19.2, 1.19.1, 1.19, 1.18.2, 1.18.1, 1.18, 1.17.1, 1.17, 1.16.5, 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.16, 1.15.2, 1.15.1, 1.15, 1.14.4, 1.14.3, 1.14.2, 1.14.1, 1.14, 1.13.2, 1.13.1, 1.13, 1.12.2, 1.12.1, 1.12, 1.11.2, 1.11.1, 1.11, 1.10.2, 1.10.1, 1.10
mcVersionRange=1.10-1.21.4
velocityVersion=3.4

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ metadata.format.version = "1.1"
[versions]

# ViaVersion
viaver = "5.2.1-SNAPSHOT"
viaver = "5.2.2-SNAPSHOT"

# Common provided
netty = "4.0.20.Final"
Expand Down
Empty file added sponge/build.gradle.kts
Empty file.

0 comments on commit a291f88

Please sign in to comment.