pick 97420a31d The big reformat of 2020
This commit is contained in:
@@ -18,20 +18,15 @@
|
||||
|
||||
package appeng.block;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import appeng.api.implementations.items.IMemoryCard;
|
||||
import appeng.api.implementations.items.MemoryCardMessages;
|
||||
import appeng.api.implementations.tiles.IColorableTile;
|
||||
import appeng.api.util.AEColor;
|
||||
import appeng.api.util.IOrientable;
|
||||
import appeng.block.networking.BlockCableBus;
|
||||
import appeng.tile.AEBaseInvTile;
|
||||
import appeng.tile.AEBaseTile;
|
||||
import appeng.tile.networking.TileCableBus;
|
||||
import appeng.tile.storage.TileSkyChest;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.SettingsFrom;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.entity.LivingEntity;
|
||||
@@ -50,292 +45,262 @@ import net.minecraft.world.IWorld;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.items.ItemHandlerHelper;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
import appeng.api.implementations.items.IMemoryCard;
|
||||
import appeng.api.implementations.items.MemoryCardMessages;
|
||||
import appeng.api.implementations.tiles.IColorableTile;
|
||||
import appeng.api.util.AEColor;
|
||||
import appeng.api.util.IOrientable;
|
||||
import appeng.block.networking.BlockCableBus;
|
||||
import appeng.tile.AEBaseInvTile;
|
||||
import appeng.tile.AEBaseTile;
|
||||
import appeng.tile.networking.TileCableBus;
|
||||
import appeng.tile.storage.TileSkyChest;
|
||||
import appeng.util.Platform;
|
||||
import appeng.util.SettingsFrom;
|
||||
|
||||
public abstract class AEBaseTileBlock<T extends AEBaseTile> extends AEBaseBlock
|
||||
{
|
||||
public abstract class AEBaseTileBlock<T extends AEBaseTile> extends AEBaseBlock {
|
||||
|
||||
@Nonnull
|
||||
private Class<T> tileEntityClass;
|
||||
@Nonnull
|
||||
private Supplier<T> tileEntityFactory;
|
||||
@Nonnull
|
||||
private Class<T> tileEntityClass;
|
||||
@Nonnull
|
||||
private Supplier<T> tileEntityFactory;
|
||||
|
||||
public AEBaseTileBlock( final Block.Properties props )
|
||||
{
|
||||
super( props );
|
||||
}
|
||||
public AEBaseTileBlock(final Block.Properties props) {
|
||||
super(props);
|
||||
}
|
||||
|
||||
// TODO : Was this change needed?
|
||||
public void setTileEntity( final Class<T> tileEntityClass, Supplier<T> factory )
|
||||
{
|
||||
this.tileEntityClass = tileEntityClass;
|
||||
this.tileEntityFactory = factory;
|
||||
this.setInventory( AEBaseInvTile.class.isAssignableFrom( tileEntityClass ) );
|
||||
}
|
||||
// TODO : Was this change needed?
|
||||
public void setTileEntity(final Class<T> tileEntityClass, Supplier<T> factory) {
|
||||
this.tileEntityClass = tileEntityClass;
|
||||
this.tileEntityFactory = factory;
|
||||
this.setInventory(AEBaseInvTile.class.isAssignableFrom(tileEntityClass));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTileEntity( BlockState state )
|
||||
{
|
||||
return this.hasBlockTileEntity();
|
||||
}
|
||||
@Override
|
||||
public boolean hasTileEntity(BlockState state) {
|
||||
return this.hasBlockTileEntity();
|
||||
}
|
||||
|
||||
private boolean hasBlockTileEntity()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
private boolean hasBlockTileEntity() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public Class<T> getTileEntityClass()
|
||||
{
|
||||
return this.tileEntityClass;
|
||||
}
|
||||
public Class<T> getTileEntityClass() {
|
||||
return this.tileEntityClass;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public T getTileEntity( final IBlockReader w, final int x, final int y, final int z )
|
||||
{
|
||||
return this.getTileEntity( w, new BlockPos( x, y, z ) );
|
||||
}
|
||||
@Nullable
|
||||
public T getTileEntity(final IBlockReader w, final int x, final int y, final int z) {
|
||||
return this.getTileEntity(w, new BlockPos(x, y, z));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public T getTileEntity( final IBlockReader w, final BlockPos pos )
|
||||
{
|
||||
if( !this.hasBlockTileEntity() )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@Nullable
|
||||
public T getTileEntity(final IBlockReader w, final BlockPos pos) {
|
||||
if (!this.hasBlockTileEntity()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final TileEntity te = w.getTileEntity( pos );
|
||||
// FIXME: This gets called as part of building the block state cache
|
||||
if( this.tileEntityClass != null && this.tileEntityClass.isInstance( te ) )
|
||||
{
|
||||
return this.tileEntityClass.cast(te);
|
||||
}
|
||||
final TileEntity te = w.getTileEntity(pos);
|
||||
// FIXME: This gets called as part of building the block state cache
|
||||
if (this.tileEntityClass != null && this.tileEntityClass.isInstance(te)) {
|
||||
return this.tileEntityClass.cast(te);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final TileEntity createTileEntity( BlockState state, IBlockReader world )
|
||||
{
|
||||
return this.tileEntityFactory.get();
|
||||
}
|
||||
@Override
|
||||
public final TileEntity createTileEntity(BlockState state, IBlockReader world) {
|
||||
return this.tileEntityFactory.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropXpOnBlockBreak(World worldIn, BlockPos pos, int amount) {
|
||||
super.dropXpOnBlockBreak(worldIn, pos, amount);
|
||||
}
|
||||
@Override
|
||||
public void dropXpOnBlockBreak(World worldIn, BlockPos pos, int amount) {
|
||||
super.dropXpOnBlockBreak(worldIn, pos, amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReplaced(BlockState state, World w, BlockPos pos, BlockState newState, boolean isMoving) {
|
||||
if (newState.getBlock() == state.getBlock()) {
|
||||
return; // Just a block state change
|
||||
}
|
||||
@Override
|
||||
public void onReplaced(BlockState state, World w, BlockPos pos, BlockState newState, boolean isMoving) {
|
||||
if (newState.getBlock() == state.getBlock()) {
|
||||
return; // Just a block state change
|
||||
}
|
||||
|
||||
final AEBaseTile te = this.getTileEntity( w, pos );
|
||||
if( te != null )
|
||||
{
|
||||
final ArrayList<ItemStack> drops = new ArrayList<>();
|
||||
if( te.dropItems() )
|
||||
{
|
||||
te.getDrops( w, pos, drops );
|
||||
}
|
||||
else
|
||||
{
|
||||
te.getNoDrops( w, pos, drops );
|
||||
}
|
||||
final AEBaseTile te = this.getTileEntity(w, pos);
|
||||
if (te != null) {
|
||||
final ArrayList<ItemStack> drops = new ArrayList<>();
|
||||
if (te.dropItems()) {
|
||||
te.getDrops(w, pos, drops);
|
||||
} else {
|
||||
te.getNoDrops(w, pos, drops);
|
||||
}
|
||||
|
||||
// Cry ;_; ...
|
||||
Platform.spawnDrops( w, pos, drops );
|
||||
}
|
||||
// Cry ;_; ...
|
||||
Platform.spawnDrops(w, pos, drops);
|
||||
}
|
||||
|
||||
// super will remove the TE, as it is not an instance of BlockContainer
|
||||
super.onReplaced( state, w, pos, newState, isMoving );
|
||||
}
|
||||
// super will remove the TE, as it is not an instance of BlockContainer
|
||||
super.onReplaced(state, w, pos, newState, isMoving);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean recolorBlock( BlockState state, final IWorld world, final BlockPos pos, final Direction side, final DyeColor color )
|
||||
{
|
||||
final TileEntity te = this.getTileEntity( world, pos );
|
||||
@Override
|
||||
public boolean recolorBlock(BlockState state, final IWorld world, final BlockPos pos, final Direction side,
|
||||
final DyeColor color) {
|
||||
final TileEntity te = this.getTileEntity(world, pos);
|
||||
|
||||
if( te instanceof IColorableTile )
|
||||
{
|
||||
final IColorableTile ct = (IColorableTile) te;
|
||||
final AEColor c = ct.getColor();
|
||||
final AEColor newColor = AEColor.values()[color.ordinal()];
|
||||
if (te instanceof IColorableTile) {
|
||||
final IColorableTile ct = (IColorableTile) te;
|
||||
final AEColor c = ct.getColor();
|
||||
final AEColor newColor = AEColor.values()[color.ordinal()];
|
||||
|
||||
if( c != newColor )
|
||||
{
|
||||
ct.recolourBlock( side, newColor, null );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (c != newColor) {
|
||||
ct.recolourBlock(side, newColor, null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return super.recolorBlock( state, world, pos, side, color );
|
||||
}
|
||||
return super.recolorBlock(state, world, pos, side, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getComparatorInputOverride( BlockState state, final World w, final BlockPos pos )
|
||||
{
|
||||
final TileEntity te = this.getTileEntity( w, pos );
|
||||
if( te instanceof AEBaseInvTile )
|
||||
{
|
||||
AEBaseInvTile invTile = (AEBaseInvTile) te;
|
||||
if( invTile.getInternalInventory().getSlots() > 0 )
|
||||
{
|
||||
return ItemHandlerHelper.calcRedstoneFromInventory( invTile.getInternalInventory() );
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
public int getComparatorInputOverride(BlockState state, final World w, final BlockPos pos) {
|
||||
final TileEntity te = this.getTileEntity(w, pos);
|
||||
if (te instanceof AEBaseInvTile) {
|
||||
AEBaseInvTile invTile = (AEBaseInvTile) te;
|
||||
if (invTile.getInternalInventory().getSlots() > 0) {
|
||||
return ItemHandlerHelper.calcRedstoneFromInventory(invTile.getInternalInventory());
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean eventReceived( final BlockState state, final World worldIn, final BlockPos pos, final int eventID, final int eventParam )
|
||||
{
|
||||
super.eventReceived( state, worldIn, pos, eventID, eventParam );
|
||||
final TileEntity tileentity = worldIn.getTileEntity( pos );
|
||||
return tileentity != null ? tileentity.receiveClientEvent( eventID, eventParam ) : false;
|
||||
}
|
||||
@Override
|
||||
public boolean eventReceived(final BlockState state, final World worldIn, final BlockPos pos, final int eventID,
|
||||
final int eventParam) {
|
||||
super.eventReceived(state, worldIn, pos, eventID, eventParam);
|
||||
final TileEntity tileentity = worldIn.getTileEntity(pos);
|
||||
return tileentity != null ? tileentity.receiveClientEvent(eventID, eventParam) : false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBlockPlacedBy( final World w, final BlockPos pos, final BlockState state, final LivingEntity placer, final ItemStack is )
|
||||
{
|
||||
if( is.hasDisplayName() )
|
||||
{
|
||||
final TileEntity te = this.getTileEntity( w, pos );
|
||||
if( te instanceof AEBaseTile )
|
||||
{
|
||||
// FIXME: Check if this will make it translated
|
||||
( (AEBaseTile) w.getTileEntity( pos ) ).setName( is.getDisplayName().getString() );
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onBlockPlacedBy(final World w, final BlockPos pos, final BlockState state, final LivingEntity placer,
|
||||
final ItemStack is) {
|
||||
if (is.hasDisplayName()) {
|
||||
final TileEntity te = this.getTileEntity(w, pos);
|
||||
if (te instanceof AEBaseTile) {
|
||||
// FIXME: Check if this will make it translated
|
||||
((AEBaseTile) w.getTileEntity(pos)).setName(is.getDisplayName().getString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ActionResultType onBlockActivated( BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit )
|
||||
{
|
||||
ItemStack heldItem;
|
||||
if( player != null && !player.getHeldItem( hand ).isEmpty() )
|
||||
{
|
||||
heldItem = player.getHeldItem( hand );
|
||||
@Override
|
||||
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player,
|
||||
Hand hand, BlockRayTraceResult hit) {
|
||||
ItemStack heldItem;
|
||||
if (player != null && !player.getHeldItem(hand).isEmpty()) {
|
||||
heldItem = player.getHeldItem(hand);
|
||||
|
||||
if( Platform.isWrench( player, heldItem, pos ) && player.isCrouching() )
|
||||
{
|
||||
final BlockState blockState = world.getBlockState( pos );
|
||||
final Block block = blockState.getBlock();
|
||||
if (Platform.isWrench(player, heldItem, pos) && player.isCrouching()) {
|
||||
final BlockState blockState = world.getBlockState(pos);
|
||||
final Block block = blockState.getBlock();
|
||||
|
||||
final AEBaseTile tile = this.getTileEntity( world, pos );
|
||||
final AEBaseTile tile = this.getTileEntity(world, pos);
|
||||
|
||||
if( tile == null )
|
||||
{
|
||||
return ActionResultType.FAIL;
|
||||
}
|
||||
if (tile == null) {
|
||||
return ActionResultType.FAIL;
|
||||
}
|
||||
|
||||
if( tile instanceof TileCableBus || tile instanceof TileSkyChest)
|
||||
{
|
||||
return ActionResultType.FAIL;
|
||||
}
|
||||
if (tile instanceof TileCableBus || tile instanceof TileSkyChest) {
|
||||
return ActionResultType.FAIL;
|
||||
}
|
||||
|
||||
final ItemStack[] itemDropCandidates = Platform.getBlockDrops( world, pos );
|
||||
final ItemStack op = new ItemStack( this );
|
||||
final ItemStack[] itemDropCandidates = Platform.getBlockDrops(world, pos);
|
||||
final ItemStack op = new ItemStack(this);
|
||||
|
||||
for( final ItemStack ol : itemDropCandidates )
|
||||
{
|
||||
if( Platform.itemComparisons().isEqualItemType( ol, op ) )
|
||||
{
|
||||
final CompoundNBT tag = tile.downloadSettings( SettingsFrom.DISMANTLE_ITEM );
|
||||
if( tag != null )
|
||||
{
|
||||
ol.setTag( tag );
|
||||
}
|
||||
}
|
||||
}
|
||||
for (final ItemStack ol : itemDropCandidates) {
|
||||
if (Platform.itemComparisons().isEqualItemType(ol, op)) {
|
||||
final CompoundNBT tag = tile.downloadSettings(SettingsFrom.DISMANTLE_ITEM);
|
||||
if (tag != null) {
|
||||
ol.setTag(tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( block.removedByPlayer( blockState, world, pos, player, false, world.getFluidState(pos) ) )
|
||||
{
|
||||
final List<ItemStack> itemsToDrop = Lists.newArrayList( itemDropCandidates );
|
||||
Platform.spawnDrops( world, pos, itemsToDrop );
|
||||
world.removeBlock(pos, false);
|
||||
}
|
||||
if (block.removedByPlayer(blockState, world, pos, player, false, world.getFluidState(pos))) {
|
||||
final List<ItemStack> itemsToDrop = Lists.newArrayList(itemDropCandidates);
|
||||
Platform.spawnDrops(world, pos, itemsToDrop);
|
||||
world.removeBlock(pos, false);
|
||||
}
|
||||
|
||||
return ActionResultType.FAIL;
|
||||
}
|
||||
return ActionResultType.FAIL;
|
||||
}
|
||||
|
||||
if( heldItem.getItem() instanceof IMemoryCard && !( this instanceof BlockCableBus) )
|
||||
{
|
||||
final IMemoryCard memoryCard = (IMemoryCard) heldItem.getItem();
|
||||
final AEBaseTile tileEntity = this.getTileEntity( world, pos );
|
||||
if (heldItem.getItem() instanceof IMemoryCard && !(this instanceof BlockCableBus)) {
|
||||
final IMemoryCard memoryCard = (IMemoryCard) heldItem.getItem();
|
||||
final AEBaseTile tileEntity = this.getTileEntity(world, pos);
|
||||
|
||||
if( tileEntity == null )
|
||||
{
|
||||
return ActionResultType.FAIL;
|
||||
}
|
||||
if (tileEntity == null) {
|
||||
return ActionResultType.FAIL;
|
||||
}
|
||||
|
||||
final String name = this.getTranslationKey();
|
||||
final String name = this.getTranslationKey();
|
||||
|
||||
if( player.isCrouching() )
|
||||
{
|
||||
final CompoundNBT data = tileEntity.downloadSettings( SettingsFrom.MEMORY_CARD );
|
||||
if( data != null )
|
||||
{
|
||||
memoryCard.setMemoryCardContents( heldItem, name, data );
|
||||
memoryCard.notifyUser( player, MemoryCardMessages.SETTINGS_SAVED );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
final String savedName = memoryCard.getSettingsName( heldItem );
|
||||
final CompoundNBT data = memoryCard.getData( heldItem );
|
||||
if (player.isCrouching()) {
|
||||
final CompoundNBT data = tileEntity.downloadSettings(SettingsFrom.MEMORY_CARD);
|
||||
if (data != null) {
|
||||
memoryCard.setMemoryCardContents(heldItem, name, data);
|
||||
memoryCard.notifyUser(player, MemoryCardMessages.SETTINGS_SAVED);
|
||||
}
|
||||
} else {
|
||||
final String savedName = memoryCard.getSettingsName(heldItem);
|
||||
final CompoundNBT data = memoryCard.getData(heldItem);
|
||||
|
||||
if( this.getTranslationKey().equals( savedName ) )
|
||||
{
|
||||
tileEntity.uploadSettings( SettingsFrom.MEMORY_CARD, data );
|
||||
memoryCard.notifyUser( player, MemoryCardMessages.SETTINGS_LOADED );
|
||||
}
|
||||
else
|
||||
{
|
||||
memoryCard.notifyUser( player, MemoryCardMessages.INVALID_MACHINE );
|
||||
}
|
||||
}
|
||||
if (this.getTranslationKey().equals(savedName)) {
|
||||
tileEntity.uploadSettings(SettingsFrom.MEMORY_CARD, data);
|
||||
memoryCard.notifyUser(player, MemoryCardMessages.SETTINGS_LOADED);
|
||||
} else {
|
||||
memoryCard.notifyUser(player, MemoryCardMessages.INVALID_MACHINE);
|
||||
}
|
||||
}
|
||||
|
||||
return ActionResultType.SUCCESS;
|
||||
}
|
||||
}
|
||||
return ActionResultType.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
return this.onActivated( world, pos, player, hand, player.getHeldItem( hand ), hit );
|
||||
}
|
||||
return this.onActivated(world, pos, player, hand, player.getHeldItem(hand), hit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IOrientable getOrientable(final IBlockReader w, final BlockPos pos )
|
||||
{
|
||||
return this.getTileEntity( w, pos );
|
||||
}
|
||||
@Override
|
||||
public IOrientable getOrientable(final IBlockReader w, final BlockPos pos) {
|
||||
return this.getTileEntity(w, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the BlockState based on the given BlockState while considering the state of the given TileEntity.
|
||||
*
|
||||
* If the given TileEntity is not of the right type for this block, the state is returned unchanged,
|
||||
* this is also the case if the given block state does not belong to this block.
|
||||
*/
|
||||
public final BlockState getTileEntityBlockState(BlockState current, TileEntity te) {
|
||||
if (current.getBlock() != this || !tileEntityClass.isInstance(te)) {
|
||||
return current;
|
||||
}
|
||||
/**
|
||||
* Returns the BlockState based on the given BlockState while considering the
|
||||
* state of the given TileEntity.
|
||||
*
|
||||
* If the given TileEntity is not of the right type for this block, the state is
|
||||
* returned unchanged, this is also the case if the given block state does not
|
||||
* belong to this block.
|
||||
*/
|
||||
public final BlockState getTileEntityBlockState(BlockState current, TileEntity te) {
|
||||
if (current.getBlock() != this || !tileEntityClass.isInstance(te)) {
|
||||
return current;
|
||||
}
|
||||
|
||||
return updateBlockStateFromTileEntity(current, tileEntityClass.cast(te));
|
||||
}
|
||||
return updateBlockStateFromTileEntity(current, tileEntityClass.cast(te));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reimplement this in subclasses to allow tile-entities to update the state of their block when their own
|
||||
* state changes.
|
||||
*
|
||||
* It is guaranteed that te is not-null and the block of the given block state is this exact block instance.
|
||||
*/
|
||||
protected BlockState updateBlockStateFromTileEntity(BlockState currentState, T te) {
|
||||
return currentState;
|
||||
}
|
||||
/**
|
||||
* Reimplement this in subclasses to allow tile-entities to update the state of
|
||||
* their block when their own state changes.
|
||||
*
|
||||
* It is guaranteed that te is not-null and the block of the given block state
|
||||
* is this exact block instance.
|
||||
*/
|
||||
protected BlockState updateBlockStateFromTileEntity(BlockState currentState, T te) {
|
||||
return currentState;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user