From 8220908259dae0346bfab8eee5aa06eb84141848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cfenn7=E2=80=9D?= Date: Tue, 19 Dec 2023 18:28:38 +0000 Subject: [PATCH] feat: implement simple cluster detonation --- ...AbstractDisguisedExplosiveBlockEntity.java | 15 ++++++- .../item/custom/misc/ClusterRoundItem.java | 44 +++++++++++++++++-- .../data/c/tags/items/payload_explosives.json | 4 +- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/main/java/fenn7/grenadesandgadgets/commonside/block/entity/AbstractDisguisedExplosiveBlockEntity.java b/src/main/java/fenn7/grenadesandgadgets/commonside/block/entity/AbstractDisguisedExplosiveBlockEntity.java index cf026c2..c7eff65 100644 --- a/src/main/java/fenn7/grenadesandgadgets/commonside/block/entity/AbstractDisguisedExplosiveBlockEntity.java +++ b/src/main/java/fenn7/grenadesandgadgets/commonside/block/entity/AbstractDisguisedExplosiveBlockEntity.java @@ -3,13 +3,17 @@ import java.util.Map; import java.util.UUID; +import fenn7.grenadesandgadgets.commonside.item.GrenadesModItems; import fenn7.grenadesandgadgets.commonside.item.custom.block.DisguisedExplosiveBlockItem; +import fenn7.grenadesandgadgets.commonside.item.custom.misc.ClusterRoundItem; import fenn7.grenadesandgadgets.commonside.util.ImplementedInventory; import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory; import net.minecraft.block.BlockState; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.MarkerEntity; import net.minecraft.entity.TntEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; @@ -34,7 +38,8 @@ public abstract class AbstractDisguisedExplosiveBlockEntity extends BlockEntity protected static final String LAST_USER = "last.user"; protected static final Map PAYLOAD_TO_ENTITY = Map.of( Items.TNT, "TNT", - Items.TNT_MINECART, "TNT" + Items.TNT_MINECART, "TNT", + GrenadesModItems.CLUSTER_ROUND, "CLUSTER" ); protected Item disguiseBlockItem; protected @Nullable PlayerEntity lastUser; @@ -79,6 +84,10 @@ protected void handlePayload(ItemStack stack, World world, BlockPos pos) { payload = new TntEntity(world, pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5, this.getLastUser()); ((TntEntity) payload).setFuse(0); } + case "CLUSTER" -> { + payload = new MarkerEntity(EntityType.ENDER_DRAGON, world); + ((ClusterRoundItem) stack.getItem()).explodeAtLocation(stack, Vec3d.ofCenter(pos), this.getLastUser()); + } default -> payload = null; }; if (payload != null) { @@ -86,7 +95,9 @@ protected void handlePayload(ItemStack stack, World world, BlockPos pos) { payload.setPosition(Vec3d.ofCenter(pos)); world.playSound(null, pos, SoundEvents.BLOCK_NOTE_BLOCK_HARP, SoundCategory.HOSTILE, 20.0F, 0.5F); world.breakBlock(pos, false); - world.spawnEntity(payload); + if (!(payload instanceof MarkerEntity)) { + world.spawnEntity(payload); + } } } diff --git a/src/main/java/fenn7/grenadesandgadgets/commonside/item/custom/misc/ClusterRoundItem.java b/src/main/java/fenn7/grenadesandgadgets/commonside/item/custom/misc/ClusterRoundItem.java index 67b6d26..c9d0f30 100644 --- a/src/main/java/fenn7/grenadesandgadgets/commonside/item/custom/misc/ClusterRoundItem.java +++ b/src/main/java/fenn7/grenadesandgadgets/commonside/item/custom/misc/ClusterRoundItem.java @@ -2,19 +2,25 @@ import java.util.List; +import fenn7.grenadesandgadgets.commonside.item.custom.grenades.AbstractGrenadeItem; +import fenn7.grenadesandgadgets.commonside.item.recipe.custom.GrenadeModifierRecipe; import fenn7.grenadesandgadgets.commonside.util.GrenadesModUtil; import net.minecraft.client.item.TooltipContext; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtList; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; +import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; +import net.minecraft.world.explosion.Explosion; import org.jetbrains.annotations.Nullable; public class ClusterRoundItem extends Item { public static final String GRENADES_KEY = "grenades"; + private static final int MAX_GRENADE_AGE = 30; public ClusterRoundItem(Settings settings) { super(settings); @@ -23,12 +29,44 @@ public ClusterRoundItem(Settings settings) { @Override public void appendTooltip(ItemStack stack, @Nullable World world, List tooltip, TooltipContext context) { super.appendTooltip(stack, world, tooltip, context); + NbtList nbtList = this.getNbtList(stack); + if (!nbtList.isEmpty()) { + for (int i = 0; i < nbtList.size(); ++i) { + ItemStack grenadeStack = ItemStack.fromNbt(nbtList.getCompound(i)); + tooltip.add(new TranslatableText(grenadeStack.getTranslationKey())); + String modifier = grenadeStack.getOrCreateNbt().getString(GrenadeModifierRecipe.MODIFIER_KEY); + if (!modifier.isBlank()) { + tooltip.add(GrenadesModUtil.textOf(" " + ("§n§6" + modifier))); + } + } + } + } + + private NbtList getNbtList(ItemStack stack) { NbtCompound stackNbt = stack.getOrCreateNbt(); if (stackNbt.contains(GRENADES_KEY) && stackNbt.get(GRENADES_KEY) instanceof NbtList) { - NbtList nbtList = stackNbt.getList(GRENADES_KEY, 10); + return stackNbt.getList(GRENADES_KEY, 10); + } + return new NbtList(); + } + + public void explodeAtLocation(ItemStack clusterStack, Vec3d pos, PlayerEntity player) { + NbtList nbtList = this.getNbtList(clusterStack); + if (!nbtList.isEmpty()) { + player.world.createExplosion(player, pos.x, pos.y, pos.z, 2F + (nbtList.size() * 0.5F), false, Explosion.DestructionType.DESTROY); for (int i = 0; i < nbtList.size(); ++i) { - ItemStack fragmentStack = ItemStack.fromNbt(nbtList.getCompound(i)); - tooltip.add(new TranslatableText(fragmentStack.getTranslationKey())); + ItemStack grenadeStack = ItemStack.fromNbt(nbtList.getCompound(i)); + if (grenadeStack.getItem() instanceof AbstractGrenadeItem g) { + var grenade = g.createGrenadeAt(player.world, player, grenadeStack); + AbstractGrenadeItem.addNbtModifier(grenadeStack, grenade); + grenade.setItem(grenadeStack); + grenade.setMaxAgeTicks(MAX_GRENADE_AGE); + grenade.setShouldBounce(false); + grenade.setPosition(pos.add(new Vec3d(i/2, 0, i/2))); + if (!player.world.isClient()) { + player.world.spawnEntity(grenade); + } + } } } } diff --git a/src/main/resources/data/c/tags/items/payload_explosives.json b/src/main/resources/data/c/tags/items/payload_explosives.json index 0e83fb6..24e1c48 100644 --- a/src/main/resources/data/c/tags/items/payload_explosives.json +++ b/src/main/resources/data/c/tags/items/payload_explosives.json @@ -1,6 +1,8 @@ { "replace": false, "values": [ - "minecraft:tnt" + "minecraft:tnt", + "minecraft:tnt_minecart", + "grenadesandgadgets:cluster_round" ] } \ No newline at end of file