Skip to content

Commit ed2c078

Browse files
committed
Bug fixes and optimizations
1 parent 6260331 commit ed2c078

7 files changed

Lines changed: 341 additions & 60 deletions

File tree

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package fr.geomtech.universegate;
2+
3+
import com.mojang.blaze3d.vertex.PoseStack;
4+
import com.mojang.blaze3d.vertex.VertexConsumer;
5+
import net.minecraft.client.renderer.MultiBufferSource;
6+
import net.minecraft.client.renderer.RenderType;
7+
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
8+
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
9+
import net.minecraft.util.Mth;
10+
import net.minecraft.world.level.Level;
11+
import net.minecraft.world.level.block.state.BlockState;
12+
import org.joml.Matrix4f;
13+
14+
public class PortalCoreGlowRenderer implements BlockEntityRenderer<PortalCoreBlockEntity> {
15+
16+
private static final float FACE_EPSILON = 0.0015F;
17+
18+
public PortalCoreGlowRenderer(BlockEntityRendererProvider.Context context) {}
19+
20+
@Override
21+
public void render(PortalCoreBlockEntity blockEntity,
22+
float partialTick,
23+
PoseStack poseStack,
24+
MultiBufferSource buffer,
25+
int packedLight,
26+
int packedOverlay) {
27+
Level level = blockEntity.getLevel();
28+
if (level == null) return;
29+
30+
var match = PortalFrameDetector.find(level, blockEntity.getBlockPos());
31+
if (match.isEmpty()) return;
32+
33+
boolean active = false;
34+
boolean unstable = false;
35+
for (var framePos : PortalFrameHelper.collectFrame(match.get(), blockEntity.getBlockPos())) {
36+
BlockState frameState = level.getBlockState(framePos);
37+
if (!frameState.is(ModBlocks.PORTAL_FRAME)) continue;
38+
if (frameState.hasProperty(PortalFrameBlock.ACTIVE) && frameState.getValue(PortalFrameBlock.ACTIVE)) {
39+
active = true;
40+
}
41+
if (frameState.hasProperty(PortalFrameBlock.UNSTABLE) && frameState.getValue(PortalFrameBlock.UNSTABLE)) {
42+
unstable = true;
43+
}
44+
if (active && unstable) break;
45+
}
46+
47+
float time = level.getGameTime() + partialTick;
48+
float pulse = unstable
49+
? 0.5F + 0.5F * Mth.sin(time * 0.9F)
50+
: active
51+
? 0.5F + 0.5F * Mth.sin(time * 0.25F)
52+
: 0.5F + 0.5F * Mth.sin(time * 0.08F);
53+
54+
int red = unstable ? 255 : active ? 112 : 84;
55+
int green = unstable ? 158 : active ? 235 : 182;
56+
int blue = unstable ? 102 : active ? 255 : 220;
57+
int alpha = unstable
58+
? Mth.clamp((int) (120 + 110 * pulse), 100, 240)
59+
: active
60+
? Mth.clamp((int) (95 + 80 * pulse), 80, 190)
61+
: Mth.clamp((int) (46 + 34 * pulse), 40, 90);
62+
63+
VertexConsumer consumer = buffer.getBuffer(RenderType.lightning());
64+
Matrix4f matrix = poseStack.last().pose();
65+
66+
float halfInner = unstable ? 0.24F + 0.06F * pulse : active ? 0.21F + 0.04F * pulse : 0.18F + 0.02F * pulse;
67+
drawCube(consumer, matrix,
68+
0.5F - halfInner,
69+
0.5F + halfInner,
70+
0.5F - halfInner,
71+
0.5F + halfInner,
72+
0.5F - halfInner,
73+
0.5F + halfInner,
74+
red, green, blue, alpha);
75+
76+
int shellAlpha = unstable
77+
? Mth.clamp(alpha - 20, 80, 220)
78+
: active
79+
? Mth.clamp(alpha - 26, 70, 170)
80+
: Mth.clamp(alpha - 12, 30, 70);
81+
float shellInset = unstable ? 0.03F : 0.05F;
82+
drawCube(consumer, matrix,
83+
shellInset,
84+
1.0F - shellInset,
85+
shellInset,
86+
1.0F - shellInset,
87+
shellInset,
88+
1.0F - shellInset,
89+
red, green, blue, shellAlpha);
90+
}
91+
92+
private static void drawCube(VertexConsumer consumer,
93+
Matrix4f matrix,
94+
float x0,
95+
float x1,
96+
float y0,
97+
float y1,
98+
float z0,
99+
float z1,
100+
int red,
101+
int green,
102+
int blue,
103+
int alpha) {
104+
addQuad(consumer, matrix, x0, y0, z0 - FACE_EPSILON, x1, y0, z0 - FACE_EPSILON, x1, y1, z0 - FACE_EPSILON, x0, y1, z0 - FACE_EPSILON, red, green, blue, alpha);
105+
addQuad(consumer, matrix, x0, y0, z1 + FACE_EPSILON, x0, y1, z1 + FACE_EPSILON, x1, y1, z1 + FACE_EPSILON, x1, y0, z1 + FACE_EPSILON, red, green, blue, alpha);
106+
107+
addQuad(consumer, matrix, x0 - FACE_EPSILON, y0, z0, x0 - FACE_EPSILON, y0, z1, x0 - FACE_EPSILON, y1, z1, x0 - FACE_EPSILON, y1, z0, red, green, blue, alpha);
108+
addQuad(consumer, matrix, x1 + FACE_EPSILON, y0, z0, x1 + FACE_EPSILON, y1, z0, x1 + FACE_EPSILON, y1, z1, x1 + FACE_EPSILON, y0, z1, red, green, blue, alpha);
109+
110+
addQuad(consumer, matrix, x0, y0 - FACE_EPSILON, z0, x0, y0 - FACE_EPSILON, z1, x1, y0 - FACE_EPSILON, z1, x1, y0 - FACE_EPSILON, z0, red, green, blue, alpha);
111+
addQuad(consumer, matrix, x0, y1 + FACE_EPSILON, z0, x1, y1 + FACE_EPSILON, z0, x1, y1 + FACE_EPSILON, z1, x0, y1 + FACE_EPSILON, z1, red, green, blue, alpha);
112+
}
113+
114+
private static void addQuad(VertexConsumer consumer,
115+
Matrix4f matrix,
116+
float x1,
117+
float y1,
118+
float z1,
119+
float x2,
120+
float y2,
121+
float z2,
122+
float x3,
123+
float y3,
124+
float z3,
125+
float x4,
126+
float y4,
127+
float z4,
128+
int red,
129+
int green,
130+
int blue,
131+
int alpha) {
132+
consumer.addVertex(matrix, x1, y1, z1).setColor(red, green, blue, alpha);
133+
consumer.addVertex(matrix, x2, y2, z2).setColor(red, green, blue, alpha);
134+
consumer.addVertex(matrix, x3, y3, z3).setColor(red, green, blue, alpha);
135+
consumer.addVertex(matrix, x4, y4, z4).setColor(red, green, blue, alpha);
136+
137+
consumer.addVertex(matrix, x4, y4, z4).setColor(red, green, blue, alpha);
138+
consumer.addVertex(matrix, x3, y3, z3).setColor(red, green, blue, alpha);
139+
consumer.addVertex(matrix, x2, y2, z2).setColor(red, green, blue, alpha);
140+
consumer.addVertex(matrix, x1, y1, z1).setColor(red, green, blue, alpha);
141+
}
142+
}

