Skip to content

Commit 35eb14e

Browse files
committed
Restore v1.5 action controls and localized labels
1 parent 3bc6c63 commit 35eb14e

31 files changed

Lines changed: 1174 additions & 151 deletions

File tree

‎craftengine/configuration/mahjong.yml‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ items:
212212
hitboxes:
213213
- type: interaction
214214
position: 0,0,0
215-
width: 0.48
216-
height: 0.2
215+
width: 0.7
216+
height: 0.22
217217
blocks_building: false
218218
interactive: true
219219
invisible: true
@@ -279,6 +279,69 @@ items:
279279
item_name: "<!i><white>Rolling Dice</white>"
280280
item_model: mahjongcraft:dice/rolling
281281

282+
# Width buckets preserve the 1.5 localized action-row feel. Java selects one only when the
283+
# authoritative action revision changes; CE owns the static collision geometry.
284+
config_factory#action_button_hitboxes:
285+
instances:
286+
- {size: "070", width: 0.7}
287+
- {size: "080", width: 0.8}
288+
- {size: "090", width: 0.9}
289+
- {size: "100", width: 1.0}
290+
- {size: "110", width: 1.1}
291+
- {size: "120", width: 1.2}
292+
- {size: "130", width: 1.3}
293+
- {size: "140", width: 1.4}
294+
- {size: "150", width: 1.5}
295+
- {size: "160", width: 1.6}
296+
- {size: "170", width: 1.7}
297+
- {size: "180", width: 1.8}
298+
- {size: "190", width: 1.9}
299+
- {size: "200", width: 2.0}
300+
- {size: "210", width: 2.1}
301+
- {size: "220", width: 2.2}
302+
blueprint:
303+
items:
304+
mahjongpaper:action_button_hitbox_${size}:
305+
material: paper
306+
settings:
307+
tags:
308+
- mahjongpaper:action_button_hitbox
309+
data:
310+
item_name: "<!i><gray>Mahjong Action Button ${size}</gray>"
311+
item_model: mahjongcraft:mahjong_tile/back
312+
behavior:
313+
type: furniture_item
314+
rules:
315+
ground:
316+
rotation: four
317+
alignment: center
318+
furniture:
319+
settings:
320+
item: mahjongpaper:action_button_hitbox_${size}
321+
variants:
322+
ground:
323+
entity_culling:
324+
aabb_expansion: 0.25
325+
view_distance: 48
326+
ray_tracing: true
327+
elements:
328+
- type: item_display
329+
item: mahjongpaper:back
330+
display_transform: none
331+
billboard: fixed
332+
position: 0,0,0
333+
translation: 0,-16,0
334+
shadow_radius: 0
335+
shadow_strength: 0
336+
hitboxes:
337+
- type: interaction
338+
position: 0,0,0
339+
width: "${width}"
340+
height: 0.22
341+
blocks_building: false
342+
interactive: true
343+
invisible: true
344+
282345
config_factory#point_sticks:
283346
instances:
284347
- {point: p100, model: "mahjongcraft:stick_p100", label: "100 Point Stick"}

