2019-06-20 00:29:15 +02:00
|
|
|
package io.github.cottonmc.templates.block;
|
2019-06-19 22:32:58 +02:00
|
|
|
|
2019-06-20 00:29:15 +02:00
|
|
|
import io.github.cottonmc.templates.Templates;
|
2019-06-20 02:51:11 +02:00
|
|
|
import io.github.cottonmc.templates.block.entity.TemplateEntity;
|
2019-06-20 00:29:15 +02:00
|
|
|
import io.github.cottonmc.templates.util.StateContainer;
|
2019-06-19 22:32:58 +02:00
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.block.BlockEntityProvider;
|
|
|
|
import net.minecraft.block.BlockState;
|
|
|
|
import net.minecraft.block.Blocks;
|
|
|
|
import net.minecraft.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.entity.ItemEntity;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.item.*;
|
|
|
|
import net.minecraft.state.property.BooleanProperty;
|
|
|
|
import net.minecraft.state.property.IntProperty;
|
|
|
|
import net.minecraft.util.Hand;
|
|
|
|
import net.minecraft.util.hit.BlockHitResult;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.Direction;
|
|
|
|
import net.minecraft.world.BlockView;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
|
|
|
public abstract class TemplateBlock extends Block implements BlockEntityProvider, StateContainer {
|
|
|
|
public static final IntProperty LIGHT = IntProperty.of("light", 0, 15);
|
|
|
|
public static final BooleanProperty REDSTONE = BooleanProperty.of("redstone");
|
|
|
|
|
|
|
|
public TemplateBlock(Settings settings) {
|
|
|
|
super(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean activate(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
|
2019-06-20 02:51:11 +02:00
|
|
|
if (world.isClient || !(world.getBlockEntity(pos) instanceof TemplateEntity)) return true;
|
|
|
|
TemplateEntity be = (TemplateEntity) world.getBlockEntity(pos);
|
2019-06-19 22:32:58 +02:00
|
|
|
ItemStack stack = player.getStackInHand(hand);
|
|
|
|
if (stack.getItem() instanceof BlockItem) {
|
|
|
|
Block block = ((BlockItem)stack.getItem()).getBlock();
|
|
|
|
if (block == Blocks.REDSTONE_TORCH) {
|
|
|
|
be.addRedstone();
|
|
|
|
if (!player.abilities.creativeMode) stack.decrement(1);
|
|
|
|
}
|
|
|
|
ItemPlacementContext ctx = new ItemPlacementContext(new ItemUsageContext(player, hand, hit));
|
|
|
|
BlockState placementState = block.getPlacementState(ctx);
|
2019-07-15 02:51:47 +02:00
|
|
|
if (Block.isShapeFullCube(placementState.getCollisionShape(world, pos)) && !(block instanceof BlockEntityProvider)) {
|
2019-06-19 22:32:58 +02:00
|
|
|
if (be.getRenderedState().getBlock() == Blocks.AIR) {
|
|
|
|
be.setRenderedState(placementState);
|
|
|
|
if (!player.abilities.creativeMode) stack.decrement(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (stack.getItem() == Items.GLOWSTONE_DUST) {
|
|
|
|
be.addGlowstone();
|
|
|
|
if (!player.abilities.creativeMode) stack.decrement(1);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isOpaque(BlockState state) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isSimpleFullBlock(BlockState state, BlockView view, BlockPos pos) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBlockRemoved(BlockState state, World world, BlockPos pos, BlockState newState, boolean bool) {
|
2019-06-20 00:29:15 +02:00
|
|
|
if (newState.getBlock() == Templates.SLOPE) return;
|
2019-06-19 22:32:58 +02:00
|
|
|
BlockEntity be = world.getBlockEntity(pos);
|
2019-06-20 02:51:11 +02:00
|
|
|
if (be instanceof TemplateEntity) {
|
|
|
|
TemplateEntity template = (TemplateEntity)be;
|
2019-06-19 22:32:58 +02:00
|
|
|
if (template.getRenderedState().getBlock() != Blocks.AIR) {
|
|
|
|
ItemStack stack = new ItemStack(template.getRenderedState().getBlock());
|
|
|
|
ItemEntity entity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), stack);
|
|
|
|
world.spawnEntity(entity);
|
|
|
|
}
|
|
|
|
if (template.hasRedstone()) {
|
|
|
|
ItemStack stack = new ItemStack(Items.REDSTONE_TORCH);
|
|
|
|
ItemEntity entity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), stack);
|
|
|
|
world.spawnEntity(entity);
|
|
|
|
}
|
|
|
|
if (template.hasGlowstone()) {
|
|
|
|
ItemStack stack = new ItemStack(Items.GLOWSTONE_DUST);
|
|
|
|
ItemEntity entity = new ItemEntity(world, pos.getX(), pos.getY(), pos.getZ(), stack);
|
|
|
|
world.spawnEntity(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.onBlockRemoved(state, world, pos, newState, bool);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos posFrom, boolean bool) {
|
|
|
|
BlockEntity be = world.getBlockEntity(pos);
|
2019-06-20 02:51:11 +02:00
|
|
|
if (be instanceof TemplateEntity) {
|
|
|
|
TemplateEntity template = (TemplateEntity)be;
|
2019-06-19 22:32:58 +02:00
|
|
|
BlockState beState = template.getRenderedState();
|
|
|
|
world.setBlockState(pos, state.with(LIGHT, template.hasGlowstone()? 15 : beState.getLuminance()).with(REDSTONE, template.hasRedstone() || beState.emitsRedstonePower()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getLuminance(BlockState state) {
|
|
|
|
return state.get(LIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean emitsRedstonePower(BlockState state) {
|
|
|
|
return state.get(REDSTONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getWeakRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) {
|
|
|
|
BlockEntity be = view.getBlockEntity(pos);
|
2019-06-20 02:51:11 +02:00
|
|
|
if (be instanceof TemplateEntity) {
|
|
|
|
TemplateEntity template = (TemplateEntity)be;
|
2019-06-19 22:32:58 +02:00
|
|
|
if (template.hasRedstone()) return 15;
|
|
|
|
BlockState beState = template.getRenderedState();
|
|
|
|
return beState.getWeakRedstonePower(view, pos, dir);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getStrongRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) {
|
|
|
|
BlockEntity be = view.getBlockEntity(pos);
|
2019-06-20 02:51:11 +02:00
|
|
|
if (be instanceof TemplateEntity) {
|
|
|
|
TemplateEntity template = (TemplateEntity)be;
|
2019-06-19 22:32:58 +02:00
|
|
|
if (template.hasRedstone()) return 15;
|
|
|
|
BlockState beState = template.getRenderedState();
|
|
|
|
return beState.getStrongRedstonePower(view, pos, dir);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BlockState getContainedState(World world, BlockPos pos) {
|
|
|
|
BlockEntity be = world.getBlockEntity(pos);
|
2019-06-20 02:51:11 +02:00
|
|
|
if (be instanceof TemplateEntity) return ((TemplateEntity)be).getRenderedState();
|
2019-06-19 22:32:58 +02:00
|
|
|
return Blocks.AIR.getDefaultState();
|
|
|
|
}
|
2019-06-20 02:51:11 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setContainedState(World world, BlockPos pos, BlockState state) {
|
|
|
|
BlockEntity be = world.getBlockEntity(pos);
|
|
|
|
if (be instanceof TemplateEntity) ((TemplateEntity)be).setRenderedState(state);
|
|
|
|
}
|
2019-06-19 22:32:58 +02:00
|
|
|
}
|