Skip to content

Commit 5bfce5e

Browse files
committed
Make it give the player the shard item
1 parent 2f8084b commit 5bfce5e

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/main/java/net/modfest/scatteredshards/client/command/CreateInstantCommand.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import net.modfest.scatteredshards.api.shard.ShardType;
1414
import net.modfest.scatteredshards.command.ShardCommand;
1515
import net.modfest.scatteredshards.command.ShardCommandNodeHelper;
16-
import net.modfest.scatteredshards.networking.C2SModifyShard;
16+
import net.modfest.scatteredshards.networking.C2SCreateShardInstant;
1717
import net.modfest.scatteredshards.util.ModMetaUtil;
1818

1919
import static net.modfest.scatteredshards.client.command.ClientShardCommand.identifierArgument;
@@ -51,7 +51,7 @@ public static int create(CommandContext<FabricClientCommandSource> ctx) throws C
5151
shard.setIcon(modIcon);
5252
shard.setSourceId(Identifier.of(modId, "shard_pack"));
5353

54-
ClientPlayNetworking.send(new C2SModifyShard(shardId, shard));
54+
ClientPlayNetworking.send(new C2SCreateShardInstant(shardId, shard));
5555

5656
source.sendFeedback(Text.translatable("commands.scattered_shards.shard.create_instant", shardId.toString()));
5757

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package net.modfest.scatteredshards.networking;
2+
3+
import me.lucko.fabric.api.permissions.v0.Permissions;
4+
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
5+
import net.minecraft.network.RegistryByteBuf;
6+
import net.minecraft.network.codec.PacketCodec;
7+
import net.minecraft.network.packet.CustomPayload;
8+
import net.minecraft.server.MinecraftServer;
9+
import net.minecraft.server.network.ServerPlayerEntity;
10+
import net.minecraft.util.Identifier;
11+
import net.modfest.scatteredshards.ScatteredShards;
12+
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
13+
import net.modfest.scatteredshards.api.impl.ShardLibraryPersistentState;
14+
import net.modfest.scatteredshards.api.shard.Shard;
15+
import net.modfest.scatteredshards.block.ShardBlock;
16+
17+
/**
18+
* Requests that a shard be created. Requires permissions!
19+
*/
20+
public record C2SCreateShardInstant(Identifier shardId, Shard shard) implements CustomPayload {
21+
public static final Id<C2SCreateShardInstant> PACKET_ID = new Id<>(ScatteredShards.id("create_shard_instant"));
22+
public static final PacketCodec<RegistryByteBuf, C2SCreateShardInstant> PACKET_CODEC = PacketCodec.tuple(Identifier.PACKET_CODEC, C2SCreateShardInstant::shardId, Shard.PACKET_CODEC, C2SCreateShardInstant::shard, C2SCreateShardInstant::new);
23+
24+
public static void receive(C2SCreateShardInstant payload, ServerPlayNetworking.Context context) {
25+
// TODO: This is mostly identical to C2SModifyShard, no need for so much duplicate code
26+
27+
MinecraftServer server = context.player().getServer();
28+
server.execute(() -> {
29+
boolean success = server.isSingleplayer() || Permissions.check(context.player(), ScatteredShardsAPI.MODIFY_SHARD_PERMISSION, 1);
30+
31+
//Let the sender know of success or failure before a shard update comes through
32+
ServerPlayNetworking.send(context.player(), new S2CModifyShardResult(payload.shardId(), success));
33+
34+
if (!success) {
35+
return;
36+
}
37+
38+
//Update our serverside library
39+
var library = ScatteredShardsAPI.getServerLibrary();
40+
library.shards().put(payload.shardId(), payload.shard());
41+
library.shardSets().put(payload.shard().sourceId(), payload.shardId());
42+
43+
//Make sure the NBT gets written on next world-save
44+
ShardLibraryPersistentState.get(server).markDirty();
45+
46+
//Update everyone's client libraries with the new shard
47+
S2CSyncShard syncShard = new S2CSyncShard(payload.shardId(), payload.shard());
48+
for (ServerPlayerEntity otherPlayer : server.getPlayerManager().getPlayerList()) {
49+
ServerPlayNetworking.send(otherPlayer, syncShard);
50+
}
51+
52+
var itemStack = ShardBlock.createShardBlock(library, payload.shardId(), false, 0.5f, 0.5f);
53+
context.player().getInventory().offerOrDrop(itemStack);
54+
});
55+
}
56+
57+
@Override
58+
public Id<? extends CustomPayload> getId() {
59+
return PACKET_ID;
60+
}
61+
}

src/main/java/net/modfest/scatteredshards/networking/ScatteredShardsNetworking.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ public static void register() {
2828
PayloadTypeRegistry.playS2C().register(S2CSyncCollection.PACKET_ID, S2CSyncCollection.PACKET_CODEC);
2929
PayloadTypeRegistry.playS2C().register(S2CSyncGlobalCollection.PACKET_ID, S2CSyncGlobalCollection.PACKET_CODEC);
3030
PayloadTypeRegistry.playC2S().register(C2SModifyShard.PACKET_ID, C2SModifyShard.PACKET_CODEC);
31+
PayloadTypeRegistry.playC2S().register(C2SCreateShardInstant.PACKET_ID, C2SCreateShardInstant.PACKET_CODEC);
3132
PayloadTypeRegistry.playC2S().register(C2SRequestGlobalCollection.PACKET_ID, C2SRequestGlobalCollection.PACKET_CODEC);
3233
PayloadTypeRegistry.playS2C().register(S2CModifyShardResult.PACKET_ID, S2CModifyShardResult.PACKET_CODEC);
3334
PayloadTypeRegistry.playS2C().register(S2CUpdateShard.PACKET_ID, S2CUpdateShard.PACKET_CODEC);
3435

3536
ServerPlayNetworking.registerGlobalReceiver(C2SModifyShard.PACKET_ID, C2SModifyShard::receive);
37+
ServerPlayNetworking.registerGlobalReceiver(C2SCreateShardInstant.PACKET_ID, C2SCreateShardInstant::receive);
3638
ServerPlayNetworking.registerGlobalReceiver(C2SRequestGlobalCollection.PACKET_ID, C2SRequestGlobalCollection::receive);
3739

3840
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {

0 commit comments

Comments
 (0)