Update major API Version to add some new interfaces.
This commit is contained in:
parent
1378a6fb21
commit
5d024f425c
26 changed files with 496 additions and 111 deletions
|
@ -1,9 +1,12 @@
|
|||
package dev.zontreck.libzontreck.networking;
|
||||
|
||||
import dev.zontreck.libzontreck.LibZontreck;
|
||||
import dev.zontreck.libzontreck.events.RegisterPacketsEvent;
|
||||
import dev.zontreck.libzontreck.networking.packets.ChestGUIOpenC2S;
|
||||
import dev.zontreck.libzontreck.networking.packets.IPacket;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.network.NetworkDirection;
|
||||
import net.minecraftforge.network.NetworkRegistry;
|
||||
import net.minecraftforge.network.PacketDistributor;
|
||||
|
@ -15,7 +18,11 @@ import net.minecraftforge.network.simple.SimpleChannel;
|
|||
public class ModMessages {
|
||||
private static SimpleChannel INSTANCE;
|
||||
private static int PACKET_ID=0;
|
||||
private static int id()
|
||||
/**
|
||||
* INTERNAL USE ONLY. DO NOT USE THIS, IF YOU ARE NOT LIBZONTRECK!!!!!!
|
||||
* @return
|
||||
*/
|
||||
public static int id()
|
||||
{
|
||||
return PACKET_ID++;
|
||||
}
|
||||
|
@ -26,9 +33,17 @@ public class ModMessages {
|
|||
.clientAcceptedVersions(s->true)
|
||||
.serverAcceptedVersions(s->true)
|
||||
.simpleChannel();
|
||||
|
||||
RegisterPacketsEvent event = new RegisterPacketsEvent();
|
||||
MinecraftForge.EVENT_BUS.post(event);
|
||||
|
||||
INSTANCE=net;
|
||||
|
||||
for(IPacket packet : event.packets)
|
||||
{
|
||||
packet.register(net);
|
||||
}
|
||||
|
||||
net.messageBuilder(ChestGUIOpenC2S.class, id(), NetworkDirection.PLAY_TO_SERVER)
|
||||
.decoder(ChestGUIOpenC2S::new)
|
||||
.encoder(ChestGUIOpenC2S::toBytes)
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package dev.zontreck.libzontreck.networking.packets;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraftforge.network.NetworkDirection;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import net.minecraftforge.network.simple.SimpleChannel;
|
||||
|
||||
public interface IPacket {
|
||||
void deserialize(CompoundTag data);
|
||||
|
||||
void serialize(CompoundTag data);
|
||||
|
||||
void toBytes(FriendlyByteBuf buf);
|
||||
|
||||
boolean handle(Supplier<NetworkEvent.Context> supplier);
|
||||
|
||||
/**
|
||||
* @return The network direction of the packet
|
||||
*/
|
||||
NetworkDirection getDirection();
|
||||
|
||||
/**
|
||||
* EXAMPLE:
|
||||
* chan.messageBuilder(S2CPlaySoundPacket.class, ModMessages.id(), getDirection())
|
||||
* .encoder(S2CPlaySoundPacket::toBytes)
|
||||
* .decoder(S2CPlaySoundPacket::new)
|
||||
* .consumer(S2CPlaySoundPacket::handle)
|
||||
* .add();
|
||||
* @param chan
|
||||
*/
|
||||
void register(SimpleChannel chan);
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package dev.zontreck.libzontreck.networking.packets;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import dev.zontreck.libzontreck.networking.ModMessages;
|
||||
import dev.zontreck.libzontreck.util.BinUtil;
|
||||
import dev.zontreck.libzontreck.util.ServerUtilities;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraftforge.network.NetworkDirection;
|
||||
import net.minecraftforge.network.NetworkEvent;
|
||||
import net.minecraftforge.network.NetworkEvent.Context;
|
||||
import net.minecraftforge.network.simple.SimpleChannel;
|
||||
|
||||
public class S2CPlaySoundPacket implements IPacket
|
||||
{
|
||||
public ResourceLocation sound;
|
||||
|
||||
public S2CPlaySoundPacket(FriendlyByteBuf buf) {
|
||||
sound = buf.readResourceLocation();
|
||||
}
|
||||
|
||||
public S2CPlaySoundPacket(ResourceLocation loc) {
|
||||
sound=loc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserialize(CompoundTag data) {
|
||||
// Deserializes the play sound packet
|
||||
throw new UnsupportedOperationException("This operation is not supported in the play sound packet!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(CompoundTag data) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new UnsupportedOperationException("Unimplemented method 'serialize'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(FriendlyByteBuf buf) {
|
||||
buf.writeResourceLocation(sound);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handle(Supplier<Context> supplier) {
|
||||
NetworkEvent.Context ctx=supplier.get();
|
||||
|
||||
ctx.enqueueWork(()->{
|
||||
// We are on the client now, enqueue the sound!
|
||||
SoundEvent ev = new SoundEvent(sound);
|
||||
// Play sound for player!
|
||||
Minecraft.getInstance().player.playSound(ev, 1, BinUtil.getARandomInstance().nextFloat(0, 1));
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkDirection getDirection() {
|
||||
return NetworkDirection.PLAY_TO_CLIENT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void register(SimpleChannel chan) {
|
||||
ServerUtilities.registerPacket(chan, S2CPlaySoundPacket.class, this, S2CPlaySoundPacket::new);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue