Update according to suggestions

This commit is contained in:
Vincent Lee 2016-03-21 20:31:33 -05:00
parent 30db41be9a
commit dabb17f869
7 changed files with 100 additions and 152 deletions

View file

@ -285,7 +285,7 @@
}
this.func_184602_cy();
@@ -2626,4 +2707,34 @@
@@ -2626,4 +2707,27 @@
{
return true;
}
@ -300,16 +300,9 @@
+ {
+ if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
+ {
+ if (facing == net.minecraft.util.EnumFacing.UP)
+ {
+ return (T) armorHandler;
+ } else if (facing.func_176740_k().func_176722_c())
+ {
+ return (T) handHandler;
+ } else
+ {
+ return (T) joinedHandler;
+ }
+ if (facing == null) return (T) joinedHandler;
+ else if (facing.func_176740_k().func_176720_b()) return (T) handHandler;
+ else if (facing.func_176740_k().func_176722_c()) return (T) armorHandler;
+ }
+ return super.getCapability(capability, facing);
+ }

View file

@ -16,7 +16,7 @@
}
this.field_70138_W = 1.0F;
@@ -1793,4 +1795,25 @@
@@ -1793,4 +1795,20 @@
this.field_188477_b = p_i46589_2_;
}
}
@ -27,18 +27,13 @@
+ @Override
+ public <T> T getCapability(net.minecraftforge.common.capabilities.Capability<T> capability, net.minecraft.util.EnumFacing facing)
+ {
+ if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY
+ && itemHandler != null)
+ {
+ return (T) itemHandler;
+ }
+ if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return (T) itemHandler;
+ return super.getCapability(capability, facing);
+ }
+
+ @Override
+ public boolean hasCapability(net.minecraftforge.common.capabilities.Capability<?> capability, net.minecraft.util.EnumFacing facing)
+ {
+ return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY && itemHandler != null
+ || super.hasCapability(capability, facing);
+ return capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
+ }
}

View file

@ -401,7 +401,7 @@
if (this.func_70608_bn())
{
@@ -2340,6 +2447,169 @@
@@ -2340,6 +2447,160 @@
return (float)this.func_110148_a(SharedMonsterAttributes.field_188792_h).func_111126_e();
}
@ -534,8 +534,9 @@
+ public java.util.Collection<ITextComponent> getSuffixes() { return this.suffixes; }
+
+ private final net.minecraftforge.items.IItemHandler playerMainHandler = new net.minecraftforge.items.wrapper.PlayerMainInvWrapper(field_71071_by);
+ private final net.minecraftforge.items.IItemHandler playerArmorHandler = new net.minecraftforge.items.wrapper.PlayerArmorInvWrapper(field_71071_by);
+ private final net.minecraftforge.items.IItemHandler playerOffhandHandler = new net.minecraftforge.items.wrapper.PlayerOffhandInvWrapper(field_71071_by);
+ private final net.minecraftforge.items.IItemHandler playerEquipmentHandler = new net.minecraftforge.items.wrapper.CombinedInvWrapper(
+ new net.minecraftforge.items.wrapper.PlayerArmorInvWrapper(field_71071_by),
+ new net.minecraftforge.items.wrapper.PlayerOffhandInvWrapper(field_71071_by));
+ private final net.minecraftforge.items.IItemHandler playerJoinedHandler = new net.minecraftforge.items.wrapper.PlayerInvWrapper(field_71071_by);
+
+ @Override
@ -543,19 +544,9 @@
+ {
+ if (capability == net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
+ {
+ if (facing == net.minecraft.util.EnumFacing.UP)
+ {
+ return (T) playerArmorHandler;
+ } else if (facing == net.minecraft.util.EnumFacing.DOWN)
+ {
+ return (T) playerMainHandler;
+ } else if (facing.func_176740_k().func_176722_c())
+ {
+ return (T) playerOffhandHandler;
+ } else
+ {
+ return (T) playerJoinedHandler;
+ }
+ if (facing == null) return (T) playerJoinedHandler;
+ else if (facing.func_176740_k().func_176720_b()) return (T) playerMainHandler;
+ else if (facing.func_176740_k().func_176722_c()) return (T) playerEquipmentHandler;
+ }
+ return super.getCapability(capability, facing);
+ }

View file

@ -4,29 +4,14 @@ import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
public class PlayerArmorInvWrapper extends InvWrapper
public class PlayerArmorInvWrapper extends RangedWrapper
{
public final InventoryPlayer inventoryPlayer;
private final int offset;
public PlayerArmorInvWrapper(InventoryPlayer inv)
{
super(inv);
super(new InvWrapper(inv), inv.mainInventory.length, inv.mainInventory.length + inv.armorInventory.length);
inventoryPlayer = inv;
offset = inventoryPlayer.mainInventory.length;
}
@Override
public int getSlots()
{
return inventoryPlayer.armorInventory.length;
}
@Override
public ItemStack getStackInSlot(int slot)
{
return super.getStackInSlot(slot + offset);
}
@Override
@ -44,20 +29,8 @@ public class PlayerArmorInvWrapper extends InvWrapper
// check if it's valid for the armor slot
if (slot < 4 && stack != null && stack.getItem().isValidArmor(stack, equ, inventoryPlayer.player))
{
return super.insertItem(slot + offset, stack, simulate);
return super.insertItem(slot, stack, simulate);
}
return stack;
}
@Override
public void setStackInSlot(int slot, ItemStack stack)
{
super.setStackInSlot(slot + offset, stack);
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
return super.extractItem(slot + offset, amount, simulate);
}
}

