Files
Botania/src/main/java/vazkii/botania/common/block/mana/BlockPrism.java
T

139 lines
4.7 KiB
Java

/**
* This class was created by <Vazkii>. It's distributed as
* part of the Botania Mod. Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under the
* Botania License: http://botaniamod.net/license.php
*
* File Created @ [Jan 17, 2015, 7:16:48 PM (GMT)]
*/
package vazkii.botania.common.block.mana;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateContainer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.items.ItemHandlerHelper;
import vazkii.botania.api.internal.IManaBurst;
import vazkii.botania.api.mana.ILens;
import vazkii.botania.api.mana.IManaCollisionGhost;
import vazkii.botania.api.mana.IManaTrigger;
import vazkii.botania.api.state.BotaniaStateProps;
import vazkii.botania.common.block.BlockMod;
import vazkii.botania.common.block.tile.TileSimpleInventory;
import vazkii.botania.common.block.tile.mana.TilePrism;
import vazkii.botania.common.core.helper.InventoryHelper;
import javax.annotation.Nonnull;
public class BlockPrism extends BlockMod implements IManaTrigger, IManaCollisionGhost {
private static final VoxelShape SHAPE = makeCuboidShape(4, 0, 4, 12, 16, 12);
public BlockPrism(Properties builder) {
super(builder);
setDefaultState(stateContainer.getBaseState()
.with(BotaniaStateProps.POWERED, false)
.with(BotaniaStateProps.HAS_LENS, false));
}
@Nonnull
@Override
public VoxelShape getShape(BlockState state, IBlockReader world, BlockPos pos, ISelectionContext ctx) {
return SHAPE;
}
@Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
builder.add(BotaniaStateProps.POWERED, BotaniaStateProps.HAS_LENS);
}
@Nonnull
@Override
@OnlyIn(Dist.CLIENT)
public BlockRenderLayer getRenderLayer() {
return BlockRenderLayer.TRANSLUCENT;
}
@Override
public boolean onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
TileEntity tile = world.getTileEntity(pos);
if(!(tile instanceof TilePrism))
return false;
TilePrism prism = (TilePrism) tile;
ItemStack lens = prism.getItemHandler().getStackInSlot(0);
ItemStack heldItem = player.getHeldItem(hand);
boolean isHeldItemLens = !heldItem.isEmpty() && heldItem.getItem() instanceof ILens;
if(lens.isEmpty() && isHeldItemLens) {
if(!player.abilities.isCreativeMode)
player.setHeldItem(hand, ItemStack.EMPTY);
prism.getItemHandler().setStackInSlot(0, heldItem.copy());
} else if(!lens.isEmpty()) {
ItemHandlerHelper.giveItemToPlayer(player, lens);
prism.getItemHandler().setStackInSlot(0, ItemStack.EMPTY);
}
return true;
}
@Override
public void neighborChanged(BlockState state, World world, BlockPos pos, Block block, BlockPos fromPos, boolean isMoving) {
boolean power = world.getRedstonePowerFromNeighbors(pos) > 0 || world.getRedstonePowerFromNeighbors(pos.up()) > 0;
boolean powered = state.get(BotaniaStateProps.POWERED);
if(!world.isRemote) {
if(power && !powered)
world.setBlockState(pos, state.with(BotaniaStateProps.POWERED, true), 1 | 2);
else if(!power && powered)
world.setBlockState(pos, state.with(BotaniaStateProps.POWERED, false), 1 | 2);
}
}
@Override
public void onReplaced(@Nonnull BlockState state, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull BlockState newState, boolean isMoving) {
if (state.getBlock() != newState.getBlock()) {
TileSimpleInventory inv = (TileSimpleInventory) world.getTileEntity(pos);
InventoryHelper.dropInventory(inv, world, state, pos);
super.onReplaced(state, world, pos, newState, isMoving);
}
}
@Override
public boolean hasTileEntity(BlockState state) {
return true;
}
@Nonnull
@Override
public TileEntity createTileEntity(@Nonnull BlockState state, @Nonnull IBlockReader world) {
return new TilePrism();
}
@Override
public void onBurstCollision(IManaBurst burst, World world, BlockPos pos) {
TileEntity tile = world.getTileEntity(pos);
if(tile instanceof TilePrism)
((TilePrism) tile).onBurstCollision(burst);
}
@Override
public boolean isGhost(BlockState state, World world, BlockPos pos) {
return true;
}
}