1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-09-20 19:29:44 +00:00
CC-Tweaked/src/main/java/dan200/computercraft/shared/computer/blocks/BlockComputerBase.java
2019-04-03 23:27:10 +01:00

187 lines
6.6 KiB
Java

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.computer.blocks;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.common.BlockGeneric;
import dan200.computercraft.shared.common.IBundledRedstoneBlock;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer;
import dan200.computercraft.shared.computer.items.IComputerItem;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
public abstract class BlockComputerBase<T extends TileComputerBase> extends BlockGeneric implements IBundledRedstoneBlock
{
public static final Identifier COMPUTER_DROP = new Identifier( ComputerCraft.MOD_ID, "computer" );
private final ComputerFamily family;
protected BlockComputerBase( Settings settings, ComputerFamily family, BlockEntityType<? extends T> type )
{
super( settings, type );
this.family = family;
}
@Override
@Deprecated
public void onBlockAdded( BlockState state, World world, BlockPos pos, BlockState oldState, boolean flag )
{
super.onBlockAdded( state, world, pos, oldState, flag );
BlockEntity tile = world.getBlockEntity( pos );
if( tile instanceof TileComputerBase ) ((TileComputerBase) tile).updateInput();
}
@Override
@Deprecated
public boolean emitsRedstonePower( BlockState state )
{
return true;
}
@Override
@Deprecated
public int getStrongRedstonePower( BlockState state, BlockView world, BlockPos pos, Direction incomingSide )
{
BlockEntity entity = world.getBlockEntity( pos );
if( !(entity instanceof TileComputerBase) ) return 0;
TileComputerBase computerEntity = (TileComputerBase) entity;
ServerComputer computer = computerEntity.getServerComputer();
if( computer == null ) return 0;
Direction localSide = computerEntity.remapToLocalSide( incomingSide.getOpposite() );
return computerEntity.isRedstoneBlockedOnSide( localSide ) ? 0 :
computer.getRedstoneOutput( localSide.getId() );
}
@Nonnull
protected abstract ItemStack getItem( TileComputerBase tile );
public ComputerFamily getFamily()
{
return family;
}
@Override
@Deprecated
public int getWeakRedstonePower( BlockState state, BlockView world, BlockPos pos, Direction incomingSide )
{
return getStrongRedstonePower( state, world, pos, incomingSide );
}
@Override
public boolean getBundledRedstoneConnectivity( World world, BlockPos pos, Direction side )
{
BlockEntity entity = world.getBlockEntity( pos );
if( !(entity instanceof TileComputerBase) ) return false;
TileComputerBase computerEntity = (TileComputerBase) entity;
return !computerEntity.isRedstoneBlockedOnSide( computerEntity.remapToLocalSide( side ) );
}
@Override
public int getBundledRedstoneOutput( World world, BlockPos pos, Direction side )
{
BlockEntity entity = world.getBlockEntity( pos );
if( !(entity instanceof TileComputerBase) ) return 0;
TileComputerBase computerEntity = (TileComputerBase) entity;
ServerComputer computer = computerEntity.getServerComputer();
if( computer == null ) return 0;
Direction localSide = computerEntity.remapToLocalSide( side );
return computerEntity.isRedstoneBlockedOnSide( localSide ) ? 0 :
computer.getBundledRedstoneOutput( localSide.getId() );
}
@Nonnull
@Override
public ItemStack getPickStack( BlockView world, BlockPos pos, BlockState state )
{
BlockEntity tile = world.getBlockEntity( pos );
if( tile instanceof TileComputerBase )
{
ItemStack result = getItem( (TileComputerBase) tile );
if( !result.isEmpty() ) return result;
}
return super.getPickStack( world, pos, state );
}
/*
TODO: Find a way of doing creative block drops
@Override
@Deprecated
public final void dropBlockAsItemWithChance( @Nonnull BlockState state, World world, @Nonnull BlockPos pos, float change, int fortune )
{
}
@Override
public final void getDrops( BlockState state, DefaultedList<ItemStack> drops, World world, BlockPos pos, int fortune )
{
BlockEntity tile = world.getBlockEntity( pos );
if( tile instanceof TileComputerBase )
{
ItemStack stack = getItem( (TileComputerBase) tile );
if( !stack.isEmpty() ) drops.add( stack );
}
}
@Override
public boolean removedByPlayer( BlockState state, World world, BlockPos pos, PlayerEntity player, boolean willHarvest, FluidState fluid )
{
if( !world.isClient )
{
// We drop the item here instead of doing it in the harvest method, as we
// need to drop it for creative players too.
BlockEntity tile = world.getBlockEntity( pos );
if( tile instanceof TileComputerBase )
{
TileComputerBase computer = (TileComputerBase) tile;
if( !player.abilities.creativeMode || computer.getLabel() != null )
{
dropStack( world, pos, getItem( computer ) );
}
}
}
return super.removedByPlayer( state, world, pos, player, willHarvest, fluid );
}
*/
@Override
public void onPlaced( World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack )
{
super.onPlaced( world, pos, state, placer, stack );
BlockEntity tile = world.getBlockEntity( pos );
if( !world.isClient && tile instanceof IComputerTile && stack.getItem() instanceof IComputerItem )
{
IComputerTile computer = (IComputerTile) tile;
IComputerItem item = (IComputerItem) stack.getItem();
int id = item.getComputerID( stack );
if( id != -1 ) computer.setComputerID( id );
String label = item.getLabel( stack );
if( label != null ) computer.setLabel( label );
}
}
}