src/client/java/fr/geomtech/universegate/PortalFrameGlowRenderer.java

Lines changed: 73 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import com.mojang.blaze3d.vertex.PoseStack;
44
import com.mojang.blaze3d.vertex.VertexConsumer;
5+
import net.minecraft.core.BlockPos;
56
import net.minecraft.client.renderer.MultiBufferSource;
67
import net.minecraft.client.renderer.RenderType;
78
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
89
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
910
import net.minecraft.util.Mth;
11+
import net.minecraft.world.level.Level;
1012
import net.minecraft.world.level.block.state.BlockState;
1113
import org.joml.Matrix4f;
1214

@@ -24,26 +26,49 @@ public void render(PortalFrameBlockEntity blockEntity,
2426
int packedLight,
2527
int packedOverlay) {
2628
BlockState state = blockEntity.getBlockState();
27-
if (!state.hasProperty(PortalFrameBlock.ACTIVE) || !state.getValue(PortalFrameBlock.ACTIVE)) return;
28-
if (blockEntity.getLevel() == null) return;
29-
if (state.hasProperty(PortalFrameBlock.BLINK_ON) && !state.getValue(PortalFrameBlock.BLINK_ON)) return;
29+
Level level = blockEntity.getLevel();
30+
if (level == null) return;
31+
if (!state.hasProperty(PortalFrameBlock.ACTIVE)) return;
32+
33+
boolean active = state.getValue(PortalFrameBlock.ACTIVE);
34+
if (active && state.hasProperty(PortalFrameBlock.BLINK_ON) && !state.getValue(PortalFrameBlock.BLINK_ON)) return;
35+
36+
if (!active && !isPartOfCompletedPortal(level, blockEntity.getBlockPos())) return;
3037

3138
boolean unstable = state.hasProperty(PortalFrameBlock.UNSTABLE) && state.getValue(PortalFrameBlock.UNSTABLE);
32-
float time = blockEntity.getLevel().getGameTime() + partialTick;
39+
float time = level.getGameTime() + partialTick;
3340
float pulse;
3441
float half;
3542
int alpha;
36-
if (unstable) {
43+
int red;
44+
int green;
45+
int blue;
46+
47+
if (!active) {
48+
float phase = (blockEntity.getBlockPos().asLong() & 31L) * 0.21F;
49+
pulse = 0.45F + 0.55F * Mth.sin(time * 0.1F + phase);
50+
half = 0.045F + 0.012F * pulse;
51+
alpha = (int) (36 + 42 * pulse);
52+
red = 110;
53+
green = 220;
54+
blue = 255;
55+
} else if (unstable) {
3756
float phase = (blockEntity.getBlockPos().asLong() & 15L) * 0.47F;
3857
float strobe = Mth.sin(time * 4.1F + phase) > 0.1F ? 1.0F : 0.25F;
3958
float wave = 0.5F + 0.5F * Mth.sin(time * 1.35F + phase * 0.6F);
4059
pulse = Mth.clamp(strobe * (0.55F + 0.45F * wave), 0.0F, 1.0F);
4160
half = 0.06F + 0.045F * pulse;
4261
alpha = (int) (120 + 125 * pulse);
62+
red = 255;
63+
green = 166;
64+
blue = 108;
4365
} else {
4466
pulse = 0.72F + 0.28F * Mth.sin(time * 0.25F);
4567
half = 0.06F + 0.02F * pulse;
4668
alpha = (int) (110 + 80 * pulse);
69+
red = 150;
70+
green = 235;
71+
blue = 255;
4772
}
4873

4974
VertexConsumer consumer = buffer.getBuffer(RenderType.lightning());
@@ -54,42 +79,67 @@ public void render(PortalFrameBlockEntity blockEntity,
5479
0.5F + half, 0.5F - half, -FACE_EPSILON,
5580
0.5F + half, 0.5F + half, -FACE_EPSILON,
5681
0.5F - half, 0.5F + half, -FACE_EPSILON,
57-
alpha);
82+
red, green, blue, alpha);
5883

5984
addQuad(consumer, matrix,
6085
0.5F - half, 0.5F - half, 1.0F + FACE_EPSILON,
6186
0.5F - half, 0.5F + half, 1.0F + FACE_EPSILON,
6287
0.5F + half, 0.5F + half, 1.0F + FACE_EPSILON,
6388
0.5F + half, 0.5F - half, 1.0F + FACE_EPSILON,
64-
alpha);
89+
red, green, blue, alpha);
6590

