use of com.almuradev.almura.shared.tileentity.SingleSlotTileEntity in project Almura by AlmuraDev.
the class CacheBlock method getPickBlock.
@Override
public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
final ItemStack pickStack = new ItemStack(this, 1, 0);
final TileEntity te = world.getTileEntity(pos);
if (te != null && te instanceof SingleSlotTileEntity) {
final SingleSlotTileEntity cte = (SingleSlotTileEntity) te;
// Alright, we have a Cache...need to put it on a stack now
this.addCacheToStack(pickStack, cte);
}
return pickStack;
}
use of com.almuradev.almura.shared.tileentity.SingleSlotTileEntity in project Almura by AlmuraDev.
the class CacheBlock method onBlockClicked.
// Left Click
@Override
public void onBlockClicked(World world, BlockPos pos, EntityPlayer player) {
if (world.isRemote) {
return;
}
final TileEntity te = world.getTileEntity(pos);
if (te == null || !(te instanceof SingleSlotTileEntity)) {
return;
}
final SingleSlotTileEntity cte = (SingleSlotTileEntity) te;
final ISingleSlotItemHandler itemHandler = (ISingleSlotItemHandler) cte.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if (itemHandler == null) {
return;
}
final ItemStack cacheStack = itemHandler.getStackInSlot(0);
if (cacheStack.isEmpty()) {
return;
}
int amountToExtract = 0;
if (player.isSneaking()) {
amountToExtract = 1;
} else {
amountToExtract = Math.min(cacheStack.getItem().getItemStackLimit(), player.inventory.getInventoryStackLimit());
}
final ItemStack toExtract = itemHandler.extractItem(0, amountToExtract, true);
if (toExtract.isEmpty()) {
return;
}
final int preAddStackSize = toExtract.getCount();
player.inventory.addItemStackToInventory(toExtract);
final int postAddStackSize = toExtract.getCount();
final int extractDiff = preAddStackSize - postAddStackSize;
if (extractDiff <= 0) {
((Player) player).sendMessage(Text.of("Cannot withdrawal from cache as your inventory is full!"));
} else {
itemHandler.extractItem(0, extractDiff, false);
((Player) player).sendMessage(Text.of("Withdrew ", TextColors.AQUA, extractDiff, TextColors.WHITE, " ", cacheStack.getDisplayName(), " " + "from the cache."));
}
}
use of com.almuradev.almura.shared.tileentity.SingleSlotTileEntity in project Almura by AlmuraDev.
the class CacheBlock method createNewTileEntity.
@Nullable
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
final SingleSlotTileEntity cte = new SingleSlotTileEntity();
final ISingleSlotItemHandler itemHandler = (ISingleSlotItemHandler) cte.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
((SingleSlotItemHandler) itemHandler).setSlotLimit(this.slotLimit);
return cte;
}
use of com.almuradev.almura.shared.tileentity.SingleSlotTileEntity in project Almura by AlmuraDev.
the class CacheBlock method onBlockActivated.
// Right click
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
if (world.isRemote) {
return true;
}
if (player.isSneaking()) {
return false;
}
final TileEntity te = world.getTileEntity(pos);
if (te == null || !(te instanceof SingleSlotTileEntity)) {
return false;
}
final SingleSlotTileEntity cte = (SingleSlotTileEntity) te;
final ISingleSlotItemHandler itemHandler = (ISingleSlotItemHandler) cte.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if (itemHandler == null) {
return false;
}
boolean isDirty = false;
/*
* How cache interaction works:
*
* 1. If filled hand and cache does not exist, merge filled hand to become a new cache (up to cache limit).
* 2. If empty hand and cache exists, withdrawal from the cache up to the inventory stack limit or the cache usage, whichever is lower.
* 3. If cache exists, search for and merge appropriate stack from inventory (up to cache limit)
*/
final ItemStack handStack = player.getHeldItem(hand);
final ItemStack cacheStack = itemHandler.getStackInSlot(0);
if (!handStack.isEmpty() && cacheStack.isEmpty()) {
final ItemStack resultStack = itemHandler.insertItem(0, handStack, false);
// Creative starts a new cache without removing items
if (!player.isCreative()) {
player.setHeldItem(hand, resultStack);
}
((Player) player).sendMessage(Text.of("Deposited ", TextColors.AQUA, CacheFeature.format.format(Math.abs(resultStack.getCount() - handStack.getCount())), TextColors.WHITE, " ", handStack.getDisplayName(), " into the cache."));
isDirty = true;
} else if (!cacheStack.isEmpty()) {
if (!handStack.isEmpty() && !ItemStack.areItemsEqual(handStack, cacheStack)) {
((Player) player).sendMessage(Text.of("This cache does not support this item! Try emptying the cache first."));
return true;
}
// Creative adds to cache indefinitely.
if (player.isCreative() && !handStack.isEmpty()) {
final ItemStack resultStack = itemHandler.insertItem(0, handStack, false);
if (resultStack.getCount() == handStack.getCount()) {
((Player) player).sendMessage(Text.of("Cannot add more to the cache as it is full!"));
} else {
((Player) player).sendMessage(Text.of("Deposited ", TextColors.AQUA, CacheFeature.format.format(handStack.getCount() - resultStack.getCount()), TextColors.WHITE, " ", handStack.getDisplayName(), " into the cache."));
isDirty = true;
}
} else {
final boolean depositAll = handStack.isEmpty();
// Search inventory for items that are similar to the cache (only non-matching aspect is the stack size)
// TODO Please be kind PhaseTracker
int deposited = 0;
for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
final ItemStack slotStack = player.inventory.getStackInSlot(i);
if (slotStack.isEmpty()) {
continue;
}
// Slot stack matches cache stack, merge it
if (slotStack.isItemEqual(cacheStack)) {
final ItemStack remaining = itemHandler.insertItem(0, slotStack, false);
player.inventory.setInventorySlotContents(i, remaining);
deposited += slotStack.getCount() - remaining.getCount();
isDirty = true;
if (!remaining.isEmpty()) {
break;
}
if (!depositAll) {
break;
}
}
}
if (deposited > 0) {
((Player) player).sendMessage(Text.of("Deposited ", TextColors.AQUA, CacheFeature.format.format(deposited), TextColors.WHITE, " ", cacheStack.getDisplayName(), " into the cache."));
}
}
}
if (isDirty) {
cte.markDirty();
}
return true;
}
use of com.almuradev.almura.shared.tileentity.SingleSlotTileEntity in project Almura by AlmuraDev.
the class CacheBlock method onBlockPlacedBy.
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
if (!worldIn.isRemote) {
final NBTTagCompound tagCompound = stack.getSubCompound("tag");
if (tagCompound == null) {
return;
}
if (!tagCompound.hasKey("Cache")) {
return;
}
final NBTTagCompound cacheCompound = tagCompound.getCompoundTag("Cache");
final TileEntity te = worldIn.getTileEntity(pos);
if (te == null || !(te instanceof SingleSlotTileEntity)) {
return;
}
final SingleSlotTileEntity cte = (SingleSlotTileEntity) te;
final ISingleSlotItemHandler itemHandler = (ISingleSlotItemHandler) cte.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if (itemHandler == null) {
return;
}
SharedCapabilities.SINGLE_SLOT_ITEM_HANDLER_CAPABILITY.readNBT(itemHandler, null, cacheCompound.getCompoundTag(Almura.ID + ":single_slot"));
}
}
Aggregations