Skip to content

Commit 92d42aa

Browse files
committed
Skip unchanged furnace progress renders
1 parent da028a5 commit 92d42aa

5 files changed

Lines changed: 57 additions & 57 deletions

File tree

common/src/main/java/com/loohp/interactionvisualizer/blocks/BlastFurnaceDisplay.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.loohp.interactionvisualizer.utils.VanishUtils;
4343
import com.loohp.interactionvisualizer.scheduler.ScheduledTask;
4444
import com.loohp.interactionvisualizer.scheduler.Scheduler;
45-
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
4645
import org.bukkit.Bukkit;
4746
import org.bukkit.GameMode;
4847
import org.bukkit.Location;
@@ -295,25 +294,13 @@ private ScheduledTask legacyRun() {
295294
symbol = symbol.replace("{CompletedAmount}", (inv.getItem(2) == null ? 0 : inv.getItem(2).getAmount()) + "");
296295
}
297296
if (hasFuel(blastfurnace)) {
298-
if (!PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).equals(symbol) || !stand.isCustomNameVisible()) {
299-
stand.setCustomNameVisible(true);
300-
stand.setCustomName(symbol);
301-
DisplayManager.updateDisplay(stand);
302-
}
297+
FurnaceDisplayUpdater.showProgress(stand, entry.getValue(), symbol);
303298
} else {
304299
symbol = noFuelColor + ChatColorUtils.stripColor(symbol);
305-
if (!PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).equals(symbol) || !stand.isCustomNameVisible()) {
306-
stand.setCustomNameVisible(true);
307-
stand.setCustomName(symbol);
308-
DisplayManager.updateDisplay(stand);
309-
}
300+
FurnaceDisplayUpdater.showProgress(stand, entry.getValue(), symbol);
310301
}
311302
} else {
312-
if (!PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).equals("") || stand.isCustomNameVisible()) {
313-
stand.setCustomNameVisible(false);
314-
stand.setCustomName("");
315-
DisplayManager.updateDisplay(stand);
316-
}
303+
FurnaceDisplayUpdater.hideProgress(stand, entry.getValue());
317304
}
318305
}
319306
} finally {

common/src/main/java/com/loohp/interactionvisualizer/blocks/FurnaceDisplay.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.loohp.interactionvisualizer.utils.VanishUtils;
4343
import com.loohp.interactionvisualizer.scheduler.ScheduledTask;
4444
import com.loohp.interactionvisualizer.scheduler.Scheduler;
45-
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
4645
import org.bukkit.Bukkit;
4746
import org.bukkit.GameMode;
4847
import org.bukkit.Location;
@@ -294,25 +293,13 @@ private ScheduledTask legacyRun() {
294293
symbol = symbol.replace("{CompletedAmount}", (inv.getItem(2) == null ? 0 : inv.getItem(2).getAmount()) + "");
295294
}
296295
if (hasFuel(furnace)) {
297-
if (!PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).equals(symbol) || !stand.isCustomNameVisible()) {
298-
stand.setCustomNameVisible(true);
299-
stand.setCustomName(symbol);
300-
DisplayManager.updateDisplay(stand);
301-
}
296+
FurnaceDisplayUpdater.showProgress(stand, entry.getValue(), symbol);
302297
} else {
303298
symbol = noFuelColor + ChatColorUtils.stripColor(symbol);
304-
if (!PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).equals(symbol) || !stand.isCustomNameVisible()) {
305-
stand.setCustomNameVisible(true);
306-
stand.setCustomName(symbol);
307-
DisplayManager.updateDisplay(stand);
308-
}
299+
FurnaceDisplayUpdater.showProgress(stand, entry.getValue(), symbol);
309300
}
310301
} else {
311-
if (!PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).equals("") || stand.isCustomNameVisible()) {
312-
stand.setCustomNameVisible(false);
313-
stand.setCustomName("");
314-
DisplayManager.updateDisplay(stand);
315-
}
302+
FurnaceDisplayUpdater.hideProgress(stand, entry.getValue());
316303
}
317304
}
318305
} finally {

common/src/main/java/com/loohp/interactionvisualizer/blocks/FurnaceDisplayUpdater.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
/** Shared hybrid update path for the three Bukkit furnace variants. */
3030
final class FurnaceDisplayUpdater {
3131

32+
static final String PROGRESS_TEXT_KEY = "ProgressText";
33+
3234
private FurnaceDisplayUpdater() {
3335
}
3436

@@ -69,7 +71,7 @@ static boolean update(org.bukkit.block.Furnace furnace, Map<String, Object> valu
6971
DisplayEntity stand = (DisplayEntity) values.get("Stand");
7072
ItemStack input = normalize(inventory.getItem(0));
7173
if (input == null) {
72-
hideProgress(stand);
74+
hideProgress(stand, values);
7375
return false;
7476
}
7577

@@ -103,12 +105,7 @@ static boolean update(org.bukkit.block.Furnace furnace, Map<String, Object> valu
103105
if (!hasFuel(furnace)) {
104106
text = noFuelColor + ChatColorUtils.stripColor(text);
105107
}
106-
if (!PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).equals(text)
107-
|| !stand.isCustomNameVisible()) {
108-
stand.setCustomNameVisible(true);
109-
stand.setCustomName(text);
110-
DisplayManager.updateDisplay(stand);
111-
}
108+
showProgress(stand, values, text);
112109
return furnace.getBurnTime() > 0 || furnace.getCookTime() > 0;
113110
}
114111

@@ -132,12 +129,27 @@ private static boolean hasFuel(org.bukkit.block.Furnace furnace) {
132129
return normalize(furnace.getInventory().getItem(1)) != null;
133130
}
134131

135-
private static void hideProgress(DisplayEntity stand) {
136-
if (!PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).isEmpty()
132+
static boolean shouldUpdateProgress(Map<String, Object> values, String text, boolean visible) {
133+
return !visible || !text.equals(values.get(PROGRESS_TEXT_KEY));
134+
}
135+
136+
static void showProgress(DisplayEntity stand, Map<String, Object> values, String text) {
137+
if (shouldUpdateProgress(values, text, stand.isCustomNameVisible())) {
138+
stand.setCustomNameVisible(true);
139+
stand.setCustomName(text);
140+
DisplayManager.updateDisplay(stand);
141+
values.put(PROGRESS_TEXT_KEY, text);
142+
}
143+
}
144+
145+
static void hideProgress(DisplayEntity stand, Map<String, Object> values) {
146+
if (values.containsKey(PROGRESS_TEXT_KEY)
147+
|| !PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).isEmpty()
137148
|| stand.isCustomNameVisible()) {
138149
stand.setCustomNameVisible(false);
139150
stand.setCustomName("");
140151
DisplayManager.updateDisplay(stand);
152+
values.remove(PROGRESS_TEXT_KEY);
141153
}
142154
}
143155

common/src/main/java/com/loohp/interactionvisualizer/blocks/SmokerDisplay.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.loohp.interactionvisualizer.utils.VanishUtils;
4343
import com.loohp.interactionvisualizer.scheduler.ScheduledTask;
4444
import com.loohp.interactionvisualizer.scheduler.Scheduler;
45-
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
4645
import org.bukkit.Bukkit;
4746
import org.bukkit.GameMode;
4847
import org.bukkit.Location;
@@ -296,25 +295,13 @@ private ScheduledTask legacyRun() {
296295
symbol = symbol.replace("{CompletedAmount}", (inv.getItem(2) == null ? 0 : inv.getItem(2).getAmount()) + "");
297296
}
298297
if (hasFuel(smoker)) {
299-
if (!PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).equals(symbol) || !stand.isCustomNameVisible()) {
300-
stand.setCustomNameVisible(true);
301-
stand.setCustomName(symbol);
302-
DisplayManager.updateDisplay(stand);
303-
}
298+
FurnaceDisplayUpdater.showProgress(stand, entry.getValue(), symbol);
304299
} else {
305300
symbol = noFuelColor + ChatColorUtils.stripColor(symbol);
306-
if (!PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).equals(symbol) || !stand.isCustomNameVisible()) {
307-
stand.setCustomNameVisible(true);
308-
stand.setCustomName(symbol);
309-
DisplayManager.updateDisplay(stand);
310-
}
301+
FurnaceDisplayUpdater.showProgress(stand, entry.getValue(), symbol);
311302
}
312303
} else {
313-
if (!PlainTextComponentSerializer.plainText().serialize(stand.getCustomName()).equals("") || stand.isCustomNameVisible()) {
314-
stand.setCustomNameVisible(false);
315-
stand.setCustomName("");
316-
DisplayManager.updateDisplay(stand);
317-
}
304+
FurnaceDisplayUpdater.hideProgress(stand, entry.getValue());
318305
}
319306
}
320307
} finally {

common/src/test/java/com/loohp/interactionvisualizer/blocks/FurnaceDisplayUpdaterTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@
1111

1212
package com.loohp.interactionvisualizer.blocks;
1313

14+
import com.loohp.interactionvisualizer.utils.ComponentFont;
15+
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
16+
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
1417
import org.junit.jupiter.api.Test;
1518

19+
import java.util.HashMap;
20+
import java.util.Map;
21+
1622
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
25+
import static org.junit.jupiter.api.Assertions.assertTrue;
1726

1827
class FurnaceDisplayUpdaterTest {
1928

@@ -26,4 +35,22 @@ void invalidOrExtremeCookTimesStayWithinTheProgressBar() {
2635
assertEquals(10.0D, FurnaceDisplayUpdater.scaledProgress(Short.MAX_VALUE, 1, 10));
2736
}
2837

38+
@Test
39+
void coloredProgressTextIsComparedByItsRawRenderedState() {
40+
Map<String, Object> values = new HashMap<>();
41+
String colored = "\u00a7e\u258e\u00a77\u258e";
42+
String plain = PlainTextComponentSerializer.plainText().serialize(ComponentFont.parseFont(
43+
LegacyComponentSerializer.legacySection().deserialize(colored)));
44+
45+
assertEquals("\u258e\u258e", plain);
46+
assertNotEquals(colored, plain);
47+
assertTrue(FurnaceDisplayUpdater.shouldUpdateProgress(values, colored, true));
48+
values.put(FurnaceDisplayUpdater.PROGRESS_TEXT_KEY, colored);
49+
assertFalse(FurnaceDisplayUpdater.shouldUpdateProgress(values, new String(colored), true));
50+
assertTrue(FurnaceDisplayUpdater.shouldUpdateProgress(values, "\u00a7c\u258e\u258e", true));
51+
assertTrue(FurnaceDisplayUpdater.shouldUpdateProgress(values, colored, false));
52+
values.remove(FurnaceDisplayUpdater.PROGRESS_TEXT_KEY);
53+
assertTrue(FurnaceDisplayUpdater.shouldUpdateProgress(values, colored, true));
54+
}
55+
2956
}

0 commit comments

Comments
 (0)