6691
addQuad(consumer, matrix,
6792
-FACE_EPSILON, 0.5F - half, 0.5F - half,
6893
-FACE_EPSILON, 0.5F - half, 0.5F + half,
6994
-FACE_EPSILON, 0.5F + half, 0.5F + half,
7095
-FACE_EPSILON, 0.5F + half, 0.5F - half,
71-
alpha);
96+
red, green, blue, alpha);
7297

7398
addQuad(consumer, matrix,
7499
1.0F + FACE_EPSILON, 0.5F - half, 0.5F - half,
75100
1.0F + FACE_EPSILON, 0.5F + half, 0.5F - half,
76101
1.0F + FACE_EPSILON, 0.5F + half, 0.5F + half,
77102
1.0F + FACE_EPSILON, 0.5F - half, 0.5F + half,
78-
alpha);
103+
red, green, blue, alpha);
79104

80105
addQuad(consumer, matrix,
81106
0.5F - half, -FACE_EPSILON, 0.5F - half,
82107
0.5F - half, -FACE_EPSILON, 0.5F + half,
83108
0.5F + half, -FACE_EPSILON, 0.5F + half,
84109
0.5F + half, -FACE_EPSILON, 0.5F - half,
85-
alpha);
110+
red, green, blue, alpha);
86111

