[1.19] Add hook for items to remain in the hotbar when picking blocks/entities (#8872)

This commit is contained in:
Take Weiland authored and GitHub committed 2022-09-03 20:12:13 +01:00
1 parent 6609708d14
commit aab0e4c82e
3 files changed
+40

No files matched your search

@@ -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_);
@@ -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();
}
}
@@ -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<CompoundTag>
{
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);
}
}