Automated particle rewriting

This commit is contained in:
KennyTV 2020-09-21 09:53:04 +02:00
parent e502f2c96d
commit 52f542774f
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
21 changed files with 481 additions and 90 deletions

View File

@ -10,6 +10,7 @@ public class MappingData {
protected final String newVersion;
protected final boolean hasDiffFile;
protected Int2IntBiMap itemMappings;
protected ParticleMappings particleMappings;
protected Mappings blockMappings;
protected Mappings blockStateMappings;
protected Mappings soundMappings;
@ -36,6 +37,12 @@ public class MappingData {
blockStateMappings = loadFromObject(oldMappings, newMappings, diffmapping, "blockstates");
soundMappings = loadFromArray(oldMappings, newMappings, diffmapping, "sounds");
statisticsMappings = loadFromArray(oldMappings, newMappings, diffmapping, "statistics");
Mappings particles = loadFromArray(oldMappings, newMappings, diffmapping, "particles");
if (particles != null) {
particleMappings = new ParticleMappings(oldMappings.getAsJsonArray("particles"), particles);
}
if (loadItems && newMappings.has("items")) {
itemMappings = new Int2IntBiMap();
itemMappings.defaultReturnValue(-1);
@ -64,11 +71,20 @@ public class MappingData {
return oldId != -1 ? oldId : 1;
}
public int getNewParticleId(int id) {
return checkValidity(id, particleMappings.getMappings().getNewId(id), "particles");
}
@Nullable
public Int2IntBiMap getItemMappings() {
return itemMappings;
}
@Nullable
public ParticleMappings getParticleMappings() {
return particleMappings;
}
@Nullable
public Mappings getBlockMappings() {
return blockMappings;

View File

@ -111,7 +111,7 @@ public class MappingDataLoader {
}
public static void mapIdentifiers(Int2IntBiMap output, JsonObject oldIdentifiers, JsonObject newIdentifiers, @Nullable JsonObject diffIdentifiers) {
Object2IntMap newIdentifierMap = MappingDataLoader.indexedObjectToMap(newIdentifiers);
Object2IntMap<String> newIdentifierMap = MappingDataLoader.indexedObjectToMap(newIdentifiers);
for (Map.Entry<String, JsonElement> entry : oldIdentifiers.entrySet()) {
int value = mapIdentifierEntry(entry, newIdentifierMap, diffIdentifiers);
if (value != -1) {
@ -159,7 +159,7 @@ public class MappingDataLoader {
}
public static void mapIdentifiers(short[] output, JsonArray oldIdentifiers, JsonArray newIdentifiers, @Nullable JsonObject diffIdentifiers, boolean warnOnMissing) {
Object2IntMap newIdentifierMap = MappingDataLoader.arrayToMap(newIdentifiers);
Object2IntMap<String> newIdentifierMap = MappingDataLoader.arrayToMap(newIdentifiers);
for (int i = 0; i < oldIdentifiers.size(); i++) {
JsonElement oldIdentifier = oldIdentifiers.get(i);
int mappedId = newIdentifierMap.getInt(oldIdentifier.getAsString());
@ -189,8 +189,8 @@ public class MappingDataLoader {
* @param object json object
* @return map with indexes hashed by their id value
*/
public static Object2IntMap indexedObjectToMap(JsonObject object) {
Object2IntMap map = new Object2IntOpenHashMap(object.size(), 1F);
public static Object2IntMap<String> indexedObjectToMap(JsonObject object) {
Object2IntMap<String> map = new Object2IntOpenHashMap<>(object.size(), 1F);
map.defaultReturnValue(-1);
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
map.put(entry.getValue().getAsString(), Integer.parseInt(entry.getKey()));
@ -202,8 +202,8 @@ public class MappingDataLoader {
* @param array json array
* @return map with indexes hashed by their id value
*/
public static Object2IntMap arrayToMap(JsonArray array) {
Object2IntMap map = new Object2IntOpenHashMap(array.size(), 1F);
public static Object2IntMap<String> arrayToMap(JsonArray array) {
Object2IntMap<String> map = new Object2IntOpenHashMap<>(array.size(), 1F);
map.defaultReturnValue(-1);
for (int i = 0; i < array.size(); i++) {
map.put(array.get(i).getAsString(), i);

View File

@ -0,0 +1,36 @@
package us.myles.ViaVersion.api.data;
import com.google.gson.JsonArray;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
public class ParticleMappings {
private final Mappings mappings;
private final int blockId;
private final int fallingDustId;
private final int itemId;
public ParticleMappings(JsonArray oldMappings, Mappings mappings) {
this.mappings = mappings;
Object2IntMap<String> map = MappingDataLoader.arrayToMap(oldMappings);
blockId = map.getInt("block");
fallingDustId = map.getInt("dust");
itemId = map.getInt("item");
}
public Mappings getMappings() {
return mappings;
}
public int getBlockId() {
return blockId;
}
public int getFallingDustId() {
return fallingDustId;
}
public int getItemId() {
return itemId;
}
}

View File

@ -1,9 +1,7 @@
package us.myles.ViaVersion.api.rewriters;
import org.jetbrains.annotations.Nullable;
import us.myles.ViaVersion.api.minecraft.BlockChangeRecord;
import us.myles.ViaVersion.api.minecraft.Position;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.ClientboundPacketType;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
@ -106,46 +104,4 @@ public class BlockRewriter {
}
});
}
public void registerSpawnParticle(ClientboundPacketType packetType, int blockId, int fallingDustId, int itemId,
ItemRewriter.RewriteFunction itemRewriteFunction, Type<Item> itemType, Type<?> coordType) {
registerSpawnParticle(packetType, blockId, fallingDustId, itemId, null, itemRewriteFunction, itemType, coordType);
}
public void registerSpawnParticle(ClientboundPacketType packetType, int blockId, int fallingDustId, int itemId,
@Nullable IdRewriteFunction particleRewriteFunction, ItemRewriter.RewriteFunction itemRewriteFunction, Type<Item> itemType, Type<?> coordType) {
protocol.registerOutgoing(packetType, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Particle ID
map(Type.BOOLEAN); // 1 - Long Distance
map(coordType); // 2 - X
map(coordType); // 3 - Y
map(coordType); // 4 - Z
map(Type.FLOAT); // 5 - Offset X
map(Type.FLOAT); // 6 - Offset Y
map(Type.FLOAT); // 7 - Offset Z
map(Type.FLOAT); // 8 - Particle Data
map(Type.INT); // 9 - Particle Count
handler(wrapper -> {
int id = wrapper.get(Type.INT, 0);
if (id == -1) return;
if (id == blockId || id == fallingDustId) {
int data = wrapper.passthrough(Type.VAR_INT);
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
} else if (id == itemId) {
// Has to be like this, until we make *everything* object oriented inside of each protocol :(
itemRewriteFunction.rewrite(wrapper.passthrough(itemType));
}
if (particleRewriteFunction != null) {
int newId = particleRewriteFunction.rewrite(id);
if (newId != id) {
wrapper.set(Type.INT, 0, newId);
}
}
});
}
});
}
}

View File

@ -1,5 +1,6 @@
package us.myles.ViaVersion.api.rewriters;
import us.myles.ViaVersion.api.data.ParticleMappings;
import us.myles.ViaVersion.api.minecraft.item.Item;
import us.myles.ViaVersion.api.protocol.ClientboundPacketType;
import us.myles.ViaVersion.api.protocol.Protocol;
@ -189,6 +190,42 @@ public class ItemRewriter {
});
}
// Not the very best place for this, but has to stay here until *everything* is abstracted
public void registerSpawnParticle(ClientboundPacketType packetType, Type<Item> itemType, Type<?> coordType) {
protocol.registerOutgoing(packetType, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.INT); // 0 - Particle ID
map(Type.BOOLEAN); // 1 - Long Distance
map(coordType); // 2 - X
map(coordType); // 3 - Y
map(coordType); // 4 - Z
map(Type.FLOAT); // 5 - Offset X
map(Type.FLOAT); // 6 - Offset Y
map(Type.FLOAT); // 7 - Offset Z
map(Type.FLOAT); // 8 - Particle Data
map(Type.INT); // 9 - Particle Count
handler(wrapper -> {
int id = wrapper.get(Type.INT, 0);
if (id == -1) return;
ParticleMappings mappings = protocol.getMappingData().getParticleMappings();
if (id == mappings.getBlockId() || id == mappings.getFallingDustId()) {
int data = wrapper.passthrough(Type.VAR_INT);
wrapper.set(Type.VAR_INT, 0, protocol.getMappingData().getNewBlockStateId(data));
} else if (id == mappings.getItemId()) {
toClient.rewrite(wrapper.passthrough(itemType));
}
int newId = protocol.getMappingData().getNewParticleId(id);
if (newId != id) {
wrapper.set(Type.INT, 0, newId);
}
});
}
});
}
// Only sent to the client
public PacketHandler itemArrayHandler(Type<Item[]> type) {
return wrapper -> {

View File

@ -75,6 +75,8 @@ public class InventoryPackets {
itemRewriter.registerClickWindow(ServerboundPackets1_13.CLICK_WINDOW, Type.FLAT_ITEM);
itemRewriter.registerCreativeInvAction(ServerboundPackets1_13.CREATIVE_INVENTORY_ACTION, Type.FLAT_ITEM);
itemRewriter.registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_ITEM, Type.FLOAT);
}
public static void toClient(Item item) {

View File

@ -75,7 +75,5 @@ public class WorldPackets {
});
}
});
blockRewriter.registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, 3, 20, 27, InventoryPackets::toClient, Type.FLAT_ITEM, Type.FLOAT);
}
}

