mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 19:15:32 +00:00
1.21 Update
Co-authored-by: RealMangoRage <[email protected]> Co-authored-by: Daniel Norris <[email protected]> Co-authored-by: Ven <[email protected]>
This commit is contained in:
941 files changed
+3119
-4266
No files matched your search
@@ -14,10 +14,10 @@ import net.minecraftforge.items.IItemHandler;
|
||||
* Exposes the armor inventory of an {@link LivingEntity} as an {@link IItemHandler} using {@link LivingEntity#getItemBySlot(EquipmentSlot)} and
|
||||
* {@link LivingEntity#setItemSlot(EquipmentSlot, ItemStack)}.
|
||||
*/
|
||||
public class EntityArmorInvWrapper extends EntityEquipmentInvWrapper
|
||||
{
|
||||
public EntityArmorInvWrapper(final LivingEntity entity)
|
||||
{
|
||||
super(entity, EquipmentSlot.Type.ARMOR);
|
||||
public class EntityArmorInvWrapper extends EntityEquipmentInvWrapper {
|
||||
public EntityArmorInvWrapper(final LivingEntity entity) {
|
||||
// some entities may not support setting values in this slot, but we never cared about that before so don't care now.
|
||||
// Modders should implement their own inventory wrappers.
|
||||
super(entity, EquipmentSlot.Type.HUMANOID_ARMOR, EquipmentSlot.Type.ANIMAL_ARMOR);
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,7 @@ import java.util.List;
|
||||
* Exposes the armor or hands inventory of an {@link LivingEntity} as an {@link IItemHandler} using {@link LivingEntity#getItemBySlot(EquipmentSlot)} and
|
||||
* {@link LivingEntity#setItemSlot(EquipmentSlot, ItemStack)}.
|
||||
*/
|
||||
public abstract class EntityEquipmentInvWrapper implements IItemHandlerModifiable
|
||||
{
|
||||
public abstract class EntityEquipmentInvWrapper implements IItemHandlerModifiable {
|
||||
/**
|
||||
* The entity.
|
||||
*/
|
||||
@@ -37,17 +36,25 @@ public abstract class EntityEquipmentInvWrapper implements IItemHandlerModifiabl
|
||||
* @param entity The entity.
|
||||
* @param slotType The slot type to expose.
|
||||
*/
|
||||
public EntityEquipmentInvWrapper(final LivingEntity entity, final EquipmentSlot.Type slotType)
|
||||
{
|
||||
public EntityEquipmentInvWrapper(final LivingEntity entity, final EquipmentSlot.Type mainType) {
|
||||
this(entity, mainType, (EquipmentSlot.Type[])null);
|
||||
}
|
||||
|
||||
public EntityEquipmentInvWrapper(final LivingEntity entity, final EquipmentSlot.Type mainType, final EquipmentSlot.Type... types) {
|
||||
this.entity = entity;
|
||||
|
||||
final List<EquipmentSlot> slots = new ArrayList<EquipmentSlot>();
|
||||
var slots = new ArrayList<EquipmentSlot>();
|
||||
|
||||
for (final EquipmentSlot slot : EquipmentSlot.values())
|
||||
{
|
||||
if (slot.getType() == slotType)
|
||||
{
|
||||
for (var slot : EquipmentSlot.values()) {
|
||||
if (slot.getType() == mainType)
|
||||
slots.add(slot);
|
||||
else if (types != null && types.length > 0) {
|
||||
for (var type : types) {
|
||||
if (slot.getType() == type) {
|
||||
slots.add(slot);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,22 +62,19 @@ public abstract class EntityEquipmentInvWrapper implements IItemHandlerModifiabl
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlots()
|
||||
{
|
||||
public int getSlots() {
|
||||
return slots.size();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ItemStack getStackInSlot(final int slot)
|
||||
{
|
||||
public ItemStack getStackInSlot(final int slot) {
|
||||
return entity.getItemBySlot(validateSlotIndex(slot));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ItemStack insertItem(final int slot, @NotNull final ItemStack stack, final boolean simulate)
|
||||
{
|
||||
public ItemStack insertItem(final int slot, @NotNull final ItemStack stack, final boolean simulate) {
|
||||
if (stack.isEmpty())
|
||||
return ItemStack.EMPTY;
|
||||
|
||||
@@ -80,8 +84,7 @@ public abstract class EntityEquipmentInvWrapper implements IItemHandlerModifiabl
|
||||
|
||||
int limit = getStackLimit(slot, stack);
|
||||
|
||||
if (!existing.isEmpty())
|
||||
{
|
||||
if (!existing.isEmpty()) {
|
||||
if (!ItemHandlerHelper.canItemStacksStack(stack, existing))
|
||||
return stack;
|
||||
|
||||
@@ -93,16 +96,11 @@ public abstract class EntityEquipmentInvWrapper implements IItemHandlerModifiabl
|
||||
|
||||
boolean reachedLimit = stack.getCount() > limit;
|
||||
|
||||
if (!simulate)
|
||||
{
|
||||
if (!simulate) {
|
||||
if (existing.isEmpty())
|
||||
{
|
||||
entity.setItemSlot(equipmentSlot, reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, limit) : stack);
|
||||
}
|
||||
else
|
||||
{
|
||||
existing.grow(reachedLimit ? limit : stack.getCount());
|
||||
}
|
||||
}
|
||||
|
||||
return reachedLimit ? ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - limit) : ItemStack.EMPTY;
|
||||
@@ -110,8 +108,7 @@ public abstract class EntityEquipmentInvWrapper implements IItemHandlerModifiabl
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public ItemStack extractItem(final int slot, final int amount, final boolean simulate)
|
||||
{
|
||||
public ItemStack extractItem(final int slot, final int amount, final boolean simulate) {
|
||||
if (amount == 0)
|
||||
return ItemStack.EMPTY;
|
||||
|
||||
@@ -124,41 +121,31 @@ public abstract class EntityEquipmentInvWrapper implements IItemHandlerModifiabl
|
||||
|
||||
final int toExtract = Math.min(amount, existing.getMaxStackSize());
|
||||
|
||||
if (existing.getCount() <= toExtract)
|
||||
{
|
||||
if (existing.getCount() <= toExtract) {
|
||||
if (!simulate)
|
||||
{
|
||||
entity.setItemSlot(equipmentSlot, ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
return existing;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
if (!simulate)
|
||||
{
|
||||
entity.setItemSlot(equipmentSlot, ItemHandlerHelper.copyStackWithSize(existing, existing.getCount() - toExtract));
|
||||
}
|
||||
|
||||
return ItemHandlerHelper.copyStackWithSize(existing, toExtract);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSlotLimit(final int slot)
|
||||
{
|
||||
public int getSlotLimit(final int slot) {
|
||||
final EquipmentSlot equipmentSlot = validateSlotIndex(slot);
|
||||
return equipmentSlot.getType() == EquipmentSlot.Type.ARMOR ? 1 : 64;
|
||||
return (equipmentSlot.getType() == EquipmentSlot.Type.HUMANOID_ARMOR || equipmentSlot.getType() == EquipmentSlot.Type.ANIMAL_ARMOR) ? 1 : 64;
|
||||
}
|
||||
|
||||
protected int getStackLimit(final int slot, @NotNull final ItemStack stack)
|
||||
{
|
||||
protected int getStackLimit(final int slot, @NotNull final ItemStack stack) {
|
||||
return Math.min(getSlotLimit(slot), stack.getMaxStackSize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStackInSlot(final int slot, @NotNull final ItemStack stack)
|
||||
{
|
||||
public void setStackInSlot(final int slot, @NotNull final ItemStack stack) {
|
||||
final EquipmentSlot equipmentSlot = validateSlotIndex(slot);
|
||||
if (ItemStack.matches(entity.getItemBySlot(equipmentSlot), stack))
|
||||
return;
|
||||
@@ -166,23 +153,21 @@ public abstract class EntityEquipmentInvWrapper implements IItemHandlerModifiabl
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemValid(int slot, @NotNull ItemStack stack)
|
||||
{
|
||||
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected EquipmentSlot validateSlotIndex(final int slot)
|
||||
{
|
||||
protected EquipmentSlot validateSlotIndex(final int slot) {
|
||||
if (slot < 0 || slot >= slots.size())
|
||||
throw new IllegalArgumentException("Slot " + slot + " not in valid range - [0," + slots.size() + ")");
|
||||
|
||||
return slots.get(slot);
|
||||
}
|
||||
|
||||
public static LazyOptional<IItemHandlerModifiable>[] create(LivingEntity entity)
|
||||
{
|
||||
public static LazyOptional<IItemHandlerModifiable>[] create(LivingEntity entity) {
|
||||
@SuppressWarnings("unchecked")
|
||||
LazyOptional<IItemHandlerModifiable>[] ret = new LazyOptional[3];
|
||||
|
||||
ret[0] = LazyOptional.of(() -> new EntityHandsInvWrapper(entity));
|
||||
ret[1] = LazyOptional.of(() -> new EntityArmorInvWrapper(entity));
|
||||
ret[2] = LazyOptional.of(() -> new CombinedInvWrapper(ret[0].orElse(null), ret[1].orElse(null)));
|
||||
|
||||
@@ -27,7 +27,7 @@ public class PlayerArmorInvWrapper extends RangedWrapper
|
||||
EquipmentSlot equ = null;
|
||||
for (EquipmentSlot s : EquipmentSlot.values())
|
||||
{
|
||||
if (s.getType() == EquipmentSlot.Type.ARMOR && s.getIndex() == slot)
|
||||
if (s.getType() == EquipmentSlot.Type.HUMANOID_ARMOR && s.getIndex() == slot)
|
||||
{
|
||||
equ = s;
|
||||
break;
|
||||
|
||||
Reference in new issue
Block a user