You've already forked Arcturus-Morningstar-Extended
mirror of
https://github.com/duckietm/Arcturus-Morningstar-Extended.git
synced 2026-06-20 15:36:17 +00:00
feat(items): reindex returns sanitized furnidata delta
This commit is contained in:
@@ -69,15 +69,30 @@ public class FurnitureTextProvider {
|
|||||||
return Files.exists(legacy) ? legacy : null;
|
return Files.exists(legacy) ? legacy : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Build a fresh sanitized index from the given entries and swap it in atomically. */
|
/**
|
||||||
public void reindex(List<FurnidataEntry> entries) {
|
* Build a fresh sanitized index, swap it in atomically, and return the
|
||||||
|
* changed/added entries (sanitized) as the delta versus the previous index.
|
||||||
|
*/
|
||||||
|
public java.util.List<FurnidataEntry> reindex(java.util.List<FurnidataEntry> entries) {
|
||||||
Map<String, FurniText> next = new HashMap<>(Math.max(16, entries.size() * 2));
|
Map<String, FurniText> next = new HashMap<>(Math.max(16, entries.size() * 2));
|
||||||
for (FurnidataEntry e : entries) {
|
for (FurnidataEntry e : entries) {
|
||||||
String key = baseKey(e.classname());
|
String key = baseKey(e.classname());
|
||||||
if (key == null) continue;
|
if (key == null) continue;
|
||||||
next.put(key, new FurniText(e.id(), e.type(), sanitize(e.name()), sanitize(e.description())));
|
next.put(key, new FurniText(e.id(), e.type(), sanitize(e.name()), sanitize(e.description())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String, FurniText> prev = this.index;
|
||||||
|
java.util.List<FurnidataEntry> delta = new java.util.ArrayList<>();
|
||||||
|
for (Map.Entry<String, FurniText> en : next.entrySet()) {
|
||||||
|
FurniText cur = en.getValue();
|
||||||
|
FurniText old = prev.get(en.getKey());
|
||||||
|
if (old == null || !old.name().equals(cur.name()) || !old.description().equals(cur.description())) {
|
||||||
|
delta.add(new FurnidataEntry(cur.id(), en.getKey(), cur.type(), cur.name(), cur.description()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.index = next; // atomic reference swap
|
this.index = next; // atomic reference swap
|
||||||
|
return delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the sanitized display name for a DB classname, or null if absent/disabled. */
|
/** Returns the sanitized display name for a DB classname, or null if absent/disabled. */
|
||||||
|
|||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
package com.eu.habbo.habbohotel.items;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class FurnitureTextProviderDeltaTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void firstReindexReturnsAllAsDelta() {
|
||||||
|
FurnitureTextProvider p = new FurnitureTextProvider(true);
|
||||||
|
List<FurnidataEntry> delta = p.reindex(List.of(
|
||||||
|
new FurnidataEntry(1, "chair", FurnitureType.FLOOR, "Chair", "Sit")));
|
||||||
|
assertEquals(1, delta.size());
|
||||||
|
assertEquals("Chair", delta.get(0).name());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void unchangedReindexReturnsEmptyDelta() {
|
||||||
|
FurnitureTextProvider p = new FurnitureTextProvider(true);
|
||||||
|
List<FurnidataEntry> first = List.of(new FurnidataEntry(1, "chair", FurnitureType.FLOOR, "Chair", "Sit"));
|
||||||
|
p.reindex(first);
|
||||||
|
List<FurnidataEntry> delta = p.reindex(first);
|
||||||
|
assertTrue(delta.isEmpty(), "no change => empty delta");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void changedNameAppearsInDeltaWithSanitizedValue() {
|
||||||
|
FurnitureTextProvider p = new FurnitureTextProvider(true);
|
||||||
|
p.reindex(List.of(new FurnidataEntry(1, "chair", FurnitureType.FLOOR, "Chair", "Sit")));
|
||||||
|
List<FurnidataEntry> delta = p.reindex(List.of(
|
||||||
|
new FurnidataEntry(1, "chair", FurnitureType.FLOOR, "New %x%", "Sit")));
|
||||||
|
assertEquals(1, delta.size());
|
||||||
|
assertFalse(delta.get(0).name().contains("%"), "delta carries the sanitized name");
|
||||||
|
assertEquals(1, delta.get(0).id());
|
||||||
|
assertEquals(FurnitureType.FLOOR, delta.get(0).type());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user