View file

@ -8,54 +8,19 @@ import net.minecraft.item.ItemStack;
* Exposes the player inventory WITHOUT the armor inventory as IItemHandler.
* Also takes core of inserting/extracting having the same logic as picking up items.
*/
public class PlayerMainInvWrapper extends InvWrapper
public class PlayerMainInvWrapper extends RangedWrapper
{
public final InventoryPlayer inventoryPlayer;
public PlayerMainInvWrapper(InventoryPlayer inv)
{
super(inv);
super(new InvWrapper(inv), 0, inv.mainInventory.length);
inventoryPlayer = inv;
}
@Override
public int getSlots()
{
return inventoryPlayer.mainInventory.length;
}
@Override
public void setStackInSlot(int slot, ItemStack stack)
{
// prevent setting of armor and offhand
if (slot > getSlots())
{
return;
}
super.setStackInSlot(slot, stack);
}
@Override
public ItemStack getStackInSlot(int slot)
{
// prevent getting of armor and offhand
if (slot > getSlots())
{
return null;
}
return super.getStackInSlot(slot);
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
// prevent inserting into armor and offhand
if (slot > getSlots())
{
return stack;
}
ItemStack rest = super.insertItem(slot, stack, simulate);
if (rest == null || rest.stackSize != stack.stackSize)
{
@ -74,15 +39,4 @@ public class PlayerMainInvWrapper extends InvWrapper
}
return rest;
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
// prevent extraction from armor and offhand
if (slot > getSlots())
{
return null;
}
return super.extractItem(slot, amount, simulate);
}
}

View file

@ -3,47 +3,11 @@ package net.minecraftforge.items.wrapper;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
public class PlayerOffhandInvWrapper extends InvWrapper
public class PlayerOffhandInvWrapper extends RangedWrapper
{
public final InventoryPlayer inventoryPlayer;
private final int offset;
public PlayerOffhandInvWrapper(InventoryPlayer inv)
{
super(inv);
inventoryPlayer = inv;
offset = inventoryPlayer.mainInventory.length + inventoryPlayer.armorInventory.length;
super(new InvWrapper(inv), inv.mainInventory.length + inv.armorInventory.length,
inv.mainInventory.length + inv.armorInventory.length + inv.offHandInventory.length);
}
@Override
public int getSlots()
{
return inventoryPlayer.offHandInventory.length;
}
@Override
public ItemStack getStackInSlot(int slot)
{
return super.getStackInSlot(slot + offset);
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
return super.insertItem(slot + offset, stack, simulate);
}
@Override
public void setStackInSlot(int slot, ItemStack stack)
{
super.setStackInSlot(slot + offset, stack);
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
return super.extractItem(slot + offset, amount, simulate);
}
}

View file

@ -0,0 +1,78 @@
package net.minecraftforge.items.wrapper;
import com.google.common.base.Preconditions;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.IItemHandlerModifiable;
/**
* A wrapper that composes another IItemHandlerModifiable, exposing only a range of the composed slots.
* Shifting of slot indices is handled automatically for you.
*/
public class RangedWrapper implements IItemHandlerModifiable {
private final IItemHandlerModifiable compose;
private final int minSlot;
private final int maxSlot;
public RangedWrapper(IItemHandlerModifiable compose, int minSlot, int maxSlotExclusive)
{
Preconditions.checkArgument(maxSlotExclusive > minSlot, "Max slot must be greater than min slot");
this.compose = compose;
this.minSlot = minSlot;
this.maxSlot = maxSlotExclusive;
}
@Override
public int getSlots()
{
return maxSlot - minSlot;
}
@Override
public ItemStack getStackInSlot(int slot)
{
if (checkSlot(slot))
{
return compose.getStackInSlot(slot + minSlot);
}
return null;
}
@Override
public ItemStack insertItem(int slot, ItemStack stack, boolean simulate)
{
if (checkSlot(slot))
{
return compose.insertItem(slot + minSlot, stack, simulate);
}
return stack;
}
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate)
{
if (checkSlot(slot))
{
return compose.extractItem(slot + minSlot, amount, simulate);
}
return null;
}
@Override
public void setStackInSlot(int slot, ItemStack stack)
{
if (checkSlot(slot))
{
compose.setStackInSlot(slot + minSlot, stack);
}
}
private boolean checkSlot(int localSlot)
{
return localSlot + minSlot < maxSlot;
}
}