Skip to content

Commit d3db5c7

Browse files
committed
working!
1 parent 4c98c87 commit d3db5c7

File tree

4 files changed

+151
-18
lines changed

4 files changed

+151
-18
lines changed

build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ repositories {
1616
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
1717
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
1818
// for more information about repositories.
19+
jcenter()
1920
}
2021

2122
dependencies {
@@ -29,6 +30,7 @@ dependencies {
2930

3031
// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
3132
// You may need to force-disable transitiveness on them.
33+
implementation 'com.shinyhut:vernacular:1.11'
3234
}
3335

3436
processResources {

src/main/java/com/emeraldodin/minecraft/pcmod/client/PCModClient.java

+10
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,27 @@
77

88
import net.fabricmc.api.ClientModInitializer;
99
import net.fabricmc.fabric.api.client.rendereregistry.v1.EntityRendererRegistry;
10+
import net.minecraft.util.Identifier;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
import java.util.UUID;
1015

1116
public class PCModClient implements ClientModInitializer {
1217
public static EntityItemPreview thePreviewEntity;
18+
public static Map<UUID, Identifier> vmScreenTextures;
1319

1420
@Override
1521
public void onInitializeClient() {
22+
vmScreenTextures = new HashMap<UUID, Identifier>();
23+
1624
EntityRendererRegistry.INSTANCE.register(EntityList.FLATSCREEN,
1725
(entityRenderDispatcher, context) -> new FlatScreenRender(entityRenderDispatcher));
1826

1927
EntityRendererRegistry.INSTANCE.register(EntityList.ITEM_PREVIEW,
2028
(entityRenderDispatcher, context) -> new ItemPreviewRender(entityRenderDispatcher));
29+
30+
new VNCReceiver("127.0.0.1", 5900).connect();
2131
}
2232

2333
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.emeraldodin.minecraft.pcmod.client;
2+
3+
import com.shinyhut.vernacular.client.VernacularClient;
4+
import com.shinyhut.vernacular.client.VernacularConfig;
5+
import com.shinyhut.vernacular.client.rendering.ColorDepth;
6+
7+
import net.minecraft.client.MinecraftClient;
8+
import net.minecraft.client.texture.NativeImage;
9+
import net.minecraft.client.texture.NativeImageBackedTexture;
10+
11+
import java.awt.Color;
12+
import java.awt.Graphics;
13+
import java.awt.Graphics2D;
14+
import java.awt.Image;
15+
import java.awt.image.BufferedImage;
16+
import java.awt.image.ColorModel;
17+
import java.awt.image.Raster;
18+
import java.io.ByteArrayOutputStream;
19+
import java.io.IOException;
20+
import java.lang.annotation.Native;
21+
import java.nio.Buffer;
22+
import java.nio.ByteBuffer;
23+
import java.util.Random;
24+
25+
import javax.imageio.ImageIO;
26+
27+
public class VNCReceiver {
28+
29+
private String host;
30+
private int port;
31+
32+
private BufferedImage bufferedImage;
33+
private Graphics graphics;
34+
private NativeImage ni;
35+
private int width = 0, height = 0;
36+
private NativeImageBackedTexture lastNIBT;
37+
38+
public VNCReceiver(String host, int port) {
39+
this.host = host;
40+
this.port = port;
41+
}
42+
43+
public void connect() {
44+
MinecraftClient mcc = MinecraftClient.getInstance();
45+
VernacularConfig config = new VernacularConfig();
46+
VernacularClient client = new VernacularClient(config);
47+
48+
// Select 8-bits per pixel indexed color, or 8/16/24 bits per pixel true color
49+
config.setColorDepth(ColorDepth.BPP_8_INDEXED);
50+
51+
// Set up callbacks for the various events that can happen in a VNC session
52+
53+
// Exception handler
54+
config.setErrorListener(Throwable::printStackTrace);
55+
56+
// Password supplier - this is only invoked if the remote server requires authentication
57+
// config.setPasswordSupplier(() -> "my secret password");
58+
59+
// Handle system bell events from the remote host
60+
config.setBellListener(v -> System.out.println("DING!"));
61+
62+
config.setScreenUpdateListener(image -> {
63+
try {
64+
if(width == 0) {
65+
width = image.getWidth(null);
66+
}
67+
if(height == 0) {
68+
height = image.getHeight(null);
69+
}
70+
@SuppressWarnings("SpellCheckingInspection")
71+
NativeImageBackedTexture nibt = createNIBT(image);
72+
PCModClient.vmScreenTextures.put(mcc.player.getUuid(), mcc.getTextureManager().registerDynamicTexture("pc_screen_mp", nibt));
73+
if(lastNIBT != null) {
74+
lastNIBT.clearGlId();
75+
lastNIBT = null;
76+
}
77+
lastNIBT = nibt;
78+
}
79+
catch (Exception e) {
80+
e.printStackTrace();
81+
}
82+
});
83+
84+
client.start(host, port);
85+
}
86+
87+
private NativeImageBackedTexture createNIBT(Image image) {
88+
toBufferedImage(image);
89+
if(ni == null) {
90+
// We need to use ABGR otherwise we can't set a custom pixel color
91+
ni = new NativeImage(NativeImage.Format.ABGR, width, height, true);
92+
}
93+
final ColorModel colorModel = bufferedImage.getColorModel();
94+
for (int x = 0; x < width; x++) {
95+
for (int y = 0; y < height; y++) {
96+
Object elements = bufferedImage.getRaster().getDataElements(x, y, (Object) null);
97+
98+
int red = colorModel.getRed(elements);
99+
int green = colorModel.getGreen(elements);
100+
int blue = colorModel.getBlue(elements);
101+
int out = 255 << 24 | blue << 16 | green << 8 | red << 0;
102+
103+
ni.setPixelColor(x, y, out);
104+
}
105+
}
106+
return new NativeImageBackedTexture(ni);
107+
}
108+
109+
private void toBufferedImage(Image img) {
110+
// Initialize cached buffers
111+
if(this.bufferedImage == null) {
112+
this.bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
113+
}
114+
if(this.graphics == null) {
115+
this.graphics = this.bufferedImage.createGraphics();
116+
}
117+
this.graphics.drawImage(img, 0, 0, null);
118+
}
119+
120+
}

src/main/java/com/emeraldodin/minecraft/pcmod/client/entities/render/FlatScreenRender.java

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.emeraldodin.minecraft.pcmod.client.entities.render;
22

3-
import java.util.UUID;
4-
53
import com.emeraldodin.minecraft.pcmod.Utils;
4+
import com.emeraldodin.minecraft.pcmod.client.PCModClient;
65
import com.emeraldodin.minecraft.pcmod.entities.EntityFlatScreen;
76
import com.emeraldodin.minecraft.pcmod.item.ItemList;
87
import com.mojang.blaze3d.systems.RenderSystem;
@@ -23,6 +22,8 @@
2322
import net.minecraft.util.math.Matrix4f;
2423
import net.minecraft.util.math.Quaternion;
2524

25+
import java.util.UUID;
26+
2627
public class FlatScreenRender extends EntityRenderer<EntityFlatScreen>{
2728
protected static final RenderPhase.Transparency TRANSLUCENT_TRANSPARENCY = new RenderPhase.Transparency("translucent_transparency", () -> {
2829
RenderSystem.enableBlend();
@@ -49,22 +50,22 @@ public void render(EntityFlatScreen entity, float yaw, float tickDelta, MatrixSt
4950
Quaternion look = Utils.lookAt(entity.getPos(), entity.getLookAtPos());
5051
matrices.multiply(look);
5152
MinecraftClient.getInstance().getItemRenderer().renderItem(new ItemStack(ItemList.ITEM_FLATSCREEN), Mode.NONE, light, OverlayTexture.DEFAULT_UV, matrices, vertexConsumers);
52-
// if(ClientMod.vmScreenTextures.containsKey(UUID.fromString(entity.getOwnerUUID()))) {
53-
// matrices.push();
54-
// matrices.scale(0.006f, 0.006f, 0.006f);
55-
// matrices.multiply(new Quaternion(0f, 0f, 0f, true));
56-
// matrices.multiply(new Quaternion(0, 0, 180, true));
57-
// matrices.translate(-59.4f, -18.3f, -13f);
58-
// matrices.scale(0.8079f, 0.488f, 1f);
59-
// matrices.translate(10, 5.4f, 7.76f);
60-
// Matrix4f matrix4f = matrices.peek().getModel();
61-
// VertexConsumer vertexConsumer = vertexConsumers.getBuffer(RenderLayer.of("vmscreen", VertexFormats.POSITION_COLOR_TEXTURE, 7, 256, false, true, RenderLayer.MultiPhaseParameters.builder().texture(new RenderPhase.Texture(ClientMod.vmScreenTextures.get(UUID.fromString(entity.getOwnerUUID())), false, false)).alpha(ONE_TENTH_ALPHA).transparency(TRANSLUCENT_TRANSPARENCY).build(false)));
62-
// vertexConsumer.vertex(matrix4f, 0.0F, 128.0F, -0.01F).color(255, 255, 255, 255).texture(0.0F, 1.0F).next();
63-
// vertexConsumer.vertex(matrix4f, 128.0F, 128.0F, -0.01F).color(255, 255, 255, 255).texture(1.0F, 1.0F).next();
64-
// vertexConsumer.vertex(matrix4f, 128.0F, 0.0F, -0.01F).color(255, 255, 255, 255).texture(1.0F, 0.0F).next();
65-
// vertexConsumer.vertex(matrix4f, 0.0F, 0.0F, -0.01F).color(255, 255, 255, 255).texture(0.0F, 0.0F).next();
66-
// matrices.pop();
67-
// }
53+
if(PCModClient.vmScreenTextures.containsKey(UUID.fromString(entity.getOwnerUUID()))) {
54+
matrices.push();
55+
matrices.scale(0.006f, 0.006f, 0.006f);
56+
matrices.multiply(new Quaternion(0f, 0f, 0f, true));
57+
matrices.multiply(new Quaternion(0, 0, 180, true));
58+
matrices.translate(-59.4f, -18.3f, -13f);
59+
matrices.scale(0.8079f, 0.488f, 1f);
60+
matrices.translate(10, 5.4f, 7.76f);
61+
Matrix4f matrix4f = matrices.peek().getModel();
62+
VertexConsumer vertexConsumer = vertexConsumers.getBuffer(RenderLayer.of("vmscreen", VertexFormats.POSITION_COLOR_TEXTURE, 7, 256, false, true, RenderLayer.MultiPhaseParameters.builder().texture(new RenderPhase.Texture(PCModClient.vmScreenTextures.get(UUID.fromString(entity.getOwnerUUID())), false, false)).alpha(ONE_TENTH_ALPHA).transparency(TRANSLUCENT_TRANSPARENCY).build(false)));
63+
vertexConsumer.vertex(matrix4f, 0.0F, 128.0F, -0.01F).color(255, 255, 255, 255).texture(0.0F, 1.0F).next();
64+
vertexConsumer.vertex(matrix4f, 128.0F, 128.0F, -0.01F).color(255, 255, 255, 255).texture(1.0F, 1.0F).next();
65+
vertexConsumer.vertex(matrix4f, 128.0F, 0.0F, -0.01F).color(255, 255, 255, 255).texture(1.0F, 0.0F).next();
66+
vertexConsumer.vertex(matrix4f, 0.0F, 0.0F, -0.01F).color(255, 255, 255, 255).texture(0.0F, 0.0F).next();
67+
matrices.pop();
68+
}
6869
matrices.pop();
6970
}
7071

0 commit comments

Comments
 (0)