Skip to content

Commit

Permalink
Starting work on fragmentation grenade
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaHelios committed Dec 20, 2024
1 parent c9984b4 commit 0262e00
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 3 deletions.
2 changes: 2 additions & 0 deletions projects/pswg_gadgets/src/main/java/dev/pswg/Gadgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.pswg.item.GrenadeItem;
import dev.pswg.item.ThermalDetonatorItem;
import dev.pswg.registry.Registrar;
import net.minecraft.block.Block;
import net.minecraft.component.ComponentType;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
Expand Down Expand Up @@ -60,6 +61,7 @@ public static Identifier id(String path)
ComponentType.<Long>builder().codec(Codec.LONG).build()
);
public static final GrenadeItem THERMAL_DETONATOR_ITEM = Registrar.item(id("thermal_detonator"), ThermalDetonatorItem::new, new Item.Settings());
public static final TagKey<Block> FRAGMENTATION_GRENADE_DESTROY = TagKey.of(RegistryKeys.BLOCK, id("fragmentation_destroy"));

@Override
public void onGalaxiesReady()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricTagProvider;
import net.fabricmc.fabric.api.tag.convention.v2.ConventionalBlockTags;
import net.minecraft.data.client.BlockStateModelGenerator;
import net.minecraft.data.client.ItemModelGenerator;
import net.minecraft.data.client.Models;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.registry.tag.BlockTags;

import java.util.concurrent.CompletableFuture;

Expand All @@ -25,7 +27,7 @@ public void onInitializeDataGenerator(FabricDataGenerator generator)
var pack = generator.createPack();

pack.addProvider(LangGenerator::new);
pack.addProvider(TagGenerator::new);
pack.addProvider(ItemTagGenerator::new);
pack.addProvider(ModelGenerator::new);
}

Expand Down Expand Up @@ -67,23 +69,50 @@ protected LangGenerator(FabricDataOutput dataOutput, CompletableFuture<RegistryW
public void generateTranslations(RegistryWrapper.WrapperLookup registryLookup, TranslationBuilder translationBuilder)
{
translationBuilder.add(Gadgets.THERMAL_DETONATOR_ITEM, "Thermal Detonator");
translationBuilder.add(Gadgets.FRAGMENTATION_GRENADE_DESTROY, "Fragmenetation Grenade Destroy");
}
}

