Skip to content

Commit

Permalink
refactor networking stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
AViewFromTheTop committed Nov 16, 2023
1 parent 7834ac2 commit bca56f9
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 161 deletions.
9 changes: 0 additions & 9 deletions src/main/java/net/frozenblock/wilderwild/WilderWild.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,10 @@
import net.frozenblock.wilderwild.registry.RegisterWorldgen;
import net.frozenblock.wilderwild.registry.WilderRegistry;
import net.frozenblock.wilderwild.world.additions.gen.WilderWorldGen;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

public final class WilderWild extends FrozenModInitializer implements FrozenMobCategoryEntrypoint {

// PACKETS
public static final ResourceLocation SEED_PACKET = WilderSharedConstants.id("seed_particle_packet");
public static final ResourceLocation CONTROLLED_SEED_PACKET = WilderSharedConstants.id("controlled_seed_particle_packet");
public static final ResourceLocation FLOATING_SCULK_BUBBLE_PACKET = WilderSharedConstants.id("floating_sculk_bubble_easy_packet");
public static final ResourceLocation TERMITE_PARTICLE_PACKET = WilderSharedConstants.id("termite_particle_packet");
public static final ResourceLocation SENSOR_HICCUP_PACKET = WilderSharedConstants.id("sensor_hiccup_packet");
public static final ResourceLocation JELLY_STING_PACKET = WilderSharedConstants.id("jelly_sting_packet");

public WilderWild() {
super(WilderSharedConstants.MOD_ID);
}
Expand Down
123 changes: 2 additions & 121 deletions src/main/java/net/frozenblock/wilderwild/WilderWildClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@
package net.frozenblock.wilderwild;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.EntityModelLayerRegistry;
import net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener;
import net.frozenblock.lib.item.api.ItemBlockStateTagUtils;
import net.frozenblock.lib.math.api.AdvancedMath;
import net.frozenblock.lib.menu.api.Panoramas;
import net.frozenblock.lib.menu.api.SplashTextAPI;
import net.frozenblock.lib.sound.api.FlyBySoundHub;
Expand All @@ -52,14 +49,13 @@
import net.frozenblock.wilderwild.entity.render.renderer.TumbleweedRenderer;
import net.frozenblock.wilderwild.item.FireflyBottle;
import net.frozenblock.wilderwild.misc.WilderSharedConstants;
import net.frozenblock.wilderwild.networking.client.WilderClientNetworking;
import net.frozenblock.wilderwild.particle.FallingParticle;
import net.frozenblock.wilderwild.particle.FloatingSculkBubbleParticle;
import net.frozenblock.wilderwild.particle.MesogleaDripParticle;
import net.frozenblock.wilderwild.particle.PollenParticle;
import net.frozenblock.wilderwild.particle.SeedParticle;
import net.frozenblock.wilderwild.particle.TermiteParticle;
import net.frozenblock.wilderwild.particle.options.FloatingSculkBubbleParticleOptions;
import net.frozenblock.wilderwild.particle.options.SeedParticleOptions;
import net.frozenblock.wilderwild.registry.RegisterBlockEntities;
import net.frozenblock.wilderwild.registry.RegisterBlocks;
import net.frozenblock.wilderwild.registry.RegisterEntities;
Expand All @@ -68,22 +64,18 @@
import net.frozenblock.wilderwild.registry.RegisterProperties;
import net.frozenblock.wilderwild.registry.RegisterSounds;
import net.minecraft.client.model.geom.ModelLayerLocation;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.client.renderer.BiomeColors;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
import net.minecraft.client.renderer.entity.ThrownItemRenderer;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;

@Environment(EnvType.CLIENT)
Expand All @@ -99,112 +91,6 @@ public final class WilderWildClient implements ClientModInitializer {
public static final ModelLayerLocation TUMBLEWEED = new ModelLayerLocation(WilderSharedConstants.id("tumbleweed"), "main");
public static final ModelLayerLocation CRAB = new ModelLayerLocation(WilderSharedConstants.id("crab"), "main");

private static void receiveEasyEchoerBubblePacket() {
ClientPlayNetworking.registerGlobalReceiver(WilderWild.FLOATING_SCULK_BUBBLE_PACKET, (ctx, handler, byteBuf, responseSender) -> {
Vec3 pos = new Vec3(byteBuf.readDouble(), byteBuf.readDouble(), byteBuf.readDouble());
double size = byteBuf.readDouble();
int age = byteBuf.readInt();
double yVel = byteBuf.readDouble();
int count = byteBuf.readVarInt();
ctx.execute(() -> {
if (ctx.level == null)
throw new IllegalStateException("why is your world null");
var random = AdvancedMath.random();
for (int i = 0; i < count; i++) {
double xVel = (random.nextDouble() - 0.5) / 9.5;
double zVel = (random.nextDouble() - 0.5) / 9.5;
if (size >= 1) {
xVel = (random.nextDouble() - 0.5) / 10.5;
zVel = (random.nextDouble() - 0.5) / 10.5;
}
ctx.level.addParticle(new FloatingSculkBubbleParticleOptions(size, age, new Vec3(xVel, yVel, zVel)), pos.x, pos.y, pos.z, 0, 0, 0);
}
});
});
}

private static void receiveSeedPacket() {
ClientPlayNetworking.registerGlobalReceiver(WilderWild.SEED_PACKET, (ctx, handler, byteBuf, responseSender) -> {
Vec3 pos = new Vec3(byteBuf.readDouble(), byteBuf.readDouble(), byteBuf.readDouble());
int count = byteBuf.readVarInt();
boolean milkweed = byteBuf.readBoolean();
ctx.execute(() -> {
if (ctx.level == null)
throw new IllegalStateException("why is your world null");
for (int i = 0; i < count; i++) {
ctx.level.addParticle(new SeedParticleOptions(milkweed, false), pos.x, pos.y, pos.z, 0, 0, 0);
}
});
});
}

private static void receiveControlledSeedPacket() {
ClientPlayNetworking.registerGlobalReceiver(WilderWild.CONTROLLED_SEED_PACKET, (ctx, handler, byteBuf, responseSender) -> {
Vec3 pos = new Vec3(byteBuf.readDouble(), byteBuf.readDouble(), byteBuf.readDouble());
double velx = byteBuf.readDouble();
double vely = byteBuf.readDouble();
double velz = byteBuf.readDouble();
int count = byteBuf.readVarInt();
boolean milkweed = byteBuf.readBoolean();
double posRandomizer = byteBuf.readDouble();
ctx.execute(() -> {
if (ctx.level == null)
throw new IllegalStateException("why is your world null");
for (int i = 0; i < count; i++) {
ctx.level.addParticle(new SeedParticleOptions(milkweed, true), pos.x, pos.y + ((ctx.level.random.nextBoolean() ? -1 : 1) * (ctx.level.random.nextDouble() * posRandomizer)), pos.z, velx, vely + (ctx.level.random.nextDouble() * 0.07), velz);
}
});
});
}

private static void receiveTermitePacket() {
ClientPlayNetworking.registerGlobalReceiver(WilderWild.TERMITE_PARTICLE_PACKET, (ctx, handler, byteBuf, responseSender) -> {
Vec3 pos = new Vec3(byteBuf.readDouble(), byteBuf.readDouble(), byteBuf.readDouble());
AtomicInteger count = new AtomicInteger(byteBuf.readVarInt());
ctx.execute(() -> {
if (ctx.level == null)
throw new IllegalStateException("why is your world null");

count.addAndGet(-1);
ctx.level.addAlwaysVisibleParticle(RegisterParticles.TERMITE, pos.x, pos.y, pos.z, 0, 0, 0);
for (int i = 0; i < count.get(); i++) {
ctx.level.addParticle(RegisterParticles.TERMITE, pos.x, pos.y, pos.z, 0, 0, 0);
}
});
});
}

private static void receiveSensorHiccupPacket() {
ClientPlayNetworking.registerGlobalReceiver(WilderWild.SENSOR_HICCUP_PACKET, (ctx, handler, byteBuf, responseSender) -> {
Vec3 pos = new Vec3(byteBuf.readDouble(), byteBuf.readDouble(), byteBuf.readDouble());
ctx.execute(() -> {
if (ctx.level == null)
throw new IllegalStateException("why is your world null");
ClientLevel level = ctx.level;
int i = 5578058;
boolean bl2 = level.random.nextBoolean();
if (bl2) {
double d = (double) (i >> 16 & 255) / 255.0D;
double e = (double) (i >> 8 & 255) / 255.0D;
double f = (double) (i & 255) / 255.0D;
level.addParticle(ParticleTypes.ENTITY_EFFECT, pos.x, pos.y, pos.z, d, e, f);
}
});
});
}

private static void receiveJellyStingPacket() {
ClientPlayNetworking.registerGlobalReceiver(WilderWild.JELLY_STING_PACKET, (ctx, handler, byteBuf, responseSender) -> ctx.execute(() -> {
boolean baby = byteBuf.readBoolean();
if (ctx.level != null) {
LocalPlayer player = ctx.player;
if (player != null) {
ctx.level.playSound(player, player.getX(), player.getY(), player.getZ(), RegisterSounds.ENTITY_JELLYFISH_STING, SoundSource.NEUTRAL, 1.0F, ctx.level.random.nextFloat() * 0.2F + (baby ? 1.2F : 0.9F));
}
}
}));
}

@Override
public void onInitializeClient() {
SplashTextAPI.addSplashLocation(WilderSharedConstants.id("texts/splashes.txt"));
Expand Down Expand Up @@ -359,12 +245,7 @@ public void onInitializeClient() {
EntityModelLayerRegistry.registerModelLayer(DOUBLE_STONE_CHEST_LEFT, StoneChestBlockEntityRenderer::createDoubleBodyLeftLayer);
EntityModelLayerRegistry.registerModelLayer(DOUBLE_STONE_CHEST_RIGHT, StoneChestBlockEntityRenderer::createDoubleBodyRightLayer);

receiveEasyEchoerBubblePacket();
receiveSeedPacket();
receiveControlledSeedPacket();
receiveTermitePacket();
receiveSensorHiccupPacket();
receiveJellyStingPacket();
WilderClientNetworking.registerPacketReceivers();

FlyBySoundHub.AUTO_ENTITIES_AND_SOUNDS.put(RegisterEntities.ANCIENT_HORN_PROJECTILE_ENTITY, new FlyBySoundHub.FlyBySound(1.0F, 0.5F, SoundSource.PLAYERS, RegisterSounds.ENTITY_ANCIENT_HORN_PROJECTILE_FLYBY));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package net.frozenblock.wilderwild.block;

import net.frozenblock.wilderwild.misc.server.EasyPacket;
import net.frozenblock.wilderwild.networking.WilderNetworking;
import net.frozenblock.wilderwild.registry.RegisterItems;
import net.frozenblock.wilderwild.registry.RegisterSounds;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -85,7 +85,7 @@ public InteractionResult use(@NotNull BlockState state, @NotNull Level level, @N
shear(level, pos, state, player);
} else {
level.playSound(null, player.getX(), player.getY(), player.getZ(), RegisterSounds.BLOCK_MILKWEED_RUSTLE, SoundSource.BLOCKS, 0.8F, 0.9F + (level.random.nextFloat() * 0.15F));
EasyPacket.EasySeedPacket.createParticle(level, Vec3.atCenterOf(pos).add(0, 0.3, 0), level.random.nextIntBetweenInclusive(14, 28), true);
WilderNetworking.EasySeedPacket.createParticle(level, Vec3.atCenterOf(pos).add(0, 0.3, 0), level.random.nextIntBetweenInclusive(14, 28), true);
setAgeOnBothHalves(this, state, level, pos, 0);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package net.frozenblock.wilderwild.block;

import net.frozenblock.wilderwild.misc.server.EasyPacket;
import net.frozenblock.wilderwild.networking.WilderNetworking;
import net.frozenblock.wilderwild.particle.options.SeedParticleOptions;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -49,7 +49,7 @@ public void animateTick(@NotNull BlockState state, @NotNull Level level, @NotNul
public void entityInside(@NotNull BlockState state, @NotNull Level level, @NotNull BlockPos pos, @NotNull Entity entity) {
if (level instanceof ServerLevel server) {
if (server.random.nextFloat() > 0.95) {
EasyPacket.EasySeedPacket.createParticle(level, Vec3.atCenterOf(pos).add(0, 0.3, 0), server.random.nextIntBetweenInclusive(1, 3), false);
WilderNetworking.EasySeedPacket.createParticle(level, Vec3.atCenterOf(pos).add(0, 0.3, 0), server.random.nextIntBetweenInclusive(1, 3), false);
}
}
}
Expand All @@ -58,7 +58,7 @@ public void entityInside(@NotNull BlockState state, @NotNull Level level, @NotNu
public void playerWillDestroy(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState state, @NotNull Player player) {
super.playerWillDestroy(level, pos, state, player);
if (level instanceof ServerLevel server) {
EasyPacket.EasySeedPacket.createParticle(level, Vec3.atCenterOf(pos).add(0, 0.3, 0), server.random.nextIntBetweenInclusive(3, 7), false);
WilderNetworking.EasySeedPacket.createParticle(level, Vec3.atCenterOf(pos).add(0, 0.3, 0), server.random.nextIntBetweenInclusive(3, 7), false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import static net.frozenblock.wilderwild.item.AncientHorn.*;
import net.frozenblock.wilderwild.misc.WilderSharedConstants;
import net.frozenblock.wilderwild.misc.mod_compat.WilderModIntegrations;
import net.frozenblock.wilderwild.misc.server.EasyPacket;
import net.frozenblock.wilderwild.networking.WilderNetworking;
import net.frozenblock.wilderwild.registry.RegisterBlocks;
import net.frozenblock.wilderwild.registry.RegisterDamageTypes;
import net.frozenblock.wilderwild.registry.RegisterEntities;
Expand Down Expand Up @@ -167,7 +167,7 @@ public void tick() {
this.shakeTime = 0;
if (this.bubbles > 0 && this.level() instanceof ServerLevel server) {
--this.bubbles;
EasyPacket.EasyFloatingSculkBubblePacket.createParticle(server, this.position(), server.random.nextDouble() > 0.7 ? 1 : 0, 20 + server.random.nextInt(40), 0.05, server.random.nextIntBetweenInclusive(1, 3));
WilderNetworking.EasyFloatingSculkBubblePacket.createParticle(server, this.position(), server.random.nextDouble() > 0.7 ? 1 : 0, 20 + server.random.nextInt(40), 0.05, server.random.nextIntBetweenInclusive(1, 3));
}
if (this.aliveTicks > ItemConfig.get().ancientHorn.ancientHornLifespan) {
this.remove(RemovalReason.DISCARDED);
Expand All @@ -192,7 +192,7 @@ public void tick() {
Vec3 deltaPosition;

if (this.isInWater() && level() instanceof ServerLevel server) {
EasyPacket.EasyFloatingSculkBubblePacket.createParticle(server, new Vec3(this.xo, this.yo, this.zo), 0, 60, 0.05, 4);
WilderNetworking.EasyFloatingSculkBubblePacket.createParticle(server, new Vec3(this.xo, this.yo, this.zo), 0, 60, 0.05, 4);
}
if (this.isInWaterOrRain() || blockState.is(Blocks.POWDER_SNOW)) {
this.clearFire();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import net.frozenblock.wilderwild.entity.ai.jellyfish.JellyfishAi;
import net.frozenblock.wilderwild.entity.ai.jellyfish.JellyfishTemptGoal;
import net.frozenblock.wilderwild.entity.variant.JellyfishVariant;
import net.frozenblock.wilderwild.misc.server.EasyPacket;
import net.frozenblock.wilderwild.networking.WilderNetworking;
import net.frozenblock.wilderwild.registry.RegisterEntities;
import net.frozenblock.wilderwild.registry.RegisterItems;
import net.frozenblock.wilderwild.registry.RegisterSounds;
Expand Down Expand Up @@ -439,7 +439,7 @@ public void stingEntities() {
if (entity instanceof ServerPlayer player) {
if (player.hurt(this.damageSources().mobAttack(this), damage)) {
player.addEffect(new MobEffectInstance(MobEffects.POISON, poisonDuration, 0, false, false), this);
EasyPacket.sendJellySting(player, baby);
WilderNetworking.sendJellySting(player, baby);
}
} else if (entity instanceof Mob mob) {
if (mob.hurt(this.damageSources().mobAttack(this), damage)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import net.frozenblock.wilderwild.block.HollowedLogBlock;
import net.frozenblock.wilderwild.config.BlockConfig;
import net.frozenblock.wilderwild.misc.WilderSharedConstants;
import net.frozenblock.wilderwild.misc.server.EasyPacket;
import net.frozenblock.wilderwild.networking.WilderNetworking;
import net.frozenblock.wilderwild.registry.RegisterBlocks;
import net.frozenblock.wilderwild.registry.RegisterProperties;
import net.frozenblock.wilderwild.registry.RegisterSounds;
Expand Down Expand Up @@ -114,7 +114,7 @@ public void tick(@NotNull Level level, @NotNull BlockPos pos, boolean natural, b
ArrayList<Termite> termitesToRemove = new ArrayList<>();
for (Termite termite : this.termites) {
if (termite.tick(level, natural)) {
EasyPacket.EasyTermitePacket.createParticle(level, Vec3.atCenterOf(termite.pos), termite.eating ? 4 : 6);
WilderNetworking.EasyTermitePacket.createParticle(level, Vec3.atCenterOf(termite.pos), termite.eating ? 4 : 6);
} else {
level.playSound(null, termite.pos, RegisterSounds.BLOCK_TERMITE_MOUND_ENTER, SoundSource.NEUTRAL, 0.6F, 1.0F);
termitesToRemove.add(termite);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package net.frozenblock.wilderwild.item;

import net.frozenblock.wilderwild.misc.server.EasyPacket;
import net.frozenblock.wilderwild.networking.WilderNetworking;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -50,7 +50,7 @@ public InteractionResultHolder<ItemStack> use(@NotNull Level level, @NotNull Pla
float f = -Mth.sin(yaw * 0.017453292F) * Mth.cos(pitch * 0.017453292F);
float g = -Mth.sin((pitch + roll) * 0.017453292F);
float h = Mth.cos(yaw * 0.017453292F) * Mth.cos(pitch * 0.017453292F);
EasyPacket.EasySeedPacket.createControlledParticle(level, user.getEyePosition().add(0, -0.1, 0), f, g, h, server.random.nextIntBetweenInclusive(5, 20), true, 48, 0.2);
WilderNetworking.EasySeedPacket.createControlledParticle(level, user.getEyePosition().add(0, -0.1, 0), f, g, h, server.random.nextIntBetweenInclusive(5, 20), true, 48, 0.2);
}

return InteractionResultHolder.consume(itemStack);
Expand Down
Loading

0 comments on commit bca56f9

Please sign in to comment.