From 2018a58e7261757d19c21fb90e64cbc678d7a196 Mon Sep 17 00:00:00 2001 From: DeltaHelios Date: Fri, 3 Jan 2025 10:04:43 +0200 Subject: [PATCH] TDET block 'gravity' --- .../java/dev/pswg/block/GrenadeBlock.java | 62 ++++++++++++++++--- .../dev/pswg/block/ThermalDetonatorBlock.java | 2 +- .../pswg/entity/GrenadeEntityWithBlock.java | 45 ++++++++++++++ .../pswg/entity/ThermalDetonatorEntity.java | 12 +++- .../main/java/dev/pswg/item/GrenadeItem.java | 30 --------- .../dev/pswg/item/ThermalDetonatorItem.java | 7 --- 6 files changed, 109 insertions(+), 49 deletions(-) create mode 100644 projects/pswg_gadgets/src/main/java/dev/pswg/entity/GrenadeEntityWithBlock.java diff --git a/projects/pswg_gadgets/src/main/java/dev/pswg/block/GrenadeBlock.java b/projects/pswg_gadgets/src/main/java/dev/pswg/block/GrenadeBlock.java index aeabe3044..ddb5f7eac 100644 --- a/projects/pswg_gadgets/src/main/java/dev/pswg/block/GrenadeBlock.java +++ b/projects/pswg_gadgets/src/main/java/dev/pswg/block/GrenadeBlock.java @@ -19,12 +19,17 @@ import net.minecraft.util.ActionResult; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; +import net.minecraft.util.math.random.Random; import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShapes; import net.minecraft.world.BlockView; import net.minecraft.world.World; import net.minecraft.world.WorldView; +import net.minecraft.world.block.WireOrientation; import net.minecraft.world.explosion.Explosion; +import net.minecraft.world.tick.ScheduledTickView; +import org.jetbrains.annotations.Nullable; public class GrenadeBlock extends WaterloggableRotatingBlock { @@ -44,7 +49,7 @@ public Item getItem() * Returns the entity type corresponding with the block * Needs to be overwritten */ - public EntityType getEntity() + public EntityType getEntityType() { return null; } @@ -100,16 +105,57 @@ public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos po return VoxelShapeUtil.rotateToFace(shape, state.get(FACING)); } + @Override + protected void neighborUpdate(BlockState state, World world, BlockPos pos, Block sourceBlock, @Nullable WireOrientation wireOrientation, boolean notify) + { + super.neighborUpdate(state, world, pos, sourceBlock, wireOrientation, notify); + } + + @Override + protected void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) + { + int count = state.get(CLUSTER_SIZE); + world.setBlockState(pos, Blocks.AIR.getDefaultState()); + for (int i = 0; i < count; i++) + { + GrenadeEntity grenade = getEntityType().create(world, SpawnReason.EVENT); + float rx = world.random.nextBetween(-5, 5) / 100f; + float rz = world.random.nextBetween(-5, 5) / 100f; + grenade.setPos(pos.getX() + 0.5f, pos.getY(), pos.getZ() + 0.5f); + grenade.setVelocity(rx, -0.1f, rz); + grenade.setPrimed(false); + world.spawnEntity(grenade); + } + super.scheduledTick(state, world, pos, random); + } + + @Override + protected BlockState getStateForNeighborUpdate(BlockState state, WorldView world, ScheduledTickView tickView, BlockPos pos, Direction direction, BlockPos neighborPos, BlockState neighborState, Random random) + { + if (neighborState.isAir() && direction == Direction.DOWN) + { + tickView.scheduleBlockTick(pos, asBlock(), 0); + } + return super.getStateForNeighborUpdate(state, world, tickView, pos, direction, neighborPos, neighborState, random); + } + @Override protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) { - if (player.getInventory().getMainHandStack().isOf(getItem()) && state.get(CLUSTER_SIZE) < MAX_CLUSTER_SIZE && player.isSneaking()) + if (player.getInventory().getMainHandStack().isOf(getItem()) && !player.isSneaking()) { - if (!player.isCreative()) - player.getInventory().getMainHandStack().decrement(1); + if (state.get(CLUSTER_SIZE) < MAX_CLUSTER_SIZE) + { + if (!player.isCreative()) + player.getInventory().getMainHandStack().decrement(1); - world.setBlockState(pos, state.with(CLUSTER_SIZE, state.get(CLUSTER_SIZE) + 1)); - return ActionResult.SUCCESS; + world.setBlockState(pos, state.with(CLUSTER_SIZE, state.get(CLUSTER_SIZE) + 1)); + return ActionResult.SUCCESS; + } + else + { + return ActionResult.PASS; + } } player.giveItemStack(new ItemStack(getItem())); @@ -125,7 +171,7 @@ protected ActionResult onUse(BlockState state, World world, BlockPos pos, Player @Override public void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity) { - if (entity instanceof GrenadeEntity grenade && entity.getType() == getEntity()) + if (entity instanceof GrenadeEntity grenade && entity.getType() == getEntityType()) { if (state.get(CLUSTER_SIZE) < MAX_CLUSTER_SIZE && !grenade.isPrimed()) { @@ -168,7 +214,7 @@ public int calculatePower(int grenadeCount) public void explode(World world, BlockPos blockPos, float explosionPower) { - var grenade = getEntity().create(world, SpawnReason.EVENT); + var grenade = getEntityType().create(world, SpawnReason.EVENT); grenade.setExplosionPower(explosionPower); grenade.setPos(blockPos.getX(), blockPos.getY(), blockPos.getZ()); grenade.setPrimed(true); diff --git a/projects/pswg_gadgets/src/main/java/dev/pswg/block/ThermalDetonatorBlock.java b/projects/pswg_gadgets/src/main/java/dev/pswg/block/ThermalDetonatorBlock.java index aeb28bdce..facebf4c6 100644 --- a/projects/pswg_gadgets/src/main/java/dev/pswg/block/ThermalDetonatorBlock.java +++ b/projects/pswg_gadgets/src/main/java/dev/pswg/block/ThermalDetonatorBlock.java @@ -15,7 +15,7 @@ public ThermalDetonatorBlock(Settings settings) } @Override - public EntityType getEntity() + public EntityType getEntityType() { return Gadgets.THERMAL_DETONATOR_ENTITY; } diff --git a/projects/pswg_gadgets/src/main/java/dev/pswg/entity/GrenadeEntityWithBlock.java b/projects/pswg_gadgets/src/main/java/dev/pswg/entity/GrenadeEntityWithBlock.java new file mode 100644 index 000000000..0ab251a20 --- /dev/null +++ b/projects/pswg_gadgets/src/main/java/dev/pswg/entity/GrenadeEntityWithBlock.java @@ -0,0 +1,45 @@ +package dev.pswg.entity; + +import dev.pswg.block.GrenadeBlock; +import net.minecraft.block.BlockState; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.projectile.thrown.ThrownEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; +import net.minecraft.world.World; + +public class GrenadeEntityWithBlock extends GrenadeEntity +{ + public static final int BLOCK_AGE = 300; + + public GrenadeEntityWithBlock(EntityType entityType, World world) + { + super(entityType, world); + } + + public GrenadeBlock getBlock() + { + return null; + } + + @Override + public void tick() + { + if (age >= BLOCK_AGE && !this.isPrimed()) + { + BlockPos pos = getBlockPos(); + BlockState state = getWorld().getBlockState(pos); + if (state.isAir()) + { + this.discard(); + getWorld().setBlockState(pos, getBlock().getDefaultState()); + } + else if (getWorld().getBlockState(pos.offset(Direction.UP)).isAir()) + { + this.discard(); + getWorld().setBlockState(pos.offset(Direction.UP), getBlock().getDefaultState().with(GrenadeBlock.CLUSTER_SIZE, 1)); + } + } + super.tick(); + } +} diff --git a/projects/pswg_gadgets/src/main/java/dev/pswg/entity/ThermalDetonatorEntity.java b/projects/pswg_gadgets/src/main/java/dev/pswg/entity/ThermalDetonatorEntity.java index aa6199f55..4df85e279 100644 --- a/projects/pswg_gadgets/src/main/java/dev/pswg/entity/ThermalDetonatorEntity.java +++ b/projects/pswg_gadgets/src/main/java/dev/pswg/entity/ThermalDetonatorEntity.java @@ -1,6 +1,7 @@ package dev.pswg.entity; import dev.pswg.Gadgets; +import dev.pswg.block.GrenadeBlock; import net.minecraft.block.BlockState; import net.minecraft.entity.EntityType; import net.minecraft.entity.player.PlayerEntity; @@ -13,13 +14,11 @@ import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.hit.HitResult; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Box; -import net.minecraft.util.math.Vec3d; import net.minecraft.world.BlockView; import net.minecraft.world.World; import net.minecraft.world.explosion.Explosion; -public class ThermalDetonatorEntity extends GrenadeEntity +public class ThermalDetonatorEntity extends GrenadeEntityWithBlock { public static final int MIN_PICKUP_AGE = 30; @@ -28,6 +27,13 @@ public ThermalDetonatorEntity(EntityType type, World wor super(type, world); setExplosionPower(5f); } + + @Override + public GrenadeBlock getBlock() + { + return Gadgets.THERMAL_DETONATOR_BLOCK; + } + @Override public void explode() { diff --git a/projects/pswg_gadgets/src/main/java/dev/pswg/item/GrenadeItem.java b/projects/pswg_gadgets/src/main/java/dev/pswg/item/GrenadeItem.java index 0cddae6db..0239b8f03 100644 --- a/projects/pswg_gadgets/src/main/java/dev/pswg/item/GrenadeItem.java +++ b/projects/pswg_gadgets/src/main/java/dev/pswg/item/GrenadeItem.java @@ -1,7 +1,6 @@ package dev.pswg.item; import dev.pswg.Gadgets; -import dev.pswg.block.GrenadeBlock; import dev.pswg.entity.GrenadeEntity; import dev.pswg.world.TickConstants; import net.minecraft.entity.Entity; @@ -40,40 +39,11 @@ public GrenadeItem(Item.Settings settings, Item item, int baseTicksToExplosion) this.baseTicksToExplosion = baseTicksToExplosion; } - public T getBlock() - { - return null; - } - public EntityType getEntityType() { return null; } - @Override - public ActionResult useOnBlock(ItemUsageContext context) - { - /*var stack = context.getStack(); - ThrowableExplosiveTag tag = new ThrowableExplosiveTag(stack.getOrCreateNbt()); - if (context.getPlayer().isSneaking() && !tag.primed) - { - var state = context.getWorld().getBlockState(context.getBlockPos()); - if (state.isOf(block) && state.get(ThermalDetonatorBlock.CLUSTER_SIZE) < 5) - { - context.getWorld().setBlockState(context.getBlockPos(), state.with(ThermalDetonatorBlock.CLUSTER_SIZE, state.get(ThermalDetonatorBlock.CLUSTER_SIZE) + 1)); - if (!context.getPlayer().isCreative()) - { - context.getStack().decrement(1); - } - return ActionResult.SUCCESS; - } - return super.useOnBlock(context); - } - use(context.getWorld(), context.getPlayer(), context.getHand()); - return ActionResult.PASS;*/ - return super.useOnBlock(context); - } - /** * Method called when a grenade is thrown by a player, not to be confused with spawnEntity */ diff --git a/projects/pswg_gadgets/src/main/java/dev/pswg/item/ThermalDetonatorItem.java b/projects/pswg_gadgets/src/main/java/dev/pswg/item/ThermalDetonatorItem.java index 3d1e7c133..6824a8adb 100644 --- a/projects/pswg_gadgets/src/main/java/dev/pswg/item/ThermalDetonatorItem.java +++ b/projects/pswg_gadgets/src/main/java/dev/pswg/item/ThermalDetonatorItem.java @@ -1,7 +1,6 @@ package dev.pswg.item; import dev.pswg.Gadgets; -import dev.pswg.block.ThermalDetonatorBlock; import dev.pswg.entity.ThermalDetonatorEntity; import net.minecraft.entity.EntityType; import net.minecraft.item.Item; @@ -18,10 +17,4 @@ public EntityType getEntityType() { return Gadgets.THERMAL_DETONATOR_ENTITY; } - - @Override - public ThermalDetonatorBlock getBlock() - { - return Gadgets.THERMAL_DETONATOR_BLOCK; - } }