Skip to content

Commit

Permalink
Merge branch 'refs/heads/refactor/submodules' into dev/1.20.1
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jun 22, 2024
2 parents 7e13711 + f6b248e commit f79ceb0
Show file tree
Hide file tree
Showing 59 changed files with 428 additions and 950 deletions.
3 changes: 3 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ description = "Core code shared by all versions"

dependencies {
implementation project(":smylib:core")
implementation 'org.apache.logging.log4j:log4j-api:2.8.1'
implementation 'com.google.code.gson:gson:2.11.0'
implementation 'javax.xml.bind:jaxb-api:2.3.1'

testImplementation project(":smylib:testing")
}
31 changes: 31 additions & 0 deletions core/src/main/java/net/smyler/terramap/Terramap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.smyler.terramap;

import com.google.gson.Gson;
import net.smyler.terramap.http.HttpClient;
import org.apache.logging.log4j.Logger;

public interface Terramap {

static Terramap instance() {
return InstanceHolder.instance;
}

String MOD_ID = "terramap";

Logger logger();

HttpClient http();

Gson gson();

Gson gsonPretty();

class InstanceHolder {
private static Terramap instance;
public static void setInstance(Terramap instance) {
instance.logger().info("Setting Terramap instance of class {}", instance.getClass().getName());
InstanceHolder.instance = instance;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

public final class TerramapResources {

public static final String MOD_ID = "terramap";
public static final Identifier TEXTURES = new Identifier(MOD_ID, "textures");
public static final Identifier TEXTURES = new Identifier(Terramap.MOD_ID, "textures");
public static final Identifier GUI_TEXTURES = TEXTURES.resolve("gui");
public static final Identifier SPRITES = GUI_TEXTURES.resolve("sprites");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.thesmyler.terramap.files.kml;
package net.smyler.terramap.files.kml;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fr.thesmyler.terramap.files.kml;
package net.smyler.terramap.files.kml;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
Expand All @@ -13,10 +15,8 @@
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import net.smyler.terramap.Terramap;

import fr.thesmyler.terramap.TerramapMod;

@XmlRootElement(name="kml")
public class KmlFile {
Expand All @@ -39,19 +39,18 @@ public void save(File file, boolean compressed) throws IOException {
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
try(OutputStream stream = Files.newOutputStream(file.toPath())) {
if(compressed) {
try(ZipArchiveOutputStream compressedStream = new ZipArchiveOutputStream(stream)) {
ZipArchiveEntry entry = new ZipArchiveEntry("terramap.kml");
compressedStream.putArchiveEntry(entry);
try(ZipOutputStream compressedStream = new ZipOutputStream(stream)) {
ZipEntry entry = new ZipEntry("terramap.kml");
compressedStream.putNextEntry(entry);
marshaller.marshal(this, compressedStream);
compressedStream.closeArchiveEntry();
}
} else {
marshaller.marshal(this, stream);
}
}
} catch(JAXBException e) {
TerramapMod.logger.error("Something went seriously wrong when saving a kml file. Save aborted.");
TerramapMod.logger.catching(e);
Terramap.instance().logger().error("Something went seriously wrong when saving a kml file. Save aborted.");
Terramap.instance().logger().catching(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.thesmyler.terramap.files.kml;
package net.smyler.terramap.files.kml;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.thesmyler.terramap.files.kml;
package net.smyler.terramap.files.kml;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
Expand Down
23 changes: 23 additions & 0 deletions core/src/main/java/net/smyler/terramap/http/HttpClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.smyler.terramap.http;

import net.smyler.terramap.Terramap;

import java.util.concurrent.CompletableFuture;

/**
* Asynchronous HTTP client abstraction.
* The implementation should focus on implementing
* efficient GET requests with compliant caching.
* Implementations should support cancellation of the CompletableFuture.
* <br>
* Access the singleton from {@link Terramap#http()}
*
* @author Smyler
*/
public interface HttpClient {

CompletableFuture<byte[]> get(String url);

void setMaxConcurrentRequests(String host, int maxConcurrentRequests);

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.thesmyler.terramap.util;
package net.smyler.terramap.util;

import net.smyler.smylib.text.Text;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fr.thesmyler.terramap.util.geo;
package net.smyler.terramap.util.geo;

import java.awt.Desktop;
import java.io.File;
Expand All @@ -14,12 +14,11 @@
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import fr.thesmyler.terramap.TerramapMod;
import fr.thesmyler.terramap.files.kml.KmlDocument;
import fr.thesmyler.terramap.files.kml.KmlFile;
import fr.thesmyler.terramap.files.kml.KmlPlacemark;
import fr.thesmyler.terramap.files.kml.KmlPoint;
import net.smyler.terramap.util.geo.GeoUtil;
import net.smyler.terramap.files.kml.KmlDocument;
import net.smyler.terramap.files.kml.KmlFile;
import net.smyler.terramap.files.kml.KmlPlacemark;
import net.smyler.terramap.files.kml.KmlPoint;
import net.smyler.terramap.Terramap;

/**
* Utility class to open various geo services such as osm, Google Maps, Earth, etc...
Expand Down Expand Up @@ -65,8 +64,8 @@ public static String formatStringWithCoords(String str, int zoomLevel, double lo
.replace("{longitude}", dispLong)
.replace("{place}", dispPlace);
} catch (UnsupportedEncodingException e) {
TerramapMod.logger.error("Failed to format a string with coordinates: ");
TerramapMod.logger.catching(e);
Terramap.instance().logger().error("Failed to format a string with coordinates: ");
Terramap.instance().logger().catching(e);
}
return str;
}
Expand Down Expand Up @@ -124,8 +123,8 @@ public static void openInGoogleEarthPro(double lon, double lat) {
Desktop.getDesktop().open(file);
file.deleteOnExit();
} catch(Exception e) {
TerramapMod.logger.error("There was an error when trying to open a place in Google Earth");
TerramapMod.logger.catching(e);
Terramap.instance().logger().error("There was an error when trying to open a place in Google Earth");
Terramap.instance().logger().catching(e);
}
}

Expand All @@ -134,10 +133,10 @@ public static void openURI(String uriStr) {
URI uri = new URI(uriStr);
Desktop.getDesktop().browse(uri);
} catch (IOException | UnsupportedOperationException e) {
TerramapMod.logger.error("Failed to open uri: " + uriStr);
TerramapMod.logger.catching(e);
Terramap.instance().logger().error("Failed to open uri: {}", uriStr);
Terramap.instance().logger().catching(e);
} catch (URISyntaxException e) {
TerramapMod.logger.error("Tried to open a malformed URI: " + uriStr);
Terramap.instance().logger().error("Tried to open a malformed URI: {}", uriStr);
}

}
Expand Down
42 changes: 38 additions & 4 deletions fabric/src/main/java/net/smyler/terramap/TerramapFabricMod.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
package net.smyler.terramap;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.api.ModInitializer;
import net.minecraft.client.Minecraft;
import net.smyler.smylib.SmyLib;
import net.smyler.smylib.game.WrappedMinecraft;
import net.smyler.smylib.json.TextJsonAdapter;
import net.smyler.smylib.text.Text;
import net.smyler.terramap.http.HttpClient;
import org.apache.logging.log4j.Logger;

import static org.apache.logging.log4j.LogManager.getLogger;

public class TerramapFabricMod implements ModInitializer {
public class TerramapFabricMod implements ModInitializer, Terramap {

private final Logger logger = getLogger("terramap");
private final Gson gson = new GsonBuilder()
.registerTypeAdapter(Text.class, new TextJsonAdapter())
.create();
private final Gson gsonPretty = new GsonBuilder()
.registerTypeAdapter(Text.class, new TextJsonAdapter())
.setPrettyPrinting()
.create();

public static final Logger LOGGER = getLogger("terramap");
@Override
public void onInitialize() {
LOGGER.info("Initializing Terramap");
SmyLib.initializeGameClient(new WrappedMinecraft(Minecraft.getInstance()), LOGGER);
Terramap.InstanceHolder.setInstance(this);
this.logger.info("Initializing Terramap");
SmyLib.initializeGameClient(new WrappedMinecraft(Minecraft.getInstance()), this.logger);
}

@Override
public Logger logger() {
return this.logger;
}

@Override
public HttpClient http() {
return null;
}

@Override
public Gson gson() {
return this.gson;
}

@Override
public Gson gsonPretty() {
return this.gsonPretty;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.thesmyler.smylibgui;

import fr.thesmyler.terramap.TerramapMod;
import net.minecraft.util.ResourceLocation;
import net.smyler.terramap.Terramap;

public final class SmyLibGuiTextures {

Expand All @@ -10,7 +10,7 @@ private SmyLibGuiTextures() {}
public static final ResourceLocation WIDGET_TEXTURES;

static {
WIDGET_TEXTURES = new ResourceLocation(TerramapMod.MODID, "textures/gui/widgets.png");
WIDGET_TEXTURES = new ResourceLocation(Terramap.MOD_ID, "textures/gui/widgets.png");
}

}
16 changes: 8 additions & 8 deletions forge/src/main/java/fr/thesmyler/smylibgui/util/Cursors.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.nio.IntBuffer;

import net.smyler.terramap.Terramap;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Cursor;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;

import fr.thesmyler.terramap.TerramapMod;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.ITextureObject;
import net.minecraft.util.ResourceLocation;
Expand All @@ -19,11 +19,11 @@
//TODO Have a wrapper class for lwjgl cursors
public final class Cursors {

public static final Cursor CURSOR_MOVE = loadCursorFromTexture(new ResourceLocation(TerramapMod.MODID, "textures/gui/cursors/move.png"), 32, 32);
public static final Cursor CURSOR_RESIZE_VERTICAL = loadCursorFromTexture(new ResourceLocation(TerramapMod.MODID, "textures/gui/cursors/resize_vertical.png"), 16, 32);
public static final Cursor CURSOR_RESIZE_HORIZONTAL = loadCursorFromTexture(new ResourceLocation(TerramapMod.MODID, "textures/gui/cursors/resize_horizontal.png"), 32, 16);
public static final Cursor CURSOR_RESIZE_DIAGONAL_1 = loadCursorFromTexture(new ResourceLocation(TerramapMod.MODID, "textures/gui/cursors/resize_diag1.png"), 32, 32);
public static final Cursor CURSOR_RESIZE_DIAGONAL_2 = loadCursorFromTexture(new ResourceLocation(TerramapMod.MODID, "textures/gui/cursors/resize_diag2.png"), 32, 32);
public static final Cursor CURSOR_MOVE = loadCursorFromTexture(new ResourceLocation(Terramap.MOD_ID, "textures/gui/cursors/move.png"), 32, 32);
public static final Cursor CURSOR_RESIZE_VERTICAL = loadCursorFromTexture(new ResourceLocation(Terramap.MOD_ID, "textures/gui/cursors/resize_vertical.png"), 16, 32);
public static final Cursor CURSOR_RESIZE_HORIZONTAL = loadCursorFromTexture(new ResourceLocation(Terramap.MOD_ID, "textures/gui/cursors/resize_horizontal.png"), 32, 16);
public static final Cursor CURSOR_RESIZE_DIAGONAL_1 = loadCursorFromTexture(new ResourceLocation(Terramap.MOD_ID, "textures/gui/cursors/resize_diag1.png"), 32, 32);
public static final Cursor CURSOR_RESIZE_DIAGONAL_2 = loadCursorFromTexture(new ResourceLocation(Terramap.MOD_ID, "textures/gui/cursors/resize_diag2.png"), 32, 32);

static {
if(Minecraft.IS_RUNNING_ON_MAC) {
Expand Down Expand Up @@ -66,8 +66,8 @@ private static Cursor loadCursorFromTexture(ResourceLocation loc, int hotX, int
try {
return new Cursor(width, height, hotX, hotY, 1, buffer, BufferUtils.createIntBuffer(1));
} catch (LWJGLException e) {
TerramapMod.logger.error("Failed to load a cutom cursor for " + loc);
TerramapMod.logger.catching(e);
Terramap.instance().logger().error("Failed to load a custom cursor for {}", loc);
Terramap.instance().logger().catching(e);
}
}
return Mouse.getNativeCursor();
Expand Down
Loading

0 comments on commit f79ceb0

Please sign in to comment.