/**
* The gadget item tag generator. All item tags should be added
* through this generator.
*/
private static class TagGenerator extends FabricTagProvider.ItemTagProvider
private static class ItemTagGenerator extends FabricTagProvider.ItemTagProvider
{
public TagGenerator(FabricDataOutput output, CompletableFuture<RegistryWrapper.WrapperLookup> completableFuture)
public ItemTagGenerator(FabricDataOutput output, CompletableFuture<RegistryWrapper.WrapperLookup> completableFuture)
{
super(output, completableFuture);
}

@Override
protected void configure(RegistryWrapper.WrapperLookup wrapperLookup)
{

}
}
/**
* The gadget block tag generator. All block tags should be added
* through this generator.
*/
private static class BlockTagGenerator extends FabricTagProvider.BlockTagProvider
{
public BlockTagGenerator(FabricDataOutput output, CompletableFuture<RegistryWrapper.WrapperLookup> completableFuture)
{
super(output, completableFuture);
}

@Override
protected void configure(RegistryWrapper.WrapperLookup wrapperLookup)
{

getOrCreateTagBuilder(Gadgets.FRAGMENTATION_GRENADE_DESTROY)
.addTag(BlockTags.LEAVES)
.addTag(BlockTags.CAVE_VINES)
.addTag(BlockTags.CROPS)
.addTag(BlockTags.FLOWERS)
.addTag(BlockTags.SAPLINGS)
.addTag(ConventionalBlockTags.GLASS_BLOCKS);

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package dev.pswg.entity;

import dev.pswg.Gadgets;
import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.networking.v1.PlayerLookup;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.block.BlockState;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.explosion.Explosion;

import java.util.List;

public class FragmentationGrenadeEntity extends GrenadeEntity
{
public static final int MIN_PICKUP_AGE = 30;
public boolean IS_EXPLODING = false;
public int EXPLOSION_TICK = 0;
private boolean COLLISION_BELOW;
public boolean SHOULD_RENDER = true;

public FragmentationGrenadeEntity(EntityType<FragmentationGrenadeEntity> type, World world)
{
super(type, world);
setExplosionPower(5f);
}

@Override
protected void createParticles(double x, double y, double z, ServerWorld serverWorld)
{
}
@Override
public void explode()
{
if (!IS_EXPLODING)
{
if (getWorld() instanceof ServerWorld serverWorld)
{
//for (ServerPlayerEntity serverPlayerEntity : serverWorld.getPlayers())
// serverWorld.spawnParticles(serverPlayerEntity, SwgParticleTypes.FRAGMENTATION_GRENADE, true, getX(), getY(), getZ(), 1, 0, 0, 0, 0);
var passedData = new PacketByteBuf(Unpooled.buffer());
passedData.writeBoolean(true);
passedData.writeInt(getId());
//for (var player : PlayerLookup.tracking((ServerWorld)getWorld(), this.getBlockPos()))
// ServerPlayNetworking.send(player, SwgPackets.S2C.FragmentationGrenadeExplode, passedData);


}
IS_EXPLODING = true;

int randomNum = Random.create().nextBetween(1, 4);

/*
switch (randomNum){
case 1: getWorld().playSound(null, getBlockPos(), SwgSounds.Explosives.FRAGMENTATION_GRENADE_EXPLOSION1, SoundCategory.PLAYERS, 4f, 1f); break;
case 2: getWorld().playSound(null, getBlockPos(), SwgSounds.Explosives.FRAGMENTATION_GRENADE_EXPLOSION2, SoundCategory.PLAYERS, 4f, 1f); break;
case 3: getWorld().playSound(null, getBlockPos(), SwgSounds.Explosives.FRAGMENTATION_GRENADE_EXPLOSION3, SoundCategory.PLAYERS, 4f, 1f); break;
case 4: getWorld().playSound(null, getBlockPos(), SwgSounds.Explosives.FRAGMENTATION_GRENADE_EXPLOSION4, SoundCategory.PLAYERS, 4f, 1f); break;
}
*/
}
}
@Override
public boolean canBeHitByProjectile()
{
return true;
}

@Override
protected void onCollision(HitResult hitResult)
{
if (hitResult.getType() == HitResult.Type.BLOCK)
{
BlockHitResult blockHitResult = (BlockHitResult)hitResult;
var pos = blockHitResult.getBlockPos();
var state = getWorld().getBlockState(pos);
this.bounce(blockHitResult);

if (getVelocity().length() > 0.01f)
this.playSound(state.getSoundGroup().getHitSound(), 0.5f, 1f);
}

super.onCollision(hitResult);
}

@Override
public ActionResult interact(PlayerEntity player, Hand hand)
{
if (!isPrimed() && age > MIN_PICKUP_AGE && player.getMainHandStack().isEmpty())
{
player.giveItemStack(new ItemStack(Gadgets.THERMAL_DETONATOR_ITEM));
this.remove(RemovalReason.KILLED);
}
return super.interact(player, hand);
}

@Override
public boolean canHit()
{
return true;
}
@Override
public boolean shouldRender(double distance)
{
if (super.shouldRender(distance) && !IS_EXPLODING)
return true;
return false;
}
@Override
public void tick()
{
this.speed = this.speed * 0.95f;
velocityModified = true;
super.tick();
//if (getWorld().isClient() && this.age == 1 && this.isPrimed())
// SoundHelper.playFragmentationEntitySound(this);
if (IS_EXPLODING)
{
this.setVelocity(Vec3d.ZERO);
this.velocityModified = true;
}

if (EXPLOSION_TICK == 7)
{
//createSparkParticles(getWorld(), getX(), getY(), getZ(), COLLISION_BELOW);
/*if (!getWorld().isClient)
{
var passedData = new PacketByteBuf(Unpooled.buffer());
passedData.writeBoolean(false);
PacketByteBufHelper.writeVec3d(passedData, getPos());
passedData.writeInt(getId());
passedData.writeBoolean(COLLISION_BELOW);
for (var player : PlayerLookup.tracking((ServerWorld)getWorld(), this.getBlockPos()))
ServerPlayNetworking.send(player, SwgPackets.S2C.FragmentationGrenadeExplode, passedData);
}*/
List<LivingEntity> entities = getWorld().getEntitiesByClass(LivingEntity.class, this.getBoundingBox().expand(3, 3, 3), entity -> {
return true;
});
for (LivingEntity entity : entities)
{
float x = (float)(entity.getX() - getX()) / 3f;
float z = (float)(entity.getZ() - getZ()) / 3f;
entity.addVelocity(-x, 0, -z);
}
}
if (EXPLOSION_TICK >= 15)
{
super.explode();
}
if (IS_EXPLODING)
EXPLOSION_TICK++;
}

@Override
public boolean canExplosionDestroyBlock(Explosion explosion, BlockView world, BlockPos pos, BlockState state, float explosionPower)
{

return state.isIn(Gadgets.FRAGMENTATION_GRENADE_DESTROY);
}

}

0 comments on commit 0262e00

Please sign in to comment.