-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basically finished it, all that is left to do is to test the thing
- Loading branch information
1 parent
c2b63b3
commit c516add
Showing
38 changed files
with
319 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 4 additions & 5 deletions
9
src/main/java/me/andreasmelone/glowingeyes/client/GlowingEyesClientEvents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...in/java/me/andreasmelone/glowingeyes/client/component/data/ClientPlayerDataComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...n/java/me/andreasmelone/glowingeyes/client/component/eyes/ClientGlowingEyesComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/me/andreasmelone/glowingeyes/client/packet/C2SComponentUpdatePacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
}); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/me/andreasmelone/glowingeyes/client/packet/C2SHasModPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/me/andreasmelone/glowingeyes/client/packet/ClientPacketManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/me/andreasmelone/glowingeyes/client/util/DynamicTextureCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.