87112
addQuad(consumer, matrix,
88113
0.5F - half, 1.0F + FACE_EPSILON, 0.5F - half,
89114
0.5F + half, 1.0F + FACE_EPSILON, 0.5F - half,
90115
0.5F + half, 1.0F + FACE_EPSILON, 0.5F + half,
91116
0.5F - half, 1.0F + FACE_EPSILON, 0.5F + half,
92-
alpha);
117+
red, green, blue, alpha);
118+
}
119+
120+
private static boolean isPartOfCompletedPortal(Level level, BlockPos framePos) {
121+
BlockPos corePos = findCoreNear(level, framePos, 4, 5);
122+
if (corePos == null) return false;
123+
124+
var match = PortalFrameDetector.find(level, corePos);
125+
if (match.isEmpty()) return false;
126+
127+
for (BlockPos pos : PortalFrameHelper.collectFrame(match.get(), corePos)) {
128+
if (pos.equals(framePos)) return true;
129+
}
130+
return false;
131+
}
132+
133+
private static BlockPos findCoreNear(Level level, BlockPos center, int rXZ, int rY) {
134+
for (int dy = -rY; dy <= rY; dy++) {
135+
for (int dx = -rXZ; dx <= rXZ; dx++) {
136+
for (int dz = -rXZ; dz <= rXZ; dz++) {
137+
BlockPos p = center.offset(dx, dy, dz);
138+
if (level.getBlockState(p).is(ModBlocks.PORTAL_CORE)) return p;
139+
}
140+
}
141+
}
142+
return null;
93143
}
94144

