forked from Z-Doctor/SkillTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkillBase.java
244 lines (196 loc) · 7.1 KB
/
SkillBase.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package zdoctor.skilltree.skills;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Nullable;
import com.ibm.icu.impl.IllegalIcuArgumentException;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import zdoctor.skilltree.ModMain;
import zdoctor.skilltree.api.enums.SkillFrameType;
import zdoctor.skilltree.api.skills.interfaces.ISkillRequirment;
import zdoctor.skilltree.api.skills.interfaces.ISkillTickable;
import zdoctor.skilltree.api.skills.requirements.DescriptionRequirment;
import zdoctor.skilltree.api.skills.requirements.NameRequirment;
import zdoctor.skilltree.api.skills.requirements.PreviousSkillRequirement;
import zdoctor.skilltree.client.SkillToolTip;
import zdoctor.skilltree.proxy.CommonProxy;
/**
* Skills should extend this class.
*
*/
public abstract class SkillBase {
protected static HashMap<ResourceLocation, SkillBase> Skill_Registry = new HashMap();
private static int nextId;
public static SkillBase getSkillById(int id) {
for (SkillBase skill : Skill_Registry.values()) {
if (skill.getId() == id)
return skill;
}
return null;
}
public static ArrayList<SkillBase> getSkillRegistry() {
return new ArrayList<>(Skill_Registry.values());
}
private String skillName;
private ResourceLocation registryName;
private int id;
private final ItemStack icon;
private SkillFrameType frameType;
private DescriptionRequirment descReq;
private NameRequirment nameReq;
private PreviousSkillRequirement parentRequirement;
private boolean drawLine = true;
private ArrayList<SkillBase> children = new ArrayList<>();
private ArrayList<ISkillRequirment> requirements = new ArrayList<>();
private SkillBase parent;
public SkillBase(String name, @Nullable ItemStack iconIn, ISkillRequirment... requirements) {
this(name, SkillFrameType.NORMAL, iconIn, requirements);
}
public SkillBase(String name, SkillFrameType type, ItemStack iconIn, ISkillRequirment... requirements) {
this.skillName = name;
this.registryName = new ResourceLocation(Loader.instance().activeModContainer().getModId() + ":" + skillName);
this.frameType = type;
this.icon = iconIn;
this.nameReq = new NameRequirment(this);
Collections.addAll(this.requirements, requirements);
this.descReq = new DescriptionRequirment(this);
if (Skill_Registry.containsKey(registryName)) {
throw new IllegalArgumentException("Attempt to register Skill '" + registryName + "' twice");
}
Skill_Registry.put(registryName, this);
ModMain.proxy.log.info("Registered Skill: {}", registryName);
this.id = nextId++;
if (this instanceof ISkillTickable)
CommonProxy.SkillWatcher_Registry.add(this);
}
public SkillBase setParent(SkillBase parent) {
if (parent == null)
return this;
if (parent == this) {
ModMain.proxy.log.catching(new IllegalArgumentException(
"Tried to register skill '" + getRegistryName() + "' parent as itself!"));
}
if (this.parent != null && !this.parent.children.remove(this)) {
ModMain.proxy.log.catching(new NullPointerException("Unable to remove '" + getRegistryName()
+ "' from previous parent '" + this.parent.getRegistryName() + "'"));
}
this.parent = parent;
parentRequirement = new PreviousSkillRequirement(parent);
parent.addChildren(this);
return this;
}
public SkillBase getParent() {
return parent;
}
public NameRequirment getNameRequirement() {
return nameReq;
}
public DescriptionRequirment getDescriptionRequirement() {
return descReq;
}
public PreviousSkillRequirement getParentRequirement() {
return parentRequirement;
}
public SkillBase setFrameType(SkillFrameType frameType) {
this.frameType = frameType;
return this;
}
protected SkillBase addChildren(SkillBase childIn) {
if (childIn != null && !children.contains(childIn))
children.add(childIn);
return this;
}
public SkillBase setNameColor(int colorObtained, int colorNotObtained) {
this.nameReq.setColor(colorObtained, colorNotObtained);
return this;
}
public SkillBase setDescriptionColor(int color) {
this.descReq.setColor(color);
return this;
}
public SkillBase addRequirement(ISkillRequirment requirment) {
if (requirements.contains(requirment))
ModMain.proxy.log.catching(new IllegalIcuArgumentException("Tried to add '" + requirment + "' twice"));
for (ISkillRequirment r : requirements) {
if (requirment.getClass().isInstance(r))
ModMain.proxy.log.catching(new IllegalIcuArgumentException("Tried to add conflicting requirment class '"
+ requirment.getClass() + "' already registered '" + r.getClass() + "'"));
}
requirements.add(requirment);
return this;
}
public ArrayList<SkillBase> getChildren() {
return new ArrayList<>(children);
}
public String getUnlocaizedName() {
return "skill." + skillName;
}
public ResourceLocation getRegistryName() {
return registryName;
}
public List<ISkillRequirment> getRequirments(EntityLivingBase entity, boolean hasSkill) {
ArrayList reqList = new ArrayList<>();
if (!hasSkill) {
if (parentRequirement != null)
reqList.add(parentRequirement);
reqList.addAll(requirements);
}
return reqList;
}
@Override
public String toString() {
return getRegistryName().toString();
}
public boolean hasParent() {
return getParent() != null;
}
public int getId() {
return id;
}
@SideOnly(Side.CLIENT)
public SkillFrameType getFrameType(EntityLivingBase entity) {
return frameType;
}
@SideOnly(Side.CLIENT)
public ItemStack getIcon(EntityLivingBase entity) {
return icon;
}
@SideOnly(Side.CLIENT)
public String getDisplayName() {
return I18n.format(getUnlocaizedName());
}
/**
* Used when drawing skill info when hovered. You can manipulate the order the
* info is given
*/
@SideOnly(Side.CLIENT)
public abstract List<SkillToolTip> getToolTip(EntityLivingBase entity);
@SideOnly(Side.CLIENT)
public abstract boolean shouldDrawSkill(EntityLivingBase entity);
@SideOnly(Side.CLIENT)
public abstract ResourceLocation getBackroundLocation(EntityLivingBase entity);
@SideOnly(Side.CLIENT)
public abstract boolean shouldRenderItem(EntityLivingBase entity);
public abstract void onSkillActivated(EntityLivingBase entity);
public abstract void onSkillDeactivated(EntityLivingBase entity);
public abstract boolean hasRequirments(EntityLivingBase entity);
public abstract void onSkillPurchase(EntityLivingBase entity);
public static SkillBase getSkillByKey(ResourceLocation key) {
return Skill_Registry.get(key);
}
@SideOnly(Side.CLIENT)
public boolean shouldDrawLineToChildren() {
return drawLine;
}
public SkillBase setDrawLineToChildren(boolean doDraw) {
drawLine = doDraw;
return this;
}
}