Some cleanup

This commit is contained in:
Nassim Jahnke 2024-05-04 13:04:39 +02:00
parent b5c718098d
commit 182148dbe8
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
4 changed files with 60 additions and 70 deletions

View File

@ -17,6 +17,8 @@
*/
package com.viaversion.viaversion.protocols.protocol1_21to1_20_5.rewriter;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_20_2;
import com.viaversion.viaversion.api.type.types.version.Types1_20_5;
import com.viaversion.viaversion.api.type.types.version.Types1_21;
@ -28,6 +30,7 @@ import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.Serverb
import com.viaversion.viaversion.protocols.protocol1_21to1_20_5.Protocol1_21To1_20_5;
import com.viaversion.viaversion.rewriter.BlockRewriter;
import com.viaversion.viaversion.rewriter.StructuredItemRewriter;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class BlockItemPacketRewriter1_21 extends StructuredItemRewriter<ClientboundPacket1_20_5, ServerboundPacket1_20_5, Protocol1_21To1_20_5> {

View File

@ -47,24 +47,18 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter<ClientboundPa
registerMetadataRewriter(ClientboundPackets1_20_5.ENTITY_METADATA, Types1_20_5.METADATA_LIST, Types1_21.METADATA_LIST);
registerRemoveEntities(ClientboundPackets1_20_5.REMOVE_ENTITIES);
protocol.registerClientbound(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA, new PacketHandlers() {
@Override
protected void register() {
map(Type.STRING); // Registry
map(Type.REGISTRY_ENTRY_ARRAY); // Data
handler(registryDataHandler1_20_5());
handler(wrapper -> {
// Add required damage type
final String type = wrapper.get(Type.STRING, 0);
final RegistryEntry[] entries = wrapper.get(Type.REGISTRY_ENTRY_ARRAY, 0);
if (Key.stripMinecraftNamespace(type).equals("damage_type")) {
final CompoundTag campfireDamageType = new CompoundTag();
campfireDamageType.putString("scaling", "when_caused_by_living_non_player");
campfireDamageType.putString("message_id", "inFire");
campfireDamageType.putFloat("exhaustion", 0.1F);
wrapper.set(Type.REGISTRY_ENTRY_ARRAY, 0, addRegistryEnties(entries, new RegistryEntry("minecraft:campfire", campfireDamageType)));
}
});
protocol.registerClientbound(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA, wrapper -> {
final String type = Key.stripMinecraftNamespace(wrapper.passthrough(Type.STRING));
final RegistryEntry[] entries = wrapper.passthrough(Type.REGISTRY_ENTRY_ARRAY);
if (Key.stripMinecraftNamespace(type).equals("damage_type")) {
// Add required damage type
final CompoundTag campfireDamageType = new CompoundTag();
campfireDamageType.putString("scaling", "when_caused_by_living_non_player");
campfireDamageType.putString("message_id", "inFire");
campfireDamageType.putFloat("exhaustion", 0.1F);
wrapper.set(Type.REGISTRY_ENTRY_ARRAY, 0, addRegistryEnties(entries, new RegistryEntry("minecraft:campfire", campfireDamageType)));
} else {
handleRegistryData1_20_5(wrapper.user(), type, entries);
}
});
@ -115,13 +109,10 @@ public final class EntityPacketRewriter1_21 extends EntityRewriter<ClientboundPa
}
});
protocol.registerClientbound(ClientboundPackets1_20_5.RESPAWN, new PacketHandlers() {
@Override
public void register() {
map(Type.VAR_INT); // Dimension
map(Type.STRING); // World
handler(worldDataTrackerHandlerByKey1_20_5(0)); // Tracks world height and name for chunk data and entity (un)tracking
}
protocol.registerClientbound(ClientboundPackets1_20_5.RESPAWN, wrapper -> {
final int dimensionId = wrapper.passthrough(Type.VAR_INT);
final String world = wrapper.passthrough(Type.STRING);
trackWorldDataByKey1_20_5(wrapper.user(), dimensionId, world); // Tracks world height and name for chunk data and entity (un)tracking
});
}

View File

@ -446,27 +446,32 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
public PacketHandler worldDataTrackerHandlerByKey1_20_5(final int dimensionIdIndex) {
return wrapper -> {
EntityTracker tracker = tracker(wrapper.user());
int dimensionId = wrapper.get(Types.VAR_INT, dimensionIdIndex);
DimensionData dimensionData = tracker.dimensionData(dimensionId);
if (dimensionData == null) {
protocol.getLogger().severe("Dimension data missing for dimension: " + dimensionId + ", falling back to overworld");
dimensionData = tracker.dimensionData("minecraft:overworld");
Preconditions.checkNotNull(dimensionData, "Overworld data missing");
}
tracker.setCurrentWorldSectionHeight(dimensionData.height() >> 4);
tracker.setCurrentMinY(dimensionData.minY());
String world = wrapper.get(Types.STRING, 0);
if (tracker.currentWorld() != null && !tracker.currentWorld().equals(world)) {
tracker.clearEntities();
tracker.trackClientEntity();
}
tracker.setCurrentWorld(world);
trackWorldDataByKey1_20_5(wrapper.user(), dimensionId, world);
};
}
public void trackWorldDataByKey1_20_5(final UserConnection connection, final int dimensionId, final String world) {
// Track world height for use in chunk data
EntityTracker tracker = tracker(connection);
DimensionData dimensionData = tracker.dimensionData(dimensionId);
if (dimensionData == null) {
protocol.getLogger().severe("Dimension data missing for dimension: " + dimensionId + ", falling back to overworld");
dimensionData = tracker.dimensionData("overworld");
Preconditions.checkNotNull(dimensionData, "Overworld data missing");
}
tracker.setCurrentWorldSectionHeight(dimensionData.height() >> 4);
tracker.setCurrentMinY(dimensionData.minY());
// Clear entities if the world changes
if (tracker.currentWorld() != null && !tracker.currentWorld().equals(world)) {
tracker.clearEntities();
tracker.trackClientEntity();
}
tracker.setCurrentWorld(world);
}
public PacketHandler biomeSizeTracker() {
return wrapper -> trackBiomeSize(wrapper.user(), wrapper.get(Types.NAMED_COMPOUND_TAG, 0));
}
@ -503,22 +508,17 @@ public abstract class EntityRewriter<C extends ClientboundPacketType, T extends
tracker(connection).setDimensions(dimensionDataMap);
}
public PacketHandler registryDataHandler1_20_5() {
return wrapper -> {
final String registryKey = Key.stripMinecraftNamespace(wrapper.get(Types.STRING, 0));
if (registryKey.equals("worldgen/biome")) {
final RegistryEntry[] entries = wrapper.get(Types.REGISTRY_ENTRY_ARRAY, 0);
tracker(wrapper.user()).setBiomesSent(entries.length);
} else if (registryKey.equals("dimension_type")) {
final RegistryEntry[] entries = wrapper.get(Types.REGISTRY_ENTRY_ARRAY, 0);
final Map<String, DimensionData> dimensionDataMap = new HashMap<>(entries.length);
for (int i = 0; i < entries.length; i++) {
final RegistryEntry entry = entries[i];
dimensionDataMap.put(Key.stripMinecraftNamespace(entry.key()), new DimensionDataImpl(i, (CompoundTag) entry.tag()));
}
tracker(wrapper.user()).setDimensions(dimensionDataMap);
public void handleRegistryData1_20_5(final UserConnection connection, final String registryKey, final RegistryEntry[] entries) {
if (registryKey.equals("worldgen/biome")) {
tracker(connection).setBiomesSent(entries.length);
} else if (registryKey.equals("dimension_type")) {
final Map<String, DimensionData> dimensionDataMap = new HashMap<>(entries.length);
for (int i = 0; i < entries.length; i++) {
final RegistryEntry entry = entries[i];
dimensionDataMap.put(Key.stripMinecraftNamespace(entry.key()), new DimensionDataImpl(i, (CompoundTag) entry.tag()));
}
};
tracker(connection).setDimensions(dimensionDataMap);
}
}
// ---------------------------------------------------------------------------

View File

@ -17,6 +17,7 @@
*/
package com.viaversion.viaversion.template.protocols.rewriter;
import com.viaversion.viaversion.api.minecraft.RegistryEntry;
import com.viaversion.viaversion.api.minecraft.entities.EntityType;
import com.viaversion.viaversion.api.minecraft.entities.EntityTypes1_20_5;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
@ -27,6 +28,7 @@ import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ClientboundPac
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ClientboundPackets1_20_5;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.template.protocols.Protocol1_99To_98;
import com.viaversion.viaversion.util.Key;
// Replace if needed
// Types1_OLD
@ -44,13 +46,10 @@ public final class EntityPacketRewriter1_99 extends EntityRewriter<ClientboundPa
registerSetEntityData(ClientboundPackets1_20_5.SET_ENTITY_DATA, /*Types1_OLD_ENTITY_DATA_LIST, */Types1_20_5.ENTITY_DATA_LIST); // Specify old and new entity data list if changed
registerRemoveEntities(ClientboundPackets1_20_5.REMOVE_ENTITIES);
protocol.registerClientbound(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA, new PacketHandlers() {
@Override
protected void register() {
map(Types.STRING); // Registry
map(Types.REGISTRY_ENTRY_ARRAY); // Data
handler(registryDataHandler1_20_5()); // Caches dimensions to access data like height later and tracks the amount of biomes sent for chunk data
}
protocol.registerClientbound(ClientboundConfigurationPackets1_20_5.REGISTRY_DATA, wrapper -> {
final String registryKey = Key.stripMinecraftNamespace(wrapper.passthrough(Types.STRING));
final RegistryEntry[] entries = wrapper.passthrough(Types.REGISTRY_ENTRY_ARRAY);
handleRegistryData1_20_5(wrapper.user(), registryKey, entries); // Caches dimensions to access data like height later and tracks the amount of biomes sent for chunk data
});
protocol.registerClientbound(ClientboundPackets1_20_5.LOGIN, new PacketHandlers() {
@ -72,13 +71,10 @@ public final class EntityPacketRewriter1_99 extends EntityRewriter<ClientboundPa
}
});
protocol.registerClientbound(ClientboundPackets1_20_5.RESPAWN, new PacketHandlers() {
@Override
public void register() {
map(Types.VAR_INT); // Dimension
map(Types.STRING); // World
handler(worldDataTrackerHandlerByKey1_20_5(0)); // Tracks world height and name for chunk data and entity (un)tracking
}
protocol.registerClientbound(ClientboundPackets1_20_5.RESPAWN, wrapper -> {
final int dimensionId = wrapper.passthrough(Types.VAR_INT);
final String world = wrapper.passthrough(Types.STRING);
trackWorldDataByKey1_20_5(wrapper.user(), dimensionId, world); // Tracks world height and name for chunk data and entity (un)tracking
});
}