From ab5aeef50ed101b11bd439abaed323e3d0708f5a Mon Sep 17 00:00:00 2001 From: Christopher Wade Date: Tue, 7 Oct 2025 22:52:40 -0500 Subject: [PATCH] Add PLACE command. --- .../simpletomb/data/TombCommands.java | 2 +- .../simpletomb/event/CommandEvents.java | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/lothrazar/simpletomb/data/TombCommands.java b/src/main/java/com/lothrazar/simpletomb/data/TombCommands.java index e1ab252..1492867 100644 --- a/src/main/java/com/lothrazar/simpletomb/data/TombCommands.java +++ b/src/main/java/com/lothrazar/simpletomb/data/TombCommands.java @@ -1,7 +1,7 @@ package com.lothrazar.simpletomb.data; public enum TombCommands { - RESTORE, LIST, DELETE, KEY; + RESTORE, LIST, DELETE, KEY, PLACE; @Override public String toString() { diff --git a/src/main/java/com/lothrazar/simpletomb/event/CommandEvents.java b/src/main/java/com/lothrazar/simpletomb/event/CommandEvents.java index e54a436..ba1d5a2 100644 --- a/src/main/java/com/lothrazar/simpletomb/event/CommandEvents.java +++ b/src/main/java/com/lothrazar/simpletomb/event/CommandEvents.java @@ -37,6 +37,7 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.block.state.BlockState; import net.minecraftforge.common.capabilities.ForgeCapabilities; +import net.minecraft.world.phys.BlockHitResult; import net.minecraftforge.event.RegisterCommandsEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.items.IItemHandler; @@ -74,6 +75,12 @@ public void onRegisterCommandsEvent(RegisterCommandsEvent event) { .executes(x -> { return exeDelete(x, getPlayerProfile(x)); }))) + .then(Commands.literal(TombCommands.PLACE.toString()) + .then(Commands.argument(ARG_PLAYER, GameProfileArgument.gameProfile()).suggests((cs, b) -> buildPlayerArg(cs, b)) + .then(Commands.argument(ARG_SELECTED, IntegerArgumentType.integer()) + .executes(x -> { + return exePlace(x, getPlayerProfile(x), IntegerArgumentType.getInteger(x, ARG_SELECTED)); + })))) // more go here ); } @@ -169,6 +176,64 @@ private int exeRestore(CommandContext ctx, GameProfile targe } return 0; } + + private int exePlace(CommandContext ctx, GameProfile target, int index) throws CommandSyntaxException { + ServerPlayer executor = ctx.getSource().getPlayerOrException(); + ServerLevel targetWorld = ctx.getSource().getLevel(); + + // Get the block the player is looking at + BlockHitResult hitResult = (BlockHitResult) executor.pick(20.0D, 0.0F, false); + BlockPos targetPos = hitResult.getBlockPos().relative(hitResult.getDirection()); + + if (!targetWorld.isInWorldBounds(targetPos)) { + MutableComponent msg = Component.translatable("Target position is out of world bounds"); + msg.setStyle(Style.EMPTY.withColor(ChatFormatting.RED)); + ctx.getSource().sendFailure(msg); + return 1; + } + + // Check if the position is valid for placement + if (!WorldHelper.isValidPlacement(targetWorld, targetPos)) { + MutableComponent msg = Component.translatable("Invalid placement location - must be air or water"); + msg.setStyle(Style.EMPTY.withColor(ChatFormatting.RED)); + ctx.getSource().sendFailure(msg); + return 1; + } + + ctx.getSource().sendSuccess(() -> Component.translatable("Attempting to place tomb [" + index + "] for player " + target.getName() + " at " + targetPos.toShortString()), false); + + PlayerTombRecords found = ModTomb.GLOBAL.findGrave(target.getId()); + if (found != null && found.playerGraves.size() > index) { + CompoundTag grave = found.playerGraves.get(index); + if (grave == null) { + ModTomb.LOGGER.error("Invalid grave index " + index + "; try between 0 and " + (found.playerGraves.size() - 1)); + return 1; + } + + List drops = PlayerTombRecords.getDrops(grave); + Direction facing = executor.getDirection().getOpposite(); + BlockState state = PlayerTombEvents.getRandomGrave(targetWorld, facing); + + boolean wasPlaced = WorldHelper.placeGrave(targetWorld, targetPos, state); + if (wasPlaced) { + BlockEntityTomb tile = (BlockEntityTomb) targetWorld.getBlockEntity(targetPos); + tile.initTombstoneOwner(target); + IItemHandler itemHandler = tile.getCapability(ForgeCapabilities.ITEM_HANDLER, null).orElse(null); + + for (ItemStack d : drops) { + ItemHandlerHelper.insertItemStacked(itemHandler, d.copy(), false); + } + + ctx.getSource().sendSuccess(() -> { + MutableComponent msg = Component.translatable("Successfully placed tomb at " + targetPos.toShortString()); + msg.setStyle(Style.EMPTY.withColor(ChatFormatting.GREEN)); + return msg; + }, true); + } + } + return 0; + } + // // private void badCommandMsg(ServerPlayerEntity player) { // //.setStyle(Style.EMPTY.setFormatting(TextFormatting.GOLD))