-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Blueprint player data and client configuration
- Loading branch information
Showing
11 changed files
with
235 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 13 additions & 2 deletions
15
src/client/kotlin/net/mcbrawls/blueprint/BlueprintClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
package net.mcbrawls.blueprint | ||
|
||
import net.fabricmc.api.ClientModInitializer | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents | ||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking | ||
import net.mcbrawls.blueprint.network.BlueprintConfigC2SPacket | ||
|
||
object BlueprintClient : ClientModInitializer { | ||
override fun onInitializeClient() { | ||
} | ||
override fun onInitializeClient() { | ||
ClientPlayConnectionEvents.JOIN.register { handler, _, _ -> | ||
// send initial config packet to server | ||
val packet = BlueprintConfigC2SPacket( | ||
renderParticles = false | ||
) | ||
|
||
handler.sendPacket(ServerPlayNetworking.createS2CPacket(packet)) | ||
} | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
src/main/java/net/mcbrawls/blueprint/mixin/ExampleMixin.java
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
src/main/java/net/mcbrawls/blueprint/mixin/ServerPlayerEntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package net.mcbrawls.blueprint.mixin; | ||
|
||
import dev.andante.codex.CodexKt; | ||
import net.mcbrawls.blueprint.BlueprintMod; | ||
import net.mcbrawls.blueprint.player.BlueprintPlayerAccessor; | ||
import net.mcbrawls.blueprint.player.BlueprintPlayerData; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.nbt.NbtElement; | ||
import net.minecraft.nbt.NbtOps; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
/** | ||
* A mixin to manage blueprint player data on the server player. | ||
*/ | ||
@Mixin(ServerPlayerEntity.class) | ||
public class ServerPlayerEntityMixin implements BlueprintPlayerAccessor { | ||
@Unique | ||
private static final String BLUEPRINT_KEY = BlueprintMod.MOD_ID; | ||
|
||
@Unique | ||
@Nullable | ||
private BlueprintPlayerData blueprintPlayerData = null; | ||
|
||
@Inject(method = "copyFrom", at = @At("TAIL")) | ||
private void copyBlueprintData(ServerPlayerEntity oldPlayer, boolean alive, CallbackInfo ci) { | ||
// copy old blueprint data to new player | ||
this.blueprintPlayerData = BlueprintPlayerData.Companion.getBlueprintData(oldPlayer); | ||
} | ||
|
||
@Inject(method = "writeCustomDataToNbt", at = @At("TAIL")) | ||
private void writeBlueprintNbt(NbtCompound nbt, CallbackInfo info) { | ||
if (this.blueprintPlayerData != null) { | ||
// write blueprint data to nbt | ||
NbtElement blueprintNbt = CodexKt.encodeQuick(BlueprintPlayerData.Companion.getCODEC(), NbtOps.INSTANCE, this.blueprintPlayerData); | ||
nbt.put(BLUEPRINT_KEY, blueprintNbt); | ||
} | ||
} | ||
|
||
@Inject(method = "readCustomDataFromNbt", at = @At("TAIL")) | ||
private void readBlueprintNbt(NbtCompound nbt, CallbackInfo info) { | ||
// read blueprint data from nbt | ||
NbtElement blueprintNbt = nbt.getCompound(BLUEPRINT_KEY); | ||
this.blueprintPlayerData = CodexKt.decodeQuick(BlueprintPlayerData.Companion.getCODEC(), NbtOps.INSTANCE, blueprintNbt); | ||
} | ||
|
||
@Unique | ||
@Override | ||
public @Nullable BlueprintPlayerData getBlueprintPlayerData() { | ||
return this.blueprintPlayerData; | ||
} | ||
|
||
@Unique | ||
@Override | ||
public void setBlueprintPlayerData(@Nullable BlueprintPlayerData data) { | ||
this.blueprintPlayerData = data; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/net/mcbrawls/blueprint/player/BlueprintPlayerAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package net.mcbrawls.blueprint.player; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
public interface BlueprintPlayerAccessor { | ||
default @Nullable BlueprintPlayerData getBlueprintPlayerData() { | ||
throw new AssertionError(); | ||
} | ||
|
||
default void setBlueprintPlayerData(@Nullable BlueprintPlayerData data) { | ||
throw new AssertionError(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/net/mcbrawls/blueprint/command/BlueprintCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net.mcbrawls.blueprint.command | ||
|
||
import com.mojang.brigadier.CommandDispatcher | ||
import net.minecraft.server.command.CommandManager.RegistrationEnvironment | ||
import net.minecraft.server.command.CommandManager.literal | ||
import net.minecraft.server.command.ServerCommandSource | ||
|
||
object BlueprintCommand { | ||
fun register(dispatcher: CommandDispatcher<ServerCommandSource>, environment: RegistrationEnvironment) { | ||
val builder = literal("blueprint") | ||
|
||
// | ||
|
||
dispatcher.register(builder) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/net/mcbrawls/blueprint/network/BlueprintConfigC2SPacket.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package net.mcbrawls.blueprint.network | ||
|
||
import net.fabricmc.fabric.api.networking.v1.FabricPacket | ||
import net.fabricmc.fabric.api.networking.v1.PacketType | ||
import net.mcbrawls.blueprint.BlueprintMod | ||
import net.mcbrawls.blueprint.player.BlueprintPlayerData | ||
import net.minecraft.network.PacketByteBuf | ||
import net.minecraft.util.Identifier | ||
|
||
/** | ||
* A packet sent by the client to modify its blueprint configuration. | ||
*/ | ||
data class BlueprintConfigC2SPacket( | ||
/** | ||
* Whether the server should render blueprint particles. | ||
*/ | ||
val renderParticles: Boolean, | ||
) : FabricPacket { | ||
constructor(buf: PacketByteBuf) : this( | ||
buf.readBoolean() | ||
) | ||
|
||
override fun write(buf: PacketByteBuf) { | ||
buf.writeBoolean(renderParticles) | ||
} | ||
|
||
override fun getType(): PacketType<*> { | ||
return TYPE | ||
} | ||
|
||
/** | ||
* Creates blueprint player data from this packet. | ||
*/ | ||
fun createBlueprintPlayerData(): BlueprintPlayerData { | ||
return BlueprintPlayerData(renderParticles) | ||
} | ||
|
||
companion object { | ||
/** | ||
* The type of the config packet. | ||
*/ | ||
val TYPE: PacketType<BlueprintConfigC2SPacket> = PacketType.create( | ||
Identifier(BlueprintMod.MOD_ID, "config"), | ||
::BlueprintConfigC2SPacket | ||
) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/net/mcbrawls/blueprint/player/BlueprintPlayerData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package net.mcbrawls.blueprint.player | ||
|
||
import com.mojang.serialization.Codec | ||
import com.mojang.serialization.codecs.RecordCodecBuilder | ||
import net.minecraft.server.network.ServerPlayerEntity | ||
|
||
/** | ||
* Blueprint data attached to the player entity. | ||
*/ | ||
data class BlueprintPlayerData( | ||
/** | ||
* Whether the server should render blueprint particles. | ||
*/ | ||
var renderParticles: Boolean, | ||
) { | ||
companion object { | ||
/** | ||
* The codec for blueprint player data. | ||
*/ | ||
val CODEC: Codec<BlueprintPlayerData> = RecordCodecBuilder.create { instance -> | ||
instance.group( | ||
Codec.BOOL | ||
.fieldOf("render_particles") | ||
.forGetter(BlueprintPlayerData::renderParticles) | ||
).apply(instance, ::BlueprintPlayerData) | ||
} | ||
|
||
/** | ||
* The blueprint player data for this player. | ||
*/ | ||
var ServerPlayerEntity.blueprintData: BlueprintPlayerData? | ||
get() { | ||
val that = this as BlueprintPlayerAccessor | ||
return that.blueprintPlayerData | ||
} | ||
|
||
set(value) { | ||
val that = this as BlueprintPlayerAccessor | ||
that.blueprintPlayerData = value | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters