Sword blocking: consumables for 1.21.4+, back to shields for 1.20.5-1.21.3 (#4299)

This commit is contained in:
Rocco 2024-12-09 10:08:51 +00:00 committed by GitHub
parent 813cf813cf
commit 75c6662665
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 32 additions and 42 deletions

View File

@ -478,12 +478,4 @@ public interface ViaVersionConfig extends Config {
* @return true if enabled * @return true if enabled
*/ */
boolean fix1_21PlacementRotation(); boolean fix1_21PlacementRotation();
/**
* If enabled, 1.20.5+ clients will have sword blocking mechanics on 1.8 servers using the consumable item component.
* Note that you won't be able to see the blocking in first person if the client is older than 1.21.4.
*
* @return true if enabled
*/
boolean swordBlockingViaConsumable();
} }

View File

@ -96,7 +96,6 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
private boolean cancelBlockSounds; private boolean cancelBlockSounds;
private boolean hideScoreboardNumbers; private boolean hideScoreboardNumbers;
private boolean fix1_21PlacementRotation; private boolean fix1_21PlacementRotation;
private boolean swordBlockingViaConsumable;
protected AbstractViaConfig(final File configFile, final Logger logger) { protected AbstractViaConfig(final File configFile, final Logger logger) {
super(configFile, logger); super(configFile, logger);
@ -166,7 +165,6 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
cancelBlockSounds = getBoolean("cancel-block-sounds", true); cancelBlockSounds = getBoolean("cancel-block-sounds", true);
hideScoreboardNumbers = getBoolean("hide-scoreboard-numbers", false); hideScoreboardNumbers = getBoolean("hide-scoreboard-numbers", false);
fix1_21PlacementRotation = getBoolean("fix-1_21-placement-rotation", true); fix1_21PlacementRotation = getBoolean("fix-1_21-placement-rotation", true);
swordBlockingViaConsumable = getBoolean("sword-blocking-via-consumable", true);
} }
private BlockedProtocolVersions loadBlockedProtocolVersions() { private BlockedProtocolVersions loadBlockedProtocolVersions() {
@ -558,9 +556,4 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
public boolean fix1_21PlacementRotation() { public boolean fix1_21PlacementRotation() {
return fix1_21PlacementRotation; return fix1_21PlacementRotation;
} }
@Override
public boolean swordBlockingViaConsumable() {
return swordBlockingViaConsumable;
}
} }

View File