95145
private static void addQuad(VertexConsumer consumer,
@@ -106,15 +156,18 @@ private static void addQuad(VertexConsumer consumer,
106156
float x4,
107157
float y4,
108158
float z4,
159+
int red,
160+
int green,
161+
int blue,
109162
int alpha) {
110-
consumer.addVertex(matrix, x1, y1, z1).setColor(255, 255, 255, alpha);
111-
consumer.addVertex(matrix, x2, y2, z2).setColor(255, 255, 255, alpha);
112-
consumer.addVertex(matrix, x3, y3, z3).setColor(255, 255, 255, alpha);
113-
consumer.addVertex(matrix, x4, y4, z4).setColor(255, 255, 255, alpha);
114-
115-
consumer.addVertex(matrix, x4, y4, z4).setColor(255, 255, 255, alpha);
116-
consumer.addVertex(matrix, x3, y3, z3).setColor(255, 255, 255, alpha);
117-
consumer.addVertex(matrix, x2, y2, z2).setColor(255, 255, 255, alpha);
118-
consumer.addVertex(matrix, x1, y1, z1).setColor(255, 255, 255, alpha);
163+
consumer.addVertex(matrix, x1, y1, z1).setColor(red, green, blue, alpha);
164+
consumer.addVertex(matrix, x2, y2, z2).setColor(red, green, blue, alpha);
165+
consumer.addVertex(matrix, x3, y3, z3).setColor(red, green, blue, alpha);
166+
consumer.addVertex(matrix, x4, y4, z4).setColor(red, green, blue, alpha);
167+
168+
consumer.addVertex(matrix, x4, y4, z4).setColor(red, green, blue, alpha);
169+
consumer.addVertex(matrix, x3, y3, z3).setColor(red, green, blue, alpha);
170+
consumer.addVertex(matrix, x2, y2, z2).setColor(red, green, blue, alpha);
171+
consumer.addVertex(matrix, x1, y1, z1).setColor(red, green, blue, alpha);
119172
}
120173
}

src/client/java/fr/geomtech/universegate/UniverseGateClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void onInitializeClient() {
3535
EntityModelLayerRegistry.registerModelLayer(RiftShadeModel.LAYER_LOCATION, RiftShadeModel::createBodyLayer);
3636
EntityRendererRegistry.register(ModEntityTypes.RIFT_BEAST, RiftBeastRenderer::new);
3737
EntityRendererRegistry.register(ModEntityTypes.RIFT_SHADE, RiftShadeRenderer::new);
38+
BlockEntityRenderers.register(ModBlockEntities.PORTAL_CORE, PortalCoreGlowRenderer::new);
3839
BlockEntityRenderers.register(ModBlockEntities.PORTAL_FRAME, PortalFrameGlowRenderer::new);
3940
BlockEntityRenderers.register(ModBlockEntities.METEOROLOGICAL_CATALYST, MeteorologicalCatalystCrystalGlowRenderer::new);
4041
BlockEntityRenderers.register(ModBlockEntities.METEOROLOGICAL_CONTROLLER, MeteorologicalControllerBeamRenderer::new);

src/main/java/fr/geomtech/universegate/PortalConnectionManager.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public static boolean openBothSides(ServerLevel sourceLevel,
6565
// Interdire self link
6666
if (a.getPortalId().equals(targetId)) return false;
6767

68+
refreshCoreConnectionState(sourceLevel, sourceCorePos, a);
69+
6870
// Refuser si déjà actif/en cours d’ouverture
6971
if (a.isActiveOrOpening()) return false;
7072

@@ -98,6 +100,9 @@ public static boolean openBothSides(ServerLevel sourceLevel,
98100
if (a.getPortalId().equals(resolvedTargetId)) {
99101
return false;
100102
}
103+
104+
refreshCoreConnectionState(targetLevel, bEntry.pos(), b);
105+
101106
if (b.isActiveOrOpening()) return false;
102107

103108
// Vérifier cadres (A et B)
@@ -127,6 +132,16 @@ public static boolean openBothSides(ServerLevel sourceLevel,
127132
return true;
128133
}
129134

135+
private static void refreshCoreConnectionState(ServerLevel level, BlockPos corePos, PortalCoreBlockEntity core) {
136+
if (core.isOpening()) {
137+
tickOpeningSequence(level, corePos, core);
138+
}
139+
140+
if (level.getBlockEntity(corePos) instanceof PortalCoreBlockEntity refreshedCore) {
141+
refreshedCore.closeIfExpired(level);
142+
}
143+
}
144+
130145
static void tickOpeningSequence(ServerLevel level, BlockPos corePos, PortalCoreBlockEntity core) {
131146
if (!core.isOpening()) return;
132147

0 commit comments

Comments
 (0)