Add online (aka ReplayCenter) module
Add localization extra (formally known as LocalizedResourcePack) Add version checker extra
This commit is contained in:
117
src/main/java/com/replaymod/extras/LocalizationExtra.java
Normal file
117
src/main/java/com/replaymod/extras/LocalizationExtra.java
Normal file
@@ -0,0 +1,117 @@
|
||||
package com.replaymod.extras;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.replaymod.core.ReplayMod;
|
||||
import com.replaymod.online.ReplayModOnline;
|
||||
import com.replaymod.online.api.ApiClient;
|
||||
import com.replaymod.online.api.ApiException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.resources.IResourcePack;
|
||||
import net.minecraft.client.resources.data.IMetadataSection;
|
||||
import net.minecraft.client.resources.data.IMetadataSerializer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.ConnectException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class LocalizationExtra implements Extra {
|
||||
@Mod.Instance(ReplayModOnline.MOD_ID)
|
||||
private static ReplayModOnline module;
|
||||
|
||||
@Override
|
||||
public void register(ReplayMod mod) throws Exception {
|
||||
final Minecraft mc = mod.getMinecraft();
|
||||
Thread localizedResourcePackLoader = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<IResourcePack> defaultResourcePacks = mc.defaultResourcePacks;
|
||||
defaultResourcePacks.add(new LocalizedResourcePack(module.getApiClient()));
|
||||
mc.addScheduledTask(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mc.refreshResources();
|
||||
}
|
||||
});
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, "localizedResourcePackLoader");
|
||||
localizedResourcePackLoader.start();
|
||||
}
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public static class LocalizedResourcePack implements IResourcePack {
|
||||
private final ApiClient apiClient;
|
||||
private Map<String, String> availableLanguages = new HashMap<>();
|
||||
private boolean websiteAvailable = true;
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream(ResourceLocation loc) {
|
||||
if(!loc.getResourcePath().endsWith(".lang")) return null;
|
||||
String langcode = loc.getResourcePath().split("/")[1].split("\\.")[0];
|
||||
if(availableLanguages.containsKey(langcode)) return new ByteArrayInputStream(availableLanguages.get(langcode).getBytes(Charsets.UTF_8));
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean resourceExists(ResourceLocation loc) {
|
||||
if(!(loc.getResourcePath().endsWith(".lang"))) return false;
|
||||
String langcode = loc.getResourcePath().split("/")[1].split("\\.")[0];
|
||||
if(availableLanguages.containsKey(langcode)) return true;
|
||||
if(!websiteAvailable) return false;
|
||||
try {
|
||||
if (Boolean.parseBoolean(System.getProperty("replaymod.offline", "false"))) {
|
||||
return false;
|
||||
}
|
||||
String lang = apiClient.getTranslation(langcode);
|
||||
String prop = StringEscapeUtils.unescapeHtml4(lang);
|
||||
availableLanguages.put(langcode, prop);
|
||||
return true;
|
||||
} catch (ApiException e) {
|
||||
if (e.getError().getId() != 16) { // This language has not been translated
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch(ConnectException ce) {
|
||||
websiteAvailable = false;
|
||||
ce.printStackTrace();
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set getResourceDomains() {
|
||||
return ImmutableSet.of("minecraft", "replaymod");
|
||||
}
|
||||
|
||||
@Override
|
||||
public IMetadataSection getPackMetadata(IMetadataSerializer p_135058_1_, String p_135058_2_) throws IOException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedImage getPackImage() throws IOException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPackName() {
|
||||
return "ReplayModLocalizationResourcePack";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,9 @@ public class ReplayModExtras {
|
||||
PlayerOverview.class,
|
||||
UriSchemeExtra.class,
|
||||
FullBrightness.class,
|
||||
HotkeyButtons.class
|
||||
HotkeyButtons.class,
|
||||
LocalizationExtra.class,
|
||||
VersionChecker.class
|
||||
);
|
||||
|
||||
private Logger logger;
|
||||
|
||||
64
src/main/java/com/replaymod/extras/VersionChecker.java
Normal file
64
src/main/java/com/replaymod/extras/VersionChecker.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package com.replaymod.extras;
|
||||
|
||||
import com.replaymod.core.ReplayMod;
|
||||
import com.replaymod.online.ReplayModOnline;
|
||||
import eu.crushedpixel.replaymod.utils.StringUtils;
|
||||
import net.minecraft.client.gui.Gui;
|
||||
import net.minecraft.client.gui.GuiMainMenu;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraftforge.client.event.GuiScreenEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class VersionChecker implements Extra {
|
||||
@Mod.Instance(ReplayModOnline.MOD_ID)
|
||||
private static ReplayModOnline module;
|
||||
|
||||
@Override
|
||||
public void register(ReplayMod mod) throws Exception {
|
||||
final String currentVersion = mod.getVersion();
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
boolean upToDate = module.getApiClient().isVersionUpToDate(currentVersion);
|
||||
if (!upToDate) {
|
||||
MinecraftForge.EVENT_BUS.register(VersionChecker.this);
|
||||
}
|
||||
} catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, "ReplayMod-VersionChecker").start();
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onDrawScreen(GuiScreenEvent.DrawScreenEvent.Post event) {
|
||||
if (!(event.gui instanceof GuiMainMenu)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int width = Math.max(100, event.gui.width / 2 - 100 - 10);
|
||||
|
||||
String[] lines = StringUtils.splitStringInMultipleRows(I18n.format("replaymod.gui.outdated"), width);
|
||||
|
||||
int maxLineWidth = 0;
|
||||
for(String line : lines) {
|
||||
int lineWidth = event.gui.mc.fontRendererObj.getStringWidth(line);
|
||||
if(lineWidth > maxLineWidth) {
|
||||
maxLineWidth = lineWidth;
|
||||
}
|
||||
}
|
||||
|
||||
Gui.drawRect(2, 77, 5 + maxLineWidth + 3, 80 + (lines.length * 10), 0x80FF0000);
|
||||
|
||||
int i = 0;
|
||||
for(String line : lines) {
|
||||
event.gui.mc.fontRendererObj.drawStringWithShadow(line, 5, 80 + (i * 10), Color.WHITE.getRGB());
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package com.replaymod.extras.urischeme;
|
||||
|
||||
import com.replaymod.core.ReplayMod;
|
||||
import com.replaymod.extras.Extra;
|
||||
import com.replaymod.online.ReplayModOnline;
|
||||
import de.johni0702.minecraft.gui.container.GuiScreen;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -10,6 +13,9 @@ import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class UriSchemeExtra implements Extra {
|
||||
@Mod.Instance(ReplayModOnline.MOD_ID)
|
||||
private static ReplayModOnline module;
|
||||
|
||||
private ReplayMod mod;
|
||||
|
||||
@Override
|
||||
@@ -72,17 +78,10 @@ public class UriSchemeExtra implements Extra {
|
||||
}
|
||||
|
||||
private void loadReplay(int id) {
|
||||
// TODO
|
||||
// File file = ReplayMod.downloadedFileHandler.getFileForID(id);
|
||||
// if (file == null) {
|
||||
// FileInfo info = new FileInfo(id, null, null, null, 0, 0, 0, String.valueOf(id), false, 0);
|
||||
// new GuiReplayDownloading(info).display();
|
||||
// } else {
|
||||
// try {
|
||||
// ReplayHandler.startReplay(file);
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
try {
|
||||
module.startReplay(id, "Replay #" + id, GuiScreen.wrap(null));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user