From 35acb36d1c08c2a78a9f682ebeb67aa8e4d2d7d2 Mon Sep 17 00:00:00 2001
From: RaphiMC <50594595+RaphiMC@users.noreply.github.com>
Date: Sun, 28 Apr 2024 14:55:11 +0200
Subject: [PATCH] Cleaned up item data fixes
---
.../IBlockItemPacketRewriter1_20_5.java | 25 -----
.../MixinBlockItemPacketRewriter1_20_5.java | 103 +++++++-----------
.../mixins/MixinProtocol1_20_5To1_20_3.java | 43 --------
.../viaproxy/data/armor-damages-b1.8.1.json | 22 ++++
4 files changed, 62 insertions(+), 131 deletions(-)
delete mode 100644 src/main/java/net/raphimc/viaproxy/injection/interfaces/IBlockItemPacketRewriter1_20_5.java
delete mode 100644 src/main/java/net/raphimc/viaproxy/injection/mixins/MixinProtocol1_20_5To1_20_3.java
create mode 100644 src/main/resources/assets/viaproxy/data/armor-damages-b1.8.1.json
diff --git a/src/main/java/net/raphimc/viaproxy/injection/interfaces/IBlockItemPacketRewriter1_20_5.java b/src/main/java/net/raphimc/viaproxy/injection/interfaces/IBlockItemPacketRewriter1_20_5.java
deleted file mode 100644
index bb6092e..0000000
--- a/src/main/java/net/raphimc/viaproxy/injection/interfaces/IBlockItemPacketRewriter1_20_5.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
- * Copyright (C) 2021-2024 RK_01/RaphiMC and contributors
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
- */
-
-package net.raphimc.viaproxy.injection.interfaces;
-
-public interface IBlockItemPacketRewriter1_20_5 {
-
- void onMappingDataLoaded();
-
-}
diff --git a/src/main/java/net/raphimc/viaproxy/injection/mixins/MixinBlockItemPacketRewriter1_20_5.java b/src/main/java/net/raphimc/viaproxy/injection/mixins/MixinBlockItemPacketRewriter1_20_5.java
index 2d96e8f..4a755d3 100644
--- a/src/main/java/net/raphimc/viaproxy/injection/mixins/MixinBlockItemPacketRewriter1_20_5.java
+++ b/src/main/java/net/raphimc/viaproxy/injection/mixins/MixinBlockItemPacketRewriter1_20_5.java
@@ -39,19 +39,20 @@ import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.packet.Serverb
import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.rewriter.BlockItemPacketRewriter1_20_5;
import com.viaversion.viaversion.rewriter.ItemRewriter;
import net.raphimc.vialegacy.api.LegacyProtocolVersion;
-import net.raphimc.viaproxy.injection.interfaces.IBlockItemPacketRewriter1_20_5;
+import net.raphimc.viaproxy.ViaProxy;
+import net.raphimc.viaproxy.plugins.events.ViaLoadingEvent;
import net.raphimc.viaproxy.protocoltranslator.impl.ViaProxyMappingDataLoader;
-import net.raphimc.viaproxy.util.logging.Logger;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.*;
@Mixin(value = BlockItemPacketRewriter1_20_5.class, remap = false)
-public abstract class MixinBlockItemPacketRewriter1_20_5 extends ItemRewriter implements IBlockItemPacketRewriter1_20_5 {
+public abstract class MixinBlockItemPacketRewriter1_20_5 extends ItemRewriter {
@Unique
private final Set foodItems_b1_7_3 = new HashSet<>();
@@ -69,8 +70,8 @@ public abstract class MixinBlockItemPacketRewriter1_20_5 extends ItemRewriter", at = @At("RETURN"))
+ public void loadItemMappings(Protocol1_20_5To1_20_3 protocol, CallbackInfo ci) {
this.foodItems_b1_7_3.add("minecraft:apple");
this.foodItems_b1_7_3.add("minecraft:mushroom_stew");
this.foodItems_b1_7_3.add("minecraft:bread");
@@ -81,26 +82,12 @@ public abstract class MixinBlockItemPacketRewriter1_20_5 extends ItemRewriter entry : armorMaxDamages.entrySet()) {
+ final String item = entry.getKey();
+ final int maxDamage = entry.getValue().getAsInt();
+ this.armorMaxDamage_b1_8_1.put(item, maxDamage);
+ }
this.swordItems1_8.add("minecraft:wooden_sword");
this.swordItems1_8.add("minecraft:stone_sword");
@@ -108,47 +95,37 @@ public abstract class MixinBlockItemPacketRewriter1_20_5 extends ItemRewriter entry : itemToolComponents.entrySet()) {
- int attempts = 0;
- while (ProtocolVersion.getClosest(entry.getKey()) == null) {
- try {
- Thread.sleep(100);
- if (attempts++ > 100) { // 10 seconds
- Logger.LOGGER.warn("Failed to load item-tool-components.json after 10 seconds. Skipping entry.");
- break;
+ ViaProxy.EVENT_MANAGER.registerRunnable(() -> {
+ final JsonObject itemToolComponents = ViaProxyMappingDataLoader.INSTANCE.loadData("item-tool-components.json");
+ for (Map.Entry entry : itemToolComponents.entrySet()) {
+ final ProtocolVersion version = ProtocolVersion.getClosest(entry.getKey());
+ if (version == null) {
+ throw new IllegalStateException("Unknown protocol version: " + entry.getKey());
+ }
+ final Map toolProperties = new HashMap<>();
+ final JsonArray toolComponents = entry.getValue().getAsJsonArray();
+ for (JsonElement toolComponent : toolComponents) {
+ final JsonObject toolComponentObject = toolComponent.getAsJsonObject();
+ final String item = toolComponentObject.get("item").getAsString();
+ final float defaultMiningSpeed = toolComponentObject.get("default_mining_speed").getAsFloat();
+ final int damagePerBlock = toolComponentObject.get("damage_per_block").getAsInt();
+ final int[] suitableFor = this.blockJsonArrayToIds(version, toolComponentObject.getAsJsonArray("suitable_for"));
+ final List toolRules = new ArrayList<>();
+ final JsonArray miningSpeeds = toolComponentObject.getAsJsonArray("mining_speeds");
+ for (JsonElement miningSpeed : miningSpeeds) {
+ final JsonObject miningSpeedObject = miningSpeed.getAsJsonObject();
+ final int[] blocks = this.blockJsonArrayToIds(version, miningSpeedObject.getAsJsonArray("blocks"));
+ final float speed = miningSpeedObject.get("speed").getAsFloat();
+ toolRules.add(new ToolRule(HolderSet.of(blocks), speed, null));
}
- } catch (InterruptedException e) {
- break;
+ if (suitableFor.length > 0) {
+ toolRules.add(new ToolRule(HolderSet.of(suitableFor), null, true));
+ }
+ toolProperties.put(item, new ToolProperties(toolRules.toArray(new ToolRule[0]), defaultMiningSpeed, damagePerBlock));
}
+ this.toolDataChanges.put(version, toolProperties);
}
- final ProtocolVersion version = ProtocolVersion.getClosest(entry.getKey());
- if (version == null) { // Only happens if the timeout above is reached or the thread is interrupted
- continue;
- }
- final Map toolProperties = new HashMap<>();
- final JsonArray toolComponents = entry.getValue().getAsJsonArray();
- for (JsonElement toolComponent : toolComponents) {
- final JsonObject toolComponentObject = toolComponent.getAsJsonObject();
- final String item = toolComponentObject.get("item").getAsString();
- final float defaultMiningSpeed = toolComponentObject.get("default_mining_speed").getAsFloat();
- final int damagePerBlock = toolComponentObject.get("damage_per_block").getAsInt();
- final int[] suitableFor = this.blockJsonArrayToIds(version, toolComponentObject.getAsJsonArray("suitable_for"));
- final List toolRules = new ArrayList<>();
- final JsonArray miningSpeeds = toolComponentObject.getAsJsonArray("mining_speeds");
- for (JsonElement miningSpeed : miningSpeeds) {
- final JsonObject miningSpeedObject = miningSpeed.getAsJsonObject();
- final int[] blocks = this.blockJsonArrayToIds(version, miningSpeedObject.getAsJsonArray("blocks"));
- final float speed = miningSpeedObject.get("speed").getAsFloat();
- toolRules.add(new ToolRule(HolderSet.of(blocks), speed, null));
- }
- if (suitableFor.length > 0) {
- toolRules.add(new ToolRule(HolderSet.of(suitableFor), null, true));
- }
- toolProperties.put(item, new ToolProperties(toolRules.toArray(new ToolRule[0]), defaultMiningSpeed, damagePerBlock));
- }
- this.toolDataChanges.put(version, toolProperties);
- }
+ }, ViaLoadingEvent.class);
}
@Inject(method = "toStructuredItem", at = @At("RETURN"))
diff --git a/src/main/java/net/raphimc/viaproxy/injection/mixins/MixinProtocol1_20_5To1_20_3.java b/src/main/java/net/raphimc/viaproxy/injection/mixins/MixinProtocol1_20_5To1_20_3.java
deleted file mode 100644
index bb2d550..0000000
--- a/src/main/java/net/raphimc/viaproxy/injection/mixins/MixinProtocol1_20_5To1_20_3.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
- * Copyright (C) 2021-2024 RK_01/RaphiMC and contributors
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
- */
-
-package net.raphimc.viaproxy.injection.mixins;
-
-import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.Protocol1_20_5To1_20_3;
-import com.viaversion.viaversion.protocols.protocol1_20_5to1_20_3.rewriter.BlockItemPacketRewriter1_20_5;
-import net.raphimc.viaproxy.injection.interfaces.IBlockItemPacketRewriter1_20_5;
-import org.spongepowered.asm.mixin.Final;
-import org.spongepowered.asm.mixin.Mixin;
-import org.spongepowered.asm.mixin.Shadow;
-import org.spongepowered.asm.mixin.injection.At;
-import org.spongepowered.asm.mixin.injection.Inject;
-import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
-
-@Mixin(value = Protocol1_20_5To1_20_3.class, remap = false)
-public abstract class MixinProtocol1_20_5To1_20_3 {
-
- @Shadow
- @Final
- private BlockItemPacketRewriter1_20_5 itemRewriter;
-
- @Inject(method = "onMappingDataLoaded", at = @At("RETURN"))
- private void callOnMappingDataLoaded(CallbackInfo ci) {
- ((IBlockItemPacketRewriter1_20_5) (Object) this.itemRewriter).onMappingDataLoaded();
- }
-
-}
diff --git a/src/main/resources/assets/viaproxy/data/armor-damages-b1.8.1.json b/src/main/resources/assets/viaproxy/data/armor-damages-b1.8.1.json
new file mode 100644
index 0000000..83865a8
--- /dev/null
+++ b/src/main/resources/assets/viaproxy/data/armor-damages-b1.8.1.json
@@ -0,0 +1,22 @@
+{
+ "minecraft:leather_helmet": 33,
+ "minecraft:leather_chestplate": 48,
+ "minecraft:leather_leggings": 45,
+ "minecraft:leather_boots": 39,
+ "minecraft:chainmail_helmet": 66,
+ "minecraft:chainmail_chestplate": 96,
+ "minecraft:chainmail_leggings": 90,
+ "minecraft:chainmail_boots": 78,
+ "minecraft:iron_helmet": 132,
+ "minecraft:iron_chestplate": 192,
+ "minecraft:iron_leggings": 180,
+ "minecraft:iron_boots": 156,
+ "minecraft:diamond_helmet": 264,
+ "minecraft:diamond_chestplate": 384,
+ "minecraft:diamond_leggings": 360,
+ "minecraft:diamond_boots": 312,
+ "minecraft:golden_helmet": 66,
+ "minecraft:golden_chestplate": 96,
+ "minecraft:golden_leggings": 90,
+ "minecraft:golden_boots": 78
+}