|
| 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 | +} |
0 commit comments