From 42baf5041c18e6f83cd9aedc851c792eec1e7ca3 Mon Sep 17 00:00:00 2001 From: MrTJP Date: Tue, 31 Jan 2023 23:55:38 -0500 Subject: [PATCH] feat: electrotine ore block particles --- .../block/ElectrotineOreBlock.java | 114 ++++++++++++++++++ .../exploration/block/OreBlock.java | 14 ++- .../exploration/init/ExplorationBlocks.java | 3 +- 3 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 src/exploration/java/mrtjp/projectred/exploration/block/ElectrotineOreBlock.java diff --git a/src/exploration/java/mrtjp/projectred/exploration/block/ElectrotineOreBlock.java b/src/exploration/java/mrtjp/projectred/exploration/block/ElectrotineOreBlock.java new file mode 100644 index 000000000..89949074c --- /dev/null +++ b/src/exploration/java/mrtjp/projectred/exploration/block/ElectrotineOreBlock.java @@ -0,0 +1,114 @@ +package mrtjp.projectred.exploration.block; + +import net.minecraft.block.AbstractBlock; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.SoundType; +import net.minecraft.block.material.Material; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.BlockItem; +import net.minecraft.item.BlockItemUseContext; +import net.minecraft.item.ItemStack; +import net.minecraft.particles.RedstoneParticleData; +import net.minecraft.state.BooleanProperty; +import net.minecraft.state.StateContainer; +import net.minecraft.state.properties.BlockStateProperties; +import net.minecraft.util.ActionResultType; +import net.minecraft.util.Direction; +import net.minecraft.util.Hand; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.BlockRayTraceResult; +import net.minecraft.world.World; +import net.minecraft.world.server.ServerWorld; +import net.minecraftforge.common.ToolType; + +import java.util.Random; + +/** + * All methods lifted straight from RedstoneOreBlock + */ +public class ElectrotineOreBlock extends OreBlock { + + public static final BooleanProperty LIT = BlockStateProperties.LIT; + + public static final RedstoneParticleData ELECTROTINE_PARTICLE = new RedstoneParticleData( + 15 / 255F, 103 / 255F, 178 / 255F, 0.6F); + + public ElectrotineOreBlock(int harvestLevel, int minExp, int maxExp) { + super(AbstractBlock.Properties.of(Material.STONE) + .strength(3.0F, 3.0F) + .harvestLevel(harvestLevel) + .requiresCorrectToolForDrops() + .harvestTool(ToolType.PICKAXE) + .sound(SoundType.STONE) + .lightLevel(s -> s.getValue(LIT) ? 9 : 0), harvestLevel, minExp, maxExp); + + registerDefaultState(defaultBlockState().setValue(LIT, false)); + } + + public void attack(BlockState state, World world, BlockPos pos, PlayerEntity player) { + interact(state, world, pos); + super.attack(state, world, pos, player); + } + + public void stepOn(World world, BlockPos pos, Entity player) { + interact(world.getBlockState(pos), world, pos); + super.stepOn(world, pos, player); + } + + public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult rayTraceResult) { + if (world.isClientSide) { + spawnParticles(world, pos); + } else { + interact(state, world, pos); + } + + ItemStack itemstack = player.getItemInHand(hand); + return itemstack.getItem() instanceof BlockItem && (new BlockItemUseContext(player, hand, itemstack, rayTraceResult)).canPlace() ? ActionResultType.PASS : ActionResultType.SUCCESS; + } + + private static void interact(BlockState state, World world, BlockPos pos) { + spawnParticles(world, pos); + if (!state.getValue(LIT)) { + world.setBlock(pos, state.setValue(LIT, true), 3); + } + } + + public boolean isRandomlyTicking(BlockState state) { + return state.getValue(LIT); + } + + public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { + if (state.getValue(LIT)) { + world.setBlock(pos, state.setValue(LIT, false), 3); + } + } + + @Override + public void animateTick(BlockState state, World world, BlockPos pos, Random random) { + if (state.getValue(LIT)) { + spawnParticles(world, pos); + } + } + + private static void spawnParticles(World world, BlockPos pos) { + double d0 = 0.5625D; + Random random = world.random; + + for (Direction direction : Direction.values()) { + BlockPos blockpos = pos.relative(direction); + if (!world.getBlockState(blockpos).isSolidRender(world, blockpos)) { + Direction.Axis axis = direction.getAxis(); + double d1 = axis == Direction.Axis.X ? 0.5D + d0 * (double) direction.getStepX() : (double) random.nextFloat(); + double d2 = axis == Direction.Axis.Y ? 0.5D + d0 * (double) direction.getStepY() : (double) random.nextFloat(); + double d3 = axis == Direction.Axis.Z ? 0.5D + d0 * (double) direction.getStepZ() : (double) random.nextFloat(); + world.addParticle(ELECTROTINE_PARTICLE, (double) pos.getX() + d1, (double) pos.getY() + d2, (double) pos.getZ() + d3, 0.0D, 0.0D, 0.0D); + } + } + } + + protected void createBlockStateDefinition(StateContainer.Builder stateBuilder) { + stateBuilder.add(LIT); + } +} diff --git a/src/exploration/java/mrtjp/projectred/exploration/block/OreBlock.java b/src/exploration/java/mrtjp/projectred/exploration/block/OreBlock.java index 01e1f086e..7652a0f89 100644 --- a/src/exploration/java/mrtjp/projectred/exploration/block/OreBlock.java +++ b/src/exploration/java/mrtjp/projectred/exploration/block/OreBlock.java @@ -18,15 +18,19 @@ public class OreBlock extends Block { private final int minExp; private final int maxExp; + public OreBlock(AbstractBlock.Properties properties, int harvestLevel, int minExp, int maxExp) { + super(properties); + this.minExp = minExp; + this.maxExp = maxExp; + } + public OreBlock(int harvestLevel, int minExp, int maxExp) { - super(AbstractBlock.Properties.of(Material.STONE) - .strength(4.0F, 5.0F) + this(AbstractBlock.Properties.of(Material.STONE) + .strength(3.0F, 3.0F) .harvestLevel(harvestLevel) .requiresCorrectToolForDrops() .harvestTool(ToolType.PICKAXE) - .sound(SoundType.STONE)); - this.minExp = minExp; - this.maxExp = maxExp; + .sound(SoundType.STONE), harvestLevel, minExp, maxExp); } @Override diff --git a/src/exploration/java/mrtjp/projectred/exploration/init/ExplorationBlocks.java b/src/exploration/java/mrtjp/projectred/exploration/init/ExplorationBlocks.java index 80f21f334..fbc18f44d 100644 --- a/src/exploration/java/mrtjp/projectred/exploration/init/ExplorationBlocks.java +++ b/src/exploration/java/mrtjp/projectred/exploration/init/ExplorationBlocks.java @@ -1,5 +1,6 @@ package mrtjp.projectred.exploration.init; +import mrtjp.projectred.exploration.block.ElectrotineOreBlock; import mrtjp.projectred.exploration.block.OreBlock; import net.minecraft.block.AbstractBlock; import net.minecraft.block.Block; @@ -62,7 +63,7 @@ public static void register() { BLOCKS.register(ID_COPPER_ORE, () -> new OreBlock(1, 0, 0)); BLOCKS.register(ID_TIN_ORE, () -> new OreBlock(1, 0, 0)); BLOCKS.register(ID_SILVER_ORE, () -> new OreBlock(2, 0, 0)); - BLOCKS.register(ID_ELECTROTINE_ORE, () -> new OreBlock(2, 1, 5)); + BLOCKS.register(ID_ELECTROTINE_ORE, () -> new ElectrotineOreBlock(2, 1, 5)); // Decorative blocks RegistryObject marbleBlock = BLOCKS.register(ID_MARBLE, () -> createDecorativeStoneBlock(2, 1F, 14F));