beginning of gunpowdering being useful

This commit is contained in:
Justin Parsell 2021-11-22 00:03:38 -05:00
parent 563d579aab
commit c735b06779
1 changed files with 110 additions and 81 deletions

View File

@ -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,22 +23,24 @@ 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 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));
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));
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder.add(LIT));
super.appendProperties(builder.add(LIT, BURNT));
}
private boolean hasUpConnection(BlockState state){
@ -42,9 +50,6 @@ public class WDGunpowderWireBlock extends WDWireBlock{
((WDWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnectedUp();
}
static boolean pp = true;
// TODO: Supprt UP state
@Environment(EnvType.CLIENT)
public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) {
if (state.get(LIT)){
@ -68,30 +73,6 @@ public class WDGunpowderWireBlock extends WDWireBlock{
}
}
System.out.printf("X: %1.2f | Y: %1.2f | Z: %1.2f\n", x_rand, y_rand, z_rand);
//NEW
/*
double rand_x, rand_y, rand_z;
boolean validParticle = false;
if (pp) {
pp = false;
System.out.println(super.STATE_TO_VOXELSHAPE.get(state).getBoundingBoxes().size());
}
do{
rand_x = random.nextDouble();
rand_y = random.nextDouble();
rand_z = random.nextDouble();
for (Box b : super.STATE_TO_VOXELSHAPE.get(state).getBoundingBoxes()){
if (b.contains(rand_x, rand_y, rand_z))
validParticle = true;
break;
}
}while (!validParticle); */
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);
}
@ -101,11 +82,14 @@ public class WDGunpowderWireBlock extends WDWireBlock{
@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);
world.setBlockState(pos, state.with(LIT, true));
light(world, pos, state);
return ActionResult.SUCCESS;
}
}
@ -114,7 +98,52 @@ public class WDGunpowderWireBlock extends WDWireBlock{
return ActionResult.PASS;
}
public void light(World world, BlockPos pos, BlockState state){
world.setBlockState(pos, state.with(LIT, true));
scheduleTick(world, pos);
}
public void scheduleTick(WorldAccess world, BlockPos pos) {
if (!world.isClient() && !world.getBlockTickScheduler().isScheduled(pos, this)) {
world.getBlockTickScheduler().schedule(pos, this, 60);
}
}
// 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<Direction> 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
;
}
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");
}
}