7
7
import com .viaversion .viaversion .api .data .FullMappings ;
8
8
import com .viaversion .viaversion .api .data .MappingData ;
9
9
import com .viaversion .viaversion .api .data .Mappings ;
10
+ import com .viaversion .viaversion .api .minecraft .item .DataItem ;
11
+ import com .viaversion .viaversion .api .minecraft .item .Item ;
10
12
import com .viaversion .viaversion .api .protocol .packet .PacketWrapper ;
11
13
import com .viaversion .viaversion .api .type .Type ;
12
14
import com .viaversion .viaversion .connection .UserConnectionImpl ;
18
20
import io .netty .buffer .ByteBuf ;
19
21
import io .netty .channel .Channel ;
20
22
import io .netty .util .AttributeKey ;
23
+ import it .unimi .dsi .fastutil .ints .Int2IntMap ;
24
+ import it .unimi .dsi .fastutil .ints .Int2IntOpenHashMap ;
21
25
import net .earthcomputer .multiconnect .api .IMulticonnectTranslator ;
22
26
import net .earthcomputer .multiconnect .api .IMulticonnectTranslatorApi ;
23
27
import net .minecraft .client .Minecraft ;
28
32
import org .jetbrains .annotations .Nullable ;
29
33
import org .slf4j .Logger ;
30
34
31
- import java .util .BitSet ;
32
-
33
35
public class ViaMulticonnectTranslator implements IMulticonnectTranslator {
34
36
private static final Logger LOGGER = LogUtils .getLogger ();
35
37
private static final AttributeKey <UserConnection > VIA_USER_CONNECTION_KEY = AttributeKey .valueOf ("multiconnect.via_user_connection" );
@@ -92,42 +94,43 @@ public void postPipelineModifiers(Channel channel) {
92
94
}
93
95
}
94
96
95
- private static BitSet invertMappings (Mappings mappings ) {
96
- BitSet result = new BitSet (mappings .mappedSize ());
97
+ private static Int2IntMap invertMappings (Mappings mappings ) {
98
+ Int2IntMap result = new Int2IntOpenHashMap (mappings .mappedSize ());
97
99
for (int oldId = 0 ; oldId < mappings .size (); oldId ++) {
98
100
int newId = mappings .getNewId (oldId );
99
101
if (newId != -1 ) {
100
- result .set (newId );
102
+ result .put (newId , oldId );
101
103
}
102
104
}
103
105
return result ;
104
106
}
105
107
106
- private static BitSet getInverseMappings (Channel channel , AttributeKey <BitSet > key , Mappings mappings ) {
108
+ private static Int2IntMap getInverseMappings (Channel channel , AttributeKey <Int2IntMap > key , Mappings mappings ) {
107
109
if (mappings == null ) {
108
110
return null ;
109
111
}
110
112
111
- BitSet result = channel .attr (key ).get ();
113
+ Int2IntMap result = channel .attr (key ).get ();
112
114
if (result == null ) {
113
115
channel .attr (key ).set (result = invertMappings (mappings ));
114
116
}
115
117
return result ;
116
118
}
117
119
118
- private static final AttributeKey <BitSet > INV_BLOCK_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invBlockMappings" );
119
- private static final AttributeKey <BitSet > INV_ENTITY_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invEntityMappings" );
120
- private static final AttributeKey <BitSet > INV_ENCHANTMENT_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invEnchantmentMappings" );
121
- private static final AttributeKey <BitSet > INV_ARGUMENT_TYPE_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invArgumentTypeMappings" );
122
- private static final AttributeKey <BitSet > INV_BLOCK_ENTITY_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invBlockEntityMappings" );
123
- private static final AttributeKey <BitSet > INV_PAINTING_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invPaintingMappings" );
124
- private static final AttributeKey <BitSet > INV_PARTICLE_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invParticleMappings" );
125
- private static final AttributeKey <BitSet > INV_SOUND_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invSoundMappings" );
126
- private static final AttributeKey <BitSet > INV_STATISTICS_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invStatisticsMappings" );
120
+ private static final AttributeKey <Int2IntMap > INV_BLOCK_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invBlockMappings" );
121
+ private static final AttributeKey <Int2IntMap > INV_ENTITY_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invEntityMappings" );
122
+ private static final AttributeKey <Int2IntMap > INV_ENCHANTMENT_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invEnchantmentMappings" );
123
+ private static final AttributeKey <Int2IntMap > INV_ARGUMENT_TYPE_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invArgumentTypeMappings" );
124
+ private static final AttributeKey <Int2IntMap > INV_BLOCK_ENTITY_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invBlockEntityMappings" );
125
+ private static final AttributeKey <Int2IntMap > INV_PAINTING_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invPaintingMappings" );
126
+ private static final AttributeKey <Int2IntMap > INV_PARTICLE_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invParticleMappings" );
127
+ private static final AttributeKey <Int2IntMap > INV_SOUND_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invSoundMappings" );
128
+ private static final AttributeKey <Int2IntMap > INV_STATISTICS_MAPPINGS_KEY = AttributeKey .valueOf ("multiconnect.invStatisticsMappings" );
127
129
128
130
@ Override
129
131
public boolean doesServerKnow (String registry , String entry ) {
130
132
int id = getRegistryId (registry , entry );
133
+ if (registry .equals ("minecraft:item" ) && id == 1 ) return true ; // Stone *always* exists
131
134
132
135
Channel channel = api .getCurrentChannel ();
133
136
if (channel == null ) {
@@ -137,36 +140,50 @@ public boolean doesServerKnow(String registry, String entry) {
137
140
if (connection == null ) {
138
141
return true ;
139
142
}
140
- MappingData mappingData = connection .getProtocolInfo ().getPipeline ().getMappingData ();
141
- if (mappingData == null ) {
142
- return true ;
143
- }
143
+ for (final var protocol : connection .getProtocolInfo ().getPipeline ().pipes ()) {
144
+ if (registry .equals ("minecraft:item" )) {
145
+ final var itemRewriter = protocol .getItemRewriter ();
146
+ if (itemRewriter != null ) {
147
+ final Item newItem = itemRewriter .handleItemToServer (new DataItem (id , (byte )1 , (short )0 , null ));
148
+ if (newItem != null ) {
149
+ id = newItem .identifier ();
150
+ if (id == 1 ) return false ;
151
+ continue ;
152
+ }
153
+ }
154
+ }
144
155
145
- return switch (registry ) {
146
- case "minecraft:block" -> doesServerKnow (channel , INV_BLOCK_MAPPINGS_KEY , mappingData .getBlockMappings (), id );
147
- case "minecraft:item" -> mappingData .getOldItemId (id ) != -1 ;
148
- case "minecraft:entity_type" -> doesServerKnow (channel , INV_ENTITY_MAPPINGS_KEY , mappingData .getEntityMappings (), id );
149
- case "minecraft:enchantment" -> doesServerKnow (channel , INV_ENCHANTMENT_MAPPINGS_KEY , mappingData .getEnchantmentMappings (), id );
150
- case "minecraft:command_argument_type" -> doesServerKnow (channel , INV_ARGUMENT_TYPE_MAPPINGS_KEY , mappingData .getArgumentTypeMappings (), id );
151
- case "minecraft:block_entity_type" -> doesServerKnow (channel , INV_BLOCK_ENTITY_MAPPINGS_KEY , mappingData .getBlockEntityMappings (), id );
152
- case "minecraft:painting_variant" -> doesServerKnow (channel , INV_PAINTING_MAPPINGS_KEY , mappingData .getPaintingMappings (), id );
153
- case "minecraft:particle_type" -> doesServerKnow (channel , INV_PARTICLE_MAPPINGS_KEY , mappingData .getParticleMappings (), id );
154
- case "minecraft:sound_event" -> doesServerKnow (channel , INV_SOUND_MAPPINGS_KEY , mappingData .getSoundMappings (), id );
155
- case "minecraft:custom_stat" -> doesServerKnow (channel , INV_STATISTICS_MAPPINGS_KEY , mappingData .getStatisticsMappings (), id );
156
- default -> true ;
157
- };
156
+ MappingData mappingData = protocol .getMappingData ();
157
+ if (mappingData == null ) continue ;
158
+
159
+ id = switch (registry ) {
160
+ case "minecraft:block" -> getOldId (channel , INV_BLOCK_MAPPINGS_KEY , mappingData .getBlockMappings (), id );
161
+ case "minecraft:item" -> mappingData .getOldItemId (id );
162
+ case "minecraft:entity_type" -> getOldId (channel , INV_ENTITY_MAPPINGS_KEY , mappingData .getEntityMappings (), id );
163
+ case "minecraft:enchantment" -> getOldId (channel , INV_ENCHANTMENT_MAPPINGS_KEY , mappingData .getEnchantmentMappings (), id );
164
+ case "minecraft:command_argument_type" -> getOldId (channel , INV_ARGUMENT_TYPE_MAPPINGS_KEY , mappingData .getArgumentTypeMappings (), id );
165
+ case "minecraft:block_entity_type" -> getOldId (channel , INV_BLOCK_ENTITY_MAPPINGS_KEY , mappingData .getBlockEntityMappings (), id );
166
+ case "minecraft:painting_variant" -> getOldId (channel , INV_PAINTING_MAPPINGS_KEY , mappingData .getPaintingMappings (), id );
167
+ case "minecraft:particle_type" -> getOldId (channel , INV_PARTICLE_MAPPINGS_KEY , mappingData .getParticleMappings (), id );
168
+ case "minecraft:sound_event" -> getOldId (channel , INV_SOUND_MAPPINGS_KEY , mappingData .getSoundMappings (), id );
169
+ case "minecraft:custom_stat" -> getOldId (channel , INV_STATISTICS_MAPPINGS_KEY , mappingData .getStatisticsMappings (), id );
170
+ default -> id ;
171
+ };
172
+ if (id == -1 || (registry .equals ("minecraft:item" ) && id == 1 )) return false ;
173
+ }
174
+ return true ;
158
175
}
159
176
160
- private static boolean doesServerKnow (Channel channel , AttributeKey <BitSet > key , @ Nullable FullMappings mappings , int id ) {
177
+ private static int getOldId (Channel channel , AttributeKey <Int2IntMap > key , @ Nullable FullMappings mappings , int id ) {
161
178
if (mappings == null ) {
162
- return true ;
179
+ return id ;
163
180
}
164
- return doesServerKnow (channel , key , mappings .mappings (), id );
181
+ return getOldId (channel , key , mappings .mappings (), id );
165
182
}
166
183
167
- private static boolean doesServerKnow (Channel channel , AttributeKey <BitSet > key , @ Nullable Mappings mappings , int id ) {
184
+ private static int getOldId (Channel channel , AttributeKey <Int2IntMap > key , @ Nullable Mappings mappings , int id ) {
168
185
if (mappings == null ) {
169
- return true ;
186
+ return id ;
170
187
}
171
188
return getInverseMappings (channel , key , mappings ).get (id );
172
189
}
0 commit comments