From c735b06779308ddfba212a4d308f982f454a2f3c Mon Sep 17 00:00:00 2001 From: Justin Parsell Date: Mon, 22 Nov 2021 00:03:38 -0500 Subject: [PATCH] beginning of gunpowdering being useful --- .../common/WDGunpowderWireBlock.java | 191 ++++++++++-------- 1 file changed, 110 insertions(+), 81 deletions(-) diff --git a/src/main/java/me/parsell/wireddust/common/WDGunpowderWireBlock.java b/src/main/java/me/parsell/wireddust/common/WDGunpowderWireBlock.java index e8fb245..f3b487f 100644 --- a/src/main/java/me/parsell/wireddust/common/WDGunpowderWireBlock.java +++ b/src/main/java/me/parsell/wireddust/common/WDGunpowderWireBlock.java @@ -1,15 +1,21 @@ package me.parsell.wireddust.common; +import java.util.Iterator; import java.util.Random; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.block.Block; import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.block.CampfireBlock; +import net.minecraft.block.TntBlock; +import net.minecraft.entity.EquipmentSlot; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Items; import net.minecraft.particle.ParticleTypes; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; import net.minecraft.state.StateManager; import net.minecraft.state.property.BooleanProperty; import net.minecraft.state.property.Properties; @@ -17,104 +23,127 @@ 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.Box; +import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction.Axis; import net.minecraft.world.World; +import net.minecraft.world.WorldAccess; // TODO: All the magic to make this work the way i want public class WDGunpowderWireBlock extends WDWireBlock{ - public static final BooleanProperty LIT; + public static final BooleanProperty LIT; + public static final BooleanProperty BURNT; - public WDGunpowderWireBlock(Settings settings) { - super(settings); - this.setDefaultState((BlockState)((BlockState)((BlockState)((BlockState)((BlockState)((BlockState)this.stateManager.getDefaultState()).with(WIRE_CONNECTION_NORTH, WDWireConnection.NONE)).with(WIRE_CONNECTION_EAST, WDWireConnection.NONE)).with(WIRE_CONNECTION_SOUTH, WDWireConnection.NONE)).with(WIRE_CONNECTION_WEST, WDWireConnection.NONE)).with(LIT, false)); - } - - @Override - protected void appendProperties(StateManager.Builder builder) { - super.appendProperties(builder.add(LIT)); + public WDGunpowderWireBlock(Settings settings) { + super(settings); + this.setDefaultState((BlockState)((BlockState)((BlockState)((BlockState)((BlockState)((BlockState)this.stateManager.getDefaultState()).with(WIRE_CONNECTION_NORTH, WDWireConnection.NONE)).with(WIRE_CONNECTION_EAST, WDWireConnection.NONE)).with(WIRE_CONNECTION_SOUTH, WDWireConnection.NONE)).with(WIRE_CONNECTION_WEST, WDWireConnection.NONE)).with(LIT, false).with(BURNT, false)); } - private boolean hasUpConnection(BlockState state){ - return ((WDWireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnectedUp() || - ((WDWireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnectedUp() || - ((WDWireConnection)state.get(WIRE_CONNECTION_EAST)).isConnectedUp() || - ((WDWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnectedUp(); - } + @Override + protected void appendProperties(StateManager.Builder builder) { + super.appendProperties(builder.add(LIT, BURNT)); + } - static boolean pp = true; + private boolean hasUpConnection(BlockState state){ + return ((WDWireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnectedUp() || + ((WDWireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnectedUp() || + ((WDWireConnection)state.get(WIRE_CONNECTION_EAST)).isConnectedUp() || + ((WDWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnectedUp(); + } - // TODO: Supprt UP state - @Environment(EnvType.CLIENT) - public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) { - if (state.get(LIT)){ - // get random double, within bounds x/y_min and x/y_max - double x_min = super.STATE_TO_VOXELSHAPE.get(state).getMin(Axis.X); - double z_min = super.STATE_TO_VOXELSHAPE.get(state).getMin(Axis.Z); - double x_max = super.STATE_TO_VOXELSHAPE.get(state).getMax(Axis.X); - double z_max = super.STATE_TO_VOXELSHAPE.get(state).getMax(Axis.Z); - - double x_rand = x_min + (x_max - x_min) * random.nextDouble(); - double z_rand = z_min + (z_max - z_min) * random.nextDouble(); + @Environment(EnvType.CLIENT) + public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) { + if (state.get(LIT)){ + // get random double, within bounds x/y_min and x/y_max + double x_min = super.STATE_TO_VOXELSHAPE.get(state).getMin(Axis.X); + double z_min = super.STATE_TO_VOXELSHAPE.get(state).getMin(Axis.Z); + double x_max = super.STATE_TO_VOXELSHAPE.get(state).getMax(Axis.X); + double z_max = super.STATE_TO_VOXELSHAPE.get(state).getMax(Axis.Z); + + double x_rand = x_min + (x_max - x_min) * random.nextDouble(); + double z_rand = z_min + (z_max - z_min) * random.nextDouble(); - double y_rand = 0.1d; + double y_rand = 0.1d; - if (hasUpConnection(state)){ - if (z_rand < 0.1d && state.get(WIRE_CONNECTION_NORTH).isConnectedUp() || - z_rand > 0.9d && state.get(WIRE_CONNECTION_SOUTH).isConnectedUp() || - x_rand < 0.1d && state.get(WIRE_CONNECTION_WEST).isConnectedUp() || - x_rand > 0.9d && state.get(WIRE_CONNECTION_EAST).isConnectedUp()) { - y_rand = 0.1D + (0.9D - 0.1D) * random.nextDouble(); - } - } + if (hasUpConnection(state)){ + if (z_rand < 0.1d && state.get(WIRE_CONNECTION_NORTH).isConnectedUp() || + z_rand > 0.9d && state.get(WIRE_CONNECTION_SOUTH).isConnectedUp() || + x_rand < 0.1d && state.get(WIRE_CONNECTION_WEST).isConnectedUp() || + x_rand > 0.9d && state.get(WIRE_CONNECTION_EAST).isConnectedUp()) { + y_rand = 0.1D + (0.9D - 0.1D) * random.nextDouble(); + } + } - System.out.printf("X: %1.2f | Y: %1.2f | Z: %1.2f\n", x_rand, y_rand, z_rand); + world.addParticle(ParticleTypes.FLAME, pos.getX() + x_rand, pos.getY() + y_rand, pos.getZ() + z_rand, 0.0, 0.0, 0.0); + super.randomDisplayTick(state, world, pos, random); + } + } - //NEW - /* - double rand_x, rand_y, rand_z; - boolean validParticle = false; + // TODO: TEST KILLING MINECRAFT WITH REDSTONE & FILL COMMANDS + @Override + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit){ + if(world.isClient) + if(player.getStackInHand(hand).getItem().equals(Items.FLINT_AND_STEEL) || !player.hasStackEquipped(EquipmentSlot.MAINHAND)) + return ActionResult.SUCCESS; + else + return ActionResult.PASS; + if(player.abilities.allowModifyWorld){ + if(!state.get(LIT) && player.getStackInHand(hand).getItem().equals(Items.FLINT_AND_STEEL)){ + player.getStackInHand(hand).damage(1, world.getRandom(), (ServerPlayerEntity)player); + light(world, pos, state); + return ActionResult.SUCCESS; + } + } - if (pp) { - pp = false; - System.out.println(super.STATE_TO_VOXELSHAPE.get(state).getBoundingBoxes().size()); - } + super.onUse(state, world, pos, player, hand, hit); + return ActionResult.PASS; + } - do{ - rand_x = random.nextDouble(); - rand_y = random.nextDouble(); - rand_z = random.nextDouble(); + public void light(World world, BlockPos pos, BlockState state){ + world.setBlockState(pos, state.with(LIT, true)); + scheduleTick(world, pos); + } - for (Box b : super.STATE_TO_VOXELSHAPE.get(state).getBoundingBoxes()){ - if (b.contains(rand_x, rand_y, rand_z)) - validParticle = true; - break; - } - }while (!validParticle); */ + public void scheduleTick(WorldAccess world, BlockPos pos) { + if (!world.isClient() && !world.getBlockTickScheduler().isScheduled(pos, this)) { + world.getBlockTickScheduler().schedule(pos, this, 60); + } + + } - world.addParticle(ParticleTypes.FLAME, pos.getX() + x_rand, pos.getY() + y_rand, pos.getZ() + z_rand, 0.0, 0.0, 0.0); - super.randomDisplayTick(state, world, pos, random); - } - } + // Three things need to happen' + // 1. Set neighbors to lit, if self and schedule another tick update ✓ + // 2. start fire on "flammable" blocks and schedule another tick update | half ✓ + // 3. Destroy self ✓ + @Override + public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { + if(!state.get(BURNT)) { + Iterator dirItr = Direction.Type.HORIZONTAL.iterator(); + while(dirItr.hasNext()) { + Direction dir = dirItr.next(); + BlockPos offsetPos = pos.offset(dir); + BlockState offsetBlockState = world.getBlockState(offsetPos); + + if(state.get(super.DIRECTION_TO_WIRE_CONNECTION.get(dir)).isConnected()) + if(offsetBlockState.isOf(this)) + light(world, offsetPos, offsetBlockState); + else if(offsetBlockState.isOf(Blocks.TNT)) { + TntBlock.primeTnt(world, offsetPos); + world.setBlockState(offsetPos, Blocks.AIR.getDefaultState()); + } else if(offsetBlockState.isOf(Blocks.CAMPFIRE) && !offsetBlockState.get(CampfireBlock.LIT)) + world.setBlockState(offsetPos, offsetBlockState.with(CampfireBlock.LIT, true)); + else if(false) // TODO: make flammable blocks array + ; + } - // TODO: TEST KILLING MINECRAFT WITH REDSTONE & FILL COMMANDS - @Override - public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit){ - if(world.isClient) - return ActionResult.SUCCESS; - if(player.abilities.allowModifyWorld){ - if(!state.get(LIT) && player.getStackInHand(hand).getItem().equals(Items.FLINT_AND_STEEL)){ - player.getStackInHand(hand).damage(1, world.getRandom(), (ServerPlayerEntity)player); - world.setBlockState(pos, state.with(LIT, true)); - return ActionResult.SUCCESS; - } - } - - super.onUse(state, world, pos, player, hand, hit); - return ActionResult.PASS; - } - - static { - LIT = Properties.LIT; - } + world.setBlockState(pos, state.with(BURNT, true)); + scheduleTick(world, pos); + } else { + world.setBlockState(pos, Blocks.AIR.getDefaultState()); + } + } + + static { + LIT = Properties.LIT; + BURNT = BooleanProperty.of("burnt"); + } }