Skip to content

Commit 068782a

Browse files
committed
fixes to flickering
1 parent 113c38c commit 068782a

File tree

6 files changed

+62
-41
lines changed

6 files changed

+62
-41
lines changed

Core/src/main/java/exposed/hydrogen/nightclub/beatmap/BeatmapPlayer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ public InfoData play(List<CrossCompatPlayer> playTo) {
6161
long startTime = System.currentTimeMillis();
6262
for (int i = 0; i < events.size(); i++) {
6363
// account for the time it takes to parse the beatmap in the first place
64-
Long diff = System.currentTimeMillis()-startTime;
64+
long diff = System.currentTimeMillis()-startTime;
6565

6666
LightEvent event = events.get(i);
6767
List<LightEvent> filteredEvents = events.parallelStream().filter(e -> e.getType().equals(event.getType())).toList();
6868
int nextIndex = filteredEvents.indexOf(event) + 1;
6969
LightEvent nextEvent = nextIndex < filteredEvents.size() ? filteredEvents.get(nextIndex) : null;
7070

7171
Runnable task = () -> handle(event, nextEvent);
72-
executorService.schedule(task, event.getTime()-diff, TimeUnit.MICROSECONDS);
72+
executorService.schedule(task, event.getTime()-(diff*1000), TimeUnit.MICROSECONDS);
7373
}
7474

7575
//schedule turn off 5s after the show is over

Core/src/main/java/exposed/hydrogen/nightclub/light/Light.java

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,6 @@ public Light(UUID uuid, String name, Location location, LightType type, LightCha
106106
timeToFade--;
107107
length -= 100.0 / this.data.getTimeToFadeToBlack();
108108
}
109-
if (length < 0.1) {
110-
off(new Color(0x000000));
111-
timeToFade = 0;
112-
length = 0.1;
113-
}
114109
if (length > 100) {
115110
length = 100.0;
116111
}
@@ -119,34 +114,39 @@ public Light(UUID uuid, String name, Location location, LightType type, LightCha
119114
// do nothing if no movement or zooming needs to be happening
120115
return;
121116
}
122-
x = (x + multipliedSpeed) % 100;
123-
x2 = (x2 + secondaryMultipliedSpeed) % 100;
124-
lastLength = length;
125-
// do chroma gradient
126117
if(currentGradient != null && length > 0.1) {
127118
long gradientEndMillis = startTime + currentGradient.getEndTime();
128119
long gradientStartMillis = startTime + currentGradient.getStartTime();
129120
if(System.currentTimeMillis() < gradientEndMillis && System.currentTimeMillis() > gradientStartMillis) {
130121
long fraction = System.currentTimeMillis() / gradientEndMillis;
131122
color = currentGradient.getLerpType().lerp(
132-
currentGradient.getLerpType(),
133123
currentGradient.getStartColor(),
134124
currentGradient.getEndColor(),
135125
fraction,
136126
currentGradient.getEasing());
137127
marker.setColor(color);
138-
marker.setDuration(DELAY+10);
128+
marker.setDuration(DELAY+20);
139129
marker.start(256);
140130
}
141131
if(System.currentTimeMillis() > gradientEndMillis) {
142132
currentGradient = null;
143133
}
144134
}
135+
x = (x + multipliedSpeed) % 100;
136+
x2 = (x2 + secondaryMultipliedSpeed) % 100;
137+
// do chroma gradient
138+
if (length < 0.1) {
139+
off(new Color(0x000000));
140+
timeToFade = 0;
141+
length = 0.1;
142+
}
143+
lastLength = length;
144+
double l = (length + this.data.getOnLength() * (color.getAlpha() / 255.0))/2;
145145

146146
// a (invisible) "ray", pointing towards the set pitch and yaw, length is set later
147147
Vector3D v = new Vector3D(Math.toRadians(this.location.getYaw()), Math.toRadians(this.location.getPitch())).normalize();
148148
// make v the size of length
149-
Vector3D v1 = v.scalarMultiply(getMaxLengthPercent());
149+
Vector3D v1 = v.scalarMultiply(getMaxLengthPercent(l));
150150
for (int i = 0; i < lasers.size(); i++) {
151151
LaserWrapper laser = lasers.get(i);
152152
/*
@@ -161,9 +161,9 @@ x value that is separated evenly for each laser. This pattern is then moved (as
161161
Rotation r = new Rotation(v1, this.data.getPatternData().getRotation(), RotationConvention.FRAME_TRANSFORM);
162162
Rotation r2 = new Rotation(v1, this.data.getSecondPatternData().getRotation(), RotationConvention.FRAME_TRANSFORM);
163163
// apply first pattern (separated evenly for each laser) to our ray
164-
Vector3D v2 = this.data.getPatternData().getPattern().apply(v1, separated, r, this.data.getPatternData().getPatternSizeMultiplier() * (length / 100));
164+
Vector3D v2 = this.data.getPatternData().getPattern().apply(v1, separated, r, this.data.getPatternData().getPatternSizeMultiplier() * (l / 100));
165165
// then apply second pattern to all lasers with the same x value
166-
Vector3D v3 = this.data.getSecondPatternData().getPattern().apply(v1, x2, r2, this.data.getSecondPatternData().getPatternSizeMultiplier() * (length / 100));
166+
Vector3D v3 = this.data.getSecondPatternData().getPattern().apply(v1, x2, r2, this.data.getSecondPatternData().getPatternSizeMultiplier() * (l / 100));
167167
Vector3D v4 = getData().getRingMovementData().calculateMovement(zoomTime);
168168
Vector3D v5 = v1.add(v4).add(v3).add(v2);
169169

@@ -226,6 +226,7 @@ public void start() {
226226
*/
227227
public void stop() {
228228
executorService.shutdownNow();
229+
currentGradient = null;
229230
}
230231

231232
/**
@@ -276,12 +277,14 @@ public void on(Color color, @Nullable JsonArray lightIDs, int duration, @Nullabl
276277
length = Math.max(data.getOnLength() * (color.getAlpha() / 255.0), 0.05);
277278
isOn = true;
278279
timeToFade = 0;
279-
this.color = color;
280280
this.duration = duration + 10;
281-
marker.setLocation(loc);
282-
marker.setColor(this.color);
283-
marker.setDuration(this.duration);
284-
marker.start(256);
281+
if(currentGradient == null) {
282+
this.color = color;
283+
marker.setLocation(loc);
284+
marker.setColor(this.color);
285+
marker.setDuration(this.duration);
286+
marker.start(256);
287+
}
285288
}
286289

287290
public void on(Color color) {
@@ -327,12 +330,14 @@ public void flash(Color color, @Nullable JsonArray lightIDs, int duration, @Null
327330
length += (100 - data.getOnLength()) / 3;
328331
timeToFade += 3;
329332
lasers.forEach(LaserWrapper::changeColor);
330-
this.color = color;
331333
this.duration = duration + 10;
332-
marker.setDuration(this.duration);
333-
marker.setLocation(loc);
334-
marker.setColor(this.color);
335-
marker.start(256);
334+
if(currentGradient == null) {
335+
this.color = color;
336+
marker.setLocation(loc);
337+
marker.setColor(this.color);
338+
marker.setDuration(this.duration);
339+
marker.start(256);
340+
}
336341
} else {
337342
flashOff(color);
338343
}
@@ -356,12 +361,14 @@ public void flashOff(Color color, @Nullable JsonArray lightIDs, int duration, @N
356361
}
357362
on(color);
358363
flash(color);
359-
this.color = color;
360364
this.duration = duration + 10;
361-
marker.setLocation(loc);
362-
marker.setDuration(this.duration);
363-
marker.setColor(this.color);
364-
marker.start(256);
365+
if(currentGradient == null) {
366+
this.color = color;
367+
marker.setLocation(loc);
368+
marker.setColor(this.color);
369+
marker.setDuration(this.duration);
370+
marker.start(256);
371+
}
365372
timeToFade = data.getTimeToFadeToBlack();
366373
}
367374

@@ -439,7 +446,7 @@ public void setSpeed(double multiplier) {
439446
run.run();
440447
}
441448

442-
private double getMaxLengthPercent() {
449+
private double getMaxLengthPercent(double length) {
443450
return data.getMaxLength() * length / 100.0;
444451
}
445452

Core/src/main/java/exposed/hydrogen/nightclub/util/Lerp.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public enum Lerp {
77
HSV,
88
RGB;
99

10-
public Color lerp(Lerp lerp, Color start, Color end, Number fraction, Easing easing) {
11-
return lerp == HSV ? hsvLerp(start,end,fraction,easing)
10+
public Color lerp(Color start, Color end, Number fraction, Easing easing) {
11+
return this == HSV ? hsvLerp(start,end,fraction,easing)
1212
: rgbLerp(start,end,fraction,easing);
1313
}
1414
private Color hsvLerp(Color start, Color end, Number fraction, Easing easing) {
@@ -24,6 +24,7 @@ private Color rgbLerp(Color start, Color end, Number fraction, Easing easing) {
2424
var r = Util.lerp(start.getRed(),end.getRed(),fraction,easing);
2525
var g = Util.lerp(start.getGreen(),end.getGreen(),fraction,easing);
2626
var b = Util.lerp(start.getBlue(),end.getBlue(),fraction,easing);
27-
return new Color((int) r, (int) g, (int) b);
27+
var a = Util.lerp(start.getAlpha(),end.getAlpha(),fraction,easing);
28+
return new Color((int) r, (int) g, (int) b, (int) a);
2829
}
2930
}

Core/src/main/java/exposed/hydrogen/nightclub/wrapper/DebugMarkerWrapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public DebugMarkerWrapper(Location location, Color color, String name, int durat
3838

3939
public abstract void setData(Location location, Color color, String name, int duration);
4040

41+
public abstract boolean isOn();
42+
4143
public void setLocation(Location location) {
4244
this.location = location;
4345
setData(this.location, this.color, this.name, this.duration);

Minestom/src/main/java/exposed/hydrogen/nightclub/DebugMarkerMinestom.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class DebugMarkerMinestom extends DebugMarkerWrapper {
1919
private static final PluginMessagePacket STOP_ALL_MARKERS;
2020
private Marker marker;
2121
private final List<Player> seen;
22+
private long endTime;
2223

2324
public DebugMarkerMinestom(Location location, Color color, String name, Integer duration) {
2425
super(location, color, name, duration);
@@ -34,10 +35,11 @@ public void start(int distance) {
3435
@Override
3536
public void start(int distance, Runnable callback) {
3637
if (!executorService.isShutdown()) {
37-
stop();
38+
seen.clear();
39+
executorService.shutdownNow();
3840
}
3941
distanceSquared = distance < 0 ? -1 : distance * distance;
40-
long endTime = System.currentTimeMillis() + duration;
42+
endTime = System.currentTimeMillis() + duration;
4143
executorService = Executors.newScheduledThreadPool(1);
4244
// probably not the most efficient way of doing this
4345
Runnable run = () -> {
@@ -64,8 +66,7 @@ public void start(int distance, Runnable callback) {
6466

6567
@Override
6668
public void stop() {
67-
marker = new Marker(location, new Color(0,0,0,0), "", 0);
68-
sendPacketToPlayerIfCloseEnough(getMarkerPacket(marker), location);
69+
sendPacketToPlayerIfCloseEnough(getMarkerPacket(new Marker(location, new Color(0,0,0,0), "", 0)), location);
6970
seen.clear();
7071
executorService.shutdownNow();
7172
}
@@ -93,6 +94,10 @@ public void setData(Location location, Color color, String name, int duration) {
9394
marker = new Marker(location, color, name, duration);
9495
}
9596

97+
public boolean isOn() {
98+
return executorService.isShutdown();
99+
}
100+
96101
/**
97102
* Sends a packet to players if they are in range
98103
* @param packet packet to send

Spigot/src/main/java/exposed/hydrogen/nightclub/Util/DebugMarker.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class DebugMarker extends DebugMarkerWrapper {
2424
private PacketDataSerializer data;
2525
private final PacketContainer marker;
2626
private final List<Player> seen;
27+
long endTime;
2728

2829
public DebugMarker(Location location, Color color, String name, Integer duration, List<Player> showTo) throws InvocationTargetException {
2930
this(location, color, name, duration);
@@ -53,11 +54,12 @@ public void start(int distance) {
5354

5455
public void start(int distance, Runnable callback) {
5556
if (!executorService.isShutdown()) {
56-
stop();
57+
seen.clear();
58+
executorService.shutdownNow();
5759
}
5860
distanceSquared = distance < 0 ? -1 : distance * distance;
5961
long startTime = System.currentTimeMillis();
60-
long endTime = System.currentTimeMillis() + duration;
62+
endTime = System.currentTimeMillis() + duration;
6163
executorService = Executors.newScheduledThreadPool(1);
6264
// probably not the most efficient way of doing this
6365
Runnable run = () -> {
@@ -152,6 +154,10 @@ public void setData(Location location, Color color, String name, int duration) {
152154
marker.getSpecificModifier(PacketDataSerializer.class).write(0, data);
153155
}
154156

157+
public boolean isOn() {
158+
return !executorService.isShutdown();
159+
}
160+
155161
public void setLocation(Location location) {
156162
this.location = location;
157163
setData(location, this.color, this.name, this.duration);

0 commit comments

Comments
 (0)