1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-27 17:34:48 +00:00

Refactor common inventory code into a shared interface

Many bits of IInventory (open/close, fields, etc...) are not actually
needed most implementations, so we can clean things up a little with a
common interface.
This commit is contained in:
SquidDev 2018-12-28 11:39:59 +00:00
parent b59dcbfc0e
commit 57fb77d7fe
5 changed files with 89 additions and 144 deletions

View File

@ -15,12 +15,12 @@ import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.common.BlockPeripheral; import dan200.computercraft.shared.peripheral.common.BlockPeripheral;
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase; import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
import dan200.computercraft.shared.util.DefaultInventory;
import dan200.computercraft.shared.util.InventoryUtil; import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.RecordUtil; import dan200.computercraft.shared.util.RecordUtil;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
@ -43,8 +43,7 @@ import java.util.Set;
import static net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY; import static net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
public class TileDiskDrive extends TilePeripheralBase public class TileDiskDrive extends TilePeripheralBase implements DefaultInventory
implements IInventory
{ {
private static class MountInfo private static class MountInfo
{ {
@ -355,28 +354,6 @@ public class TileDiskDrive extends TilePeripheralBase
} }
} }
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public void openInventory( @Nonnull EntityPlayer player )
{
}
@Override
public void closeInventory( @Nonnull EntityPlayer player )
{
}
@Override
public boolean isItemValidForSlot( int i, @Nonnull ItemStack itemstack )
{
return true;
}
@Override @Override
public boolean isUsableByPlayer( @Nonnull EntityPlayer player ) public boolean isUsableByPlayer( @Nonnull EntityPlayer player )
{ {
@ -389,23 +366,6 @@ public class TileDiskDrive extends TilePeripheralBase
setInventorySlotContents( 0, ItemStack.EMPTY ); setInventorySlotContents( 0, ItemStack.EMPTY );
} }
@Override
public int getFieldCount()
{
return 0;
}
@Override
public int getField( int id )
{
return 0;
}
@Override
public void setField( int id, int value )
{
}
// IPeripheralTile implementation // IPeripheralTile implementation
@Override @Override

View File

@ -12,12 +12,12 @@ import dan200.computercraft.core.terminal.Terminal;
import dan200.computercraft.shared.media.items.ItemPrintout; import dan200.computercraft.shared.media.items.ItemPrintout;
import dan200.computercraft.shared.peripheral.PeripheralType; import dan200.computercraft.shared.peripheral.PeripheralType;
import dan200.computercraft.shared.peripheral.common.TilePeripheralBase; import dan200.computercraft.shared.peripheral.common.TilePeripheralBase;
import dan200.computercraft.shared.util.DefaultSidedInventory;
import dan200.computercraft.shared.util.InventoryUtil; import dan200.computercraft.shared.util.InventoryUtil;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items; import net.minecraft.init.Items;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
@ -41,8 +41,7 @@ import javax.annotation.Nullable;
import static net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY; import static net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
public class TilePrinter extends TilePeripheralBase public class TilePrinter extends TilePeripheralBase implements DefaultSidedInventory
implements ISidedInventory
{ {
// Statics // Statics
@ -298,51 +297,12 @@ public class TilePrinter extends TilePeripheralBase
} }
} }
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public void openInventory( @Nonnull EntityPlayer player )
{
}
@Override
public void closeInventory( @Nonnull EntityPlayer player )
{
}
@Override
public boolean isItemValidForSlot( int slot, @Nonnull ItemStack itemstack )
{
return true;
}
@Override @Override
public boolean isUsableByPlayer( @Nonnull EntityPlayer player ) public boolean isUsableByPlayer( @Nonnull EntityPlayer player )
{ {
return isUsable( player, false ); return isUsable( player, false );
} }
@Override
public int getFieldCount()
{
return 0;
}
@Override
public int getField( int id )
{
return 0;
}
@Override
public void setField( int id, int value )
{
}
// ISidedInventory implementation // ISidedInventory implementation
@Nonnull @Nonnull
@ -360,18 +320,6 @@ public class TilePrinter extends TilePeripheralBase
} }
} }
@Override
public boolean canInsertItem( int slot, @Nonnull ItemStack itemstack, @Nonnull EnumFacing face )
{
return isItemValidForSlot( slot, itemstack );
}
@Override
public boolean canExtractItem( int slot, @Nonnull ItemStack itemstack, @Nonnull EnumFacing face )
{
return true;
}
// IPeripheralTile implementation // IPeripheralTile implementation
@Override @Override