View File

@ -149,7 +149,7 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter {
} else if (type.is(Entity1_14Types.EntityType.AREA_EFFECT_CLOUD)) {
if (metadata.getId() == 10) {
Particle particle = (Particle) metadata.getValue();
particle.setId(getNewParticleId(particle.getId()));
particle.setId(protocol.getMappingData().getNewParticleId(particle.getId()));
}
}
@ -224,23 +224,4 @@ public class MetadataRewriter1_14To1_13_2 extends MetadataRewriter {
}
return pose;
}
public static int getNewParticleId(int id) {
if (id >= 10) {
id += 2; // new lava drips 10, 11
}
if (id >= 13) {
id += 1; // new water drip 11 -> 13
}
if (id >= 27) {
id += 1; // new 24 -> 27
}
if (id >= 29) {
id += 1; // skip new short happy villager
}
if (id >= 44) {
id += 1; // new 39 -> 44
}
return id;
}
}

View File

@ -24,7 +24,6 @@ import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.RecipeRewriter1_13_2;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.data.MappingData;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14;
import java.util.Set;
@ -230,6 +229,8 @@ public class InventoryPackets {
});
itemRewriter.registerCreativeInvAction(ServerboundPackets1_14.CREATIVE_INVENTORY_ACTION, Type.FLAT_VAR_INT_ITEM);
itemRewriter.registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
}
public static void toClient(Item item) {

View File

@ -17,7 +17,6 @@ import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14;
import us.myles.ViaVersion.protocols.protocol1_14to1_13_2.types.Chunk1_14Type;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld;
@ -272,9 +271,6 @@ public class WorldPackets {
}
});
blockRewriter.registerSpawnParticle(ClientboundPackets1_13.SPAWN_PARTICLE, 3, 20, 27,
MetadataRewriter1_14To1_13_2::getNewParticleId, InventoryPackets::toClient, Type.FLAT_VAR_INT_ITEM, Type.FLOAT);
protocol.registerOutgoing(ClientboundPackets1_13.JOIN_GAME, new PacketRemapper() {
@Override
public void registerMap() {

View File

@ -49,6 +49,8 @@ public class InventoryPackets {
handler(wrapper -> InventoryPackets.toServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
}
});
itemRewriter.registerSpawnParticle(ClientboundPackets1_16.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
}
public static void toClient(Item item) {

View File

@ -91,7 +91,5 @@ public class WorldPackets {
});
blockRewriter.registerEffect(ClientboundPackets1_16.EFFECT, 1010, 2001);
blockRewriter.registerSpawnParticle(ClientboundPackets1_16.SPAWN_PARTICLE, 3, 23, 34,
null, InventoryPackets::toClient, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
}
}

View File

@ -11,7 +11,6 @@ import us.myles.ViaVersion.api.rewriters.MetadataRewriter;
import us.myles.ViaVersion.api.type.types.Particle;
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2;
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.packets.InventoryPackets;
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.packets.WorldPackets;
import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.storage.EntityTracker1_16;
import java.util.List;
@ -38,7 +37,7 @@ public class MetadataRewriter1_16To1_15_2 extends MetadataRewriter {
if (type == Entity1_16Types.EntityType.AREA_EFFECT_CLOUD) {
if (metadata.getId() == 10) {
Particle particle = (Particle) metadata.getValue();
particle.setId(WorldPackets.getNewParticleId(particle.getId()));
particle.setId(protocol.getMappingData().getNewParticleId(particle.getId()));
}
} else if (type.isOrHasParent(Entity1_16Types.EntityType.ABSTRACT_ARROW)) {
if (metadata.getId() == 8) {

View File

@ -116,6 +116,8 @@ public class InventoryPackets {
handler(wrapper -> InventoryPackets.toServer(wrapper.passthrough(Type.FLAT_VAR_INT_ITEM)));
}
});
itemRewriter.registerSpawnParticle(ClientboundPackets1_15.SPAWN_PARTICLE, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
}
public static void toClient(Item item) {

View File

@ -86,8 +86,6 @@ public class WorldPackets {
});
blockRewriter.registerEffect(ClientboundPackets1_15.EFFECT, 1010, 2001);
blockRewriter.registerSpawnParticle(ClientboundPackets1_15.SPAWN_PARTICLE, 3, 23, 32,
WorldPackets::getNewParticleId, InventoryPackets::toClient, Type.FLAT_VAR_INT_ITEM, Type.DOUBLE);
}
private static void handleBlockEntity(CompoundTag compoundTag) {
@ -118,11 +116,4 @@ public class WorldPackets {
compoundTag.put(skullOwnerTag);
}
}
public static int getNewParticleId(int id) {
if (id >= 27) {
id += 2; // soul flame, soul
}
return id;
}
}

