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

130 lines
4.5 KiB
Java

/*
* This class is 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
*/
package vazkii.botania.common.block.mana;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
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 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.BlockModWaterloggable;
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 BlockModWaterloggable implements ITileEntityProvider, IManaTrigger, IManaCollisionGhost {
private static final VoxelShape SHAPE = makeCuboidShape(4, 0, 4, 12, 16, 12);
public BlockPrism(Properties builder) {
super(builder);
setDefaultState(getDefaultState()
.with(BlockStateProperties.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) {
super.fillStateContainer(builder);
builder.add(BlockStateProperties.POWERED, BotaniaStateProps.HAS_LENS);
}
@Override
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
TileEntity tile = world.getTileEntity(pos);
if (!(tile instanceof TilePrism)) {
return ActionResultType.PASS;
}
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().setInventorySlotContents(0, heldItem.copy());
} else if (!lens.isEmpty()) {
player.inventory.placeItemBackInInventory(player.world, lens);
prism.getItemHandler().setInventorySlotContents(0, ItemStack.EMPTY);
}
return ActionResultType.SUCCESS;
}
@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(BlockStateProperties.POWERED);
if (!world.isRemote) {
if (power && !powered) {
world.setBlockState(pos, state.with(BlockStateProperties.POWERED, true));
} else if (!power && powered) {
world.setBlockState(pos, state.with(BlockStateProperties.POWERED, false));
}
}
}
@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);
}
}
@Nonnull
@Override
public TileEntity createNewTileEntity(@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;
}
}