refactor for inventories, allowing for null types.
This commit is contained in:
1 parent
2d92a82ed0
commit
79cb4a20d8
3 files changed
+89
-75
No files matched your search
@@ -0,0 +1,126 @@
|
||||
package carpet.script.utils;
|
||||
|
||||
import net.minecraft.entity.EquipmentSlot;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.inventory.Inventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class EquipmentInventory implements Inventory
|
||||
{
|
||||
private static final List<EquipmentSlot> slotToSlot = Arrays.asList(
|
||||
EquipmentSlot.MAINHAND,
|
||||
EquipmentSlot.FEET, EquipmentSlot.LEGS, EquipmentSlot.CHEST, EquipmentSlot.HEAD,
|
||||
EquipmentSlot.OFFHAND
|
||||
);
|
||||
|
||||
LivingEntity mob;
|
||||
public EquipmentInventory(LivingEntity mob)
|
||||
{
|
||||
this.mob = mob;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size()
|
||||
{
|
||||
return 6;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
{
|
||||
for (EquipmentSlot slot: slotToSlot)
|
||||
if (!mob.getEquippedStack(slot).isEmpty()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack getStack(int slot)
|
||||
{
|
||||
EquipmentSlot slotSlot;
|
||||
try
|
||||
{
|
||||
slotSlot = slotToSlot.get(slot);
|
||||
}
|
||||
catch (IndexOutOfBoundsException ignored)
|
||||
{
|
||||
//going out of the index should be really exceptional
|
||||
return null;
|
||||
}
|
||||
return mob.getEquippedStack(slotSlot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack removeStack(int slot, int amount)
|
||||
{
|
||||
EquipmentSlot slotSlot;
|
||||
try
|
||||
{
|
||||
slotSlot = slotToSlot.get(slot);
|
||||
}
|
||||
catch (IndexOutOfBoundsException ignored)
|
||||
{
|
||||
//going out of the index should be really exceptional
|
||||
return null;
|
||||
}
|
||||
ItemStack stack = mob.getEquippedStack(slotSlot);
|
||||
stack.decrement(amount);
|
||||
return stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack removeStack(int slot)
|
||||
{
|
||||
EquipmentSlot slotSlot;
|
||||
try
|
||||
{
|
||||
slotSlot = slotToSlot.get(slot);
|
||||
}
|
||||
catch (IndexOutOfBoundsException ignored)
|
||||
{
|
||||
//going out of the index should be really exceptional
|
||||
return null;
|
||||
}
|
||||
ItemStack previous = mob.getEquippedStack(slotSlot);
|
||||
mob.equipStack(slotSlot, ItemStack.EMPTY);
|
||||
return previous;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStack(int slot, ItemStack stack)
|
||||
{
|
||||
EquipmentSlot slotSlot;
|
||||
try
|
||||
{
|
||||
slotSlot = slotToSlot.get(slot);
|
||||
}
|
||||
catch (IndexOutOfBoundsException ignored)
|
||||
{
|
||||
//going out of the index should be really exceptional
|
||||
return;
|
||||
}
|
||||
mob.equipStack(slotSlot, stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markDirty()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canPlayerUse(PlayerEntity player)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear()
|
||||
{
|
||||
for (EquipmentSlot slot: slotToSlot)
|
||||
mob.equipStack(slot, ItemStack.EMPTY);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user