View File

@ -10715,5 +10715,57 @@
"minecraft.open_chest",
"minecraft.sleep_in_bed",
"minecraft.open_shulker_box"
],
"particles": [
"ambient_entity_effect",
"angry_villager",
"barrier",
"block",
"bubble",
"cloud",
"crit",
"damage_indicator",
"dragon_breath",
"dripping_lava",
"dripping_water",
"dust",
"effect",
"elder_guardian",
"enchanted_hit",
"enchant",
"end_rod",
"entity_effect",
"explosion_emitter",
"explosion",
"falling_dust",
"firework",
"fishing",
"flame",
"happy_villager",
"heart",
"instant_effect",
"item",
"item_slime",
"item_snowball",
"large_smoke",
"lava",
"mycelium",
"note",
"poof",
"portal",
"rain",
"smoke",
"spit",
"squid_ink",
"sweep_attack",
"totem_of_undying",
"underwater",
"splash",
"witch",
"bubble_pop",
"current_down",
"bubble_column_up",
"nautilus",
"dolphin"
]
}

View File

@ -11390,5 +11390,57 @@
"minecraft.open_chest",
"minecraft.sleep_in_bed",
"minecraft.open_shulker_box"
],
"particles": [
"ambient_entity_effect",
"angry_villager",
"barrier",
"block",
"bubble",
"cloud",
"crit",
"damage_indicator",
"dragon_breath",
"dripping_lava",
"dripping_water",
"dust",
"effect",
"elder_guardian",
"enchanted_hit",
"enchant",
"end_rod",
"entity_effect",
"explosion_emitter",
"explosion",
"falling_dust",
"firework",
"fishing",
"flame",
"happy_villager",
"heart",
"instant_effect",
"item",
"item_slime",
"item_snowball",
"large_smoke",
"lava",
"mycelium",
"note",
"poof",
"portal",
"rain",
"smoke",
"spit",
"squid_ink",
"sweep_attack",
"totem_of_undying",
"underwater",
"splash",
"witch",
"bubble_pop",
"current_down",
"bubble_column_up",
"nautilus",
"dolphin"
]
}

