|
| 1 | +package mrtjp.projectred.exploration.block; |
| 2 | + |
| 3 | +import net.minecraft.block.AbstractBlock; |
| 4 | +import net.minecraft.block.Block; |
| 5 | +import net.minecraft.block.BlockState; |
| 6 | +import net.minecraft.block.SoundType; |
| 7 | +import net.minecraft.block.material.Material; |
| 8 | +import net.minecraft.entity.Entity; |
| 9 | +import net.minecraft.entity.player.PlayerEntity; |
| 10 | +import net.minecraft.item.BlockItem; |
| 11 | +import net.minecraft.item.BlockItemUseContext; |
| 12 | +import net.minecraft.item.ItemStack; |
| 13 | +import net.minecraft.particles.RedstoneParticleData; |
| 14 | +import net.minecraft.state.BooleanProperty; |
| 15 | +import net.minecraft.state.StateContainer; |
| 16 | +import net.minecraft.state.properties.BlockStateProperties; |
| 17 | +import net.minecraft.util.ActionResultType; |
| 18 | +import net.minecraft.util.Direction; |
| 19 | +import net.minecraft.util.Hand; |
| 20 | +import net.minecraft.util.math.BlockPos; |
| 21 | +import net.minecraft.util.math.BlockRayTraceResult; |
| 22 | +import net.minecraft.world.World; |
| 23 | +import net.minecraft.world.server.ServerWorld; |
| 24 | +import net.minecraftforge.common.ToolType; |
| 25 | + |
| 26 | +import java.util.Random; |
| 27 | + |
| 28 | +/** |
| 29 | + * All methods lifted straight from RedstoneOreBlock |
| 30 | + */ |
| 31 | +public class ElectrotineOreBlock extends OreBlock { |
| 32 | + |
| 33 | + public static final BooleanProperty LIT = BlockStateProperties.LIT; |
| 34 | + |
| 35 | + public static final RedstoneParticleData ELECTROTINE_PARTICLE = new RedstoneParticleData( |
| 36 | + 15 / 255F, 103 / 255F, 178 / 255F, 0.6F); |
| 37 | + |
| 38 | + public ElectrotineOreBlock(int harvestLevel, int minExp, int maxExp) { |
| 39 | + super(AbstractBlock.Properties.of(Material.STONE) |
| 40 | + .strength(3.0F, 3.0F) |
| 41 | + .harvestLevel(harvestLevel) |
| 42 | + .requiresCorrectToolForDrops() |
| 43 | + .harvestTool(ToolType.PICKAXE) |
| 44 | + .sound(SoundType.STONE) |
| 45 | + .lightLevel(s -> s.getValue(LIT) ? 9 : 0), harvestLevel, minExp, maxExp); |
| 46 | + |
| 47 | + registerDefaultState(defaultBlockState().setValue(LIT, false)); |
| 48 | + } |
| 49 | + |
| 50 | + public void attack(BlockState state, World world, BlockPos pos, PlayerEntity player) { |
| 51 | + interact(state, world, pos); |
| 52 | + super.attack(state, world, pos, player); |
| 53 | + } |
| 54 | + |
| 55 | + public void stepOn(World world, BlockPos pos, Entity player) { |
| 56 | + interact(world.getBlockState(pos), world, pos); |
| 57 | + super.stepOn(world, pos, player); |
| 58 | + } |
| 59 | + |
| 60 | + public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult rayTraceResult) { |
| 61 | + if (world.isClientSide) { |
| 62 | + spawnParticles(world, pos); |
| 63 | + } else { |
| 64 | + interact(state, world, pos); |
| 65 | + } |
| 66 | + |
| 67 | + ItemStack itemstack = player.getItemInHand(hand); |
| 68 | + return itemstack.getItem() instanceof BlockItem && (new BlockItemUseContext(player, hand, itemstack, rayTraceResult)).canPlace() ? ActionResultType.PASS : ActionResultType.SUCCESS; |
| 69 | + } |
| 70 | + |
| 71 | + private static void interact(BlockState state, World world, BlockPos pos) { |
| 72 | + spawnParticles(world, pos); |
| 73 | + if (!state.getValue(LIT)) { |
| 74 | + world.setBlock(pos, state.setValue(LIT, true), 3); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public boolean isRandomlyTicking(BlockState state) { |
| 79 | + return state.getValue(LIT); |
| 80 | + } |
| 81 | + |
| 82 | + public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { |
| 83 | + if (state.getValue(LIT)) { |
| 84 | + world.setBlock(pos, state.setValue(LIT, false), 3); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void animateTick(BlockState state, World world, BlockPos pos, Random random) { |
| 90 | + if (state.getValue(LIT)) { |
| 91 | + spawnParticles(world, pos); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static void spawnParticles(World world, BlockPos pos) { |
| 96 | + double d0 = 0.5625D; |
| 97 | + Random random = world.random; |
| 98 | + |
| 99 | + for (Direction direction : Direction.values()) { |
| 100 | + BlockPos blockpos = pos.relative(direction); |
| 101 | + if (!world.getBlockState(blockpos).isSolidRender(world, blockpos)) { |
| 102 | + Direction.Axis axis = direction.getAxis(); |
| 103 | + double d1 = axis == Direction.Axis.X ? 0.5D + d0 * (double) direction.getStepX() : (double) random.nextFloat(); |
| 104 | + double d2 = axis == Direction.Axis.Y ? 0.5D + d0 * (double) direction.getStepY() : (double) random.nextFloat(); |
| 105 | + double d3 = axis == Direction.Axis.Z ? 0.5D + d0 * (double) direction.getStepZ() : (double) random.nextFloat(); |
| 106 | + world.addParticle(ELECTROTINE_PARTICLE, (double) pos.getX() + d1, (double) pos.getY() + d2, (double) pos.getZ() + d3, 0.0D, 0.0D, 0.0D); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + protected void createBlockStateDefinition(StateContainer.Builder<Block, BlockState> stateBuilder) { |
| 112 | + stateBuilder.add(LIT); |
| 113 | + } |
| 114 | +} |
0 commit comments