/
D3Core.java
223 lines (192 loc) · 9.4 KB
/
D3Core.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
/*
* Copyright (c) 2014,
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the {organization} nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
package net.doubledoordev.d3core;
import cpw.mods.fml.client.config.IConfigElement;
import cpw.mods.fml.client.event.ConfigChangedEvent;
import cpw.mods.fml.common.*;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import cpw.mods.fml.common.registry.LanguageRegistry;
import cpw.mods.fml.common.versioning.ArtifactVersion;
import cpw.mods.fml.common.versioning.DefaultArtifactVersion;
import net.doubledoordev.d3core.util.CoreHelper;
import net.doubledoordev.d3core.util.DevPerks;
import net.doubledoordev.d3core.util.ID3Mod;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.common.config.Configuration;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import static net.doubledoordev.d3core.util.CoreConstants.*;
/**
* @author Dries007
*/
@Mod(modid = MODID, name = NAME, canBeDeactivated = false, guiFactory = MOD_GUI_FACTORY)
public class D3Core implements ID3Mod
{
@Mod.Instance(MODID)
public static D3Core instance;
@Mod.Metadata
private ModMetadata metadata;
private Logger logger;
private DevPerks devPerks;
private Configuration configuration;
private boolean debug = false;
private boolean sillyness = true;
private boolean updateWarning = true;
private List<CoreHelper.ModUpdateDate> updateDateList = new ArrayList<>();
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event)
{
FMLCommonHandler.instance().bus().register(this);
logger = event.getModLog();
configuration = new Configuration(event.getSuggestedConfigurationFile());
syncConfig();
}
@Mod.EventHandler
public void init(FMLInitializationEvent event)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
for (ModContainer modContainer : Loader.instance().getActiveModList())
{
try
{
if (modContainer instanceof FMLModContainer && modContainer.getMod() instanceof ID3Mod)
{
if (debug()) logger.info(String.format("[%s] Found a D3 Mod!", modContainer.getModId()));
TreeSet<ArtifactVersion> availableVersions = new TreeSet<>();
String group = GROUP + modContainer.getModId().toLowerCase();
String artifactId = modContainer.getModId();
if (debug()) logger.info(String.format("[%s] Group: %s ArtifactId: %s", modContainer.getModId(), group, artifactId));
URL url = new URL(MAVENURL + group.replace('.', '/') + '/' + artifactId + "/maven-metadata.xml");
if (debug()) logger.info(String.format("[%s] Maven URL: %s", modContainer.getModId(), url));
DocumentBuilder builder = dbf.newDocumentBuilder();
Document document = builder.parse(url.toURI().toString());
NodeList list = document.getDocumentElement().getElementsByTagName("version");
for (int i = 0; i < list.getLength(); i++)
{
String version = list.item(i).getFirstChild().getNodeValue();
if (version.startsWith(Loader.MC_VERSION + "-"))
{
availableVersions.add(new DefaultArtifactVersion(version.replace(Loader.MC_VERSION + "-", "")));
}
}
DefaultArtifactVersion current = new DefaultArtifactVersion(modContainer.getVersion().replace(Loader.MC_VERSION + "-", ""));
if (debug()) logger.info(String.format("[%s] Current: %s Latest: %s All versions for MC %s: %s", modContainer.getModId(), current, availableVersions.last(), Loader.MC_VERSION, availableVersions));
if (current.compareTo(availableVersions.last()) < 0)
{
updateDateList.add(new CoreHelper.ModUpdateDate(modContainer.getName(), modContainer.getModId(), current.toString(), availableVersions.last().toString()));
}
}
}
catch (Exception e)
{
logger.info("D3 Mod " + modContainer.getModId() + " Version check FAILED. Please report this error!");
e.printStackTrace();
}
}
}
@Mod.EventHandler
public void postInit(FMLPostInitializationEvent event)
{
for (CoreHelper.ModUpdateDate updateDate : updateDateList)
{
logger.warn(String.format("Update available for %s (%s)! Current version: %s New version: %s. Please update ASAP!", updateDate.getName(), updateDate.getModId(), updateDate.getCurrentVersion(), updateDate.getLatestVersion()));
}
}
@SubscribeEvent
public void nameFormatEvent(PlayerEvent.PlayerLoggedInEvent event)
{
if (!updateWarning) return;
event.player.addChatComponentMessage(IChatComponent.Serializer.func_150699_a("{\"text\":\"\",\"extra\":[{\"text\":\"Updates available for these mods:\",\"color\":\"gold\"}]}"));
for (CoreHelper.ModUpdateDate updateDate : updateDateList)
{
event.player.addChatComponentMessage(IChatComponent.Serializer.func_150699_a(String.format("{\"text\":\"\",\"extra\":[{\"text\":\"%s: %s -> %s\"}]}", updateDate.getName(), updateDate.getCurrentVersion(), updateDate.getLatestVersion())));
}
event.player.addChatComponentMessage(IChatComponent.Serializer.func_150699_a("{\"text\":\"\",\"extra\":[{\"text\":\"Download here!\",\"color\":\"gold\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"http://doubledoordev.net\"}},{\"text\":\" <- That is a link btw :p\"}]}"));
}
@SubscribeEvent
public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent eventArgs)
{
for (ModContainer modContainer : Loader.instance().getActiveModList())
{
if (modContainer.getMod() instanceof ID3Mod)
{
((ID3Mod) modContainer.getMod()).syncConfig();
}
}
}
@Override
public void syncConfig()
{
configuration.setCategoryLanguageKey(MODID, "d3.core.config.core").setCategoryComment(MODID, LanguageRegistry.instance().getStringLocalization("d3.core.config.core"));
debug = configuration.getBoolean("debug", MODID, debug, "Enable debug mode", "d3.core.config.debug");
sillyness = configuration.getBoolean("sillyness", MODID, sillyness, "Enable sillyness", "d3.core.config.sillyness");
updateWarning = configuration.getBoolean("updateWarning", MODID, updateWarning, "Allow update warnings on login", "d3.core.config.updateWarning");
if (sillyness) MinecraftForge.EVENT_BUS.register(getDevPerks());
else MinecraftForge.EVENT_BUS.unregister(getDevPerks());
if (configuration.hasChanged()) configuration.save();
}
@Override
public void addConfigElements(List<IConfigElement> list)
{
list.add(new ConfigElement(configuration.getCategory(MODID.toLowerCase())));
}
public static Logger getLogger()
{
return instance.logger;
}
public static boolean debug()
{
return instance.debug;
}
public static Configuration getConfiguration()
{
return instance.configuration;
}
public static DevPerks getDevPerks()
{
if (instance.devPerks == null) instance.devPerks = new DevPerks();
return instance.devPerks;
}
}