improve obstruction messages

This commit is contained in:
QuImUfu 2023-07-03 11:45:35 +02:00
parent 897c1e5975
commit e1eef02be4
4 changed files with 41 additions and 17 deletions

View File

@ -255,16 +255,21 @@ 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) {
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");
sendPlayer(player, message);

View File

@ -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;
}

View File

@ -1,4 +1,22 @@
package quimufu.structure_item;
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;
}
}

View File

@ -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",