‎modules/mahjong-craftengine/src/main/java/top/ellan/mahjong/craftengine/privateview/PrivateNodeRenderer.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ private void applyLabel(
194194
FakeTextDisplay display = displays.createText(
195195
PrivateSceneGeometry.localToWorld(anchor, label.transform()));
196196
display.name(PrivatePresentationAssets.labelJson(label, player.locale(), messages));
197-
if (label.emphasized()) {
198-
display.rgba(92, 63, 0, 190);
199-
} else {
200-
display.rgba(0, 62, 70, 190);
201-
}
197+
display.rgba(0, 0, 0, 60);
202198
display.spawn(player);
203199
ActiveLabel replaced =
204200
state.putActiveLabel(key, new ActiveLabel(expectedGeneration, display));

‎modules/mahjong-craftengine/src/main/java/top/ellan/mahjong/craftengine/privateview/PrivatePresentationAssets.java‎

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import top.ellan.mahjong.presentation.node.ActionLabelNode;
66
import top.ellan.mahjong.presentation.node.PrivateItemNode;
77
import top.ellan.mahjong.presentation.asset.TileAssetName;
8+
import top.ellan.mahjong.presentation.label.ActionLabelText;
89

910
/** Stateless mapping from semantic private nodes to CE assets and translated label JSON. */
1011
final class PrivatePresentationAssets {
@@ -17,43 +18,14 @@ static String itemAsset(PrivateItemNode item) {
1718
static String labelJson(
1819
ActionLabelNode label, Locale locale, PlayerTextResolver messages) {
1920
String color = label.emphasized() ? "gold" : "aqua";
20-
LabelText text = labelText(label.labelKey());
21-
String localized = messages.resolve(locale, text.translationKey(), text.fallback());
21+
String localized = ActionLabelText.resolve(
22+
label.labelKey(),
23+
(key, fallback) -> messages.resolve(locale, key, fallback));
2224
return "{\"text\":\"[\",\"color\":\""
2325
+ color
2426
+ "\",\"extra\":[{\"text\":\""
2527
+ escape(localized)
26-
+ "\"}"
27-
+ text.suffixJson()
28-
+ ",{\"text\":\"]\"}]}";
29-
}
30-
31-
private static LabelText labelText(String labelKey) {
32-
int separator = labelKey.indexOf(':');
33-
if (separator < 0) {
34-
return new LabelText("mahjongpaper." + labelKey, labelKey, "");
35-
}
36-
String base = labelKey.substring(0, separator);
37-
String suffix = labelKey.substring(separator + 1);
38-
if (base.equals("action.respond")) {
39-
int nextSeparator = suffix.indexOf(':');
40-
String reaction = nextSeparator < 0 ? suffix : suffix.substring(0, nextSeparator);
41-
String choices = nextSeparator < 0 ? "" : suffix.substring(nextSeparator + 1);
42-
return new LabelText(
43-
"mahjongpaper.action." + reaction,
44-
reaction,
45-
literalSuffix(choices));
46-
}
47-
return new LabelText(
48-
"mahjongpaper." + base,
49-
base,
50-
literalSuffix(suffix));
51-
}
52-
53-
private static String literalSuffix(String suffix) {
54-
return suffix.isEmpty()
55-
? ""
56-
: ",{\"text\":\" " + escape(suffix) + "\",\"color\":\"gray\"}";
28+
+ "\"},{\"text\":\"]\"}]}";
5729
}
5830

5931
private static String escape(String value) {
@@ -79,9 +51,4 @@ private static String escape(String value) {
7951
}
8052
return escaped.toString();
8153
}
82-
83-
private record LabelText(
84-
String translationKey,
85-
String fallback,
86-
String suffixJson) {}
8754
}

‎modules/mahjong-craftengine/src/main/java/top/ellan/mahjong/craftengine/privateview/SparrowPrivateProjectionGateway.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package top.ellan.mahjong.craftengine.privateview;
22

3+
import java.util.Locale;
34
import java.util.Map;
45
import java.util.Objects;
56
import java.util.Optional;
67
import java.util.concurrent.CompletionStage;
8+
import java.util.concurrent.ConcurrentHashMap;
79
import org.bukkit.Bukkit;
810
import org.bukkit.entity.Player;
911
import org.bukkit.event.EventHandler;
@@ -12,6 +14,7 @@
1214
import org.bukkit.event.entity.EntityDismountEvent;
1315
import org.bukkit.event.entity.PlayerDeathEvent;
1416
import org.bukkit.event.player.PlayerJoinEvent;
17+
import org.bukkit.event.player.PlayerLocaleChangeEvent;
1518
import org.bukkit.event.player.PlayerQuitEvent;
1619
import org.bukkit.event.player.PlayerTeleportEvent;
1720
import org.bukkit.plugin.Plugin;
@@ -44,6 +47,7 @@ public final class SparrowPrivateProjectionGateway
4447
Listener,
4548
AutoCloseable {
4649
private final PrivateProjectionState state = new PrivateProjectionState();
50+
private final ConcurrentHashMap<PlayerId, Locale> locales = new ConcurrentHashMap<>();
4751
private final PlayerRegionTaskScheduler tasks;
4852
private final PrivateNodeRenderer renderer;
4953
private final OverheadCameraController cameras;
@@ -117,6 +121,7 @@ public void upsert(TableId tableId, SceneNode node) {
117121
for (UpsertedNode upserted : state.upsert(tableId, node)) {
118122
Player player = Bukkit.getPlayer(upserted.key().viewer().value());
119123
if (player != null) {
124+
locales.putIfAbsent(upserted.key().viewer(), player.locale());
120125
tasks.execute(
121126
player,
122127
() -> renderer.apply(player, upserted.key(), upserted.generation()));
@@ -151,6 +156,7 @@ public void remove(TableId tableId, SceneNodeId nodeId) {
151156
public void onJoin(PlayerJoinEvent event) {
152157
Player player = event.getPlayer();
153158
PlayerId viewer = new PlayerId(player.getUniqueId());
159+
locales.put(viewer, player.locale());
154160
tasks.execute(
155161
player,
156162
() -> {
@@ -170,10 +176,21 @@ public void onJoin(PlayerJoinEvent event) {
170176
@EventHandler(priority = EventPriority.MONITOR)
171177
public void onQuit(PlayerQuitEvent event) {
172178
PlayerId playerId = new PlayerId(event.getPlayer().getUniqueId());
179+
locales.remove(playerId);
173180
renderer.onQuit(playerId);
174181
cameras.onQuit(playerId);
175182
}
176183

184+
@EventHandler(priority = EventPriority.MONITOR)
185+
public void onLocaleChange(PlayerLocaleChangeEvent event) {
186+
locales.put(new PlayerId(event.getPlayer().getUniqueId()), event.locale());
187+
}
188+
189+
/** Locale snapshot used by the render worker; never calls Bukkit off-thread. */
190+
public Locale locale(PlayerId playerId) {
191+
return locales.getOrDefault(playerId, Locale.ENGLISH);
192+
}
193+
177194
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
178195
public void onTeleport(PlayerTeleportEvent event) {
179196
exit(new PlayerId(event.getPlayer().getUniqueId()));
@@ -217,6 +234,7 @@ public boolean exit(PlayerId playerId) {
217234
public void close() {
218235
cameras.close();
219236
renderer.close();
237+
locales.clear();
220238
state.clear();
221239
}
222240

‎modules/mahjong-craftengine/src/test/java/top/ellan/mahjong/craftengine/privateview/PrivatePresentationAssetsTest.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,28 @@ void literalTranslationAndChoiceSuffixAreJsonEscaped() {
5353
assertTrue(json.contains("line\\n\\\"quoted\\\""));
5454
assertTrue(json.contains("one.two"));
5555
}
56+
57+
@Test
58+
void semanticTileArgumentsAreLocalizedAsOneV15StyleLabel() {
59+
ActionLabelNode label = new ActionLabelNode(
60+
new SceneNodeId("label/chii"),
61+
SceneVisibility.privateTo(VIEWER),
62+
"action.chii:tile.m2:tile.m3",
63+
TRANSFORM,
64+
false);
65+
66+
String json = PrivatePresentationAssets.labelJson(
67+
label,
68+
Locale.SIMPLIFIED_CHINESE,
69+
(locale, key, fallback) -> switch (key) {
70+
case "mahjongpaper.action.chii" -> "吃";
71+
case "mahjongpaper.tile.m2" -> "二万";
72+
case "mahjongpaper.tile.m3" -> "三万";
73+
default -> fallback;
74+
});
75+
76+
assertTrue(json.contains("吃 二万 三万"));
77+
assertFalse(json.contains("tile.m2"));
78+
assertFalse(json.contains("translate"));
79+
}
5680
}

‎modules/mahjong-plugin/src/main/java/top/ellan/mahjong/plugin/config/PluginConfiguration.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public static PluginConfiguration load(JavaPlugin plugin) {
6868
config.getString(
6969
"craftengine.assets.action-hitbox",
7070
"mahjongpaper:action_button_hitbox"),
71+
config.getString(
72+
"craftengine.assets.action-hitbox-prefix",
73+
"mahjongpaper:action_button_hitbox_"),
7174
config.getString(
7275
"craftengine.assets.opening-die-slot-prefix",
7376
"mahjongpaper:opening_die_slot_")),
@@ -122,6 +125,7 @@ public record CraftEngineAssets(
122125
String flatBack,
123126
String handHitbox,
124127
String actionHitbox,
128+
String actionHitboxPrefix,
125129
String openingDieSlotPrefix) {
126130
public CraftEngineAssets {
127131
table = requireAsset(table, "table furniture");
@@ -130,6 +134,8 @@ public record CraftEngineAssets(
130134
flatBack = requireAsset(flatBack, "flat back furniture");
131135
handHitbox = requireAsset(handHitbox, "hand hitbox furniture");
132136
actionHitbox = requireAsset(actionHitbox, "action hitbox furniture");
137+
actionHitboxPrefix = requireAsset(
138+
actionHitboxPrefix, "action hitbox furniture prefix");
133139
openingDieSlotPrefix = requireAsset(
134140
openingDieSlotPrefix, "opening die slot furniture prefix");
135141
}

‎modules/mahjong-plugin/src/main/java/top/ellan/mahjong/plugin/i18n/LocalizedMessageCatalog.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@
88
import java.util.Locale;
99
import java.util.Map;
1010
import java.util.Objects;
11+
import java.util.concurrent.atomic.AtomicReferenceArray;
1112
import top.ellan.mahjong.craftengine.port.PlayerTextResolver;
13+
import top.ellan.mahjong.presentation.label.ActionLabelPolicy;
14+
import top.ellan.mahjong.presentation.label.ActionLabelText;
1215
import top.ellan.mahjong.runtime.common.RulePackException;
1316
import top.ellan.mahjong.runtime.registry.MiniJson;
1417

1518
/** Immutable server-side view of the same locale files shipped to CraftEngine clients. */
1619
public final class LocalizedMessageCatalog implements PlayerTextResolver {
20+
private static final int ACTION_WIDTH_CACHE_SIZE = 256;
1721
private static final String RESOURCE_ROOT =
1822
"craftengine/mahjongpaper/resourcepack/assets/mahjongcraft/lang/";
1923
private static final List<String> LOCALES =
2024
List.of("en_us", "zh_cn", "zh_tw", "zh_hk", "zh_mo", "ja_jp");
2125
private final Map<String, Map<String, String>> translations;
26+
private final AtomicReferenceArray<ActionWidthEntry> actionWidths =
27+
new AtomicReferenceArray<>(ACTION_WIDTH_CACHE_SIZE);
2228

2329
private LocalizedMessageCatalog(Map<String, Map<String, String>> translations) {
2430
this.translations = Map.copyOf(translations);
@@ -61,6 +67,31 @@ public String format(
6167
List.copyOf(arguments).toArray());
6268
}
6369

70+
/** Returns the v1.5-compatible button width for a semantic label in one player's locale. */
71+
public double actionButtonWidth(Locale locale, String labelKey) {
72+
Objects.requireNonNull(labelKey, "labelKey");
73+
String localeId = localeId(locale);
74+
String cacheKey = localeId + '\0' + labelKey;
75+
int slot = cacheKey.hashCode() & (ACTION_WIDTH_CACHE_SIZE - 1);
76+
ActionWidthEntry cached = actionWidths.get(slot);
77+
if (cached != null && cached.key().equals(cacheKey)) {
78+
return cached.width();
79+
}
80+
Map<String, String> selected = translations.get(localeId);
81+
Map<String, String> english = translations.get("en_us");
82+
String label = ActionLabelText.resolve(
83+
labelKey,
84+
(translationKey, fallback) -> {
85+
String translated = selected == null ? null : selected.get(translationKey);
86+
return translated == null
87+
? english.getOrDefault(translationKey, fallback)
88+
: translated;
89+
});
90+
double width = ActionLabelPolicy.buttonWidth(label);
91+
actionWidths.set(slot, new ActionWidthEntry(cacheKey, width));
92+
return width;
93+
}
94+
6495
static String localeId(Locale locale) {
6596
if (locale == null) {
6697
return "en_us";
@@ -101,4 +132,6 @@ private static Map<String, String> readLocale(ClassLoader classLoader, String lo
101132
throw new IllegalStateException("Cannot load bundled locale " + resource, failure);
102133
}
103134
}
135+
136+
private record ActionWidthEntry(String key, double width) {}
104137
}

‎modules/mahjong-plugin/src/main/java/top/ellan/mahjong/plugin/platform/CraftEnginePlatformRuntime.java‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import top.ellan.mahjong.platform.paper.anchor.PaperTableAnchorService;
5353
import top.ellan.mahjong.plugin.MahjongPaperPlugin;
5454
import top.ellan.mahjong.plugin.config.PluginConfiguration;
55+
import top.ellan.mahjong.plugin.i18n.LocalizedMessageCatalog;
5556
import top.ellan.mahjong.plugin.runtime.FailureSupport;
5657
import top.ellan.mahjong.presentation.projection.DefaultTableSceneMapper;
5758
import top.ellan.mahjong.presentation.projection.LatestSceneProjector;
@@ -92,7 +93,7 @@ public CraftEnginePlatformRuntime(
9293
BoundedPlatformExecutors executors,
9394
BoundedDeadlineScheduler deadlines,
9495
TableActorRegistry actors,
95-
PlayerTextResolver messages) {
96+
LocalizedMessageCatalog messages) {
9697
this.plugin = Objects.requireNonNull(plugin, "plugin");
9798
this.configuration = Objects.requireNonNull(configuration, "configuration");
9899
this.executors = Objects.requireNonNull(executors, "executors");
@@ -134,7 +135,10 @@ public CraftEnginePlatformRuntime(
134135
tableGeometry(configuration.layoutGeometry())),
135136
sceneAssets(configuration.craftEngineAssets()),
136137
configuration.viewSettings().overheadHeight(),
137-
configuration.viewSettings().overheadEnabled()),
138+
configuration.viewSettings().overheadEnabled(),
139+
(player, labelKey) ->
140+
messages.actionButtonWidth(
141+
privateProjection.locale(player), labelKey)),
138142
sceneBackend,
139143
new SceneGraphDiffer(),
140144
deadlines);
@@ -419,7 +423,9 @@ private static TableSceneAssets sceneAssets(
419423
configured.standingBack(),
420424
configured.flatBack(),
421425
configured.handHitbox(),
422-
configured.actionHitbox());
426+
configured.actionHitbox(),
427+
TableSceneAssets.actionInteractionVariants(
428+
configured.actionHitboxPrefix()));
423429
}
424430

425431
private static TableGeometry tableGeometry(

0 commit comments

Comments
 (0)