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

120 lines
4.0 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 @ [Feb 2, 2014, 2:10:14 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.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.IBooleanFunction;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import vazkii.botania.api.internal.VanillaPacketDispatcher;
import vazkii.botania.api.wand.IWandable;
import vazkii.botania.common.block.BlockMod;
import vazkii.botania.common.block.tile.TileRuneAltar;
import vazkii.botania.common.block.tile.TileSimpleInventory;
import vazkii.botania.common.core.helper.InventoryHelper;
import javax.annotation.Nonnull;
public class BlockRuneAltar extends BlockMod implements IWandable {
private static final VoxelShape TOP = Block.makeCuboidShape(0, 6, 0, 16, 12, 16);
private static final VoxelShape BOTTOM = Block.makeCuboidShape(2, 0, 2, 14, 6, 14);
private static final VoxelShape SHAPE = VoxelShapes.combineAndSimplify(TOP, BOTTOM, IBooleanFunction.OR);
public BlockRuneAltar(Properties builder) {
super(builder);
}
@Nonnull
@Override
public VoxelShape getShape(BlockState state, IBlockReader world, BlockPos pos, ISelectionContext ctx) {
return SHAPE;
}
@Override
public ActionResultType onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
if(world.isRemote)
return ActionResultType.SUCCESS;
TileRuneAltar altar = (TileRuneAltar) world.getTileEntity(pos);
ItemStack stack = player.getHeldItem(hand);
if(player.isSneaking()) {
if(altar.manaToGet == 0) {
InventoryHelper.withdrawFromInventory(altar, player);
VanillaPacketDispatcher.dispatchTEToNearbyPlayers(altar);
return ActionResultType.SUCCESS;
}
} else if(altar.isEmpty() && stack.isEmpty()) {
altar.trySetLastRecipe(player);
VanillaPacketDispatcher.dispatchTEToNearbyPlayers(altar);
return ActionResultType.SUCCESS;
} else if(!stack.isEmpty()) {
boolean result = altar.addItem(player, stack, hand);
VanillaPacketDispatcher.dispatchTEToNearbyPlayers(altar);
return result ? ActionResultType.SUCCESS : ActionResultType.PASS;
}
return ActionResultType.PASS;
}
@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 TileRuneAltar();
}
@Override
public boolean hasComparatorInputOverride(BlockState state) {
return true;
}
@Override
public int getComparatorInputOverride(BlockState state, World world, BlockPos pos) {
TileRuneAltar altar = (TileRuneAltar) world.getTileEntity(pos);
return altar.signal;
}
@Override
public boolean onUsedByWand(PlayerEntity player, ItemStack stack, World world, BlockPos pos, Direction side) {
((TileRuneAltar) world.getTileEntity(pos)).onWanded(player, stack);
return true;
}
}