Skip to content

Commit 836184a

Browse files
Implement custom teleport click actions
1 parent 1272a6e commit 836184a

4 files changed

Lines changed: 51 additions & 12 deletions

File tree

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package com.github.quiltservertools.ledger.mixin;
22

33
import com.github.quiltservertools.ledger.commands.subcommands.PageCommand;
4+
import com.github.quiltservertools.ledger.commands.subcommands.TeleportCommand;
45
import com.github.quiltservertools.ledger.utility.MessageUtils;
6+
import net.minecraft.core.BlockPos;
7+
import net.minecraft.core.registries.Registries;
58
import net.minecraft.nbt.Tag;
69
import net.minecraft.network.protocol.common.ServerboundCustomClickActionPacket;
10+
import net.minecraft.resources.Identifier;
11+
import net.minecraft.resources.ResourceKey;
12+
import net.minecraft.server.level.ServerLevel;
713
import net.minecraft.server.level.ServerPlayer;
814
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
915
import net.minecraft.server.network.ServerGamePacketListenerImpl;
16+
import net.minecraft.world.level.Level;
1017
import org.spongepowered.asm.mixin.Mixin;
1118
import org.spongepowered.asm.mixin.injection.At;
1219
import org.spongepowered.asm.mixin.injection.Inject;
@@ -16,14 +23,30 @@
1623
public class ServerCommonPacketListenerImplMixin {
1724
@Inject(method = "handleCustomClickAction", at = @At("HEAD"), cancellable = true)
1825
private void handleLedgerPageChangeClick(ServerboundCustomClickActionPacket packet, CallbackInfo ci) {
19-
if (!packet.id().equals(MessageUtils.INSTANCE.getPageChangeAction())) return;
2026
if (!((Object) this instanceof ServerGamePacketListenerImpl game)) return;
21-
ServerPlayer player = game.player;
22-
packet.payload().flatMap(Tag::asCompound)
23-
.flatMap(root -> root.getInt("page"))
24-
.ifPresent(page ->
25-
PageCommand.INSTANCE.page(player.createCommandSourceStack(), page)
27+
28+
if (packet.id().equals(MessageUtils.INSTANCE.getPageChangeAction())) {
29+
ServerPlayer player = game.player;
30+
packet.payload().flatMap(Tag::asCompound)
31+
.flatMap(root -> root.getInt("page"))
32+
.ifPresent(page ->
33+
PageCommand.INSTANCE.page(player.createCommandSourceStack(), page)
34+
);
35+
ci.cancel();
36+
} else if (packet.id().equals(MessageUtils.INSTANCE.getTeleportAction())) {
37+
ServerPlayer player = game.player;
38+
packet.payload().flatMap(Tag::asCompound).ifPresent(tag -> {
39+
var pos = new BlockPos(
40+
tag.getIntOr("x", 0),
41+
tag.getIntOr("y", 0),
42+
tag.getIntOr("z", 0)
2643
);
27-
ci.cancel();
44+
var world = Identifier.parse(tag.getStringOr("world", ""));
45+
ResourceKey<Level> resourceKey = ResourceKey.create(Registries.DIMENSION, world);
46+
ServerLevel serverLevel = game.player.level().getServer().getLevel(resourceKey);
47+
TeleportCommand.INSTANCE.teleport(player, serverLevel, pos);
48+
});
49+
ci.cancel();
50+
}
2851
}
2952
}

src/main/kotlin/com/github/quiltservertools/ledger/actions/AbstractActionType.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.github.quiltservertools.ledger.utility.literal
88
import net.minecraft.ChatFormatting
99
import net.minecraft.commands.CommandSourceStack
1010
import net.minecraft.core.BlockPos
11+
import net.minecraft.nbt.CompoundTag
1112
import net.minecraft.network.chat.ClickEvent
1213
import net.minecraft.network.chat.Component
1314
import net.minecraft.network.chat.HoverEvent
@@ -18,6 +19,7 @@ import net.minecraft.server.players.NameAndId
1819
import net.minecraft.util.Util
1920
import net.minecraft.world.level.Level
2021
import java.time.Instant
22+
import java.util.*
2123
import kotlin.time.ExperimentalTime
2224

2325
abstract class AbstractActionType : ActionType {
@@ -98,15 +100,20 @@ abstract class AbstractActionType : ActionType {
98100
open fun getLocationMessage(): Component = "${pos.x} ${pos.y} ${pos.z}".literal()
99101
.setStyle(TextColorPallet.secondary)
100102
.withStyle {
103+
val tag: CompoundTag = CompoundTag().apply {
104+
this.putInt("x", pos.x)
105+
this.putInt("y", pos.y)
106+
this.putInt("z", pos.z)
107+
this.putString("world", (world ?: Level.OVERWORLD.identifier()).toString())
108+
}
109+
101110
it.withHoverEvent(
102111
HoverEvent.ShowText(
103112
Component.literal(world?.let { "$it\n" } ?: "")
104113
.append(Component.translatable("text.ledger.action_message.location.hover"))
105114
)
106115
).withClickEvent(
107-
ClickEvent.RunCommand(
108-
"/lg tp ${world ?: Level.OVERWORLD.identifier()} ${pos.x} ${pos.y} ${pos.z}"
109-
)
116+
ClickEvent.Custom(MessageUtils.teleportAction, Optional.of(tag))
110117
)
111118
}
112119
}

src/main/kotlin/com/github/quiltservertools/ledger/commands/subcommands/TeleportCommand.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import net.minecraft.commands.Commands
99
import net.minecraft.commands.arguments.DimensionArgument
1010
import net.minecraft.commands.arguments.coordinates.Coordinates
1111
import net.minecraft.commands.arguments.coordinates.Vec3Argument
12+
import net.minecraft.core.BlockPos
1213
import net.minecraft.server.level.ServerLevel
14+
import net.minecraft.server.level.ServerPlayer
1315

1416
object TeleportCommand : BuildableCommand {
1517
private const val BLOCK_CENTER_OFFSET = 0.5
@@ -35,6 +37,14 @@ object TeleportCommand : BuildableCommand {
3537
val player = context.source.playerOrException
3638
val pos = posArg.getBlockPos(context.source)
3739

40+
teleport(player, world, pos)
41+
42+
return 1
43+
}
44+
45+
fun teleport(player: ServerPlayer, world: ServerLevel, pos: BlockPos) {
46+
if (!Permissions.check(player, "ledger.commands.tp", CommandConsts.PERMISSION_LEVEL)) return
47+
3848
val x = pos.x.toDouble() + BLOCK_CENTER_OFFSET
3949
val z = pos.z.toDouble() + BLOCK_CENTER_OFFSET
4050

@@ -48,7 +58,5 @@ object TeleportCommand : BuildableCommand {
4858
player.xRot,
4959
true
5060
)
51-
52-
return 1
5361
}
5462
}

src/main/kotlin/com/github/quiltservertools/ledger/utility/MessageUtils.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import kotlin.time.toKotlinDuration
2525

2626
object MessageUtils {
2727
val pageChangeAction: Identifier = Ledger.identifier("page-change")
28+
val teleportAction: Identifier = Ledger.identifier("teleport")
2829

2930
@OptIn(ExperimentalTime::class)
3031
suspend fun sendSearchResults(source: CommandSourceStack, results: SearchResults, header: Component) {

0 commit comments

Comments
 (0)