View File

@ -13696,5 +13696,65 @@
"minecraft.bell_ring",
"minecraft.raid_trigger",
"minecraft.raid_win"
],
"particles": [
"ambient_entity_effect",
"angry_villager",
"barrier",
"block",
"bubble",
"cloud",
"crit",
"damage_indicator",
"dragon_breath",
"dripping_lava",
"falling_lava",
"landing_lava",
"dripping_water",
"falling_water",
"dust",
"effect",
"elder_guardian",
"enchanted_hit",
"enchant",
"end_rod",
"entity_effect",
"explosion_emitter",
"explosion",
"falling_dust",
"firework",
"fishing",
"flame",
"flash",
"happy_villager",
"composter",
"heart",
"instant_effect",
"item",
"item_slime",
"item_snowball",
"large_smoke",
"lava",
"mycelium",
"note",
"poof",
"portal",
"rain",
"smoke",
"sneeze",
"spit",
"squid_ink",
"sweep_attack",
"totem_of_undying",
"underwater",
"splash",
"witch",
"bubble_pop",
"current_down",
"bubble_column_up",
"nautilus",
"dolphin",
"campfire_cosy_smoke",
"campfire_signal_smoke"
]
}

View File

@ -13790,5 +13790,69 @@
"minecraft.raid_win",
"minecraft.interact_with_anvil",
"minecraft.interact_with_grindstone"
],
"particles": [
"ambient_entity_effect",
"angry_villager",
"barrier",
"block",
"bubble",
"cloud",
"crit",
"damage_indicator",
"dragon_breath",
"dripping_lava",
"falling_lava",
"landing_lava",
"dripping_water",
"falling_water",
"dust",
"effect",
"elder_guardian",
"enchanted_hit",
"enchant",
"end_rod",
"entity_effect",
"explosion_emitter",
"explosion",
"falling_dust",
"firework",
"fishing",
"flame",
"flash",
"happy_villager",
"composter",
"heart",
"instant_effect",
"item",
"item_slime",
"item_snowball",
"large_smoke",
"lava",
"mycelium",
"note",
"poof",
"portal",
"rain",
"smoke",
"sneeze",
"spit",
"squid_ink",
"sweep_attack",
"totem_of_undying",
"underwater",
"splash",
"witch",
"bubble_pop",
"current_down",
"bubble_column_up",
"nautilus",
"dolphin",
"campfire_cosy_smoke",
"campfire_signal_smoke",
"dripping_honey",
"falling_honey",
"landing_honey",
"falling_nectar"
]
}

