Skip to content

Instantly share code, notes, and snippets.

@LegendaryReaper670
Last active April 11, 2023 23:42
Show Gist options
  • Save LegendaryReaper670/641ae8695016076752d6f96fd4baddff to your computer and use it in GitHub Desktop.
Save LegendaryReaper670/641ae8695016076752d6f96fd4baddff to your computer and use it in GitHub Desktop.
Packet code.java
@SubscribeEvent
public void onAddPotionEffect(MobEffectEvent.Added event) {
if (event.getEffectInstance().getEffect() == FableMobEffects.PETRIFIED.get()) {
if (!event.getEntity().level.isClientSide() && event.getEffectSource() instanceof LivingEntity entity) {
PacketsHandler.CHANNEL.send(PacketDistributor.TRACKING_ENTITY_AND_SELF.with(event::getEntity), new PetrifyPacket((LivingEntity) event.getEffectSource(), true));
IPetrified cap = CapabilityManager.getCapability(event.getEntity(), CapabilityManager.PETRIFIED_CAPABILITY);
if (cap != null) {
cap.onPetrified(event.getEntity());
}
}
}
}
public class PetrifyPacket extends BaseSimplePacket {
private int entityID;
private boolean isPetrified;
public PetrifyPacket(LivingEntity entity, boolean activate) {
entityID = entity.getId();
this.isPetrified = activate;
}
public PetrifyPacket(FriendlyByteBuf friendlyByteBuf) {
this.entityID = friendlyByteBuf.readInt();
this.isPetrified = friendlyByteBuf.readBoolean();
}
@Override
public void write(FriendlyByteBuf buffer) {
buffer.writeVarInt(entityID);
buffer.writeBoolean(isPetrified);
}
@Override
public void handle(Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() -> {
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
if (Minecraft.getInstance().level != null) {
Entity entity = Minecraft.getInstance().level.getEntity(entityID);
if (entity instanceof LivingEntity living) {
IPetrified livingCapability = CapabilityManager.getCapability(living, CapabilityManager.PETRIFIED_CAPABILITY);
if (livingCapability != null) {
if (isPetrified) livingCapability.onPetrified(living);
else livingCapability.onUnPetrified(living);
}
}
}
});
});
context.get().setPacketHandled(true);
}
public enum PacketsHandler {
//Server -> Client
MOUNT_RIDE(MountRidePacket.class, MountRidePacket::new, PLAY_TO_CLIENT),
PETRIFY_EFFECT(PetrifyPacket.class, PetrifyPacket::new, PLAY_TO_CLIENT),
SENT_MAIL(SentMailPacket.class, SentMailPacket::new, PLAY_TO_CLIENT),
SYNC_TRAVELER(TravelerPacket.class, TravelerPacket::new, PLAY_TO_CLIENT),
//Client -> Server
OPEN_CONTAINER(OpenMailboxContainerPacket.class, OpenMailboxContainerPacket::new, PLAY_TO_SERVER),
CLEAR_CONTAINER(ClearContainerPacket.class, ClearContainerPacket::new, PLAY_TO_SERVER),
EDIT_TRAVELLER(TravelerEditPacket.class, TravelerEditPacket::new, PLAY_TO_SERVER);
public static final ResourceLocation CHANNEL_NAME = Fable.asResource("main");
public static final int NETWORK_VERSION = 1;
public static final String NETWORK_VERSION_STR = String.valueOf(NETWORK_VERSION);
private static final List<LoadedPacket<?>> PACKETS = new ArrayList<>();
public static SimpleChannel CHANNEL;
private final LoadedPacket<?> packet;
<T extends BaseSimplePacket> PacketsHandler(Class<T> type, Function<FriendlyByteBuf, T> factory, NetworkDirection direction) {
packet = new PacketsHandler.LoadedPacket<>(type, factory, direction);
}
public static void sendToNear(Level world, BlockPos pos, int range, Object message) {
CHANNEL.send(
PacketDistributor.NEAR.with(PacketDistributor.TargetPoint.p(pos.getX(), pos.getY(), pos.getZ(), range, world.dimension())),
message);
}
public static void registerPackets() {
CHANNEL = NetworkRegistry.ChannelBuilder.named(CHANNEL_NAME)
.serverAcceptedVersions(NETWORK_VERSION_STR::equals)
.clientAcceptedVersions(NETWORK_VERSION_STR::equals)
.networkProtocolVersion(() -> NETWORK_VERSION_STR)
.simpleChannel();
// for (LoadedPacket<?> packet : PACKETS) {
// packet.register();
// }
for (PacketsHandler packet : values())
packet.packet.register();
}
private static class LoadedPacket<T extends BaseSimplePacket> {
private static int index = 0;
private final BiConsumer<T, FriendlyByteBuf> encoder;
private final Function<FriendlyByteBuf, T> decoder;
private final BiConsumer<T, Supplier<NetworkEvent.Context>> handler;
private final Class<T> type;
private final NetworkDirection direction;
private LoadedPacket(Class<T> type, Function<FriendlyByteBuf, T> factory, NetworkDirection direction) {
encoder = T::write;
decoder = factory;
handler = T::handle;
this.type = type;
this.direction = direction;
}
private void register() {
CHANNEL.messageBuilder(type, index++, direction).encoder(encoder).decoder(decoder).consumer(handler).add();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment