From fa708f93ee3ab2ad6fa1237bdd8fdda51ca82be8 Mon Sep 17 00:00:00 2001 From: Ben Staddon Date: Fri, 12 Apr 2019 13:26:15 -0400 Subject: [PATCH] Allow items to control the rate of repair from mending (#5102) --- .../entity/item/EntityXPOrb.java.patch | 21 +++- .../common/extensions/IForgeItem.java | 9 ++ .../debug/item/MendingRepairTest.java | 95 +++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/minecraftforge/debug/item/MendingRepairTest.java diff --git a/patches/minecraft/net/minecraft/entity/item/EntityXPOrb.java.patch b/patches/minecraft/net/minecraft/entity/item/EntityXPOrb.java.patch index 26e6b8e13..f7ee5277c 100644 --- a/patches/minecraft/net/minecraft/entity/item/EntityXPOrb.java.patch +++ b/patches/minecraft/net/minecraft/entity/item/EntityXPOrb.java.patch @@ -18,7 +18,7 @@ if (this.func_180431_b(p_70097_1_)) { return false; } else { -@@ -178,6 +180,7 @@ +@@ -178,12 +180,14 @@ public void func_70100_b_(EntityPlayer p_70100_1_) { if (!this.field_70170_p.field_72995_K) { if (this.field_70532_c == 0 && p_70100_1_.field_71090_bL == 0) { @@ -26,3 +26,22 @@ p_70100_1_.field_71090_bL = 2; p_70100_1_.func_71001_a(this, 1); ItemStack itemstack = EnchantmentHelper.func_92099_a(Enchantments.field_185296_A, p_70100_1_); + if (!itemstack.func_190926_b() && itemstack.func_77951_h()) { +- int i = Math.min(this.func_184514_c(this.field_70530_e), itemstack.func_77952_i()); +- this.field_70530_e -= this.func_184515_b(i); ++ float ratio = itemstack.func_77973_b().getXpRepairRatio(itemstack); ++ int i = Math.min(roundAverage(this.field_70530_e * ratio), itemstack.func_77952_i()); ++ this.field_70530_e -= roundAverage(i / ratio); + itemstack.func_196085_b(itemstack.func_77952_i() - i); + } + +@@ -261,4 +265,9 @@ + public boolean func_70075_an() { + return false; + } ++ ++ private static int roundAverage(float value) { ++ double floor = Math.floor(value); ++ return (int) floor + (Math.random() < value - floor ? 1 : 0); ++ } + } diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java b/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java index 72ab747a8..1c047dfa1 100644 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java @@ -113,6 +113,15 @@ public interface IForgeItem * @return True if reparable */ boolean isRepairable(); + + /** + * Determines the amount of durability the mending enchantment + * will repair, on average, per point of experience. + */ + default float getXpRepairRatio(ItemStack stack) + { + return 2f; + } /** * Override this method to change the NBT data being sent to the client. You diff --git a/src/test/java/net/minecraftforge/debug/item/MendingRepairTest.java b/src/test/java/net/minecraftforge/debug/item/MendingRepairTest.java new file mode 100644 index 000000000..32860230a --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/item/MendingRepairTest.java @@ -0,0 +1,95 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2018. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.debug.item; + +import net.minecraft.client.renderer.block.model.ModelResourceLocation; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.init.Enchantments; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.util.NonNullList; +import net.minecraftforge.client.event.ModelRegistryEvent; +import net.minecraftforge.client.model.ModelLoader; +import net.minecraftforge.event.RegistryEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.registry.GameRegistry; +import net.minecraftforge.fml.relauncher.Side; + +@Mod.EventBusSubscriber +@Mod(modid = MendingRepairTest.MOD_ID, name = "Mending repair amount test mod", version = "1.0") +public class MendingRepairTest +{ + static final boolean ENABLED = true; + static final String MOD_ID = "mending_repair_test"; + + @GameRegistry.ObjectHolder(MOD_ID + ":test_item") + public static final Item TEST_ITEM = null; + + @SubscribeEvent + public static void registerItems(RegistryEvent.Register event) + { + if (!ENABLED) return; + event.getRegistry().register( + new TestItem() + .setRegistryName(MOD_ID, "test_item") + .setUnlocalizedName(MOD_ID + ".test_item") + ); + } + + @Mod.EventBusSubscriber(modid = MOD_ID, value = Side.CLIENT) + public static final class ClientEventHandler + { + @SubscribeEvent + public static void registerModels(ModelRegistryEvent event) + { + if (!ENABLED) return; + ModelLoader.setCustomModelResourceLocation(TEST_ITEM, 0, new ModelResourceLocation("minecraft:blaze_rod", "inventory")); + } + } + + private static final class TestItem extends Item + { + TestItem() + { + maxStackSize = 1; + setMaxDamage(10); + setCreativeTab(CreativeTabs.TOOLS); + } + + @Override + public void getSubItems(CreativeTabs tab, NonNullList items) + { + if (this.isInCreativeTab(tab)) + { + ItemStack stack = new ItemStack(this); + stack.addEnchantment(Enchantments.MENDING, 1); + stack.setItemDamage(stack.getMaxDamage()); + items.add(stack); + } + } + + @Override + public float getXpRepairRatio(ItemStack stack) + { + return 0.1f; + } + } +} \ No newline at end of file