View File

@ -19849,5 +19849,79 @@
"entity.zombie_villager.death",
"entity.zombie_villager.hurt",
"entity.zombie_villager.step"
],
"particles": [
"ambient_entity_effect",
"angry_villager",
"barrier",
"block",
"bubble",
"cloud",
"crit",
"damage_indicator",
"dragon_breath",
"dripping_lava",
"falling_lava",
"landing_lava",
"dripping_water",
"falling_water",
"dust",
"effect",
"elder_guardian",
"enchanted_hit",
"enchant",
"end_rod",
"entity_effect",
"explosion_emitter",
"explosion",
"falling_dust",
"firework",
"fishing",
"flame",
"soul_fire_flame",
"soul",
"flash",
"happy_villager",
"composter",
"heart",
"instant_effect",
"item",
"item_slime",
"item_snowball",
"large_smoke",
"lava",
"mycelium",
"note",
"poof",
"portal",
"rain",
"smoke",
"sneeze",
"spit",
"squid_ink",
"sweep_attack",
"totem_of_undying",
"underwater",
"splash",
"witch",
"bubble_pop",
"current_down",
"bubble_column_up",
"nautilus",
"dolphin",
"campfire_cosy_smoke",
"campfire_signal_smoke",
"dripping_honey",
"falling_honey",
"landing_honey",
"falling_nectar",
"ash",
"crimson_spore",
"warped_spore",
"dripping_obsidian_tear",
"falling_obsidian_tear",
"landing_obsidian_tear",
"reverse_portal",
"white_ash"
]
}

View File

@ -19909,5 +19909,79 @@
"minecraft.interact_with_grindstone",
"minecraft.target_hit",
"minecraft.interact_with_smithing_table"
],
"particles": [
"ambient_entity_effect",
"angry_villager",
"barrier",
"block",
"bubble",
"cloud",
"crit",
"damage_indicator",
"dragon_breath",
"dripping_lava",
"falling_lava",
"landing_lava",
"dripping_water",
"falling_water",
"dust",
"effect",
"elder_guardian",
"enchanted_hit",
"enchant",
"end_rod",
"entity_effect",
"explosion_emitter",
"explosion",
"falling_dust",
"firework",
"fishing",
"flame",
"soul_fire_flame",
"soul",
"flash",
"happy_villager",
"composter",
"heart",
"instant_effect",
"item",
"item_slime",
"item_snowball",
"large_smoke",
"lava",
"mycelium",
"note",
"poof",
"portal",
"rain",
"smoke",
"sneeze",
"spit",
"squid_ink",
"sweep_attack",
"totem_of_undying",
"underwater",
"splash",
"witch",
"bubble_pop",
"current_down",
"bubble_column_up",
"nautilus",
"dolphin",
"campfire_cosy_smoke",
"campfire_signal_smoke",
"dripping_honey",
"falling_honey",
"landing_honey",
"falling_nectar",
"ash",
"crimson_spore",
"warped_spore",
"dripping_obsidian_tear",
"falling_obsidian_tear",
"landing_obsidian_tear",
"reverse_portal",
"white_ash"
]
}