4545import com .viaversion .viaversion .api .minecraft .item .data .Instrument1_21_2 ;
4646import com .viaversion .viaversion .api .minecraft .item .data .PotionEffect ;
4747import com .viaversion .viaversion .api .protocol .packet .PacketWrapper ;
48+ import com .viaversion .viaversion .api .protocol .version .ProtocolVersion ;
4849import com .viaversion .viaversion .api .type .Types ;
4950import com .viaversion .viaversion .api .type .types .chunk .ChunkType1_20_2 ;
5051import com .viaversion .viaversion .api .type .types .version .Types1_21 ;
@@ -417,6 +418,9 @@ public Item handleItemToClient(final UserConnection connection, final Item item)
417418 super .handleItemToClient (connection , item );
418419 updateItemData (item );
419420
421+ // Add data components to fix issues in older protocols
422+ appendItemDataFixComponents (connection , item );
423+
420424 // Item name is now overridden by custom implemented display names (compass, player head, potion, shield, tipped arrow)
421425 final int identifier = item .identifier ();
422426 if (identifier == 952 || identifier == 1147 || identifier == 1039 || identifier == 1203 || identifier == 1200 || identifier == 1204 || identifier == 1202 ) {
@@ -469,6 +473,17 @@ private void updateContainerIdServerbound(final PacketWrapper wrapper) {
469473 wrapper .write (Types .UNSIGNED_BYTE , (short ) containerId );
470474 }
471475
476+ private void appendItemDataFixComponents (final UserConnection connection , final Item item ) {
477+ final ProtocolVersion serverVersion = connection .getProtocolInfo ().serverProtocolVersion ();
478+ if (serverVersion .olderThanOrEqualTo (ProtocolVersion .v1_8 ) && item .dataContainer ().hasValue (StructuredDataKey .CONSUMABLE1_21_2 )) {
479+ if (item .identifier () == 840 || item .identifier () == 845 || item .identifier () == 850 || item .identifier () == 855 || item .identifier () == 860 ) { // swords
480+ // Change the consume animation of swords to block
481+ final Consumable1_21_2 consumable = item .dataContainer ().get (StructuredDataKey .CONSUMABLE1_21_2 );
482+ item .dataContainer ().set (StructuredDataKey .CONSUMABLE1_21_2 , new Consumable1_21_2 (consumable .consumeSeconds (), 3 , consumable .sound (), consumable .hasConsumeParticles (), consumable .consumeEffects ()));
483+ }
484+ }
485+ }
486+
472487 public static void updateItemData (final Item item ) {
473488 final StructuredDataContainer dataContainer = item .dataContainer ();
474489 dataContainer .replace (StructuredDataKey .INSTRUMENT1_20_5 , StructuredDataKey .INSTRUMENT1_21_2 , instrument -> {
@@ -486,6 +501,7 @@ public static void updateItemData(final Item item) {
486501 final Consumable1_21_2 .ApplyStatusEffects applyStatusEffects = new Consumable1_21_2 .ApplyStatusEffects (new PotionEffect []{effect .effect ()}, effect .probability ());
487502 consumeEffects [i ] = new Consumable1_21_2 .ConsumeEffect <>(0 /* add status effect */ , Consumable1_21_2 .ApplyStatusEffects .TYPE , applyStatusEffects );
488503 }
504+
489505 dataContainer .set (StructuredDataKey .CONSUMABLE1_21_2 , new Consumable1_21_2 (food .eatSeconds (), 1 /* eat */ , sound , true , consumeEffects ));
490506 if (food .usingConvertsTo () != null ) {
491507 dataContainer .set (StructuredDataKey .USE_REMAINDER , food .usingConvertsTo ());
0 commit comments