Skip to content

Commit fecdd84

Browse files
committed
Clean up code
1 parent 5bfce5e commit fecdd84

File tree

2 files changed

+24
-38
lines changed

2 files changed

+24
-38
lines changed

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

+9-31
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package net.modfest.scatteredshards.networking;
22

3-
import me.lucko.fabric.api.permissions.v0.Permissions;
43
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
4+
import net.minecraft.item.ItemStack;
55
import net.minecraft.network.RegistryByteBuf;
66
import net.minecraft.network.codec.PacketCodec;
77
import net.minecraft.network.packet.CustomPayload;
8-
import net.minecraft.server.MinecraftServer;
9-
import net.minecraft.server.network.ServerPlayerEntity;
108
import net.minecraft.util.Identifier;
119
import net.modfest.scatteredshards.ScatteredShards;
1210
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
13-
import net.modfest.scatteredshards.api.impl.ShardLibraryPersistentState;
11+
import net.modfest.scatteredshards.api.ShardLibrary;
1412
import net.modfest.scatteredshards.api.shard.Shard;
1513
import 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

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,38 @@ public record C2SModifyShard(Identifier shardId, Shard shard) implements CustomP
2121
public static final PacketCodec<RegistryByteBuf, C2SModifyShard> PACKET_CODEC = PacketCodec.tuple(Identifier.PACKET_CODEC, C2SModifyShard::shardId, Shard.PACKET_CODEC, C2SModifyShard::shard, C2SModifyShard::new);
2222

2323
public static void receive(C2SModifyShard payload, ServerPlayNetworking.Context context) {
24-
MinecraftServer server = context.player().getServer();
25-
server.execute(() -> {
26-
boolean success = server.isSingleplayer() || Permissions.check(context.player(), ScatteredShardsAPI.MODIFY_SHARD_PERMISSION, 1);
24+
modify(context.player(), payload.shardId, payload.shard);
25+
}
26+
27+
public static boolean modify(ServerPlayerEntity player, Identifier shardId, Shard shard) {
28+
MinecraftServer server = player.getServer();
29+
assert server != null;
2730

31+
boolean success = server.isSingleplayer() || Permissions.check(player, ScatteredShardsAPI.MODIFY_SHARD_PERMISSION, 1);
32+
33+
server.execute(() -> {
2834
//Let the sender know of success or failure before a shard update comes through
29-
ServerPlayNetworking.send(context.player(), new S2CModifyShardResult(payload.shardId(), success));
35+
ServerPlayNetworking.send(player, new S2CModifyShardResult(shardId, success));
3036

3137
if (!success) {
3238
return;
3339
}
3440

3541
//Update our serverside library
36-
ScatteredShardsAPI.getServerLibrary().shards().put(payload.shardId(), payload.shard());
37-
ScatteredShardsAPI.getServerLibrary().shardSets().put(payload.shard().sourceId(), payload.shardId());
42+
ScatteredShardsAPI.getServerLibrary().shards().put(shardId, shard);
43+
ScatteredShardsAPI.getServerLibrary().shardSets().put(shard.sourceId(), shardId);
3844

3945
//Make sure the NBT gets written on next world-save
4046
ShardLibraryPersistentState.get(server).markDirty();
4147

4248
//Update everyone's client libraries with the new shard
43-
S2CSyncShard syncShard = new S2CSyncShard(payload.shardId(), payload.shard());
49+
S2CSyncShard syncShard = new S2CSyncShard(shardId, shard);
4450
for (ServerPlayerEntity otherPlayer : server.getPlayerManager().getPlayerList()) {
4551
ServerPlayNetworking.send(otherPlayer, syncShard);
4652
}
4753
});
54+
55+
return success;
4856
}
4957

5058
@Override

0 commit comments

Comments
 (0)