1
0
Fork 0
mirror of https://github.com/TheClashFruit/CreatePissAndShit.git synced 2024-09-19 17:36:47 +00:00

feat: sync piss packet

This commit is contained in:
TheClashFruit 2024-07-28 20:57:43 +02:00
parent 009eb0aba0
commit cf636aca69
Signed by: TheClashFruit
GPG key ID: 09BB24C34C2F3204
7 changed files with 95 additions and 5 deletions

View file

@ -4,6 +4,7 @@ import com.simibubi.create.Create;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.Toml4jConfigSerializer;
import me.theclashfruit.pissnshit.config.MainConfig;
import me.theclashfruit.pissnshit.network.PissSyncPacket;
import me.theclashfruit.pissnshit.registry.Blocks;
import me.theclashfruit.pissnshit.registry.Fluids;
import me.theclashfruit.pissnshit.registry.Items;
@ -60,5 +61,8 @@ public class PissAndShit implements ModInitializer {
);
}
});
// Register Packets
PissSyncPacket.register();
}
}

View file

@ -1,9 +1,13 @@
package me.theclashfruit.pissnshit.client;
import me.theclashfruit.pissnshit.client.gui.ProofOfConceptHudOverlay;
import me.theclashfruit.pissnshit.network.PissSyncPacket;
import me.theclashfruit.pissnshit.registry.Fluids;
import me.theclashfruit.pissnshit.util.PissManager;
import me.theclashfruit.pissnshit.util.PlayerEntityUtil;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry;
import net.fabricmc.fabric.api.client.render.fluid.v1.SimpleFluidRenderHandler;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
@ -11,6 +15,8 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.util.Identifier;
import static me.theclashfruit.pissnshit.PissAndShit.LOGGER;
public class PissAndShitClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
@ -31,5 +37,17 @@ public class PissAndShitClient implements ClientModInitializer {
hudOverlay.render(matrices, 0, 0, tickDelta);
}
});
ClientPlayNetworking.registerGlobalReceiver(PissSyncPacket.ID, (c, handler, buf, responseSender) -> {
PissSyncPacket.SyncPacket packet = PissSyncPacket.SyncPacket.decode(buf);
c.execute(() -> {
if (c.player != null) {
PissManager pissManager = ((PlayerEntityUtil) c.player).getPissManager();
pissManager.setPissLevel(packet.pissLevel());
}
});
});
}
}

View file

@ -1,6 +1,7 @@
package me.theclashfruit.pissnshit.client.gui;
import com.mojang.blaze3d.systems.RenderSystem;
import me.theclashfruit.pissnshit.util.PlayerEntityUtil;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Drawable;
@ -30,7 +31,9 @@ public class ProofOfConceptHudOverlay implements Drawable {
RenderSystem.setShaderTexture(0, ICONS);
int pissLevel = this.randFirst;
assert this.client.player != null;
int pissLevel = ((PlayerEntityUtil) this.client.player).getPissManager().getPissLevel();
int fullPissIcons = pissLevel / 10;
int remainingPissLevel = pissLevel - (fullPissIcons * 10);

View file

@ -1,20 +1,18 @@
package me.theclashfruit.pissnshit.mixin;
import me.theclashfruit.pissnshit.util.PissManager;
import me.theclashfruit.pissnshit.util.PlayerEntityUtil;
import me.theclashfruit.pissnshit.util.ShitManager;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
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;
@Mixin(PlayerEntity.class)
public class PlayerEntityMixin {
public class PlayerEntityMixin implements PlayerEntityUtil {
@Unique
public PissManager pissManager = new PissManager();
@Unique
@ -30,6 +28,9 @@ public class PlayerEntityMixin {
}
}
// Lnet/minecraft/entity/player/PlayerEntity;eatFood(Lnet/minecraft/world/World;Lnet/minecraft/item/ItemStack;)Lnet/minecraft/item/ItemStack;
@Inject(
at = @At("TAIL"),
method = "writeCustomDataToNbt(Lnet/minecraft/nbt/NbtCompound;)V"
@ -45,4 +46,15 @@ public class PlayerEntityMixin {
public void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
pissManager.readNbt(nbt);
}
@Override
public PissManager getPissManager() {
return pissManager;
}
@Override
public ShitManager getShitManager() {
return shitManager;
}
}

View file

@ -0,0 +1,36 @@
package me.theclashfruit.pissnshit.network;
import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import static me.theclashfruit.pissnshit.PissAndShit.MOD_ID;
public class PissSyncPacket {
public static final Identifier ID = new Identifier(MOD_ID, "piss_sync");
public static void register() {}
public static void sendToClient(ServerPlayerEntity player, int pissLevel) {
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
new SyncPacket(pissLevel)
.encode(buf);
ServerPlayNetworking.send(player, ID, buf);
}
public record SyncPacket(int pissLevel) {
public static SyncPacket decode(PacketByteBuf buf) {
int pissLevel = buf.readInt();
return new SyncPacket(pissLevel);
}
public void encode(PacketByteBuf buf) {
buf.writeInt(pissLevel);
}
}
}

View file

@ -1,9 +1,11 @@
package me.theclashfruit.pissnshit.util;
import me.theclashfruit.pissnshit.network.PissSyncPacket;
import me.theclashfruit.pissnshit.registry.DamageTypes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.server.network.ServerPlayerEntity;
import static me.theclashfruit.pissnshit.PissAndShit.CONFIG;
@ -34,6 +36,9 @@ public class PissManager {
this.pissLevel += 1;
}
// Sync Piss Level
PissSyncPacket.sendToClient((ServerPlayerEntity) player, this.pissLevel);
this.lastPissTick = currentWorldTicks;
}
@ -67,4 +72,8 @@ public class PissManager {
public int getPissLevel() {
return this.pissLevel;
}
public void setPissLevel(int pissLevel) {
this.pissLevel = pissLevel;
}
}

View file

@ -0,0 +1,8 @@
package me.theclashfruit.pissnshit.util;
import org.spongepowered.asm.mixin.Unique;
public interface PlayerEntityUtil {
@Unique PissManager getPissManager();
@Unique ShitManager getShitManager();
}