diff --git a/projects/pswg_gadgets/src/main/java/dev/pswg/Gadgets.java b/projects/pswg_gadgets/src/main/java/dev/pswg/Gadgets.java index 466ed9204..381409d44 100644 --- a/projects/pswg_gadgets/src/main/java/dev/pswg/Gadgets.java +++ b/projects/pswg_gadgets/src/main/java/dev/pswg/Gadgets.java @@ -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; @@ -60,6 +61,7 @@ public static Identifier id(String path) ComponentType.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 FRAGMENTATION_GRENADE_DESTROY = TagKey.of(RegistryKeys.BLOCK, id("fragmentation_destroy")); @Override public void onGalaxiesReady() diff --git a/projects/pswg_gadgets/src/main/java/dev/pswg/datagen/GadgetsDataGenerator.java b/projects/pswg_gadgets/src/main/java/dev/pswg/datagen/GadgetsDataGenerator.java index 5d85790a6..c87fc5cf9 100644 --- a/projects/pswg_gadgets/src/main/java/dev/pswg/datagen/GadgetsDataGenerator.java +++ b/projects/pswg_gadgets/src/main/java/dev/pswg/datagen/GadgetsDataGenerator.java @@ -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; @@ -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); } @@ -67,6 +69,7 @@ protected LangGenerator(FabricDataOutput dataOutput, CompletableFuture completableFuture) + public ItemTagGenerator(FabricDataOutput output, CompletableFuture completableFuture) { super(output, completableFuture); } @@ -84,6 +87,32 @@ public TagGenerator(FabricDataOutput output, CompletableFuture 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); + } } } diff --git a/projects/pswg_gadgets/src/main/java/dev/pswg/entity/FragmentationGrenadeEntity.java b/projects/pswg_gadgets/src/main/java/dev/pswg/entity/FragmentationGrenadeEntity.java new file mode 100644 index 000000000..731f3fdb6 --- /dev/null +++ b/projects/pswg_gadgets/src/main/java/dev/pswg/entity/FragmentationGrenadeEntity.java @@ -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 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 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); + } + +} \ No newline at end of file