Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(network): ClientboundDrawParticleShapePacket
- Loading branch information
1 parent
9700875
commit 6a9c5de
Showing
3 changed files
with
76 additions
and
3 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
12 changes: 12 additions & 0 deletions
12
src/main/java/org/auioc/mcmod/arnicalib/common/network/AHPackets.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,12 @@ | ||
package org.auioc.mcmod.arnicalib.common.network; | ||
|
||
import static org.auioc.mcmod.arnicalib.common.network.AHPacketHandler.HANDLER; | ||
import org.auioc.mcmod.arnicalib.common.network.packet.client.ClientboundDrawParticleShapePacket; | ||
|
||
public final class AHPackets { | ||
|
||
protected static void register() { | ||
HANDLER.registerServerToClient(ClientboundDrawParticleShapePacket.class, ClientboundDrawParticleShapePacket::decode); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...uioc/mcmod/arnicalib/common/network/packet/client/ClientboundDrawParticleShapePacket.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,59 @@ | ||
package org.auioc.mcmod.arnicalib.common.network.packet.client; | ||
|
||
import org.auioc.mcmod.arnicalib.api.game.network.IHPacket; | ||
import org.auioc.mcmod.arnicalib.api.game.phys.Shape; | ||
import org.auioc.mcmod.arnicalib.utils.game.ParticlePainter; | ||
import org.auioc.mcmod.arnicalib.utils.game.ParticlePainter.Options; | ||
import net.minecraft.core.particles.ParticleOptions; | ||
import net.minecraft.core.particles.ParticleType; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraftforge.api.distmarker.Dist; | ||
import net.minecraftforge.api.distmarker.OnlyIn; | ||
import net.minecraftforge.network.NetworkEvent.Context; | ||
import net.minecraftforge.registries.ForgeRegistries; | ||
|
||
public class ClientboundDrawParticleShapePacket implements IHPacket { | ||
|
||
private final Shape shape; | ||
private final CompoundTag data; | ||
private final Options options; | ||
|
||
public ClientboundDrawParticleShapePacket(Shape shape, CompoundTag data, Options options) { | ||
this.shape = shape; | ||
this.data = data; | ||
this.options = options; | ||
} | ||
|
||
@Override | ||
@OnlyIn(Dist.CLIENT) | ||
public void handle(Context ctx) { | ||
ParticlePainter.Client.draw(this.shape, this.data, this.options); | ||
} | ||
|
||
@Override | ||
public void encode(FriendlyByteBuf buffer) { | ||
buffer.writeEnum(this.shape); | ||
buffer.writeNbt(this.data); | ||
buffer.writeDouble(this.options.stepLength()); | ||
buffer.writeResourceLocation(this.options.particle().getType().getRegistryName()); | ||
this.options.particle().writeToNetwork(buffer); | ||
buffer.writeBoolean(this.options.force()); | ||
} | ||
|
||
public static ClientboundDrawParticleShapePacket decode(FriendlyByteBuf buffer) { | ||
var shape = buffer.readEnum(Shape.class); | ||
var data = buffer.readNbt(); | ||
var stepLength = buffer.readDouble(); | ||
var _particleType = ForgeRegistries.PARTICLE_TYPES.getValue(buffer.readResourceLocation()); | ||
var particle = readParticle(buffer, _particleType); | ||
var force = buffer.readBoolean(); | ||
return new ClientboundDrawParticleShapePacket(shape, data, new Options(stepLength, particle, force)); | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
private static <T extends ParticleOptions> T readParticle(FriendlyByteBuf buffer, ParticleType<T> particleType) { | ||
return particleType.getDeserializer().fromNetwork(particleType, buffer); | ||
} | ||
|
||
} |