forked from Z-Doctor/SkillTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkillPageBase.java
187 lines (150 loc) · 4.83 KB
/
SkillPageBase.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package zdoctor.skilltree.skills.pages;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import net.minecraft.client.resources.I18n;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import zdoctor.skilltree.ModMain;
import zdoctor.skilltree.events.SkillEvent;
import zdoctor.skilltree.skills.SkillBase;
public abstract class SkillPageBase {
protected static final HashMap<String, SkillPageBase> SkillPage_Registry = new HashMap();
public static final SkillPageBase EMPTY = new SkillPageBase("Empty") {
@Override
public SkillPageBase addSkill(SkillBase skillIn, int column, int row) {
return this;
}
@Override
public List<SkillBase> getSkillList() {
return Collections.emptyList();
}
@Override
public void registerSkills() {
}
@Override
public void loadPage() {
}
};
private String unlocalizedName;
private String registryName;
protected final HashMap<SkillBase, Vector<Integer>> SKILL_ENTRIES = new HashMap();
private SkillBase lastAddedSkill;
public SkillPageBase(String pageName) {
this.unlocalizedName = pageName;
this.registryName = Loader.instance().activeModContainer().getModId() + ":" + pageName;
if (SkillPage_Registry.containsKey(registryName)) {
throw new IllegalArgumentException("Attempt to register Skill Page '" + registryName + "' twice");
}
SkillPage_Registry.put(registryName, this);
ModMain.proxy.log.debug("Register Page: {}", registryName);
registerSkills();
loadPage();
MinecraftForge.EVENT_BUS.register(this);
}
/**
* Create your skills here
*/
public abstract void registerSkills();
/**
* Add skills during this method. Will be called so skills can be reloaded
*/
public abstract void loadPage();
public SkillPageBase addSkill(SkillBase skillIn, int column, int row) {
if (skillIn == null)
throw new IllegalArgumentException("Tried to add null skill");
Vector<Integer> pos = new Vector<>(2);
pos.add(column);
pos.add(row);
for (SkillBase skill1 : SKILL_ENTRIES.keySet()) {
Vector<Integer> pos1 = SKILL_ENTRIES.get(skill1);
if (pos.equals(pos1))
throw new IllegalArgumentException("Tried to add skill '" + skillIn.getRegistryName() + "' to page '"
+ getRegistryName() + "' in the same spot as '" + skill1.getRegistryName() + "'");
}
if (SKILL_ENTRIES.get(skillIn) == null) {
SKILL_ENTRIES.put(skillIn, pos);
lastAddedSkill = skillIn;
} else
throw new IllegalArgumentException(
"Tried to add skill '" + skillIn.getRegistryName() + "' to page '" + getRegistryName() + "' twice");
return this;
}
public SkillBase getLastAddedSkill() {
return lastAddedSkill;
}
public String getUnlocalizedName() {
return "page." + unlocalizedName;
}
public String getDisplayName() {
return I18n.format(unlocalizedName);
}
public String getRegistryName() {
return registryName;
}
public List<SkillBase> getSkillList() {
return new ArrayList(SKILL_ENTRIES.keySet());
}
public static SkillPageBase getpageFromKey(String key) {
return SkillPage_Registry.get(key);
}
public BackgroundType getBackgroundType() {
return BackgroundType.DEFAULT;
}
public boolean hasSkillInPage(SkillBase skill) {
return SKILL_ENTRIES.get(skill) != null;
}
@SideOnly(Side.CLIENT)
public int getColumn(SkillBase skill) {
if (!hasSkillInPage(skill)) {
ModMain.proxy.log.catching(new NullPointerException("Tried to get column of skill '"
+ skill.getRegistryName() + "' that is not on page '" + getRegistryName() + "'"));
return 0;
}
return SKILL_ENTRIES.get(skill).get(0);
}
@SideOnly(Side.CLIENT)
public int getRow(SkillBase skill) {
if (!hasSkillInPage(skill)) {
ModMain.proxy.log.catching(new NullPointerException("Tried to get row of skill '" + skill.getRegistryName()
+ "' that is not on page '" + getRegistryName() + "'"));
return 0;
}
return SKILL_ENTRIES.get(skill).get(1);
}
public static enum BackgroundType {
DEFAULT,
SANDSTONE,
ENDSTONE,
DIRT,
NETHERRACK,
STONE,
CUSTOM;
private static int count = 0;
private int column;
private BackgroundType() {
column = getCount();
}
private int getCount() {
return count++;
}
@SideOnly(Side.CLIENT)
public int getColumn() {
return column > 3 ? column - 4 : column;
}
@SideOnly(Side.CLIENT)
public int getRow() {
return column > 3 ? 1 : 0;
}
}
@SubscribeEvent
public void reloadPage(SkillEvent.ReloadPages.Pre e) {
SKILL_ENTRIES.clear();
loadPage();
}
}