View File

@ -19,12 +19,12 @@ import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.turtle.apis.TurtleAPI; import dan200.computercraft.shared.turtle.apis.TurtleAPI;
import dan200.computercraft.shared.turtle.core.TurtleBrain; import dan200.computercraft.shared.turtle.core.TurtleBrain;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory; import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
import dan200.computercraft.shared.util.DefaultInventory;
import dan200.computercraft.shared.util.InventoryUtil; import dan200.computercraft.shared.util.InventoryUtil;
import dan200.computercraft.shared.util.RedstoneUtil; import dan200.computercraft.shared.util.RedstoneUtil;
import dan200.computercraft.shared.util.WorldUtil; import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items; import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList; import net.minecraft.nbt.NBTTagList;
@ -32,7 +32,6 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand; import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList; import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.ITextComponent;
@ -48,8 +47,7 @@ import javax.annotation.Nullable;
import static net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY; import static net.minecraftforge.items.CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
public class TileTurtle extends TileComputerBase public class TileTurtle extends TileComputerBase implements ITurtleTile, DefaultInventory
implements ITurtleTile, IInventory
{ {
// Statics // Statics
@ -594,28 +592,6 @@ public class TileTurtle extends TileComputerBase
} }
} }
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public void openInventory( @Nonnull EntityPlayer player )
{
}
@Override
public void closeInventory( @Nonnull EntityPlayer player )
{
}
@Override
public boolean isItemValidForSlot( int slot, @Nonnull ItemStack stack )
{
return true;
}
@Override @Override
public void markDirty() public void markDirty()
{ {
@ -642,28 +618,6 @@ public class TileTurtle extends TileComputerBase
return isUsable( player, false ); return isUsable( player, false );
} }
@Override
public int getFieldCount()
{
return 0;
}
@Override
public int getField( int id )
{
return 0;
}
@Override
public void setField( int id, int value )
{
}
public boolean isUseableByRemote( EntityPlayer player )
{
return isUsable( player, true );
}
public void onInventoryDefinitelyChanged() public void onInventoryDefinitelyChanged()
{ {
super.markDirty(); super.markDirty();

View File

@ -0,0 +1,55 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.util;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import javax.annotation.Nonnull;
public interface DefaultInventory extends IInventory
{
@Override
default int getInventoryStackLimit()
{
return 64;
}
@Override
default void openInventory( @Nonnull EntityPlayer player )
{
}
@Override
default void closeInventory( @Nonnull EntityPlayer player )
{
}
@Override
default boolean isItemValidForSlot( int slot, @Nonnull ItemStack stack )
{
return true;
}
@Override
default int getField( int field )
{
return 0;
}
@Override
default void setField( int field, int value )
{
}
@Override
default int getFieldCount()
{
return 0;
}
}

View File

@ -0,0 +1,28 @@
/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.util;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import javax.annotation.Nonnull;
public interface DefaultSidedInventory extends DefaultInventory, ISidedInventory
{
@Override
default boolean canInsertItem( int slot, @Nonnull ItemStack stack, @Nonnull EnumFacing side )
{
return isItemValidForSlot( slot, stack );
}
@Override
default boolean canExtractItem( int slot, @Nonnull ItemStack stack, @Nonnull EnumFacing side )
{
return true;
}
}