mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-26 06:52:58 +00:00
190 lines
6.0 KiB
Java
190 lines
6.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 @ [May 29, 2015, 8:17:08 PM (GMT)]
|
|
*/
|
|
package vazkii.botania.common.block;
|
|
|
|
import net.minecraft.block.Block;
|
|
import net.minecraft.block.BlockRenderType;
|
|
import net.minecraft.block.BlockState;
|
|
import net.minecraft.client.Minecraft;
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.state.StateContainer;
|
|
import net.minecraft.tileentity.TileEntity;
|
|
import net.minecraft.block.BlockRenderType;
|
|
import net.minecraft.util.Direction;
|
|
import net.minecraft.util.Direction;
|
|
import net.minecraft.util.Hand;
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.util.math.shapes.VoxelShape;
|
|
import net.minecraft.util.text.TranslationTextComponent;
|
|
import net.minecraft.util.text.TranslationTextComponent;
|
|
import net.minecraft.world.IBlockReader;
|
|
import net.minecraft.world.IWorldReader;
|
|
import net.minecraft.world.IWorldReader;
|
|
import net.minecraft.world.World;
|
|
import net.minecraftforge.client.ForgeHooksClient;
|
|
import net.minecraftforge.client.model.ModelLoader;
|
|
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.internal.VanillaPacketDispatcher;
|
|
import vazkii.botania.api.lexicon.ILexiconable;
|
|
import vazkii.botania.api.lexicon.LexiconEntry;
|
|
import vazkii.botania.api.mana.IManaTrigger;
|
|
import vazkii.botania.api.state.BotaniaStateProps;
|
|
import vazkii.botania.api.wand.IWandHUD;
|
|
import vazkii.botania.api.wand.IWandable;
|
|
import vazkii.botania.client.core.handler.ModelHandler;
|
|
import vazkii.botania.common.block.tile.TileHourglass;
|
|
import vazkii.botania.common.block.tile.TileSimpleInventory;
|
|
import vazkii.botania.common.core.helper.InventoryHelper;
|
|
import vazkii.botania.common.item.ModItems;
|
|
import vazkii.botania.common.lexicon.LexiconData;
|
|
|
|
import javax.annotation.Nonnull;
|
|
import java.util.Random;
|
|
|
|
public class BlockHourglass extends BlockMod implements IManaTrigger, IWandable, IWandHUD, ILexiconable {
|
|
|
|
private static final VoxelShape SHAPE = makeCuboidShape(4, 0, 4, 12, 18.4, 12);
|
|
|
|
protected BlockHourglass(Properties builder) {
|
|
super(builder);
|
|
setDefaultState(stateContainer.getBaseState().with(BotaniaStateProps.POWERED, false));
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public VoxelShape getShape(BlockState state, IBlockReader world, BlockPos pos) {
|
|
return SHAPE;
|
|
}
|
|
|
|
@Override
|
|
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
|
|
builder.add(BotaniaStateProps.POWERED);
|
|
}
|
|
|
|
@Override
|
|
public boolean onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, Direction side, float xs, float ys, float zs) {
|
|
TileHourglass hourglass = (TileHourglass) world.getTileEntity(pos);
|
|
ItemStack hgStack = hourglass.getItemHandler().getStackInSlot(0);
|
|
ItemStack stack = player.getHeldItem(hand);
|
|
if(!stack.isEmpty() && stack.getItem() == ModItems.twigWand)
|
|
return false;
|
|
|
|
if(hourglass.lock) {
|
|
if(!player.world.isRemote)
|
|
player.sendMessage(new TranslationTextComponent("botaniamisc.hourglassLock"));
|
|
return true;
|
|
}
|
|
|
|
if(hgStack.isEmpty() && TileHourglass.getStackItemTime(stack) > 0) {
|
|
hourglass.getItemHandler().setStackInSlot(0, stack.copy());
|
|
hourglass.markDirty();
|
|
stack.setCount(0);
|
|
return true;
|
|
} else if(!hgStack.isEmpty()) {
|
|
ItemHandlerHelper.giveItemToPlayer(player, hgStack);
|
|
hourglass.getItemHandler().setStackInSlot(0, ItemStack.EMPTY);
|
|
hourglass.markDirty();
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean canProvidePower(BlockState state) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public int getWeakPower(BlockState state, IBlockReader world, BlockPos pos, Direction side) {
|
|
return state.get(BotaniaStateProps.POWERED) ? 15 : 0;
|
|
}
|
|
|
|
@Override
|
|
public int tickRate(IWorldReader world) {
|
|
return 4;
|
|
}
|
|
|
|
@Override
|
|
public void tick(BlockState state, World world, BlockPos pos, Random rand) {
|
|
if(state.get(BotaniaStateProps.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) {
|
|
TileSimpleInventory inv = (TileSimpleInventory) world.getTileEntity(pos);
|
|
|
|
InventoryHelper.dropInventory(inv, world, state, pos);
|
|
|
|
super.onReplaced(state, world, pos, newState, isMoving);
|
|
}
|
|
|
|
@Override
|
|
public boolean isFullCube(BlockState state) {
|
|
return false;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public BlockRenderType getRenderType(BlockState state) {
|
|
return BlockRenderType.ENTITYBLOCK_ANIMATED;
|
|
}
|
|
|
|
@Override
|
|
public boolean hasTileEntity(BlockState state) {
|
|
return true;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public TileEntity createTileEntity(@Nonnull BlockState state, @Nonnull IBlockReader world) {
|
|
return new TileHourglass();
|
|
}
|
|
|
|
@Override
|
|
public void onBurstCollision(IManaBurst burst, World world, BlockPos pos) {
|
|
if(!burst.isFake()) {
|
|
TileHourglass tile = (TileHourglass) world.getTileEntity(pos);
|
|
tile.onManaCollide();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean onUsedByWand(PlayerEntity player, ItemStack stack, World world, BlockPos pos, Direction side) {
|
|
TileHourglass tile = (TileHourglass) world.getTileEntity(pos);
|
|
tile.lock = !tile.lock;
|
|
if(!world.isRemote)
|
|
VanillaPacketDispatcher.dispatchTEToNearbyPlayers(tile);
|
|
return true;
|
|
}
|
|
|
|
@OnlyIn(Dist.CLIENT)
|
|
@Override
|
|
public void renderHUD(Minecraft mc, World world, BlockPos pos) {
|
|
TileHourglass tile = (TileHourglass) world.getTileEntity(pos);
|
|
tile.renderHUD();
|
|
}
|
|
|
|
@Override
|
|
public LexiconEntry getEntry(World world, BlockPos pos, PlayerEntity player, ItemStack lexicon) {
|
|
return LexiconData.hourglass;
|
|
}
|
|
|
|
}
|