simpler
This commit is contained in:
1 parent
dd9d6659fb
commit
2d92a82ed0
1 file changed
+51
-21
@@ -1,23 +1,21 @@
|
|||||||
package carpet.script.utils;
|
package carpet.script.utils;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
|
||||||
import net.minecraft.entity.EquipmentSlot;
|
import net.minecraft.entity.EquipmentSlot;
|
||||||
import net.minecraft.entity.LivingEntity;
|
import net.minecraft.entity.LivingEntity;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.inventory.Inventory;
|
import net.minecraft.inventory.Inventory;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class LivingInventory implements Inventory
|
public class LivingInventory implements Inventory
|
||||||
{
|
{
|
||||||
private static final Int2ObjectMap<EquipmentSlot> slotToSlot = new Int2ObjectOpenHashMap<EquipmentSlot>(){{
|
private static final List<EquipmentSlot> slotToSlot = Arrays.asList(
|
||||||
put(0, EquipmentSlot.MAINHAND);
|
EquipmentSlot.MAINHAND,
|
||||||
put(1, EquipmentSlot.FEET);
|
EquipmentSlot.FEET, EquipmentSlot.LEGS, EquipmentSlot.CHEST, EquipmentSlot.HEAD,
|
||||||
put(2, EquipmentSlot.LEGS);
|
EquipmentSlot.OFFHAND
|
||||||
put(3, EquipmentSlot.CHEST);
|
);
|
||||||
put(4, EquipmentSlot.HEAD);
|
|
||||||
put(5, EquipmentSlot.OFFHAND);
|
|
||||||
}};
|
|
||||||
|
|
||||||
LivingEntity mob;
|
LivingEntity mob;
|
||||||
public LivingInventory(LivingEntity mob)
|
public LivingInventory(LivingEntity mob)
|
||||||
@@ -34,7 +32,7 @@ public class LivingInventory implements Inventory
|
|||||||
@Override
|
@Override
|
||||||
public boolean isEmpty()
|
public boolean isEmpty()
|
||||||
{
|
{
|
||||||
for (EquipmentSlot slot: EquipmentSlot.values())
|
for (EquipmentSlot slot: slotToSlot)
|
||||||
if (!mob.getEquippedStack(slot).isEmpty()) return false;
|
if (!mob.getEquippedStack(slot).isEmpty()) return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -42,16 +40,32 @@ public class LivingInventory implements Inventory
|
|||||||
@Override
|
@Override
|
||||||
public ItemStack getStack(int slot)
|
public ItemStack getStack(int slot)
|
||||||
{
|
{
|
||||||
EquipmentSlot slotSlot = slotToSlot.getOrDefault(slot, null);
|
EquipmentSlot slotSlot;
|
||||||
if (slotSlot == null) return null;
|
try
|
||||||
return mob.getEquippedStack(slotToSlot.get(slot));
|
{
|
||||||
|
slotSlot = slotToSlot.get(slot);
|
||||||
|
}
|
||||||
|
catch (IndexOutOfBoundsException ignored)
|
||||||
|
{
|
||||||
|
//going out of the index should be really exceptional
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return mob.getEquippedStack(slotSlot);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack removeStack(int slot, int amount)
|
public ItemStack removeStack(int slot, int amount)
|
||||||
{
|
{
|
||||||
EquipmentSlot slotSlot = slotToSlot.getOrDefault(slot, null);
|
EquipmentSlot slotSlot;
|
||||||
if (slotSlot == null) return null;
|
try
|
||||||
|
{
|
||||||
|
slotSlot = slotToSlot.get(slot);
|
||||||
|
}
|
||||||
|
catch (IndexOutOfBoundsException ignored)
|
||||||
|
{
|
||||||
|
//going out of the index should be really exceptional
|
||||||
|
return null;
|
||||||
|
}
|
||||||
ItemStack stack = mob.getEquippedStack(slotSlot);
|
ItemStack stack = mob.getEquippedStack(slotSlot);
|
||||||
stack.decrement(amount);
|
stack.decrement(amount);
|
||||||
return stack;
|
return stack;
|
||||||
@@ -60,8 +74,16 @@ public class LivingInventory implements Inventory
|
|||||||
@Override
|
@Override
|
||||||
public ItemStack removeStack(int slot)
|
public ItemStack removeStack(int slot)
|
||||||
{
|
{
|
||||||
EquipmentSlot slotSlot = slotToSlot.getOrDefault(slot, null);
|
EquipmentSlot slotSlot;
|
||||||
if (slotSlot == null) return null;
|
try
|
||||||
|
{
|
||||||
|
slotSlot = slotToSlot.get(slot);
|
||||||
|
}
|
||||||
|
catch (IndexOutOfBoundsException ignored)
|
||||||
|
{
|
||||||
|
//going out of the index should be really exceptional
|
||||||
|
return null;
|
||||||
|
}
|
||||||
ItemStack previous = mob.getEquippedStack(slotSlot);
|
ItemStack previous = mob.getEquippedStack(slotSlot);
|
||||||
mob.equipStack(slotSlot, ItemStack.EMPTY);
|
mob.equipStack(slotSlot, ItemStack.EMPTY);
|
||||||
return previous;
|
return previous;
|
||||||
@@ -70,8 +92,16 @@ public class LivingInventory implements Inventory
|
|||||||
@Override
|
@Override
|
||||||
public void setStack(int slot, ItemStack stack)
|
public void setStack(int slot, ItemStack stack)
|
||||||
{
|
{
|
||||||
EquipmentSlot slotSlot = slotToSlot.getOrDefault(slot, null);
|
EquipmentSlot slotSlot;
|
||||||
if (slotSlot == null) return;
|
try
|
||||||
|
{
|
||||||
|
slotSlot = slotToSlot.get(slot);
|
||||||
|
}
|
||||||
|
catch (IndexOutOfBoundsException ignored)
|
||||||
|
{
|
||||||
|
//going out of the index should be really exceptional
|
||||||
|
return;
|
||||||
|
}
|
||||||
mob.equipStack(slotSlot, stack);
|
mob.equipStack(slotSlot, stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +120,7 @@ public class LivingInventory implements Inventory
|
|||||||
@Override
|
@Override
|
||||||
public void clear()
|
public void clear()
|
||||||
{
|
{
|
||||||
for (EquipmentSlot slot: EquipmentSlot.values())
|
for (EquipmentSlot slot: slotToSlot)
|
||||||
mob.equipStack(slot, ItemStack.EMPTY);
|
mob.equipStack(slot, ItemStack.EMPTY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user