-
Notifications
You must be signed in to change notification settings - Fork 6
/
ScreenOpenCloseEvent.java
92 lines (78 loc) · 3.07 KB
/
ScreenOpenCloseEvent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.denizenscript.clientizen.events;
import com.denizenscript.denizencore.events.ScriptEvent;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import net.minecraft.client.gui.screen.GameMenuScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.advancement.AdvancementsScreen;
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.client.gui.screen.option.OptionsScreen;
import java.util.HashMap;
import java.util.Map;
public class ScreenOpenCloseEvent extends ScriptEvent {
// <--[event]
// @Events
// <'screen_type'> screen opened|closed
//
// @Triggers when a screen is opened or closed.
//
// @Context
// <context.screen_type> returns an ElementTag of the screen type that opened.
// <context.switched> returns an ElementTag(Boolean) of whether the screen was opened from another screen.
//
// -->
// TODO: This event needs a partial redo, mainly:
// - CreativeInventoryScreen no longer extends InventoryScreen
// - Add all relevant screen types
// - Potentially add support for handling screens that aren't directly defined via class name
public static ScreenOpenCloseEvent instance;
public static Map<String, Class<?>> TYPE_MAP = new HashMap<>();
static {
TYPE_MAP.put("inventory", InventoryScreen.class);
TYPE_MAP.put("creative", CreativeInventoryScreen.class);
TYPE_MAP.put("pause", GameMenuScreen.class);
TYPE_MAP.put("options", OptionsScreen.class);
TYPE_MAP.put("advancements", AdvancementsScreen.class);
}
public String type;
public boolean opened;
public boolean switched;
public ScreenOpenCloseEvent() {
registerCouldMatcher("<'screen_type'> screen opened|closed");
instance = this;
}
@Override
public boolean matches(ScriptPath path) {
String screenMatcher = path.eventArgLowerAt(0);
if (!runGenericCheck(screenMatcher, type)) {
return false;
}
if (opened != path.eventArgLowerAt(2).equals("opened")) {
return false;
}
return super.matches(path);
}
@Override
public ObjectTag getContext(String name) {
return switch (name) {
case "screen_type" -> new ElementTag(type);
case "switched" -> new ElementTag(switched);
default -> super.getContext(name);
};
}
public void handleScreenChange(Screen screen, Screen otherScreen, boolean open) {
for (Map.Entry<String, Class<?>> pair : TYPE_MAP.entrySet()) {
if (pair.getKey().equals("inventory") && screen instanceof CreativeInventoryScreen) {
continue;
}
if (pair.getValue().isInstance(screen)) {
type = pair.getKey();
opened = open;
switched = otherScreen != null;
fire();
return;
}
}
}
}