ReFramed/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java

139 lines
5.2 KiB
Java
Raw Normal View History

2019-06-20 00:29:15 +02:00
package io.github.cottonmc.templates.block;
2019-06-20 00:29:15 +02:00
import io.github.cottonmc.templates.Templates;
import io.github.cottonmc.templates.block.entity.TemplateEntity;
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;
2023-06-15 07:59:48 +02:00
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.Items;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.IntProperty;
2023-06-15 09:08:20 +02:00
import net.minecraft.util.ActionResult;
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;
2023-06-16 05:09:57 +02:00
public abstract class TemplateBlock extends Block implements BlockEntityProvider {
public static final IntProperty LIGHT = IntProperty.of("light", 0, 15);
public static final BooleanProperty REDSTONE = BooleanProperty.of("redstone");
2023-06-15 07:59:48 +02:00
public TemplateBlock(Settings settings) {
super(settings);
}
2023-06-15 07:59:48 +02:00
2023-06-15 09:08:20 +02:00
public static Settings configureSettings(Settings s) {
2023-06-15 10:24:11 +02:00
return s
.luminance(state -> ((TemplateBlock) state.getBlock()).luminance(state))
.nonOpaque();
2023-06-15 09:08:20 +02:00
}
@Override
2023-06-15 09:08:20 +02:00
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if(world.isClient || !(world.getBlockEntity(pos) instanceof TemplateEntity be)) return ActionResult.SUCCESS;
ItemStack stack = player.getStackInHand(hand);
2023-06-15 07:59:48 +02:00
if(stack.getItem() instanceof BlockItem) {
Block block = ((BlockItem) stack.getItem()).getBlock();
if(block == Blocks.REDSTONE_TORCH) {
2023-06-15 10:24:11 +02:00
be.setRedstone(true);
2023-06-15 09:08:20 +02:00
if(!player.isCreative()) stack.decrement(1);
}
ItemPlacementContext ctx = new ItemPlacementContext(new ItemUsageContext(player, hand, hit));
BlockState placementState = block.getPlacementState(ctx);
2023-06-15 09:08:20 +02:00
if(placementState != null &&
Block.isShapeFullCube(placementState.getCollisionShape(world, pos)) &&
!(block instanceof BlockEntityProvider) &&
be.getRenderedState().getBlock() == Blocks.AIR)
{
be.setRenderedState(placementState);
if(!player.isCreative()) stack.decrement(1);
}
2023-06-15 07:59:48 +02:00
} else if(stack.getItem() == Items.GLOWSTONE_DUST) {
2023-06-15 10:24:11 +02:00
be.setGlowstone(true);
2023-06-15 09:08:20 +02:00
if(!player.isCreative()) stack.decrement(1);
}
2023-06-15 09:08:20 +02:00
return ActionResult.SUCCESS;
}
2023-06-15 07:59:48 +02:00
@Override
2023-06-15 09:08:20 +02:00
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
2023-06-15 07:59:48 +02:00
if(newState.getBlock() == Templates.SLOPE) return;
BlockEntity be = world.getBlockEntity(pos);
2023-06-15 07:59:48 +02:00
if(be instanceof TemplateEntity) {
TemplateEntity template = (TemplateEntity) be;
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);
}
2023-06-15 07:59:48 +02:00
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);
}
2023-06-15 07:59:48 +02:00
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);
}
}
2023-06-15 09:08:20 +02:00
super.onStateReplaced(state, world, pos, newState, moved);
}
2023-06-15 07:59:48 +02:00
@Override
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos posFrom, boolean bool) {
BlockEntity be = world.getBlockEntity(pos);
2023-06-15 07:59:48 +02:00
if(be instanceof TemplateEntity) {
TemplateEntity template = (TemplateEntity) be;
BlockState beState = template.getRenderedState();
2023-06-15 07:59:48 +02:00
world.setBlockState(pos, state.with(LIGHT, template.hasGlowstone() ? 15 : beState.getLuminance()).with(REDSTONE, template.hasRedstone() || beState.emitsRedstonePower()));
}
}
2023-06-15 07:59:48 +02:00
@Override
public boolean emitsRedstonePower(BlockState state) {
return state.get(REDSTONE);
}
2023-06-15 07:59:48 +02:00
@Override
public int getWeakRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) {
BlockEntity be = view.getBlockEntity(pos);
2023-06-15 07:59:48 +02:00
if(be instanceof TemplateEntity) {
TemplateEntity template = (TemplateEntity) be;
if(template.hasRedstone()) return 15;
BlockState beState = template.getRenderedState();
return beState.getWeakRedstonePower(view, pos, dir);
}
return 0;
}
2023-06-15 07:59:48 +02:00
@Override
public int getStrongRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) {
BlockEntity be = view.getBlockEntity(pos);
2023-06-15 07:59:48 +02:00
if(be instanceof TemplateEntity) {
TemplateEntity template = (TemplateEntity) be;
if(template.hasRedstone()) return 15;
BlockState beState = template.getRenderedState();
return beState.getStrongRedstonePower(view, pos, dir);
}
return 0;
}
2023-06-15 07:59:48 +02:00
2023-06-15 09:08:20 +02:00
//TODO: pass to Block.Settings
// "Cannot reference 'TemplateBlock.luminance' before supertype constructor has been called"
public int luminance(BlockState state) {
return state.get(LIGHT);
}
}