Skip to content

Commit

Permalink
Fix signs being empty when placed
Browse files Browse the repository at this point in the history
As of 1.20, sign messages are immutable - we need to do
text = text.setMesssage(...) instead. Also do a tiny bit of cleanup to
this function while we're here.

Probably not the best use of my lunch break :D:.

Fixes #1611.
  • Loading branch information
SquidDev committed Oct 20, 2023
1 parent e3ced84 commit 8eabd4f
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.world.level.block.entity.SignText;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -186,7 +185,7 @@ private static boolean deployOnBlock(
tile = world.getBlockEntity(position.relative(side));
}

if (tile instanceof SignBlockEntity) setSignText(world, tile, message);
if (tile instanceof SignBlockEntity sign) setSignText(world, sign, message);
}

return placed;
Expand Down Expand Up @@ -220,23 +219,20 @@ private static InteractionResult doDeployOnBlock(ItemStack stack, TurtlePlayer t
return InteractionResult.PASS;
}

private static void setSignText(Level world, BlockEntity tile, String message) {
var signTile = (SignBlockEntity) tile;
var split = Splitter.on('\n').splitToList(message);
var firstLine = split.size() <= 2 ? 1 : 0;
private static void setSignText(Level world, SignBlockEntity sign, String message) {
var lines = Splitter.on('\n').splitToList(message);
var firstLine = lines.size() <= 2 ? 1 : 0;

var signText = new SignText();
for (var i = 0; i < 4; i++) {
if (i >= firstLine && i < firstLine + split.size()) {
var line = split.get(i - firstLine);
signText.setMessage(i, line.length() > 15
? Component.literal(line.substring(0, 15))
: Component.literal(line)
);
}
for (int i = 0, len = Math.min(lines.size(), 4); i < len; i++) {
var line = lines.get(i);
signText = signText.setMessage(i + firstLine, line.length() > 15
? Component.literal(line.substring(0, 15))
: Component.literal(line)
);
}
signTile.setText(signText, true);
world.sendBlockUpdated(tile.getBlockPos(), tile.getBlockState(), tile.getBlockState(), Block.UPDATE_ALL);
sign.setText(signText, true);
world.sendBlockUpdated(sign.getBlockPos(), sign.getBlockState(), sign.getBlockState(), Block.UPDATE_ALL);
}

private static final class ErrorMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import net.minecraft.world.item.Items
import net.minecraft.world.item.enchantment.Enchantments
import net.minecraft.world.level.block.Blocks
import net.minecraft.world.level.block.FenceBlock
import net.minecraft.world.level.block.entity.BlockEntityType
import net.minecraft.world.level.block.state.properties.BlockStateProperties
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.array
Expand Down Expand Up @@ -86,6 +87,26 @@ class Turtle_Test {
thenExecute { helper.assertBlockPresent(Blocks.LAVA, BlockPos(2, 2, 2)) }
}

/**
* Checks turtles can write to signs.
*
* @see [#1611](https://github.com/cc-tweaked/CC-Tweaked/issues/1611)
*/
@GameTest
fun Place_sign(helper: GameTestHelper) = helper.sequence {
thenOnComputer {
turtle.place(ObjectArguments("Test\nmessage")).await()
.assertArrayEquals(true, message = "Placed sign")
}
thenExecute {
val sign = helper.getBlockEntity(BlockPos(2, 2, 1), BlockEntityType.SIGN)
val lines = listOf("", "Test", "message", "")
for ((i, line) in lines.withIndex()) {
assertEquals(line, sign.frontText.getMessage(i, false).string, "Line $i")
}
}
}

/**
* Checks that calling [net.minecraft.world.item.Item.use] will not place blocks too far away.
*
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8eabd4f

Please sign in to comment.