11package net .modfest .scatteredshards .networking ;
22
3- import me .lucko .fabric .api .permissions .v0 .Permissions ;
43import net .fabricmc .fabric .api .networking .v1 .ServerPlayNetworking ;
4+ import net .minecraft .item .ItemStack ;
55import net .minecraft .network .RegistryByteBuf ;
66import net .minecraft .network .codec .PacketCodec ;
77import net .minecraft .network .packet .CustomPayload ;
8- import net .minecraft .server .MinecraftServer ;
9- import net .minecraft .server .network .ServerPlayerEntity ;
108import net .minecraft .util .Identifier ;
119import net .modfest .scatteredshards .ScatteredShards ;
1210import net .modfest .scatteredshards .api .ScatteredShardsAPI ;
13- import net .modfest .scatteredshards .api .impl . ShardLibraryPersistentState ;
11+ import net .modfest .scatteredshards .api .ShardLibrary ;
1412import net .modfest .scatteredshards .api .shard .Shard ;
1513import net .modfest .scatteredshards .block .ShardBlock ;
1614
@@ -22,36 +20,16 @@ public record C2SCreateShardInstant(Identifier shardId, Shard shard) implements
2220 public static final PacketCodec <RegistryByteBuf , C2SCreateShardInstant > PACKET_CODEC = PacketCodec .tuple (Identifier .PACKET_CODEC , C2SCreateShardInstant ::shardId , Shard .PACKET_CODEC , C2SCreateShardInstant ::shard , C2SCreateShardInstant ::new );
2321
2422 public static void receive (C2SCreateShardInstant payload , ServerPlayNetworking .Context context ) {
25- // TODO: This is mostly identical to C2SModifyShard, no need for so much duplicate code
23+ boolean success = C2SModifyShard . modify ( context . player (), payload . shardId , payload . shard );
2624
27- MinecraftServer server = context . player (). getServer ();
28- server . execute (() -> {
29- boolean success = server . isSingleplayer () || Permissions . check ( context . player (), ScatteredShardsAPI . MODIFY_SHARD_PERMISSION , 1 );
25+ if (! success ) {
26+ return ;
27+ }
3028
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 ));
29+ ShardLibrary library = ScatteredShardsAPI .getServerLibrary ();
3330
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- });
31+ ItemStack itemStack = ShardBlock .createShardBlock (library , payload .shardId (), false , 0.5f , 0.5f );
32+ context .player ().getInventory ().offerOrDrop (itemStack );
5533 }
5634
5735 @ Override
0 commit comments