blob: 7930210aec7d3c4e9ed1cef647f61651abab5f1d (
plain)
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
|
package de.torui.coflsky.configuration;
import java.lang.reflect.Field;
import java.util.Arrays;
import de.torui.coflsky.network.WSClient;
import net.minecraft.client.Minecraft;
import net.minecraft.event.HoverEvent;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
public class ConfigurationManager {
public Configuration Config;
public ConfigurationManager() {
this.Config = Configuration.getInstance();
}
public void UpdateConfiguration(String data) {
Configuration newConfig = WSClient.gson.fromJson(data, Configuration.class);
if (newConfig == null) {
System.out.println("Could not deserialize configuration " + data);
}
try {
if (CompareProperties(Config, newConfig)) {
Configuration.setInstance(newConfig);
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean CompareProperties(Configuration old, Configuration newConfiguration)
throws IllegalArgumentException, IllegalAccessException {
int updatedProperties = 0;
for (Field f : Configuration.class.getFields()) {
switch (f.getGenericType().getTypeName()) {
case "int":
if (f.getInt(old) != f.getInt(newConfiguration)) {
UpdatedProperty(f);
updatedProperties++;
}
break;
case "boolean":
if (f.getBoolean(old) != f.getBoolean(newConfiguration)) {
UpdatedProperty(f);
updatedProperties++;
}
break;
case "java.lang.String":
if (f.get(old) != null && !f.get(old).equals(f.get(newConfiguration))) {
UpdatedProperty(f);
updatedProperties++;
}
break;
case "java.lang.String[]":
if (!Arrays.deepEquals((String[]) f.get(old), (String[]) f.get(newConfiguration))) {
UpdatedProperty(f);
updatedProperties++;
}
break;
default:
throw new RuntimeException("Invalid Configuration Type " + f.getGenericType().getTypeName());
}
}
return updatedProperties > 0;
}
private IChatComponent GetNameFormatted(Field propertyName) {
Description description = propertyName.getAnnotation(Description.class);
ChatComponentText toReturn = new ChatComponentText(propertyName.getName());
ChatStyle style = new ChatStyle();
style.setColor(EnumChatFormatting.WHITE);
if (description != null) {
style.setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new ChatComponentText(description.value())));
}
return toReturn.setChatStyle(style);
}
public void UpdatedProperty(Field propertyName) {
IChatComponent comp;
comp = new ChatComponentText("The Configuration Setting ")
.setChatStyle(new ChatStyle().setColor(EnumChatFormatting.BLUE))
.appendSibling(GetNameFormatted(propertyName))
.appendSibling(new ChatComponentText(" has been updated")
.setChatStyle(new ChatStyle().setColor(EnumChatFormatting.BLUE)));
System.out.println("Field " + propertyName.getName() + " has no description!");
Minecraft.getMinecraft().thePlayer.addChatMessage(comp);
}
}
|