Skip to content

Commit

Permalink
basically finished it, all that is left to do is to test the thing
Browse files Browse the repository at this point in the history
  • Loading branch information
RaydanOMGr committed May 3, 2024
1 parent c2b63b3 commit c516add
Show file tree
Hide file tree
Showing 38 changed files with 319 additions and 243 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build_fabric.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle

name: Build GlowingEyes for fabric/1.19.2
name: Build GlowingEyes for fabric/1.19.4

on:
push:
branches: [ "fabric/1.19.2" ]
branches: [ "fabric/1.19.4" ]
pull_request:
branches: [ "fabric/1.19.2" ]
branches: [ "fabric/1.19.4" ]

jobs:
build:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ gradle-app.setting

# Common working directory
run/
run_server/

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar
8 changes: 4 additions & 4 deletions src/main/java/me/andreasmelone/glowingeyes/GlowingEyes.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package me.andreasmelone.glowingeyes;

import me.andreasmelone.glowingeyes.common.GlowingEyesEvents;
import me.andreasmelone.glowingeyes.common.packet.PacketManager;
import me.andreasmelone.glowingeyes.common.scheduler.CodeScheduler;
import me.andreasmelone.glowingeyes.common.scheduler.Scheduler;
import me.andreasmelone.glowingeyes.server.GlowingEyesEvents;
import me.andreasmelone.glowingeyes.server.packet.PacketManager;
import me.andreasmelone.glowingeyes.server.scheduler.CodeScheduler;
import me.andreasmelone.glowingeyes.server.scheduler.Scheduler;
import net.fabricmc.api.ModInitializer;

import java.awt.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import me.andreasmelone.glowingeyes.client.commands.EyesCommand;
import me.andreasmelone.glowingeyes.client.presets.PresetManager;
import me.andreasmelone.glowingeyes.client.render.RenderManager;
import me.andreasmelone.glowingeyes.common.scheduler.CodeScheduler;
import me.andreasmelone.glowingeyes.common.scheduler.Scheduler;
import me.andreasmelone.glowingeyes.client.packet.ClientPacketManager;
import me.andreasmelone.glowingeyes.server.scheduler.CodeScheduler;
import me.andreasmelone.glowingeyes.server.scheduler.Scheduler;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import org.slf4j.Logger;
Expand All @@ -20,6 +21,8 @@ public void onInitializeClient() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
EyesCommand.register(dispatcher);
});

ClientPacketManager.registerHandlers();
GlowingEyesKeybindings.register();
GlowingEyesClientEvents.registerEvents();
RenderManager.init();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package me.andreasmelone.glowingeyes.client;

import me.andreasmelone.glowingeyes.client.gui.EyesEditorScreen;
import me.andreasmelone.glowingeyes.common.component.data.PlayerDataComponent;
import me.andreasmelone.glowingeyes.common.component.eyes.GlowingEyesComponent;
import me.andreasmelone.glowingeyes.client.component.data.ClientPlayerDataComponent;
import me.andreasmelone.glowingeyes.client.util.DynamicTextureCache;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.minecraft.world.entity.player.Player;

public class GlowingEyesClientEvents {
public static void registerEvents() {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
GlowingEyesClient.CLIENT_SCHEDULER.tick();
});

ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> PlayerDataComponent.sendRequest());
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> ClientPlayerDataComponent.sendRequest());
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> DynamicTextureCache.clear());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mojang.blaze3d.platform.InputConstants;
import me.andreasmelone.glowingeyes.client.gui.EyesEditorScreen;
import me.andreasmelone.glowingeyes.common.component.eyes.GlowingEyesComponent;
import me.andreasmelone.glowingeyes.server.component.eyes.GlowingEyesComponent;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.KeyMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.andreasmelone.glowingeyes.client.component.data;

import me.andreasmelone.glowingeyes.client.packet.C2SHasModPacket;

