Skip to content

Commit

Permalink
TDET block 'gravity'
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaHelios committed Jan 3, 2025
1 parent 518b7b5 commit 2018a58
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -44,7 +49,7 @@ public Item getItem()
* Returns the entity type corresponding with the block
* Needs to be overwritten
*/
public EntityType<? extends GrenadeEntity> getEntity()
public EntityType<? extends GrenadeEntity> getEntityType()
{
return null;
}
Expand Down Expand Up @@ -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()));
Expand All @@ -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())
{
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ThermalDetonatorBlock(Settings settings)
}

@Override
public EntityType<ThermalDetonatorEntity> getEntity()
public EntityType<ThermalDetonatorEntity> getEntityType()
{
return Gadgets.THERMAL_DETONATOR_ENTITY;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<? extends ThrownEntity> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -28,6 +27,13 @@ public ThermalDetonatorEntity(EntityType<ThermalDetonatorEntity> type, World wor
super(type, world);
setExplosionPower(5f);
}

@Override
public GrenadeBlock getBlock()
{
return Gadgets.THERMAL_DETONATOR_BLOCK;
}

@Override
public void explode()
{
Expand Down
30 changes: 0 additions & 30 deletions projects/pswg_gadgets/src/main/java/dev/pswg/item/GrenadeItem.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,40 +39,11 @@ public GrenadeItem(Item.Settings settings, Item item, int baseTicksToExplosion)
this.baseTicksToExplosion = baseTicksToExplosion;
}

public <T extends GrenadeBlock> T getBlock()
{
return null;
}

public EntityType<? extends GrenadeEntity> 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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,10 +17,4 @@ public EntityType<ThermalDetonatorEntity> getEntityType()
{
return Gadgets.THERMAL_DETONATOR_ENTITY;
}

@Override
public ThermalDetonatorBlock getBlock()
{
return Gadgets.THERMAL_DETONATOR_BLOCK;
}
}

0 comments on commit 2018a58

Please sign in to comment.