mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-26 14:53:14 +00:00
Add world conversion warning system (#409)
This commit is contained in:
1 parent
4d152c8ac7
commit
14e53fcaa1
12 files changed
+290
-1
No files matched your search
@@ -0,0 +1,26 @@
|
||||
package com.gtnewhorizon.gtnhlib.api.gui;
|
||||
|
||||
import net.minecraft.client.gui.GuiOptionButton;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
|
||||
import cpw.mods.fml.client.GuiConfirmation;
|
||||
import cpw.mods.fml.common.StartupQuery;
|
||||
|
||||
/**
|
||||
* The default missing mapping gui but with the yes/no buttons moved up. We need to use this because FML made these guis
|
||||
* run at very low fps, which could make the user think the gui isn't registering their clicks and prompt them to click
|
||||
* again, and accidentally skip past the missing mapping gui without reading it.
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class GuiConfirmationWCW extends GuiConfirmation {
|
||||
|
||||
public GuiConfirmationWCW(StartupQuery query) {
|
||||
super(query);
|
||||
}
|
||||
|
||||
public void initGui() {
|
||||
this.buttonList.add(new GuiOptionButton(0, this.width / 2 - 155, this.height - 58, I18n.format("gui.yes")));
|
||||
this.buttonList
|
||||
.add(new GuiOptionButton(1, this.width / 2 - 155 + 160, this.height - 58, I18n.format("gui.no")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.gtnewhorizon.gtnhlib.api.gui;
|
||||
|
||||
import cpw.mods.fml.common.StartupQuery;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
public interface IWorldConversionWarning {
|
||||
|
||||
/**
|
||||
* Will be called right after {@link cpw.mods.fml.common.event.FMLMissingMappingsEvent}s are fired to determine if
|
||||
* the warning should be shown.
|
||||
*
|
||||
* @return True if the warning should be shown
|
||||
*/
|
||||
boolean shouldShow();
|
||||
|
||||
/**
|
||||
* Server-side only a simple text message will be printed.
|
||||
*
|
||||
* @return The text message
|
||||
*/
|
||||
String getServerMessage();
|
||||
|
||||
/**
|
||||
* Client-side you can choose to either supply a custom {@link com.gtnewhorizon.gtnhlib.api.gui.GuiConfirmationWCW}
|
||||
* or the default one with a text message. To show a default
|
||||
* {@link com.gtnewhorizon.gtnhlib.api.gui.GuiConfirmationWCW} return null in {@link #getGui(StartupQuery)}.
|
||||
*
|
||||
* @return The text message for default gui class
|
||||
*/
|
||||
@SideOnly(Side.CLIENT)
|
||||
String getClientMessage();
|
||||
|
||||
/**
|
||||
* Children MUST annotate with @SideOnly(Side.CLIENT)
|
||||
* <p>
|
||||
* Client-side you can choose to either supply a custom {@link com.gtnewhorizon.gtnhlib.api.gui.GuiConfirmationWCW}
|
||||
* or the default one with a text message. To show a default
|
||||
* {@link com.gtnewhorizon.gtnhlib.api.gui.GuiConfirmationWCW} return null.
|
||||
*
|
||||
* @return Gui to be used in the warning. null to use default.
|
||||
*/
|
||||
@SideOnly(Side.CLIENT)
|
||||
default GuiConfirmationWCW getGui(StartupQuery startupQuery) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.gtnewhorizon.gtnhlib.api.gui;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.StartupQuery;
|
||||
|
||||
public class WCWHelper {
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static void fireWarnings() {
|
||||
WorldConversionWarningManager.WARNINGS.forEach((id, wcw) -> {
|
||||
if (wcw.shouldShow()) {
|
||||
boolean confirmed = StartupQuery
|
||||
.confirm(FMLCommonHandler.instance().getSide().isServer() ? wcw.getServerMessage() : id);
|
||||
if (!confirmed) StartupQuery.abort();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.gtnewhorizon.gtnhlib.api.gui;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Allows to register more confirmation screens to show before the FML missing mapping warning screen. Specifically
|
||||
* designed for world conversion warnings, these will only fire if the currently loading world contains any missing
|
||||
* mapping. Fires *after* {@link cpw.mods.fml.common.event.FMLMissingMappingsEvent}s but *before* the warning gui /
|
||||
* server message.
|
||||
*/
|
||||
public class WorldConversionWarningManager {
|
||||
|
||||
public static final HashMap<String, IWorldConversionWarning> WARNINGS = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Call during game startup to register a {@link IWorldConversionWarning}.
|
||||
*
|
||||
* @param id A unique id for your warning
|
||||
*/
|
||||
public static void register(String id, IWorldConversionWarning wcw) {
|
||||
WARNINGS.put(id, wcw);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.gtnewhorizon.gtnhlib.eventhandlers;
|
||||
|
||||
import net.minecraftforge.client.event.GuiOpenEvent;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.api.gui.GuiConfirmationWCW;
|
||||
import com.gtnewhorizon.gtnhlib.api.gui.WorldConversionWarningManager;
|
||||
import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber;
|
||||
import com.gtnewhorizon.gtnhlib.mixins.early.AccessorGuiNotification;
|
||||
import com.gtnewhorizon.gtnhlib.mixins.early.AccessorStartupQuery;
|
||||
|
||||
import cpw.mods.fml.client.GuiConfirmation;
|
||||
import cpw.mods.fml.common.StartupQuery;
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
@SideOnly(Side.CLIENT)
|
||||
@EventBusSubscriber
|
||||
public class ClientEventHandler {
|
||||
|
||||
// Put here because WorldConversionWarningManager is common and cannot try to import gui classes
|
||||
@SubscribeEvent
|
||||
public static void onGuiOpen(GuiOpenEvent event) {
|
||||
if (!(event.gui instanceof GuiConfirmation confirmationGui)) return;
|
||||
|
||||
StartupQuery query = ((AccessorGuiNotification) confirmationGui).gtnhlib$getQuery();
|
||||
|
||||
WorldConversionWarningManager.WARNINGS.forEach((id, wcw) -> {
|
||||
if (query.getText().equals(id)) {
|
||||
((AccessorStartupQuery) query).gtnhlib$setText(wcw.getClientMessage());
|
||||
GuiConfirmationWCW gui = wcw.getGui(query);
|
||||
|
||||
if (gui != null) {
|
||||
event.gui = gui;
|
||||
} else {
|
||||
event.gui = new GuiConfirmationWCW(query);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -59,7 +59,9 @@ public enum Mixins implements IMixins {
|
||||
.setPhase(Phase.LATE).addCommonMixins("MixinEnhancedInfusionRecipe")
|
||||
.addRequiredMod(TargetMods.THAUMCRAFT)),
|
||||
CONFIG_ORDER(Side.CLIENT, "fml.MixinGuiConfig"),
|
||||
WORLD_DELETION_EVENT(Side.CLIENT, "MixinGuiSelectWorld")
|
||||
WORLD_DELETION_EVENT(Side.CLIENT, "MixinGuiSelectWorld"),
|
||||
WORLD_LOAD_WARNING(new MixinBuilder("Accessors for the world conversion warning screen system")
|
||||
.addCommonMixins("AccessorGuiNotification", "AccessorStartupQuery").setPhase(Phase.EARLY)),
|
||||
//
|
||||
;
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.gtnewhorizon.gtnhlib.mixins;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.spongepowered.asm.lib.tree.ClassNode;
|
||||
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
|
||||
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
|
||||
|
||||
public class PreInitMixinPlugin implements IMixinConfigPlugin {
|
||||
|
||||
@Override
|
||||
public void onLoad(String mixinPackage) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRefMapperConfig() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {}
|
||||
|
||||
@Override
|
||||
public List<String> getMixins() {
|
||||
List<String> mixins = new ArrayList<>();
|
||||
|
||||
mixins.add("MixinGameData_WorldConversionWarning");
|
||||
|
||||
return mixins;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preApply(String s, ClassNode classNode, String s1, IMixinInfo iMixinInfo) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postApply(String s, ClassNode classNode, String s1, IMixinInfo iMixinInfo) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.gtnewhorizon.gtnhlib.mixins.early;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
||||
|
||||
import cpw.mods.fml.client.GuiNotification;
|
||||
import cpw.mods.fml.common.StartupQuery;
|
||||
|
||||
@Mixin(GuiNotification.class)
|
||||
public interface AccessorGuiNotification {
|
||||
|
||||
@Accessor(value = "query", remap = false)
|
||||
StartupQuery gtnhlib$getQuery();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.gtnewhorizon.gtnhlib.mixins.early;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
||||
|
||||
import cpw.mods.fml.common.StartupQuery;
|
||||
|
||||
@Mixin(StartupQuery.class)
|
||||
public interface AccessorStartupQuery {
|
||||
|
||||
@Accessor(value = "text", remap = false)
|
||||
void gtnhlib$setText(String text);
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.gtnewhorizon.gtnhlib.mixins.preinit;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.api.gui.WCWHelper;
|
||||
import com.llamalad7.mixinextras.expression.Definition;
|
||||
import com.llamalad7.mixinextras.expression.Expression;
|
||||
import com.llamalad7.mixinextras.sugar.Local;
|
||||
|
||||
import cpw.mods.fml.common.event.FMLMissingMappingsEvent;
|
||||
import cpw.mods.fml.common.registry.GameData;
|
||||
|
||||
@Mixin(GameData.class)
|
||||
public class MixinGameData_WorldConversionWarning {
|
||||
|
||||
// This pre-init mixin is required to inject into the exact moment after missing mapping events are fired
|
||||
// but before the confirmation message appears.
|
||||
@Definition(id = "defaulted", local = @Local(type = List.class, name = "defaulted"))
|
||||
@Definition(id = "isEmpty", method = "Ljava/util/List;isEmpty()Z")
|
||||
@Expression("defaulted.isEmpty()")
|
||||
@Inject(
|
||||
method = "processIdRematches",
|
||||
at = @At(value = "MIXINEXTRAS:EXPRESSION", shift = At.Shift.BY, by = -2),
|
||||
remap = false)
|
||||
private static void gtnhlib$processIdRematches(Iterable<FMLMissingMappingsEvent.MissingMapping> missedMappings,
|
||||
boolean isLocalWorld, GameData gameData, Map<String, Integer[]> remaps,
|
||||
CallbackInfoReturnable<List<String>> cir) {
|
||||
WCWHelper.fireWarnings();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user