Skip to content

Commit

Permalink
IDrawerGroup implementations all provide IItemHandler and IItemReposi…
Browse files Browse the repository at this point in the history
…tory capabilities.
  • Loading branch information
jaquadro committed Jul 8, 2017
1 parent e58a839 commit f6b878e
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public interface IDrawerGroup extends ICapabilityProvider
@Nonnull
int[] getAccessibleDrawerSlots ();

/**
* Implementors are strongly encouraged to provide IItemHandler and IItemRepository capabilities.
*/
@Override
default boolean hasCapability (@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IProtectable;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute;
import com.jaquadro.minecraft.storagedrawers.block.BlockSlave;
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerGroupItemRepository;
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerItemRepository;
import com.jaquadro.minecraft.storagedrawers.core.ModBlocks;
import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler;
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerItemHandler;
import com.jaquadro.minecraft.storagedrawers.security.SecurityManager;
import com.jaquadro.minecraft.storagedrawers.util.ItemMetaCollectionRegistry;
import com.mojang.authlib.GameProfile;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
Expand Down Expand Up @@ -677,7 +675,7 @@ public <T> T getCapability (@Nonnull Capability<T> capability, @Nullable EnumFac
return super.getCapability(capability, facing);
}

private class ItemRepository extends DrawerGroupItemRepository
private class ItemRepository extends DrawerItemRepository
{
public ItemRepository (IDrawerGroup group) {
super(group);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.ControllerData;
import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.MaterialData;
import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.UpgradeData;
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerGroupItemRepository;
import com.jaquadro.minecraft.storagedrawers.core.ModItems;
import com.jaquadro.minecraft.storagedrawers.capabilities.BasicDrawerAttributes;
import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler;
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerItemHandler;
import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeRedstone;
import com.jaquadro.minecraft.storagedrawers.item.EnumUpgradeStorage;
import com.jaquadro.minecraft.storagedrawers.network.CountUpdateMessage;
Expand Down Expand Up @@ -618,23 +617,15 @@ public int[] getAccessibleDrawerSlots () {
@CapabilityInject(IDrawerGroup.class)
public static Capability<IDrawerGroup> DRAWER_GROUP_CAPABILITY = null;
@CapabilityInject(IItemRepository.class)
public static Capability<IItemRepository> ITEM_REPOSITORY_CAPABILITY = null;
@CapabilityInject(IDrawerAttributes.class)
static Capability<IDrawerAttributes> DRAWER_ATTRIBUTES_CAPABILITY = null;
@CapabilityInject(IItemHandler.class)
static Capability<IItemHandler> ITEM_HANDLER_CAPABILITY = null;

private IItemHandler itemHandler = new DrawerItemHandler(this);
private IItemRepository itemRepository = new DrawerGroupItemRepository(this);

@SuppressWarnings("unchecked")
@Override
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing)
{
if (capability == ITEM_HANDLER_CAPABILITY)
return (T) itemHandler;
if (capability == ITEM_REPOSITORY_CAPABILITY)
return (T) itemRepository;
if (getGroup().hasCapability(capability, facing))
return getGroup().getCapability(capability, facing);

if (capability == DRAWER_ATTRIBUTES_CAPABILITY)
return (T) drawerAttributes;
if (capability == DRAWER_GROUP_CAPABILITY)
Expand All @@ -646,9 +637,10 @@ public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFaci
@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing)
{
return capability == ITEM_HANDLER_CAPABILITY
|| capability == ITEM_REPOSITORY_CAPABILITY
|| capability == DRAWER_ATTRIBUTES_CAPABILITY
if (getGroup().hasCapability(capability, facing))
return true;

return capability == DRAWER_ATTRIBUTES_CAPABILITY
|| capability == DRAWER_GROUP_CAPABILITY
|| super.hasCapability(capability, facing);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer;
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup;
import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.ControllerData;
import com.jaquadro.minecraft.storagedrawers.inventory.DrawerItemHandler;
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerItemHandler;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
Expand All @@ -15,7 +15,6 @@
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;

import javax.annotation.Nonnull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.jaquadro.minecraft.storagedrawers.block.tile.tiledata;

import com.jaquadro.minecraft.chameleon.block.tiledata.TileDataShim;
import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository;
import com.jaquadro.minecraft.storagedrawers.api.storage.*;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute;
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerItemHandler;
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerItemRepository;
import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper;
import com.jaquadro.minecraft.storagedrawers.util.CompactingHelper;
import com.jaquadro.minecraft.storagedrawers.util.ItemStackMatcher;
Expand All @@ -11,24 +14,38 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.items.IItemHandler;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Stack;
import java.util.function.Predicate;

public class FractionalDrawerGroup extends TileDataShim implements IDrawerGroup
{
@CapabilityInject(IItemHandler.class)
public static Capability<IItemHandler> ITEM_HANDLER_CAPABILITY = null;
@CapabilityInject(IItemRepository.class)
public static Capability<IItemRepository> ITEM_REPOSITORY_CAPABILITY = null;

private FractionalStorage storage;
private FractionalDrawer[] slots;
private int[] order;

private final IItemHandler itemHandler;
private final IItemRepository itemRepository;

public FractionalDrawerGroup (int slotCount) {
itemHandler = new DrawerItemHandler(this);
itemRepository = new DrawerItemRepository(this);

storage = new FractionalStorage(this, slotCount);

slots = new FractionalDrawer[slotCount];
Expand Down Expand Up @@ -86,6 +103,24 @@ public NBTTagCompound writeToNBT (NBTTagCompound tag) {
return tag;
}

@Override
public boolean hasCapability (@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
return capability == ITEM_HANDLER_CAPABILITY
|| capability == ITEM_REPOSITORY_CAPABILITY;
}

@Nullable
@Override
@SuppressWarnings("unchecked")
public <T> T getCapability (@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
if (capability == ITEM_HANDLER_CAPABILITY)
return (T) itemHandler;
if (capability == ITEM_REPOSITORY_CAPABILITY)
return (T) itemRepository;

return null;
}

public void syncAttributes () {
storage.syncAttributes();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
package com.jaquadro.minecraft.storagedrawers.block.tile.tiledata;

import com.jaquadro.minecraft.chameleon.block.tiledata.TileDataShim;
import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository;
import com.jaquadro.minecraft.storagedrawers.api.storage.*;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute;
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerItemHandler;
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerItemRepository;
import com.jaquadro.minecraft.storagedrawers.inventory.ItemStackHelper;
import com.jaquadro.minecraft.storagedrawers.util.ItemStackMatcher;
import com.jaquadro.minecraft.storagedrawers.util.ItemStackOreMatcher;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.items.IItemHandler;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.function.Predicate;

public abstract class StandardDrawerGroup extends TileDataShim implements IDrawerGroup
{
@CapabilityInject(IItemHandler.class)
public static Capability<IItemHandler> ITEM_HANDLER_CAPABILITY = null;
@CapabilityInject(IItemRepository.class)
public static Capability<IItemRepository> ITEM_REPOSITORY_CAPABILITY = null;

private DrawerData[] slots;
private int[] order;

private final IItemHandler itemHandler;
private final IItemRepository itemRepository;

public StandardDrawerGroup (int slotCount) {
itemHandler = new DrawerItemHandler(this);
itemRepository = new DrawerItemRepository(this);

slots = new DrawerData[slotCount];
for (int i = 0; i < slotCount; i++)
slots[i] = createDrawer(i);
Expand Down Expand Up @@ -100,6 +117,24 @@ public NBTTagCompound writeToNBT (NBTTagCompound tag) {
return tag;
}

@Override
public boolean hasCapability (@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
return capability == ITEM_HANDLER_CAPABILITY
|| capability == ITEM_REPOSITORY_CAPABILITY;
}

@Nullable
@Override
@SuppressWarnings("unchecked")
public <T> T getCapability (@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
if (capability == ITEM_HANDLER_CAPABILITY)
return (T) itemHandler;
if (capability == ITEM_REPOSITORY_CAPABILITY)
return (T) itemRepository;

return null;
}

@Nonnull
protected abstract DrawerData createDrawer (int slot);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jaquadro.minecraft.storagedrawers.inventory;
package com.jaquadro.minecraft.storagedrawers.capabilities;

import com.jaquadro.minecraft.storagedrawers.StorageDrawers;
import com.jaquadro.minecraft.storagedrawers.api.capabilities.IItemRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import javax.annotation.Nonnull;
import java.util.function.Predicate;

public class DrawerGroupItemRepository implements IItemRepository
public class DrawerItemRepository implements IItemRepository
{
protected IDrawerGroup group;

public DrawerGroupItemRepository (IDrawerGroup group) {
public DrawerItemRepository (IDrawerGroup group) {
this.group = group;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.fml.common.event.FMLInterModComms;

import javax.annotation.Nonnull;
Expand Down

0 comments on commit f6b878e

Please sign in to comment.