@ -619,12 +619,6 @@ public final class BlockItemPacketRewriter1_20_5 extends ItemRewriter<Clientboun
item.dataContainer().set(StructuredDataKey.MAX_DAMAGE, 326); item.dataContainer().set(StructuredDataKey.MAX_DAMAGE, 326);
} }
} }
if (Via.getConfig().swordBlockingViaConsumable() && serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_8)) {
if (item.identifier() == 814 || item.identifier() == 819 || item.identifier() == 824 || item.identifier() == 829 || item.identifier() == 834) { // swords
// Make sword "eatable" to enable clientside instant blocking on 1.8. Consume time is set really high, so the eating animation doesn't play
item.dataContainer().set(StructuredDataKey.FOOD1_20_5, new FoodProperties1_20_5(0, 0F, true, 3600, null, new FoodEffect[0]));
}
}
} }
private int unmappedItemId(final String name) { private int unmappedItemId(final String name) {

View File

@ -22,10 +22,14 @@ import com.viaversion.nbt.tag.IntTag;
import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.BlockPosition; import com.viaversion.viaversion.api.minecraft.BlockPosition;
import com.viaversion.viaversion.api.minecraft.Holder;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer; import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer;
import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey; import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey;
import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.minecraft.item.data.Consumable1_21_2;
import com.viaversion.viaversion.api.minecraft.item.data.CustomModelData1_21_4; import com.viaversion.viaversion.api.minecraft.item.data.CustomModelData1_21_4;
import com.viaversion.viaversion.api.minecraft.item.data.FoodProperties1_20_5;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_20_2; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_20_2;
import com.viaversion.viaversion.api.type.types.version.Types1_21_2; import com.viaversion.viaversion.api.type.types.version.Types1_21_2;
@ -111,6 +115,9 @@ public final class BlockItemPacketRewriter1_21_4 extends StructuredItemRewriter<
} }
updateItemData(item); updateItemData(item);
// Add data components to fix issues in older protocols
appendItemDataFixComponents(connection, item);
return item; return item;
} }
@ -131,6 +138,18 @@ public final class BlockItemPacketRewriter1_21_4 extends StructuredItemRewriter<
return item; return item;
} }
private void appendItemDataFixComponents(final UserConnection connection, final Item item) {
final ProtocolVersion serverVersion = connection.getProtocolInfo().serverProtocolVersion();
if (serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_8)) {
if (item.identifier() == 849 || item.identifier() == 854 || item.identifier() == 859 || item.identifier() == 864 || item.identifier() == 869) { // swords
// Make sword "eatable" to enable clientside instant blocking on 1.8. Set consume animation to block,
// and consume time really high, so the eating animation doesn't play
item.dataContainer().set(StructuredDataKey.CONSUMABLE1_21_2,
new Consumable1_21_2(3600, 3, Holder.of(0), false, new Consumable1_21_2.ConsumeEffect[0]));
}
}
}
public static void updateItemData(final Item item) { public static void updateItemData(final Item item) {
final StructuredDataContainer dataContainer = item.dataContainer(); final StructuredDataContainer dataContainer = item.dataContainer();
dataContainer.replaceKey(StructuredDataKey.CHARGED_PROJECTILES1_21_2, StructuredDataKey.CHARGED_PROJECTILES1_21_4); dataContainer.replaceKey(StructuredDataKey.CHARGED_PROJECTILES1_21_2, StructuredDataKey.CHARGED_PROJECTILES1_21_4);

View File

@ -47,7 +47,6 @@ import com.viaversion.viaversion.api.minecraft.item.data.Instrument1_20_5;
import com.viaversion.viaversion.api.minecraft.item.data.Instrument1_21_2; import com.viaversion.viaversion.api.minecraft.item.data.Instrument1_21_2;
import com.viaversion.viaversion.api.minecraft.item.data.PotionEffect; import com.viaversion.viaversion.api.minecraft.item.data.PotionEffect;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_20_2; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_20_2;
import com.viaversion.viaversion.api.type.types.version.Types1_21; import com.viaversion.viaversion.api.type.types.version.Types1_21;
@ -442,9 +441,6 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter<
updateItemData(item); updateItemData(item);
// Add data components to fix issues in older protocols
appendItemDataFixComponents(connection, item);
final Enchantments enchantments = data.get(StructuredDataKey.ENCHANTMENTS); final Enchantments enchantments = data.get(StructuredDataKey.ENCHANTMENTS);
if (enchantments != null && enchantments.size() != 0) { if (enchantments != null && enchantments.size() != 0) {
// Level 0 is no longer allowed // Level 0 is no longer allowed
@ -538,17 +534,6 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter<
wrapper.write(Types.UNSIGNED_BYTE, (short) containerId); wrapper.write(Types.UNSIGNED_BYTE, (short) containerId);
} }
private void appendItemDataFixComponents(final UserConnection connection, final Item item) {
final ProtocolVersion serverVersion = connection.getProtocolInfo().serverProtocolVersion();
if (serverVersion.olderThanOrEqualTo(ProtocolVersion.v1_8) && item.dataContainer().hasValue(StructuredDataKey.CONSUMABLE1_21_2)) {
if (item.identifier() == 840 || item.identifier() == 845 || item.identifier() == 850 || item.identifier() == 855 || item.identifier() == 860) { // swords
// Change the consume animation of swords to block
final Consumable1_21_2 consumable = item.dataContainer().get(StructuredDataKey.CONSUMABLE1_21_2);
item.dataContainer().set(StructuredDataKey.CONSUMABLE1_21_2, new Consumable1_21_2(consumable.consumeSeconds(), 3, consumable.sound(), consumable.hasConsumeParticles(), consumable.consumeEffects()));
}
}
}
public static void updateItemData(final Item item) { public static void updateItemData(final Item item) {
final StructuredDataContainer dataContainer = item.dataContainer(); final StructuredDataContainer dataContainer = item.dataContainer();
dataContainer.replace(StructuredDataKey.INSTRUMENT1_20_5, StructuredDataKey.INSTRUMENT1_21_2, instrument -> { dataContainer.replace(StructuredDataKey.INSTRUMENT1_20_5, StructuredDataKey.INSTRUMENT1_21_2, instrument -> {

View File

@ -30,6 +30,7 @@ import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.api.type.types.chunk.BulkChunkType1_8; import com.viaversion.viaversion.api.type.types.chunk.BulkChunkType1_8;
@ -297,8 +298,9 @@ public class WorldPacketRewriter1_9 {
wrapper.write(Types.UNSIGNED_BYTE, (short) 255); wrapper.write(Types.UNSIGNED_BYTE, (short) 255);
// Write item in hand // Write item in hand
Item item = Via.getManager().getProviders().get(HandItemProvider.class).getHandItem(wrapper.user()); Item item = Via.getManager().getProviders().get(HandItemProvider.class).getHandItem(wrapper.user());
// Blocking patch // Blocking patch for 1.9-1.21.3 clients
if (Via.getConfig().isShieldBlocking()) { if (Via.getConfig().isShieldBlocking() &&
wrapper.user().getProtocolInfo().protocolVersion().olderThan(ProtocolVersion.v1_21_4)) {
EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_8To1_9.class); EntityTracker1_9 tracker = wrapper.user().getEntityTracker(Protocol1_8To1_9.class);
// Check if the shield is already there or if we have to give it here // Check if the shield is already there or if we have to give it here

View File

@ -31,6 +31,7 @@ import com.viaversion.viaversion.api.minecraft.entitydata.types.EntityDataTypes1
import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.Types;
import com.viaversion.viaversion.data.entity.EntityTrackerBase; import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.v1_8to1_9.Protocol1_8To1_9; import com.viaversion.viaversion.protocols.v1_8to1_9.Protocol1_8To1_9;
@ -102,6 +103,11 @@ public class EntityTracker1_9 extends EntityTrackerBase {
* The item in the offhand will be cleared if there is no sword in the main hand. * The item in the offhand will be cleared if there is no sword in the main hand.
*/ */
public void syncShieldWithSword() { public void syncShieldWithSword() {
if (user().getProtocolInfo().protocolVersion().newerThanOrEqualTo(ProtocolVersion.v1_21_4)) {
// If sword blocking is done through consumables, don't add a shield.
return;
}
boolean swordInHand = hasSwordInHand(); boolean swordInHand = hasSwordInHand();
// Update if there is no sword in the main hand or if the player has no shield in the second hand but a sword in the main hand // Update if there is no sword in the main hand or if the player has no shield in the second hand but a sword in the main hand
@ -183,7 +189,9 @@ public class EntityTracker1_9 extends EntityTrackerBase {
if (entityData.id() == 0) { if (entityData.id() == 0) {
// Byte // Byte
byte data = (byte) entityData.getValue(); byte data = (byte) entityData.getValue();
if (entityId != getProvidedEntityId() && Via.getConfig().isShieldBlocking()) { // If sword blocking is done through consumables (1.21.4+), don't add a shield.
if (entityId != getProvidedEntityId() && Via.getConfig().isShieldBlocking()
&& user().getProtocolInfo().protocolVersion().olderThan(ProtocolVersion.v1_21_4)) {
if ((data & 0x10) == 0x10) { if ((data & 0x10) == 0x10) {
if (validBlocking.contains(entityId)) { if (validBlocking.contains(entityId)) {
Item shield = new DataItem(442, (byte) 1, (short) 0, null); Item shield = new DataItem(442, (byte) 1, (short) 0, null);

View File

@ -215,6 +215,3 @@ chunk-border-fix: false
left-handed-handling: true left-handed-handling: true
# Tries to cancel block break/place sounds sent by 1.8 servers to 1.9+ clients to prevent them from playing twice # Tries to cancel block break/place sounds sent by 1.8 servers to 1.9+ clients to prevent them from playing twice
cancel-block-sounds: true cancel-block-sounds: true
# If enabled, 1.20.5+ clients will have sword blocking mechanics on 1.8 servers using the consumable item component.
# Note that you won't be able to see the blocking in first person if the client is older than 1.21.4.
sword-blocking-via-consumable: true