Skip to content

Commit

Permalink
Use Text instead of TextComponent for copyrights
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Feb 23, 2024
1 parent 511cce7 commit f874bda
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
import net.smyler.smylib.Color;
import net.smyler.smylib.gui.widgets.Widget;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.text.ITextComponent;
import net.smyler.smylib.gui.Font;

import static net.smyler.smylib.text.ImmutableText.ofPlainText;

public class TextWidget implements Widget {

protected Text text;
Expand Down Expand Up @@ -53,11 +50,6 @@ public TextWidget(float x, float y, int z, TextAlignment alignment, Font font) {
this(x, y, z, Float.MAX_VALUE, ImmutableText.EMPTY, alignment, Color.WHITE, true, font);
}

@Deprecated
public TextWidget(float x, float y, int z, ITextComponent text, Font font) {
this(x, y, z, ofPlainText(text.getUnformattedText()), font);
}

public TextWidget(float x, float y, int z, Text text, Font font) {
this(x, y, z, text, TextAlignment.RIGHT, font);
}
Expand Down Expand Up @@ -170,12 +162,6 @@ public Text getText() {
return this.text;
}

@Deprecated
public TextWidget setText(ITextComponent component) {
//FIXME do not use ITextComponent at all in TextWidget
return this.setText(ofPlainText(component.getUnformattedText()));
}

public TextWidget setText(Text text) {
this.text = text;
this.updateCoords();
Expand Down
18 changes: 18 additions & 0 deletions forge/src/main/java/fr/thesmyler/terramap/TerramapMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import java.io.File;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import fr.thesmyler.terramap.util.json.EarthGeneratorSettingsAdapter;
import net.buildtheearth.terraplusplus.generator.EarthGenerator;
import net.buildtheearth.terraplusplus.generator.EarthGeneratorSettings;
import net.smyler.smylib.json.TextJsonAdapter;
import net.smyler.smylib.text.Text;
import org.apache.logging.log4j.Logger;

import fr.thesmyler.terramap.TerramapVersion.InvalidVersionString;
Expand Down Expand Up @@ -37,6 +44,17 @@ public class TerramapMod {

public static Logger logger;

public static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(EarthGeneratorSettings.class, new EarthGeneratorSettingsAdapter())
.registerTypeAdapter(Text.class, new TextJsonAdapter())
.create();

public static final Gson GSON_PRETTY = new GsonBuilder()
.registerTypeAdapter(EarthGeneratorSettings.class, new EarthGeneratorSettingsAdapter())
.registerTypeAdapter(Text.class, new TextJsonAdapter())
.setPrettyPrinting()
.create();

/* Proxy things */
private static final String CLIENT_PROXY_CLASS = "fr.thesmyler.terramap.proxy.TerramapClientProxy";
private static final String SERVER_PROXY_CLASS = "fr.thesmyler.terramap.proxy.TerramapServerProxy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
import fr.thesmyler.terramap.gui.widgets.markers.markers.entities.MainPlayerMarker;
import fr.thesmyler.terramap.util.CopyrightHolder;
import net.minecraft.profiler.Profiler;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.smyler.smylib.text.ImmutableText;
import net.smyler.smylib.text.Text;
import net.smyler.terramap.util.geo.GeoPoint;
import net.smyler.terramap.util.geo.GeoPointMutable;
import net.smyler.terramap.util.geo.GeoPointReadOnly;

import static java.util.Comparator.comparingInt;
import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.smylib.text.ImmutableText.of;
import static net.smyler.smylib.text.ImmutableText.ofPlainText;

/**
Expand Down Expand Up @@ -397,12 +397,15 @@ private void updateMarkers(float mouseX, float mouseY) {
}

public void updateCopyright() {
ITextComponent component = new TextComponentString("");
ImmutableText component = ImmutableText.EMPTY;
ImmutableText separator = ofPlainText(" | ");
for(Widget widget: this.widgets)
if(widget instanceof CopyrightHolder){
if(!component.getFormattedText().isEmpty()) component.appendText(" | ");
ITextComponent copyright = ((CopyrightHolder)widget).getCopyright(getGameClient().translator().language());
component.appendSibling(copyright);
if(!component.getFormattedText().isEmpty()) {
component = component.withNewSiblings(separator);
}
Text copyright = ((CopyrightHolder)widget).getCopyright(getGameClient().translator().language());
component = component.withNewSiblings(of(copyright));
}
this.copyright.setText(component);
this.copyright.setVisibility(!component.getFormattedText().isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
import fr.thesmyler.terramap.maps.raster.RasterTiledMap;
import fr.thesmyler.terramap.maps.raster.imp.ColorTiledMap;
import fr.thesmyler.terramap.util.CopyrightHolder;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.smyler.smylib.game.GameClient;
import net.smyler.smylib.gui.DrawContext;
import net.smyler.smylib.gui.Font;

import net.smyler.smylib.text.ImmutableText;
import net.smyler.smylib.text.Text;
import org.jetbrains.annotations.Nullable;
import java.util.List;
Expand Down Expand Up @@ -48,11 +47,11 @@ public void setTiledMap(RasterTiledMap map) {
}

@Override
public ITextComponent getCopyright(String localeKey) {
public Text getCopyright(String localeKey) {
if(this.tiledMap instanceof CopyrightHolder) {
return ((CopyrightHolder)this.tiledMap).getCopyright(localeKey);
}
return new TextComponentString("");
return ImmutableText.EMPTY;
}

@Override
Expand Down Expand Up @@ -131,7 +130,7 @@ public StyleEntry(RasterTiledMap style) {
this.addWidget(this.infoText);
if (style instanceof CopyrightHolder) {
CopyrightHolder copyrightHolder = (CopyrightHolder) style;
ITextComponent copyright = copyrightHolder.getCopyright(language);
Text copyright = copyrightHolder.getCopyright(language);
y += this.infoText.getHeight() + margin;
this.copyrightText = new TextWidget(
this.infoText.getX(), y, 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import fr.thesmyler.terramap.TerramapMod;
import fr.thesmyler.terramap.TerramapConfig;
import fr.thesmyler.terramap.maps.raster.imp.UrlTiledMap;
import net.smyler.smylib.text.Text;
import net.smyler.terramap.util.geo.WebMercatorBounds;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import net.buildtheearth.terraplusplus.util.http.Http;

import static fr.thesmyler.terramap.TerramapMod.GSON;
import static fr.thesmyler.terramap.TerramapMod.GSON_PRETTY;

public class MapStylesLibrary {

private static final String BUILT_IN_MAPS = "assets/terramap/mapstyles.json";
Expand Down Expand Up @@ -153,9 +154,7 @@ public static void loadFromConfigFile() {
try {
TerramapMod.logger.debug("Map config file did not exist, creating a blank one.");
MapStyleFile mapFile = new MapStyleFile(new MapFileMetadata(0, "Add custom map styles here. See an example at styles.terramap.thesmyler.fr (open in your browser, do not add http or https prefix)"));
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Files.write(configMapsFile.toPath(), builder.create().toJson(mapFile).getBytes(Charset.defaultCharset()));
Files.write(configMapsFile.toPath(), GSON_PRETTY.toJson(mapFile).getBytes(Charset.defaultCharset()));
} catch (IOException e) {
TerramapMod.logger.error("Failed to create map style config file!");
TerramapMod.logger.catching(e);
Expand Down Expand Up @@ -232,8 +231,7 @@ private static Map<String, UrlTiledMap> loadFromFile(File file, TiledMapProvider
}

private static Map<String, UrlTiledMap> loadFromJson(String json, TiledMapProvider provider) {
Gson gson = new Gson();
MapStyleFile savedStyles = gson.fromJson(json, MapStyleFile.class);
MapStyleFile savedStyles = GSON.fromJson(json, MapStyleFile.class);
Map<String, UrlTiledMap> styles = new HashMap<>();
for(String id: savedStyles.maps.keySet()) {
UrlTiledMap style = readFromSaved(id, savedStyles.maps.get(id), provider, savedStyles.metadata.version, savedStyles.metadata.comment);
Expand Down Expand Up @@ -272,7 +270,7 @@ private static class SavedMapStyle {
String url; // Used by legacy versions
String[] urls;
Map<String, String> name;
Map<String, String> copyright;
Map<String, Text> copyright;
int min_zoom;
int max_zoom;
int display_priority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import fr.thesmyler.terramap.maps.raster.TiledMapProvider;
import fr.thesmyler.terramap.network.SP2CMapStylePacket;
import fr.thesmyler.terramap.util.CopyrightHolder;
import net.smyler.smylib.text.Text;
import net.smyler.terramap.util.ImageUtil;
import net.smyler.terramap.util.geo.TilePosImmutable;
import net.smyler.terramap.util.geo.WebMercatorBounds;
Expand Down Expand Up @@ -42,14 +43,13 @@ public class UrlTiledMap extends CachingRasterTiledMap<UrlRasterTile> implements
private final String id;
private final TiledMapProvider provider;
private final Map<String, String> names; // A map of language key => name
private final Map<String, String> copyrightJsons;
private final Map<String, Text> copyrightJsons;
private final long version;
private final String comment;
private final int maxConcurrentRequests; // How many concurrent http connections are allowed by this map provider. This should be two by default, as that's what OSM requires
private final boolean debug;
private final Map<Integer, WebMercatorBounds> bounds;

private static final ITextComponent FALLBACK_COPYRIGHT = ITextComponent.Serializer.jsonToComponent("{\"text\":\"The text component for this copyright notice was malformatted!\",\"color\":\"dark_red\"}");
private ResourceLocation errorTileTexture = null;

public UrlTiledMap(
Expand All @@ -58,7 +58,7 @@ public UrlTiledMap(
int maxZoom,
String id,
Map<String, String> names,
Map<String, String> copyright,
Map<String, Text> copyright,
int displayPriority,
boolean allowOnMinimap,
TiledMapProvider provider,
Expand Down Expand Up @@ -169,25 +169,14 @@ public String getId() {
* @return a copyright as a {@link ITextComponent}, translated to the appropriate language.
*/
@Override
public ITextComponent getCopyright(String localeKey) {
String result = this.copyrightJsons.getOrDefault(localeKey, this.copyrightJsons.get("en_us"));
if(result == null) {
return FALLBACK_COPYRIGHT;
} else {
try {
return ITextComponent.Serializer.jsonToComponent(result);
} catch (Exception e) {
TerramapMod.logger.error("Copyright notice json failed to be parsing!");
TerramapMod.logger.catching(e);
return FALLBACK_COPYRIGHT;
}
}
public Text getCopyright(String localeKey) {
return this.copyrightJsons.getOrDefault(localeKey, this.copyrightJsons.get("en_us"));
}

/**
* @return the language key => copyright json value map for this map
*/
public Map<String, String> getUnlocalizedCopyrights() {
public Map<String, Text> getUnlocalizedCopyrights() {
return this.copyrightJsons;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.HashMap;
import java.util.Map;

import com.google.gson.JsonParseException;
import net.smyler.smylib.text.Text;
import org.apache.logging.log4j.util.Strings;

import fr.thesmyler.terramap.TerramapClientContext;
Expand All @@ -17,13 +19,15 @@
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import static fr.thesmyler.terramap.TerramapMod.GSON;

public class SP2CMapStylePacket implements IMessage {

private String id;
private long providerVersion;
private String[] urlPatterns;
private Map<String, String> names;
private Map<String, String> copyrights;
private Map<String, Text> copyrights;
private int minZoom;
private int maxZoom;
private int displayPriority;
Expand Down Expand Up @@ -71,11 +75,16 @@ public void fromBytes(ByteBuf buf) {
}
this.names = names;
int copyrightCount = buf.readInt();
Map<String, String> copyrights = new HashMap<>();
Map<String, Text> copyrights = new HashMap<>();
for(int i=0; i < copyrightCount; i++) {
String key = NetworkUtil.decodeStringFromByteBuf(buf);
String copyright = NetworkUtil.decodeStringFromByteBuf(buf);
copyrights.put(key, copyright);
String copyrightJson = NetworkUtil.decodeStringFromByteBuf(buf);
try {
Text copyright = GSON.fromJson(copyrightJson, Text.class);
copyrights.put(key, copyright);
} catch (JsonParseException e) {
TerramapMod.logger.warn("Received invalid map style copyright from server.");
}
}
this.copyrights = copyrights;
this.minZoom = buf.readInt();
Expand Down Expand Up @@ -120,7 +129,7 @@ public void toBytes(ByteBuf buf) {
buf.writeInt(this.copyrights.size());
for(String key: this.copyrights.keySet()) {
NetworkUtil.encodeStringToByteBuf(key, buf);
NetworkUtil.encodeStringToByteBuf(this.copyrights.get(key), buf);
NetworkUtil.encodeStringToByteBuf(GSON.toJson(this.copyrights.get(key)), buf);
}
buf.writeInt(this.minZoom);
buf.writeInt(this.maxZoom);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fr.thesmyler.terramap.util;

import net.minecraft.util.text.ITextComponent;
import net.smyler.smylib.text.Text;

public interface CopyrightHolder {

Expand All @@ -9,8 +9,8 @@ public interface CopyrightHolder {
* or English if it isn't available.
*
* @param localeKey - the language key to get the copyright for
* @return a copyright as a {@link ITextComponent}, translated to the appropriate language.
* @return a copyright as a {@link Text}, translated to the appropriate language.
*/
ITextComponent getCopyright(String localeKey);
Text getCopyright(String localeKey);

}
16 changes: 8 additions & 8 deletions forge/src/main/resources/assets/terramap/mapstyles.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"metadata": {
"version": 15,
"version": 16,
"comment": "This file contains the tile servers included by default in Terramap's jar as a fallback to the online version, and is maintained by SmylerMC."
},
"maps": {
Expand All @@ -22,10 +22,10 @@
"ko_kr": "OpenStreetMap"
},
"copyright": {
"en_us": "[\"\",{\"text\":\"© \",\"color\":\"white\"},{\"text\":\"OpenStreetMap\",\"underlined\":true,\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.openstreetmap.org\"}},{\"text\":\" contributors\",\"color\":\"white\"}]",
"fr_fr": "[\"\",{\"text\":\"© Contributeurs de \",\"color\":\"white\"},{\"text\":\"OpenStreetMap\",\"underlined\":true,\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.openstreetmap.org\"}}]",
"cz_cz": "[\"\",{\"text\":\"© \"},{\"text\":\"p\\u0159isp\\u011bvatelé OpenStreetMap\",\"underlined\":true,\"color\":\"dark_aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.openstreetmap.org\"}}]",
"sk_sk": "[\"\",{\"text\":\"© \"},{\"text\":\"prispievatelia OpenStreetMap\",\"underlined\":true,\"color\":\"dark_aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.openstreetmap.org\"}}]"
"en_us": ["",{"text":"© ","color":"white"},{ "text":"OpenStreetMap","underlined":true,"color":"aqua","clickEvent":{"action":"open_url","value":"https://www.openstreetmap.org"}},{"text":" contributors","color":"white"}],
"fr_fr": ["",{"text":"© Contributeurs de ","color":"white"},{"text":"OpenStreetMap","underlined":true,"color":"aqua","clickEvent":{"action":"open_url","value":"https://www.openstreetmap.org"}}],
"cz_cz": ["",{"text":"© "},{"text":"p\u0159isp\u011bvatelé OpenStreetMap","underlined":true,"color":"dark_aqua","clickEvent":{"action":"open_url","value":"https://www.openstreetmap.org"}}],
"sk_sk": ["",{"text":"© "},{"text":"prispievatelia OpenStreetMap","underlined":true,"color":"dark_aqua","clickEvent":{"action":"open_url","value":"https://www.openstreetmap.org"}}]
},
"min_zoom": 0,
"max_zoom": 19,
Expand All @@ -51,9 +51,9 @@
"ko_kr": "Humanitarian"
},
"copyright": {
"en_us": "[\"\",{\"text\":\"© \"},{\"text\":\"OpenStreetMap contributors\",\"underlined\":true,\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.openstreetmap.org/copyright\"}},{\"text\":\". Tiles style by \"},{\"text\":\"Humanitarian OpenStreetMap Team\",\"underlined\":true,\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.hotosm.org/\"}},{\"text\":\" hosted by \"},{\"text\":\"OpenStreetMap France\",\"underlined\":true,\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://www.openstreetmap.fr/\"}}]",
"fr_fr": "[\"\",{\"text\":\"Données ©; \"},{\"text\":\"OpenStreetMap\",\"underlined\":true,\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://osm.org/copyright\"}},{\"text\":\"/ODbL - Tiles courtesy of \"},{\"text\":\"Humanitarian OpenStreetMap Team\",\"underlined\":true,\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://hot.openstreetmap.org/\"}}]",
"es_es": "[\"\",{\"text\":\"Datos ©; \"},{\"text\":\"OpenStreetMap\",\"underlined\":true,\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://osm.org/copyright\"}},{\"text\":\"/ODbL - Losetas cortesía de \"},{\"text\":\"Humanitarian OpenStreetMap Team\",\"underlined\":true,\"color\":\"aqua\",\"clickEvent\":{\"action\":\"open_url\",\"value\":\"https://hot.openstreetmap.org/\"}}]"
"en_us": ["",{"text":"© "},{"text":"OpenStreetMap contributors","underlined":true,"color":"aqua","clickEvent":{"action":"open_url","value":"https://www.openstreetmap.org/copyright"}},{"text":". Tiles style by "},{"text":"Humanitarian OpenStreetMap Team","underlined":true,"color":"aqua","clickEvent":{"action":"open_url","value":"https://www.hotosm.org/"}},{"text":" hosted by "},{"text":"OpenStreetMap France","underlined":true,"color":"aqua","clickEvent":{"action":"open_url","value":"https://www.openstreetmap.fr/"}}],
"fr_fr": ["",{"text":"Données ©; "},{"text":"OpenStreetMap","underlined":true,"color":"aqua","clickEvent":{"action":"open_url","value":"https://osm.org/copyright"}},{"text":"/ODbL - Tiles courtesy of "},{"text":"Humanitarian OpenStreetMap Team","underlined":true,"color":"aqua","clickEvent":{"action":"open_url","value":"https://hot.openstreetmap.org/"}}],
"es_es": ["",{"text":"Datos ©; "},{"text":"OpenStreetMap","underlined":true,"color":"aqua","clickEvent":{"action":"open_url","value":"https://osm.org/copyright"}},{"text":"/ODbL - Losetas cortesía de "},{"text":"Humanitarian OpenStreetMap Team","underlined":true,"color":"aqua","clickEvent":{"action":"open_url","value":"https://hot.openstreetmap.org/"}}]
},
"min_zoom": 0,
"max_zoom": 19,
Expand Down

0 comments on commit f874bda

Please sign in to comment.