From aab0e4c82e5921d56487099b46d475db8266d792 Mon Sep 17 00:00:00 2001 From: Take Weiland Date: Sat, 3 Sep 2022 21:12:13 +0200 Subject: [PATCH] [1.19] Add hook for items to remain in the hotbar when picking blocks/entities (#8872) --- .../world/entity/player/Inventory.java.patch | 9 +++++++++ .../common/extensions/IForgeItem.java | 15 +++++++++++++++ .../common/extensions/IForgeItemStack.java | 16 ++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/patches/minecraft/net/minecraft/world/entity/player/Inventory.java.patch b/patches/minecraft/net/minecraft/world/entity/player/Inventory.java.patch index b48b495d47..afd02fce9f 100644 --- a/patches/minecraft/net/minecraft/world/entity/player/Inventory.java.patch +++ b/patches/minecraft/net/minecraft/world/entity/player/Inventory.java.patch @@ -1,5 +1,14 @@ --- a/net/minecraft/world/entity/player/Inventory.java +++ b/net/minecraft/world/entity/player/Inventory.java +@@ -129,7 +_,7 @@ + + for(int k = 0; k < 9; ++k) { + int l = (this.f_35977_ + k) % 9; +- if (!this.f_35974_.get(l).m_41793_()) { ++ if (!this.f_35974_.get(l).isNotReplaceableByPickAction(this.f_35978_, l)) { + return l; + } + } @@ -177,7 +_,8 @@ int i = p_36049_.m_41613_(); ItemStack itemstack = this.m_8020_(p_36048_); diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java b/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java index 0ebbaaf943..9727300c1e 100644 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java @@ -14,6 +14,7 @@ import java.util.function.Consumer; import com.google.common.collect.Multimap; import net.minecraft.world.damagesource.DamageSource; +import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.food.FoodProperties; import net.minecraft.world.item.*; import net.minecraft.world.item.enchantment.Enchantment; @@ -828,4 +829,18 @@ public interface IForgeItem return self().getFoodProperties(); } + /** + * Whether the given ItemStack should be excluded (if possible) when selecting the target hotbar slot of a "pick" action. + * By default, this returns true for enchanted stacks. + * + * @see Inventory#getSuitableHotbarSlot() + * @param player the player performing the picking + * @param inventorySlot the inventory slot of the item being up for replacement + * @return true to leave this stack in the hotbar if possible + */ + default boolean isNotReplaceableByPickAction(ItemStack stack, Player player, int inventorySlot) + { + return stack.isEnchanted(); + } + } diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeItemStack.java b/src/main/java/net/minecraftforge/common/extensions/IForgeItemStack.java index e01d204955..53b05b4925 100644 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeItemStack.java +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeItemStack.java @@ -8,6 +8,7 @@ package net.minecraftforge.common.extensions; import net.minecraft.core.Registry; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.monster.EnderMan; +import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.food.FoodProperties; import net.minecraft.world.item.crafting.RecipeType; import net.minecraft.world.level.block.Block; @@ -556,4 +557,19 @@ public interface IForgeItemStack extends ICapabilitySerializable { return self().getItem().getFoodProperties(self(), entity); } + + /** + * Whether this stack should be excluded (if possible) when selecting the target hotbar slot of a "pick" action. + * By default, this returns true for enchanted stacks. + * + * @see Inventory#getSuitableHotbarSlot() + * @param player the player performing the picking + * @param inventorySlot the inventory slot of the item being up for replacement + * @return true to leave this stack in the hotbar if possible + */ + default boolean isNotReplaceableByPickAction(Player player, int inventorySlot) + { + return self().getItem().isNotReplaceableByPickAction(self(), player, inventorySlot); + } + }