Skip to content

Commit 7451bfa

Browse files
committed
Fixed command permissions and packets
1 parent 77dee73 commit 7451bfa

4 files changed

Lines changed: 50 additions & 42 deletions

File tree

common/src/main/java/com/faboslav/structurify/common/commands/StructurifyCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class StructurifyCommand
5555
public static void createCommand(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
5656
dispatcher.register(
5757
Commands.literal("structurify")
58+
.requires(source -> VersionedPermission.hasPermissions(source, VersionedPermission.PERMISSION_GAMEMASTER))
5859
.then(Commands.literal("dump")
5960
.requires(source -> VersionedPermission.hasPermissions(source, VersionedPermission.PERMISSION_GAMEMASTER))
6061
.executes(ctx -> {
@@ -67,6 +68,7 @@ public static void createCommand(CommandDispatcher<CommandSourceStack> dispatche
6768
})
6869
)
6970
.then(Commands.literal("config")
71+
.requires(source -> VersionedPermission.hasPermissions(source, VersionedPermission.PERMISSION_OWNER))
7072
.then(Commands.literal("sync")
7173
.then(Commands.literal("toServer")
7274
.requires(source -> VersionedPermission.hasPermissions(source, VersionedPermission.PERMISSION_OWNER))
@@ -118,6 +120,7 @@ public static void createCommand(CommandDispatcher<CommandSourceStack> dispatche
118120
)
119121
)
120122
.then(Commands.literal("debug")
123+
.requires(source -> VersionedPermission.hasPermissions(source, VersionedPermission.PERMISSION_GAMEMASTER))
121124
.then(Commands.literal("enable")
122125
.requires(source -> VersionedPermission.hasPermissions(source, VersionedPermission.PERMISSION_GAMEMASTER))
123126
.executes(ctx -> {

common/src/main/java/com/faboslav/structurify/common/network/packet/ConfigStatusToClientPacket.java

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
import com.google.gson.Gson;
1313
import com.google.gson.JsonObject;
1414
import net.minecraft.ChatFormatting;
15-
import net.minecraft.client.Minecraft;
1615
//? if >= 1.20.2 {
16+
import net.minecraft.client.Minecraft;
1717
import net.minecraft.network.RegistryFriendlyByteBuf;
18-
//?} else {
18+
//?} else {
1919
/*import net.minecraft.network.FriendlyByteBuf;
20-
*///?}
20+
*///?}
2121
import net.minecraft.network.chat.Component;
2222
import net.minecraft.network.chat.MutableComponent;
2323
import net.minecraft.resources.Identifier;
2424
import net.minecraft.world.entity.player.Player;
2525

26-
public record ConfigStatusToClientPacket(String config) implements Packet<ConfigStatusToClientPacket>
26+
import java.util.UUID;
27+
28+
public record ConfigStatusToClientPacket(String config, UUID playerId) implements Packet<ConfigStatusToClientPacket>
2729
{
2830
private static final Gson GSON = new Gson();
2931
public static final Identifier ID = Structurify.makeId("config_status_to_client_packet");
@@ -32,7 +34,8 @@ public record ConfigStatusToClientPacket(String config) implements Packet<Config
3234
public static void sendToClient(StructurifyConfig config, Player player) {
3335
MessageHandler.DEFAULT_CHANNEL.sendToPlayer(
3436
new ConfigStatusToClientPacket(
35-
GSON.toJson(StructurifyConfigSerializer.save(config))
37+
GSON.toJson(StructurifyConfigSerializer.save(config)),
38+
player.getUUID()
3639
),
3740
player
3841
);
@@ -51,43 +54,57 @@ public Identifier id() {
5154
}
5255

5356
@Override
54-
public Runnable handle(final ConfigStatusToClientPacket packet) {
57+
public Runnable handle(
58+
final ConfigStatusToClientPacket packet
59+
) {
5560
return () -> {
5661
JsonObject serverConfigJson;
57-
Player player = Minecraft.getInstance().player;
62+
var player = Minecraft.getInstance().level.getPlayerByUUID(packet.playerId);
5863

5964
try {
6065
serverConfigJson = GSON.fromJson(packet.config(), JsonObject.class);
6166
} catch (Throwable e) {
6267
Structurify.getLogger().error("Failed to read config status from server.", e);
6368

64-
if (player != null) {
65-
VersionedPlayer.sendSystemMessage(player, Component.literal("Failed to check the Structurify config status."));
66-
}
69+
VersionedPlayer.sendSystemMessage(
70+
player,
71+
Component.literal("Failed to check the Structurify config status.")
72+
);
6773

6874
return;
6975
}
7076

71-
if (player != null) {
72-
VersionedPlayer.sendSystemMessage(player, describeConfigStatus(Structurify.getConfig(), serverConfigJson));
73-
}
77+
VersionedPlayer.sendSystemMessage(
78+
player,
79+
describeConfigStatus(
80+
Structurify.getConfig(),
81+
serverConfigJson
82+
)
83+
);
7484
};
7585
}
7686

7787
//? if >= 1.20.2 {
7888
public ConfigStatusToClientPacket decode(final RegistryFriendlyByteBuf buf) {
79-
return new ConfigStatusToClientPacket(buf.readUtf());
89+
return new ConfigStatusToClientPacket(buf.readUtf(), buf.readUUID());
8090
}
8191

82-
public void encode(final ConfigStatusToClientPacket packet, final RegistryFriendlyByteBuf buf) {
92+
public void encode(
93+
final ConfigStatusToClientPacket packet,
94+
final RegistryFriendlyByteBuf buf
95+
) {
8396
buf.writeUtf(packet.config());
97+
buf.writeUUID(packet.playerId());
8498
}
8599
//?} else {
86100
/*public ConfigStatusToClientPacket decode(final FriendlyByteBuf buf) {
87101
return new ConfigStatusToClientPacket(buf.readUtf());
88102
}
89103
90-
public void encode(final ConfigStatusToClientPacket packet, final FriendlyByteBuf buf) {
104+
public void encode(
105+
final ConfigStatusToClientPacket packet,
106+
final FriendlyByteBuf buf
107+
) {
91108
buf.writeUtf(packet.config());
92109
}
93110
@@ -105,30 +122,24 @@ private static Component describeConfigStatus(
105122
String localHash = StructurifyConfigSerializer.computeConfigHash(localConfig);
106123
String serverHash = StructurifyConfigSerializer.hashConfigJson(serverConfigJson);
107124

108-
String localVersion = PlatformHooks.PLATFORM_HELPER.getModVersion();
109-
String serverVersion = serverConfigJson.has(
110-
StructurifyConfigSerializer.CONFIG_VERSION_PROPERTY
111-
)
112-
? serverConfigJson.get(
113-
StructurifyConfigSerializer.CONFIG_VERSION_PROPERTY
114-
).getAsString()
115-
:"unknown";
116-
117125
boolean isSynchronized = localHash.equals(serverHash);
118126

119127
MutableComponent message = isSynchronized
120128
? Component.literal("Structurify config is synchronized with the server.")
121129
.withStyle(ChatFormatting.GREEN)
122-
:Component.literal("Structurify config differs from the server.")
130+
: Component.literal("Structurify config differs from the server.")
123131
.withStyle(ChatFormatting.RED);
124132

133+
String localHashShort = localHash.substring(0, Math.min(16, localHash.length()));
134+
String serverHashShort = serverHash.substring(0, Math.min(16, serverHash.length()));
135+
125136
message.append(
126-
Component.literal("\nLocal version: " + localVersion)
137+
Component.literal("\nLocal version: " + localHashShort)
127138
.withStyle(ChatFormatting.GRAY)
128139
);
129140

130141
message.append(
131-
Component.literal("\nServer version: " + serverVersion)
142+
Component.literal("\nServer version: " + serverHashShort)
132143
.withStyle(ChatFormatting.GRAY)
133144
);
134145

common/src/main/java/com/faboslav/structurify/common/network/packet/ConfigSyncToClientPacket.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import net.minecraft.resources.Identifier;
2121
import net.minecraft.world.entity.player.Player;
2222

23-
public record ConfigSyncToClientPacket(String config, boolean save) implements Packet<ConfigSyncToClientPacket>
23+
import java.util.UUID;
24+
25+
public record ConfigSyncToClientPacket(String config, boolean save, UUID playerId) implements Packet<ConfigSyncToClientPacket>
2426
{
2527
private static final Gson GSON = new Gson();
2628
public static final Identifier ID = Structurify.makeId("config_sync_to_client_packet");
@@ -30,7 +32,8 @@ public static void sendToClient(StructurifyConfig config, Player player, boolean
3032
MessageHandler.DEFAULT_CHANNEL.sendToPlayer(
3133
new ConfigSyncToClientPacket(
3234
GSON.toJson(StructurifyConfigSerializer.save(config)),
33-
save
35+
save,
36+
player.getUUID()
3437
),
3538
player
3639
);
@@ -51,7 +54,7 @@ public Identifier id() {
5154
@Override
5255
public Runnable handle(final ConfigSyncToClientPacket packet) {
5356
return () -> {
54-
Player player = Minecraft.getInstance().player;
57+
var player = Minecraft.getInstance().level.getPlayerByUUID(packet.playerId);
5558

5659
try {
5760
StructurifyConfigSerializer.load(Structurify.getConfig(), GSON.fromJson(packet.config(), JsonObject.class));
@@ -77,12 +80,13 @@ public Runnable handle(final ConfigSyncToClientPacket packet) {
7780

7881
//? if >= 1.20.2 {
7982
public ConfigSyncToClientPacket decode(final RegistryFriendlyByteBuf buf) {
80-
return new ConfigSyncToClientPacket(buf.readUtf(), buf.readBoolean());
83+
return new ConfigSyncToClientPacket(buf.readUtf(), buf.readBoolean(), buf.readUUID());
8184
}
8285

8386
public void encode(final ConfigSyncToClientPacket packet, final RegistryFriendlyByteBuf buf) {
8487
buf.writeUtf(packet.config());
8588
buf.writeBoolean(packet.save());
89+
buf.writeUUID(packet.playerId());
8690
}
8791
//?} else {
8892
/*public ConfigSyncToClientPacket decode(final FriendlyByteBuf buf) {

common/src/main/java/com/faboslav/structurify/common/network/packet/ConfigSyncToServerPacket.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ public Consumer<Player> handle(final ConfigSyncToServerPacket packet) {
5757
}
5858

5959
VersionedPlayer.sendSystemMessage(player, Component.literal("Structurify config synced to the server."));
60-
61-
if (player instanceof ServerPlayer serverPlayer) {
62-
MessageHandler.DEFAULT_CHANNEL.sendToAllPlayers(
63-
new ConfigSyncToClientPacket(
64-
GSON.toJson(StructurifyConfigSerializer.save(Structurify.getConfig())),
65-
false
66-
),
67-
serverPlayer.level().getServer()
68-
);
69-
}
7060
};
7161
}
7262

0 commit comments

Comments
 (0)