public class ClientPlayerDataComponent {
/**
* Sends a packet to the server so it knows the player has the mod installed
* This is needed, so the server can send the player the glowing eyes data
*/
public static void sendRequest() {
C2SHasModPacket.sendToServer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.andreasmelone.glowingeyes.client.component.eyes;

import me.andreasmelone.glowingeyes.client.packet.C2SComponentUpdatePacket;
import me.andreasmelone.glowingeyes.server.component.eyes.GlowingEyesComponent;
import net.minecraft.client.Minecraft;
import net.minecraft.world.entity.player.Player;

public class ClientGlowingEyesComponent {
private ClientGlowingEyesComponent() {
}

/**
* Client-side only method to send an update to the server
* Throws an exception when ran on the wrong side, so make sure to check the side before calling this method
*/
public static void sendUpdate() {
Player localPlayer = Minecraft.getInstance().player;
new C2SComponentUpdatePacket(localPlayer, GlowingEyesComponent.getComponent(localPlayer)).sendToServer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import me.andreasmelone.glowingeyes.client.util.ColorUtil;
import me.andreasmelone.glowingeyes.client.util.GuiUtil;
import me.andreasmelone.glowingeyes.client.util.TextureLocations;
import me.andreasmelone.glowingeyes.common.util.Util;
import me.andreasmelone.glowingeyes.server.util.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.lwjgl.glfw.GLFW;

import java.awt.*;
import java.util.function.Consumer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.logging.LogUtils;
import me.andreasmelone.glowingeyes.client.component.eyes.ClientGlowingEyesComponent;
import me.andreasmelone.glowingeyes.client.gui.preset.PresetsScreen;
import me.andreasmelone.glowingeyes.client.util.ColorUtil;
import me.andreasmelone.glowingeyes.client.util.GuiUtil;
import me.andreasmelone.glowingeyes.client.util.TextureLocations;
import me.andreasmelone.glowingeyes.common.component.eyes.GlowingEyesComponent;
import me.andreasmelone.glowingeyes.server.component.eyes.GlowingEyesComponent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.components.Button;
Expand Down Expand Up @@ -256,7 +257,7 @@ public void onClose() {
GlowingEyesComponent.setGlowingEyesMap(player, pixels);
saved.set(true);

GlowingEyesComponent.sendUpdate();
ClientGlowingEyesComponent.sendUpdate();
}
if(!saved.get()) {
LogUtils.getLogger().error("Could not save glowing eyes map to player capability");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.andreasmelone.glowingeyes.client.presets.PresetManager;
import me.andreasmelone.glowingeyes.client.util.GuiUtil;
import me.andreasmelone.glowingeyes.client.util.TextureLocations;
import me.andreasmelone.glowingeyes.common.component.eyes.GlowingEyesComponent;
import me.andreasmelone.glowingeyes.server.component.eyes.GlowingEyesComponent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import me.andreasmelone.glowingeyes.client.presets.PresetManager;
import me.andreasmelone.glowingeyes.client.util.GuiUtil;
import me.andreasmelone.glowingeyes.client.util.TextureLocations;
import me.andreasmelone.glowingeyes.common.component.eyes.GlowingEyesComponent;
import me.andreasmelone.glowingeyes.server.component.eyes.GlowingEyesComponent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package me.andreasmelone.glowingeyes.client.packet;

import me.andreasmelone.glowingeyes.server.component.eyes.GlowingEyesComponent;
import me.andreasmelone.glowingeyes.server.component.eyes.IGlowingEyes;
import me.andreasmelone.glowingeyes.server.packet.S2CComponentUpdatePacket;
import me.andreasmelone.glowingeyes.server.util.Util;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;

import java.util.UUID;

public class C2SComponentUpdatePacket extends S2CComponentUpdatePacket {
public C2SComponentUpdatePacket() {}

public C2SComponentUpdatePacket(UUID playerUUID, IGlowingEyes glowingEyes) {
this.playerUUID = playerUUID;
this.glowingEyes = glowingEyes;
}

public C2SComponentUpdatePacket(Player player, IGlowingEyes glowingEyes) {
this(player.getUUID(), glowingEyes);
}

public void sendToServer() {
FriendlyByteBuf buf = PacketByteBufs.create();
buf.writeUUID(this.playerUUID);
buf.writeBoolean(glowingEyes.isToggledOn());
buf.writeByteArray(Util.serializeMap(glowingEyes.getGlowingEyesMap()));
ClientPlayNetworking.send(ID, buf);
}

public static void registerHandlers() {
ClientPlayNetworking.registerGlobalReceiver(S2CComponentUpdatePacket.ID, (client, handler, buf, responseSender) -> {
S2CComponentUpdatePacket packet = new S2CComponentUpdatePacket();
packet.deserialize(buf);
client.execute(() -> {
Player target = client.level.getPlayerByUUID(packet.playerUUID);
if (target == null) {
return;
}
GlowingEyesComponent.setGlowingEyesMap(target, packet.glowingEyes.getGlowingEyesMap());
GlowingEyesComponent.setToggledOn(target, packet.glowingEyes.isToggledOn());
});
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.andreasmelone.glowingeyes.client.packet;

import me.andreasmelone.glowingeyes.server.component.data.PlayerDataComponent;
import me.andreasmelone.glowingeyes.server.packet.S2CHasModPacket;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;

public class C2SHasModPacket extends S2CHasModPacket {
public static void sendToServer() {
ClientPlayNetworking.send(ID, PacketByteBufs.create());
}

public static void registerHandlers() {
ClientPlayNetworking.registerGlobalReceiver(S2CHasModPacket.ID, (client, handler, buf, responseSender) -> {
client.execute(() -> {
PlayerDataComponent.setHasMod(client.player, true);
});
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.andreasmelone.glowingeyes.client.packet;

public class ClientPacketManager {
public static void registerHandlers() {
C2SComponentUpdatePacket.registerHandlers();
C2SHasModPacket.registerHandlers();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.mojang.logging.LogUtils;
import me.andreasmelone.glowingeyes.client.component.eyes.ClientGlowingEyesComponent;
import me.andreasmelone.glowingeyes.client.presets.gson.GsonPresetsFileModel;
import me.andreasmelone.glowingeyes.client.presets.gson.PointColorMapSerializer;
import me.andreasmelone.glowingeyes.common.component.eyes.GlowingEyesComponent;
import me.andreasmelone.glowingeyes.server.component.eyes.GlowingEyesComponent;
import net.minecraft.client.Minecraft;
import org.slf4j.Logger;

Expand Down Expand Up @@ -100,7 +101,7 @@ public void applyPreset(int id) {
Preset preset = this.presets.get(id);

GlowingEyesComponent.setGlowingEyesMap(Minecraft.getInstance().player, preset.getContent());
GlowingEyesComponent.sendUpdate();
ClientGlowingEyesComponent.sendUpdate();
}

public boolean hasPreset(int id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import me.andreasmelone.glowingeyes.common.component.eyes.GlowingEyesComponent;
import me.andreasmelone.glowingeyes.client.util.DynamicTextureCache;
import me.andreasmelone.glowingeyes.server.component.eyes.GlowingEyesComponent;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.renderer.MultiBufferSource;
Expand All @@ -23,7 +24,7 @@ public void render(PoseStack poseStack, MultiBufferSource multiBufferSource,
int i, T t, float v, float v1, float v2,
float v3, float v4, float v5) {
if(GlowingEyesComponent.isToggledOn(t) && !t.isInvisible()) {
ResourceLocation eyeOverlayResource = GlowingEyesComponent.getGlowingEyesTexture(t);
ResourceLocation eyeOverlayResource = DynamicTextureCache.getTexture(GlowingEyesComponent.getGlowingEyesMap(t));
if(eyeOverlayResource == null) return;

RenderType eyeRenderType = RenderType.eyes(eyeOverlayResource);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package me.andreasmelone.glowingeyes.client.util;

import com.mojang.blaze3d.platform.NativeImage;
import me.andreasmelone.glowingeyes.GlowingEyes;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.resources.ResourceLocation;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class DynamicTextureCache {
private DynamicTextureCache() {
}

private static final Map<Map<Point, Color>, ResourceLocation> cache = new HashMap<>();

public static ResourceLocation getTexture(Map<Point, Color> glowingEyesMap) {
ResourceLocation texture = cache.get(glowingEyesMap);
if (texture != null) {
return texture;
}

texture = createTexture(glowingEyesMap);
cache.put(glowingEyesMap, texture);
return texture;
}

public static void clear() {
for(ResourceLocation texture : cache.values()) {
Minecraft.getInstance().getTextureManager().release(texture);
}
cache.clear();
}

private static ResourceLocation createTexture(Map<Point, Color> glowingEyesMap) {
BufferedImage image = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
for (Map.Entry<Point, Color> entry : glowingEyesMap.entrySet()) {
Point point = entry.getKey();
Color color = entry.getValue();
image.setRGB(point.x + 8, point.y + 8, color.getRGB());
}

NativeImage nativeImage = GuiUtil.toNativeImage(image);
DynamicTexture dynamicTexture = new DynamicTexture(nativeImage);
return Minecraft.getInstance().getTextureManager().register(GlowingEyes.MOD_ID + "_" + UUID.randomUUID(), dynamicTexture);
}

private static <K, V> boolean areEqual(Map<K, V> map1, Map<K, V> map2) {
if (map1.size() != map2.size()) {
return false;
}

for (Map.Entry<K, V> entry : map1.entrySet()) {
K key = entry.getKey();
V value = entry.getValue();
if (!map2.containsKey(key) || !map2.get(key).equals(value)) {
return false;
}
}

return true;
}
}
Loading

0 comments on commit c516add

Please sign in to comment.