Skip to content

Commit

Permalink
Added Panorama-Specific Splash Text
Browse files Browse the repository at this point in the history
- Panorama Specific Splash Text
- Internal improvements
  • Loading branch information
LudoCrypt committed Apr 6, 2021
1 parent aa9f5be commit 1decb19
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 34 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ archive_name=vistas

minecraft_version=1.16.5
yarn_mappings=1.16.5+build.5
loader_version=0.11.2
loader_version=0.11.3
fabric_version=0.31.0+1.16
modmenu_version=1.16.8
clothconfig_version=4.11.14
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/terraformersmc/vistas/api/panorama/Panorama.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ public class Panorama {
private MusicSound music;
private int weight = 1;
private MovementSettings movementSettings;
private Identifier splashTexts;

private Panorama(String name, Identifier id, MusicSound music, MovementSettings movementSettings, int weight) {
private Panorama(String name, Identifier id, MusicSound music, MovementSettings movementSettings, int weight, Identifier splashTexts) {
this.name = name;
this.backgroundId = id;
this.music = music;
this.movementSettings = movementSettings;
this.weight = weight;
this.splashTexts = splashTexts;
}

private Panorama(String name, Identifier id, MusicSound music, MovementSettings movementSettings, int weight) {
this(name, id, music, movementSettings, weight, new Identifier("texts/splashes.txt"));
}

public String getName() {
Expand All @@ -45,12 +51,17 @@ public int getWeight() {
return weight;
}

public Identifier getSplashTexts() {
return splashTexts;
}

public static class Builder {

private String name;
private Identifier backgroundId = new Identifier("textures/gui/title/background/panorama");
private MusicSound music = createMenuSound(SoundEvents.MUSIC_MENU);
private int weight = 1;
private Identifier splashTexts = new Identifier("texts/splashes.txt");

private boolean frozen = false;
private float addedX = 0.0F;
Expand Down Expand Up @@ -133,8 +144,13 @@ public Builder setYEquation(Function<Float, Float> yEquation) {
return this;
}

public Builder setSplashTexts(Identifier splashTexts) {
this.splashTexts = splashTexts;
return this;
}

public Panorama build() {
return new Panorama(name, backgroundId, music, new MovementSettings(frozen, addedX, addedY, speedMultiplier, woozy, useXEquation, useYEquation, XEquation, YEquation), weight);
return new Panorama(name, backgroundId, music, new MovementSettings(frozen, addedX, addedY, speedMultiplier, woozy, useXEquation, useYEquation, XEquation, YEquation), weight, splashTexts);
}

public static MusicSound createMenuSound(SoundEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,63 @@

import com.terraformersmc.vistas.Vistas;
import com.terraformersmc.vistas.config.PanoramaConfig;
import com.terraformersmc.vistas.resource.InvalidPanoramaException;

public class Panoramas {

public static Panorama getCurrent() {
return Vistas.PANORAMAS.getOrDefault(PanoramaConfig.getInstance().panorama + "_0", Panorama.DEFAULT);
return get(PanoramaConfig.getInstance().panorama);
}

public static Panorama getRandom() {
return Vistas.PANORAMAS.values().toArray(new Panorama[0])[new Random().nextInt(Vistas.PANORAMAS.size())];
return Vistas.PANORAMAS.size() > 0 ? Vistas.PANORAMAS.values().toArray(new Panorama[0])[new Random().nextInt(Vistas.PANORAMAS.size())] : Panorama.DEFAULT;
}

public static void setRandom() {
if (!PanoramaConfig.getInstance().forcePanorama) {
if (Vistas.PANORAMAS.size() > 0) {
PanoramaConfig.getInstance().panorama = getRandom().getName();
}
public static Panorama get(String id) {
return PanoramasInternals.getForced(id + "_0");
}

public static Panorama getOrThrow(String id) throws InvalidPanoramaException {
if (Vistas.PANORAMAS.get(id) == null) {
throw new InvalidPanoramaException(id);
} else {
return Vistas.PANORAMAS.get(id);
}
}

public static void set(String id) {
PanoramasInternals.setForced(id + "_0");
}

public static void setRandom() {
PanoramasInternals.setForced(getRandom().getName());
}

public static void reload() {
Vistas.PANORAMAS.clear();
Vistas.PANORAMAS.putAll(Vistas.BUILTIN_PANORAMAS);
Vistas.PANORAMAS.putAll(Vistas.RESOURCE_PANORAMAS);
setRandom();
}

@Deprecated
public static void add(Panorama panorama) {
Vistas.addBuiltInPanorama(panorama);
}

public static class PanoramasInternals {
public static boolean shouldRedoSplash = true;

public static void setForced(String id) {
if (!PanoramaConfig.getInstance().forcePanorama) {
if (Vistas.PANORAMAS.size() > 0) {
PanoramaConfig.getInstance().panorama = id;
}
}
}

public static Panorama getForced(String id) {
return Vistas.PANORAMAS.getOrDefault(id, Panorama.DEFAULT);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void VISTAS_modifyMusic(CallbackInfoReturnable<MusicSound> ci) {
}
}

@Inject(method = "<init>", at = @At(value = "NEW", target = "Lnet/minecraft/client/texture/PlayerSkinProvider;", shift = Shift.BEFORE))
@Inject(method = "<init>", at = @At(value = "NEW", target = "Lnet/minecraft/client/sound/MusicTracker;", shift = Shift.BEFORE))
private void VISTAS_appendPanoramaManager(CallbackInfo ci) {
this.panoramaManager = new PanoramaManager();
this.resourceManager.registerListener(panoramaManager);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.terraformersmc.vistas.mixin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import com.google.common.collect.Lists;
import com.terraformersmc.vistas.Vistas;
import com.terraformersmc.vistas.api.panorama.Panorama;
import com.terraformersmc.vistas.api.panorama.Panoramas;
import com.terraformersmc.vistas.resource.InvalidPanoramaException;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.resource.SplashTextResourceSupplier;
import net.minecraft.client.util.Session;
import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.profiler.Profiler;

@Mixin(SplashTextResourceSupplier.class)
public abstract class SplashTextResourceSupplierMixin {

@Shadow
@Final
private static Identifier RESOURCE_ID;

@Shadow
@Final
private Session field_18934;

@Unique
private List<String> storedSplashList = Lists.newArrayList();

@Unique
private ArrayList<Panorama> THIS_PANORAMA = Lists.newArrayList();

@Unique
private ArrayList<Panorama> READ_SPLASHES = Lists.newArrayList();

@Unique
private boolean isUpFirst = true;

@Unique
private static final String panoramaRegex = "\\B\\$.*(\\$Splash)(\\n|$)";

@ModifyVariable(method = "Lnet/minecraft/client/resource/SplashTextResourceSupplier;prepare(Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/util/profiler/Profiler;)Ljava/util/List;", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/resource/ResourceManager;getResource(Lnet/minecraft/util/Identifier;)Lnet/minecraft/resource/Resource;"), ordinal = 0)
private Resource VISTAS_redirectSplashReading(Resource in) {
if (isUpFirst) {
THIS_PANORAMA.add(Panoramas.getCurrent());
}
try {
return MinecraftClient.getInstance().getResourceManager().getResource(THIS_PANORAMA.get(THIS_PANORAMA.size() - 1).getSplashTexts());
} catch (IOException var36) {
return in;
}
}

@ModifyVariable(method = "Lnet/minecraft/client/resource/SplashTextResourceSupplier;prepare(Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/util/profiler/Profiler;)Ljava/util/List;", at = @At(value = "RETURN", ordinal = 0, shift = Shift.BEFORE), name = "var7")
private List<String> VISTAS_setList(List<String> in) {
this.storedSplashList = Lists.newArrayList((List<String>) in);
return in;
}

@Inject(method = "Lnet/minecraft/client/resource/SplashTextResourceSupplier;prepare(Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/util/profiler/Profiler;)Ljava/util/List;", at = @At(value = "RETURN", ordinal = 0, shift = Shift.BEFORE), cancellable = true)
private void VISTAS_addOtherSplash(ResourceManager resourceManager, Profiler profiler, CallbackInfoReturnable<List<String>> ci) {
isUpFirst = false;
VISTAS_addOtherSplash(resourceManager, profiler, ci, THIS_PANORAMA.get(THIS_PANORAMA.size() - 1));
THIS_PANORAMA.clear();
THIS_PANORAMA.add(Panoramas.getCurrent());
READ_SPLASHES.clear();
}

@Unique
private void VISTAS_addOtherSplash(ResourceManager resourceManager, Profiler profiler, CallbackInfoReturnable<List<String>> ci, Panorama thisPanorama) {
if (!READ_SPLASHES.contains(thisPanorama)) {
READ_SPLASHES.add(thisPanorama);
ArrayList<String> splashTexts = Lists.newArrayList(storedSplashList);
ArrayList<String> newSplashTexts = Lists.newArrayList();
ArrayList<Panorama> needToAdd = Lists.newArrayList();
Pattern pattern = Pattern.compile(panoramaRegex);
for (String splash : splashTexts) {
Matcher matcher = pattern.matcher(splash);
if (matcher.find()) {
try {
needToAdd.add(Panoramas.getOrThrow(splash.substring(1, splash.lastIndexOf("$Splash")) + "_0"));
} catch (InvalidPanoramaException ex) {
Vistas.LOGGER.warn("\"" + thisPanorama.getSplashTexts().toString() + "\" calls \"" + splash + "\" which leads to no registered Panorama!");
}
} else {
newSplashTexts.add(splash.replace("$P$Splash", field_18934.getUsername().toUpperCase(Locale.ROOT)).replace("$p$Splash", field_18934.getUsername()));
}
}

for (Panorama panorama : needToAdd) {
if (!READ_SPLASHES.contains(panorama)) {
THIS_PANORAMA.clear();
THIS_PANORAMA.add(panorama);
newSplashTexts.addAll(prepare(resourceManager, profiler));
READ_SPLASHES.clear();
}
}

ci.setReturnValue(newSplashTexts);
}
}

@Inject(method = "get", at = @At("HEAD"), cancellable = true)
private void VISTAS_reloadBeforeGet(CallbackInfoReturnable<String> ci) {
apply(prepare(MinecraftClient.getInstance().getResourceManager(), MinecraftClient.getInstance().getProfiler()), MinecraftClient.getInstance().getResourceManager(), MinecraftClient.getInstance().getProfiler());
}

@Shadow
protected abstract List<String> prepare(ResourceManager resourceManager, Profiler profiler);

@Shadow
protected abstract void apply(List<String> list, ResourceManager resourceManager, Profiler profiler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

import com.terraformersmc.vistas.access.TimeAccess;
import com.terraformersmc.vistas.api.panorama.Panoramas;
import com.terraformersmc.vistas.api.panorama.Panoramas.PanoramasInternals;
import com.terraformersmc.vistas.config.PanoramaConfig;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.RotatingCubeMapRenderer;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.TitleScreen;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

Expand All @@ -27,6 +29,9 @@ public abstract class TitleScreenBackgroundMixin extends Screen {
@Shadow
private RotatingCubeMapRenderer backgroundRenderer;

@Shadow
private String splashText;

protected TitleScreenBackgroundMixin(Text title) {
super(title);
}
Expand All @@ -35,6 +40,7 @@ protected TitleScreenBackgroundMixin(Text title) {
private void VISTAS_updateScreen(CallbackInfo ci) {
if (PanoramaConfig.getInstance().randomPerScreen) {
Panoramas.setRandom();
PanoramasInternals.shouldRedoSplash = true;
}
updateScreen();
}
Expand All @@ -50,6 +56,14 @@ private Identifier VISTAS_modifyOverlay(Identifier defaultOverlay) {
return defaultOverlay;
}

@Inject(method = "render", at = @At("HEAD"))
private void VISTAS_modifySplashOnRender(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo ci) {
if (PanoramasInternals.shouldRedoSplash) {
this.splashText = this.client.getSplashTextLoader().get();
PanoramasInternals.shouldRedoSplash = false;
}
}

@Unique
private void updateScreen() {
if (Panoramas.getCurrent() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.terraformersmc.vistas.resource;

@SuppressWarnings("serial")
public class InvalidPanoramaException extends RuntimeException {

public InvalidPanoramaException(String string) {
super(string);
}

public InvalidPanoramaException(String string, Throwable throwable) {
super(string, throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,9 @@ public Panorama deserialize(JsonElement jsonElement, Type type, JsonDeserializat
MovementSettings movementSettings = deserializeMovementSettings(jsonObject);
int weight = JsonHelper.getInt(jsonObject, "weight", 1);

return new Panorama.Builder(name)
.setBackgroundId(new Identifier(panoramaId))
.setMusic(music)
.setFrozen(movementSettings.isFrozen())
.setAddedX(movementSettings.getAddedX())
.setAddedY(movementSettings.getAddedY())
.setSpeedMultiplier(movementSettings.getSpeedMultiplier())
.setWoozy(movementSettings.isWoozy())
.setWeight(weight)
.build();
String splashTexts = JsonHelper.getString(jsonObject, "splashTexts", "texts/splashes.txt");

return new Panorama.Builder(name).setBackgroundId(new Identifier(panoramaId)).setMusic(music).setFrozen(movementSettings.isFrozen()).setAddedX(movementSettings.getAddedX()).setAddedY(movementSettings.getAddedY()).setSpeedMultiplier(movementSettings.getSpeedMultiplier()).setWoozy(movementSettings.isWoozy()).setWeight(weight).setSplashTexts(new Identifier(splashTexts)).build();
}

public MovementSettings deserializeMovementSettings(JsonObject json) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/vistas/panoramas.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"name": "minecraft",
"panoramaId": "textures/gui/title/background/panorama",
"musicId": "music.menu",
"splashTexts": "texts/splashes.txt",
"movementSettings": {
"frozen": false,
"addedX": 0.0,
Expand Down
27 changes: 14 additions & 13 deletions src/main/resources/vistas.mixins.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"required": true,
"minVersion": "0.8",
"package": "com.terraformersmc.vistas.mixin",
"compatibilityLevel": "JAVA_8",
"client": [
"GameRendererMixin",
"MinecraftClientMixin",
"RotatingCubeMapRendererMixin",
"TitleScreenBackgroundMixin"
],
"injectors": {
"defaultRequire": 1
}
"required": true,
"minVersion": "0.8",
"package": "com.terraformersmc.vistas.mixin",
"compatibilityLevel": "JAVA_8",
"client": [
"GameRendererMixin",
"MinecraftClientMixin",
"RotatingCubeMapRendererMixin",
"TitleScreenBackgroundMixin",
"SplashTextResourceSupplierMixin"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit 1decb19

Please sign in to comment.