Skip to content

Commit

Permalink
feat: electrotine ore block particles
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTJP committed Feb 1, 2023
1 parent b9f5c52 commit 42baf50
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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<Block, BlockState> stateBuilder) {
stateBuilder.add(LIT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Block> marbleBlock = BLOCKS.register(ID_MARBLE, () -> createDecorativeStoneBlock(2, 1F, 14F));
Expand Down

0 comments on commit 42baf50

Please sign in to comment.