Skip to content

Commit

Permalink
Merge pull request #10 from Maxwell-lt/1.15
Browse files Browse the repository at this point in the history
Update to Minecraft 1.15
  • Loading branch information
Maxwell-lt authored Jun 23, 2020
2 parents 1452f32 + cb44ddd commit 9ea6b4e
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 164 deletions.
26 changes: 20 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ext.modid = "titlechanger"
ext.modname = "Title Changer"
ext.moddescription = "Changes the Minecraft window title."
ext.modauthors = "maxwell-lt"
ext.modversion = "2.2"
ext.modversion = "3.1.0"
ext.modpackage = "maxwell_lt.titlechanger"
ext.modarchive = "titlechanger"

Expand All @@ -36,7 +36,7 @@ minecraft {
// stable_# Stables are built at the discretion of the MCP team.
// Use non-default mappings at your own risk. they may not always work.
// Simply re-run your setup task after changing the mappings to update your workspace.
mappings channel: 'snapshot', version: '20190719-1.14.3'
mappings channel: 'snapshot', version: '20200622-1.15.1'
// makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.

// accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
Expand All @@ -54,7 +54,7 @@ minecraft {
property 'forge.logging.console.level', 'debug'

mods {
examplemod {
titlechanger {
source sourceSets.main
}
}
Expand All @@ -70,7 +70,7 @@ minecraft {
property 'forge.logging.console.level', 'debug'

mods {
examplemod {
titlechanger {
source sourceSets.main
}
}
Expand All @@ -88,19 +88,29 @@ minecraft {
args '--mod', 'titlechanger', '--all', '--output', file('src/generated/resources/')

mods {
examplemod {
titlechanger {
source sourceSets.main
}
}
}
}
}

repositories {
mavenCentral()
jcenter()
}

dependencies {
// Specify the version of Minecraft to use, If this is any group other then 'net.minecraft' it is assumed
// that the dep is a ForgeGradle 'patcher' dependency. And it's patches will be applied.
// The userdev artifact is a special name and will get all sorts of transformations applied to it.
minecraft 'net.minecraftforge:forge:1.14.4-28.1.0'
minecraft 'net.minecraftforge:forge:1.15.2-31.2.22'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
testCompile 'org.mockito:mockito-core:2.+'
testCompile 'org.assertj:assertj-core:3.11.1'

// You may put jars on which you depend on in ./libs or you may define them like so..
// compile "some.group:artifact:version:classifier"
Expand All @@ -122,6 +132,10 @@ dependencies {

}

test {
useJUnitPlatform()
}

// Example for how to get properties into the manifest for reading by the runtime..
jar {
manifest {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/maxwell_lt/titlechanger/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
public class ClientProxy implements IProxy {
@Override
public void init() {
MinecraftForge.EVENT_BUS.register(new ReplaceTitle());
ReplaceTitle.Replace();
ReplaceTitle replaceTitle = new ReplaceTitle();
MinecraftForge.EVENT_BUS.register(replaceTitle);
}

@Override
Expand Down
68 changes: 0 additions & 68 deletions src/main/java/maxwell_lt/titlechanger/Config.java

This file was deleted.

174 changes: 94 additions & 80 deletions src/main/java/maxwell_lt/titlechanger/ReplaceTitle.java
Original file line number Diff line number Diff line change
@@ -1,118 +1,132 @@
package maxwell_lt.titlechanger;

import maxwell_lt.titlechanger.config.Config;
import maxwell_lt.titlechanger.util.InfoRetriever;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.loading.FMLLoader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import static org.lwjgl.glfw.GLFW.glfwSetWindowTitle;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

import static org.lwjgl.glfw.GLFW.*;

public class ReplaceTitle {
public static final String VERSION = "%mcver%";
public static final String MOD_COUNT = "%modcount%";
public static final String TIME = "%time%";
public static final String PLAYER_LOCATION = "%playerloc%";
public static final String SCORE = "%score%";
public static final String BIOME = "%biome%";
public static final String CHUNK = "%chunk%";

private static final Logger LOGGER = LogManager.getLogger();
private final InfoRetriever infoRetriever;

@SubscribeEvent(priority = EventPriority.LOWEST)
public void clientTick(TickEvent.ClientTickEvent e) {
Replace();
}
private final String mcVersion;
private final String modCount;
private final DateFormat timeFormatter;
private final Map<String, Supplier<String>> transformations;

public static void Replace() {
if (!Config.WINDOW_TITLE.get().equals("")) {
glfwSetWindowTitle(Minecraft.getInstance().mainWindow.getHandle(), processText(Config.WINDOW_TITLE.get()));
}
}
private PlayerEntity playerEntity;
private World world;

private static String processText(String formatString) {
String mcVersion = Minecraft.getInstance().getVersion();
String modCount = Integer.toString(FMLLoader.getLoadingModList().getMods().size());
String time = new SimpleDateFormat(Config.TIME_FORMAT.get()).format(new Date()).toString();
String location = getPlayerLocationOrPlaceholder();
String score = getPlayerScoreOrPlaceholder();
String biome = getPlayerBiomeOrPlaceholder();
String chunk = getPlayerChunkOrPlaceholder();

formatString = formatString.replaceAll("%mcver%", mcVersion);
formatString = formatString.replaceAll("%modcount%", modCount);
formatString = formatString.replaceAll("%time%", time);
formatString = formatString.replaceAll("%playerloc%", location);
formatString = formatString.replaceAll("%score%", score);
formatString = formatString.replaceAll("%biome%", biome);
formatString = formatString.replaceAll("%chunk%", chunk);
public ReplaceTitle() {
this.infoRetriever = new InfoRetriever(Config.getPlaceholderText());
this.mcVersion = Minecraft.getInstance().getVersion();
this.modCount = Integer.toString(FMLLoader.getLoadingModList().getMods().size());
this.timeFormatter = new SimpleDateFormat(Config.getTimeFormat());

return formatString;
this.transformations = generateTransformationMap();
}

private static String getPlayerBiomeOrPlaceholder() {
PlayerEntity playerEntity = null;
World world = null;
try {
playerEntity = TitleChanger.proxy.getClientPlayer();
world = TitleChanger.proxy.getClientWorld();
} catch (IllegalStateException e) {
LOGGER.debug("Attempted to call proxy.getClientPlayer() in serverside code.");
/**
* Builds a map of required string replacements based on the config
* <p>
* This prevents unneeded information retrieval methods from running every client tick. Because the map contains
* suppliers, the output values can vary based on current conditions.
*
* @return Map with patterns and replacement suppliers
*/
private Map<String, Supplier<String>> generateTransformationMap() {
Map<String, Supplier<String>> transformationMap = new HashMap<>();
if (Config.getWindowTitle().contains(VERSION)) {
transformationMap.put(VERSION, () -> mcVersion);
}
if (playerEntity != null && world != null) {
Biome biome = world.getBiome(new BlockPos(playerEntity.posX, playerEntity.posY, playerEntity.posZ));
return biome.getDisplayName().getString();
} else {
return Config.PLACEHOLDER_TEXT.get();
if (Config.getWindowTitle().contains(MOD_COUNT)) {
transformationMap.put(MOD_COUNT, () -> modCount);
}
}

private static String getPlayerScoreOrPlaceholder() {
PlayerEntity playerEntity = null;
try {
playerEntity = TitleChanger.proxy.getClientPlayer();
} catch (IllegalStateException e) {
LOGGER.debug("Attempted to call proxy.getClientPlayer() in serverside code.");
if (Config.getWindowTitle().contains(TIME)) {
transformationMap.put(TIME, () -> timeFormatter.format(new Date()));
}
if (playerEntity != null) {
return Integer.toString(playerEntity.getScore());
} else {
return Config.PLACEHOLDER_TEXT.get();
if (Config.getWindowTitle().contains(PLAYER_LOCATION)) {
transformationMap.put(PLAYER_LOCATION, this::getLocation);
}
}

private static String getPlayerLocationOrPlaceholder() {
PlayerEntity playerEntity = null;
String posX, posY, posZ;
try {
playerEntity = TitleChanger.proxy.getClientPlayer();
} catch (IllegalStateException e) {
LOGGER.debug("Attempted to call proxy.getClientPlayer() in serverside code.");
if (Config.getWindowTitle().contains(SCORE)) {
transformationMap.put(SCORE, this::getScore);
}
if (Config.getWindowTitle().contains(BIOME)) {
transformationMap.put(BIOME, this::getBiome);
}
if (playerEntity != null) {
posX = String.format("%.0f", playerEntity.posX);
posY = String.format("%.0f", playerEntity.posY);
posZ = String.format("%.0f", playerEntity.posZ);
} else {
posX = posY = posZ = Config.PLACEHOLDER_TEXT.get();
if (Config.getWindowTitle().contains(CHUNK)) {
transformationMap.put(CHUNK, this::getChunk);
}
return String.format("%s %s %s", posX, posY, posZ);
return transformationMap;
}

private static String getPlayerChunkOrPlaceholder() {
PlayerEntity playerEntity = null;
String chunkX, chunkY, chunkZ;
@SubscribeEvent(priority = EventPriority.LOWEST)
public void clientTick(TickEvent.ClientTickEvent event) {
if (Config.getWindowTitle().equals("")) {
return;
}

playerEntity = null;
world = null;
try {
playerEntity = TitleChanger.proxy.getClientPlayer();
world = TitleChanger.proxy.getClientWorld();
glfwSetWindowTitle(Minecraft.getInstance().getMainWindow().getHandle(), processText(Config.getWindowTitle()));
} catch (IllegalStateException e) {
LOGGER.debug("Attempted to call proxy.getClientPlayer() in serverside code.");
}
if (playerEntity != null) {
chunkX = String.format("%d", playerEntity.chunkCoordX);
chunkY = String.format("%d", playerEntity.chunkCoordY);
chunkZ = String.format("%d", playerEntity.chunkCoordZ);
} else {
chunkX = chunkY = chunkZ = Config.PLACEHOLDER_TEXT.get();
}

/**
* Apply transformations to the provided format string to generate the window title
*
* @param formatString Format template
* @return Final window title
*/
private String processText(String formatString) {
for (Map.Entry<String, Supplier<String>> entry : transformations.entrySet()) {
formatString = formatString.replace(entry.getKey(), entry.getValue().get());
}
return String.format("%s %s %s", chunkX, chunkY, chunkZ);
return formatString;
}

private String getLocation() {
return infoRetriever.getLocation(playerEntity);
}

private String getScore() {
return infoRetriever.getScore(playerEntity);
}

private String getBiome() {
return infoRetriever.getBiome(playerEntity, world);
}

private String getChunk() {
return infoRetriever.getChunk(playerEntity);
}
}
Loading

0 comments on commit 9ea6b4e

Please sign in to comment.