Skip to content

Commit

Permalink
feat: implement simple cluster detonation
Browse files Browse the repository at this point in the history
  • Loading branch information
fenn7 committed Dec 19, 2023
1 parent cca4708 commit 8220908
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +38,8 @@ public abstract class AbstractDisguisedExplosiveBlockEntity extends BlockEntity
protected static final String LAST_USER = "last.user";
protected static final Map<Item, String> 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;
Expand Down Expand Up @@ -79,14 +84,20 @@ 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) {
payload.setNoGravity(true);
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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -23,12 +29,44 @@ public ClusterRoundItem(Settings settings) {
@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> 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);
}
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/data/c/tags/items/payload_explosives.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"replace": false,
"values": [
"minecraft:tnt"
"minecraft:tnt",
"minecraft:tnt_minecart",
"grenadesandgadgets:cluster_round"
]
}

0 comments on commit 8220908

Please sign in to comment.