Cleanup code

This commit is contained in:
FlorianMichael 2024-04-28 12:36:15 +02:00
parent 642b4de7a0
commit ff942ccf09
2 changed files with 43 additions and 12 deletions

View File

@ -21,8 +21,8 @@ package de.florianmichael.viafabricplus.injection.mixin.fixes.minecraft.item;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import de.florianmichael.viafabricplus.protocoltranslator.ProtocolTranslator;
import de.florianmichael.viafabricplus.util.ItemUtil;
import net.minecraft.client.item.TooltipType;
import net.minecraft.component.ComponentMap;
import net.minecraft.component.DataComponentType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.enchantment.Enchantment;
@ -45,7 +45,6 @@ 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.Redirect;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Optional;
@ -57,25 +56,21 @@ public abstract class MixinItemStack {
@Shadow
public abstract Item getItem();
@Shadow
public abstract ComponentMap getComponents();
@Inject(method = "appendTooltip", at = @At("HEAD"), cancellable = true)
private <T extends TooltipAppender> void replaceEnchantmentTooltip(DataComponentType<T> componentType, Item.TooltipContext context, Consumer<Text> textConsumer, TooltipType type, CallbackInfo ci) {
if (ProtocolTranslator.getTargetVersion().newerThan(ProtocolVersion.v1_14_4)) {
return;
}
// Item has no nbt and can't have enchantments.
if (!this.getComponents().contains(DataComponentTypes.CUSTOM_DATA)) return;
// Via 1.20.5->.3 will always put the original item data into CUSTOM_DATA if it's not empty.
final NbtCompound customData = this.getComponents().get(DataComponentTypes.CUSTOM_DATA).getNbt();
final NbtCompound tag = ItemUtil.getOrNull((ItemStack) (Object) this);
if (tag == null) {
return;
}
if (componentType == DataComponentTypes.ENCHANTMENTS) {
this.viaFabricPlus$appendEnchantments1_14_4("Enchantments", customData, textConsumer);
this.viaFabricPlus$appendEnchantments1_14_4("Enchantments", tag, textConsumer);
ci.cancel();
} else if (componentType == DataComponentTypes.STORED_ENCHANTMENTS) {
this.viaFabricPlus$appendEnchantments1_14_4("StoredEnchantments", customData, textConsumer);
this.viaFabricPlus$appendEnchantments1_14_4("StoredEnchantments", tag, textConsumer);
ci.cancel();
}
}

View File

@ -0,0 +1,36 @@
/*
* This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus
* Copyright (C) 2021-2024 FlorianMichael/EnZaXD <florian.michael07@gmail.com> and RK_01/RaphiMC
* Copyright (C) 2023-2024 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 <http://www.gnu.org/licenses/>.
*/
package de.florianmichael.viafabricplus.util;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
public class ItemUtil {
// Via 1.20.5->.3 will always put the original item data into CUSTOM_DATA if it's not empty.
public static NbtCompound getOrNull(final ItemStack stack) {
if (!stack.contains(DataComponentTypes.CUSTOM_DATA)) {
return null;
}
return stack.get(DataComponentTypes.CUSTOM_DATA).getNbt();
}
}