From e1eef02be4c0651c7eaf93a189e9d33973227e92 Mon Sep 17 00:00:00 2001 From: QuImUfu Date: Mon, 3 Jul 2023 11:45:35 +0200 Subject: [PATCH] improve obstruction messages --- .../java/quimufu/structure_item/MyItem.java | 27 +++++++++++-------- .../structure_item/MyPlacementSettings.java | 8 +++--- .../PlacementNotAllowedException.java | 20 +++++++++++++- .../assets/structure_item/lang/en_us.json | 3 ++- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/main/java/quimufu/structure_item/MyItem.java b/src/main/java/quimufu/structure_item/MyItem.java index 3fd6cbf..fb66fd0 100644 --- a/src/main/java/quimufu/structure_item/MyItem.java +++ b/src/main/java/quimufu/structure_item/MyItem.java @@ -200,9 +200,9 @@ public class MyItem extends Item { Vec3i size = x.getSize(); switch (rotation) { - case CLOCKWISE_90 -> loc = loc.add(new Vec3i(size.getX()-1, 0, 0)); - case CLOCKWISE_180 -> loc = loc.add(new Vec3i(size.getX()-1, 0, size.getZ()-1)); - case COUNTERCLOCKWISE_90 -> loc = loc.add(new Vec3i(0, 0, size.getZ()-1)); + case CLOCKWISE_90 -> loc = loc.add(new Vec3i(size.getX() - 1, 0, 0)); + case CLOCKWISE_180 -> loc = loc.add(new Vec3i(size.getX() - 1, 0, size.getZ() - 1)); + case COUNTERCLOCKWISE_90 -> loc = loc.add(new Vec3i(0, 0, size.getZ() - 1)); } } else if (tag.contains("offsetV2", 10)) { Direction direction = c.getSide().getOpposite(); @@ -255,15 +255,20 @@ public class MyItem extends Item { .setSize(x.getSize()) .setMirror(BlockMirror.NONE) .setRotation(rotation); - boolean success = false; try { - if (x.place((ServerWorld) c.getWorld(), loc, BlockPos.ORIGIN, ps, c.getWorld().getRandom(), 2)) - success = true; - } catch (PlacementNotAllowedException ignored) { - } - if (success) { - c.getStack().decrement(1); - return ActionResult.SUCCESS; + if (x.place((ServerWorld) c.getWorld(), loc, BlockPos.ORIGIN, ps, c.getWorld().getRandom(), 2)) { + c.getStack().decrement(1); + return ActionResult.SUCCESS; + } + } catch (PlacementNotAllowedException placementNotAllowedException) { + Text message = + Text.translatable("items.structure.spawner.invalid.location.reason", + placementNotAllowedException.getName(), + Text.literal(String.valueOf(placementNotAllowedException.getPos().getX())), + Text.literal(String.valueOf(placementNotAllowedException.getPos().getY())), + Text.literal(String.valueOf(placementNotAllowedException.getPos().getZ()))); + sendPlayer(player, message); + return ActionResult.FAIL; } Text message = Text.translatable("items.structure.spawner.invalid.location"); diff --git a/src/main/java/quimufu/structure_item/MyPlacementSettings.java b/src/main/java/quimufu/structure_item/MyPlacementSettings.java index 59ec9bb..96f1355 100644 --- a/src/main/java/quimufu/structure_item/MyPlacementSettings.java +++ b/src/main/java/quimufu/structure_item/MyPlacementSettings.java @@ -17,7 +17,6 @@ import net.minecraft.world.World; import net.minecraft.world.WorldView; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.List; public class MyPlacementSettings extends StructurePlacementData { @@ -75,12 +74,13 @@ public class MyPlacementSettings extends StructurePlacementData { if (currentBlockInfo != null && World.isValid(posToCheck = currentBlockInfo.pos())) { for (Entity e : entitiesWithinAABB) { if (e.getBoundingBox().intersects(new Box(posToCheck))) { - throw new PlacementNotAllowedException(); + throw new PlacementNotAllowedException(e.getName(), posToCheck); } } Block blockToCheck = world.getBlockState(posToCheck).getBlock(); - if (blacklist.contains(blockToCheck)) - throw new PlacementNotAllowedException(); + if (blacklist.contains(blockToCheck)) { + throw new PlacementNotAllowedException(blockToCheck.getName(), posToCheck); + } } return currentBlockInfo; } diff --git a/src/main/java/quimufu/structure_item/PlacementNotAllowedException.java b/src/main/java/quimufu/structure_item/PlacementNotAllowedException.java index 48fc18e..f326218 100644 --- a/src/main/java/quimufu/structure_item/PlacementNotAllowedException.java +++ b/src/main/java/quimufu/structure_item/PlacementNotAllowedException.java @@ -1,4 +1,22 @@ package quimufu.structure_item; -public class PlacementNotAllowedException extends RuntimeException{ +import net.minecraft.text.Text; +import net.minecraft.util.math.BlockPos; + +public class PlacementNotAllowedException extends RuntimeException { + private final Text name; + private final BlockPos pos; + + public PlacementNotAllowedException(Text name, BlockPos pos) { + this.name = name; + this.pos = pos; + } + + public BlockPos getPos() { + return pos; + } + + public Text getName() { + return name; + } } diff --git a/src/main/resources/assets/structure_item/lang/en_us.json b/src/main/resources/assets/structure_item/lang/en_us.json index 4adee5a..840b9b6 100644 --- a/src/main/resources/assets/structure_item/lang/en_us.json +++ b/src/main/resources/assets/structure_item/lang/en_us.json @@ -1,5 +1,6 @@ { - "items.structure.spawner.invalid.block.clicked": "Digga %s is net %s, maan.", + "items.structure.spawner.invalid.block.clicked": "Can only be placed on %2$s, not %1$s.", + "items.structure.spawner.invalid.location.reason": "%s is in the way at %s, %s, %s!", "items.structure.spawner.invalid.location": "Something is in the way!", "items.structure.spawner.invalid.structure.name": "Invalid structure name", "items.structure.spawner.structure.nonexistent": "Structure: %s does not exist",