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 02:51:11 +02:00
|
|
|
import io.github.cottonmc.templates.block.entity.TemplateEntity;
|
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.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;
|
2023-07-03 09:35:07 +02:00
|
|
|
import net.minecraft.sound.SoundCategory;
|
|
|
|
import net.minecraft.sound.SoundEvents;
|
|
|
|
import net.minecraft.state.StateManager;
|
2019-06-19 22:32:58 +02:00
|
|
|
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;
|
2019-06-19 22:32:58 +02:00
|
|
|
import net.minecraft.util.Hand;
|
2023-07-03 09:35:07 +02:00
|
|
|
import net.minecraft.util.ItemScatterer;
|
|
|
|
import net.minecraft.util.collection.DefaultedList;
|
2019-06-19 22:32:58 +02:00
|
|
|
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 {
|
2019-06-19 22:32:58 +02:00
|
|
|
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
|
|
|
|
2019-06-19 22:32:58 +02:00
|
|
|
public TemplateBlock(Settings settings) {
|
|
|
|
super(settings);
|
2023-07-03 09:35:07 +02:00
|
|
|
|
|
|
|
setDefaultState(getDefaultState().with(LIGHT, 0).with(REDSTONE, false));
|
2019-06-19 22:32:58 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-07-03 09:35:07 +02:00
|
|
|
@Override
|
|
|
|
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
|
|
|
|
super.appendProperties(builder.add(LIGHT, REDSTONE));
|
|
|
|
}
|
|
|
|
|
2019-06-19 22:32:58 +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) {
|
2023-07-03 09:35:07 +02:00
|
|
|
if(!state.isOf(this) || !(world.getBlockEntity(pos) instanceof TemplateEntity be)) return ActionResult.PASS; //shouldn't happen
|
|
|
|
if(!player.canModifyBlocks() || !world.canPlayerModifyAt(player, pos)) return ActionResult.PASS;
|
2023-06-15 09:08:20 +02:00
|
|
|
|
2023-07-03 09:35:07 +02:00
|
|
|
ItemStack held = player.getStackInHand(hand);
|
|
|
|
|
|
|
|
//Glowstone
|
|
|
|
if(held.getItem() == Items.GLOWSTONE_DUST && state.get(LIGHT) != 15 && !be.hasSpentGlowstoneDust()) {
|
|
|
|
world.setBlockState(pos, state.with(LIGHT, 15));
|
|
|
|
be.spentGlowstoneDust();
|
|
|
|
|
|
|
|
if(!player.isCreative()) held.decrement(1);
|
|
|
|
world.playSound(player, pos, SoundEvents.BLOCK_GLASS_HIT, SoundCategory.BLOCKS, 1f, 1f);
|
|
|
|
return ActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Redstone
|
|
|
|
if(held.getItem() == Blocks.REDSTONE_TORCH.asItem() && !state.get(REDSTONE) && !be.hasSpentRedstoneTorch()) {
|
|
|
|
world.setBlockState(pos, state.with(REDSTONE, true));
|
|
|
|
be.spentRedstoneTorch();
|
|
|
|
|
|
|
|
if(!player.isCreative()) held.decrement(1);
|
|
|
|
world.playSound(player, pos, SoundEvents.BLOCK_LEVER_CLICK, SoundCategory.BLOCKS, 1f, 1f);
|
|
|
|
return ActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Changing the theme
|
|
|
|
if(held.getItem() instanceof BlockItem bi && be.getRenderedState().getBlock() == Blocks.AIR) {
|
|
|
|
Block block = bi.getBlock();
|
2019-06-19 22:32:58 +02:00
|
|
|
ItemPlacementContext ctx = new ItemPlacementContext(new ItemUsageContext(player, hand, hit));
|
|
|
|
BlockState placementState = block.getPlacementState(ctx);
|
2023-07-03 09:35:07 +02:00
|
|
|
if(placementState != null && Block.isShapeFullCube(placementState.getCollisionShape(world, pos)) && !(block instanceof BlockEntityProvider)) {
|
|
|
|
if(!world.isClient) be.setRenderedState(placementState);
|
|
|
|
|
2023-07-03 09:38:35 +02:00
|
|
|
world.setBlockState(pos, state
|
|
|
|
.with(LIGHT, be.hasSpentGlowstoneDust() ? 15 : placementState.getLuminance())
|
|
|
|
.with(REDSTONE, be.hasSpentRedstoneTorch() || placementState.getWeakRedstonePower(world, pos, Direction.NORTH) != 0));
|
2023-07-03 09:35:07 +02:00
|
|
|
|
|
|
|
if(!player.isCreative()) held.decrement(1);
|
|
|
|
world.playSound(player, pos, state.getSoundGroup().getPlaceSound(), SoundCategory.BLOCKS, 1f, 1f);
|
|
|
|
return ActionResult.SUCCESS;
|
2019-06-19 22:32:58 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-03 09:35:07 +02:00
|
|
|
|
|
|
|
return ActionResult.PASS;
|
2019-06-19 22:32:58 +02:00
|
|
|
}
|
2023-06-15 07:59:48 +02:00
|
|
|
|
2019-06-19 22:32:58 +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-07-03 09:35:07 +02:00
|
|
|
if(!state.isOf(newState.getBlock()) && world.getBlockEntity(pos) instanceof TemplateEntity template) {
|
|
|
|
DefaultedList<ItemStack> drops = DefaultedList.of();
|
|
|
|
|
|
|
|
Block theme = template.getRenderedState().getBlock();
|
|
|
|
if(theme != Blocks.AIR) drops.add(new ItemStack(theme));
|
|
|
|
if(template.hasSpentRedstoneTorch()) drops.add(new ItemStack(Items.REDSTONE_TORCH));
|
|
|
|
if(template.hasSpentGlowstoneDust()) drops.add(new ItemStack(Items.GLOWSTONE_DUST));
|
|
|
|
|
|
|
|
ItemScatterer.spawn(world, pos, drops);
|
2019-06-19 22:32:58 +02:00
|
|
|
}
|
2023-07-03 09:35:07 +02:00
|
|
|
|
2023-06-15 09:08:20 +02:00
|
|
|
super.onStateReplaced(state, world, pos, newState, moved);
|
2019-06-19 22:32:58 +02:00
|
|
|
}
|
2023-06-15 07:59:48 +02:00
|
|
|
|
2019-06-19 22:32:58 +02:00
|
|
|
@Override
|
|
|
|
public boolean emitsRedstonePower(BlockState state) {
|
|
|
|
return state.get(REDSTONE);
|
|
|
|
}
|
2023-06-15 07:59:48 +02:00
|
|
|
|
2019-06-19 22:32:58 +02:00
|
|
|
@Override
|
|
|
|
public int getWeakRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) {
|
2023-07-03 09:38:35 +02:00
|
|
|
return state.get(REDSTONE) ? 15 : 0;
|
2019-06-19 22:32:58 +02:00
|
|
|
}
|
2023-06-15 07:59:48 +02:00
|
|
|
|
2019-06-19 22:32:58 +02:00
|
|
|
@Override
|
|
|
|
public int getStrongRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) {
|
2023-07-03 09:38:35 +02:00
|
|
|
return state.get(REDSTONE) ? 15 : 0;
|
2019-06-19 22:32:58 +02:00
|
|
|
}
|
2023-06-15 07:59:48 +02:00
|
|
|
|
2023-06-15 09:08:20 +02:00
|
|
|
public int luminance(BlockState state) {
|
|
|
|
return state.get(LIGHT);
|
|
|
|
}
|
2019-06-19 22:32:58 +02:00
|
|
|
}
|