Skip to content

Commit

Permalink
Refactor InvMove singleton a bit to avoid a crash
Browse files Browse the repository at this point in the history
  • Loading branch information
PieKing1215 committed Jun 21, 2022
1 parent 055a0ec commit e487b37
Show file tree
Hide file tree
Showing 21 changed files with 140 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.client.player.Input;
import net.minecraft.network.chat.MutableComponent;

import javax.annotation.Nullable;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
Expand All @@ -20,7 +21,19 @@

public abstract class InvMove {

public static InvMove instance;
private static InvMove instance;

public static InvMove instance() {
if (instance == null) {
instance = new InvMoveNoOp();
}

return instance;
}

public static void setInstance(InvMove newInstance) {
instance = newInstance;
}

public static final String MOD_ID = "invmove";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

public class InvMoveConfig {

public static final Function<Boolean, Component> MOVEMENT_YES_NO_TEXT = b -> InvMove.instance.literalComponent(b ? ChatFormatting.GREEN + "Allow Movement" : ChatFormatting.RED + "Disallow Movement");
public static final Function<Boolean, Component> BACKGROUND_YES_NO_TEXT = b -> InvMove.instance.literalComponent(b ? ChatFormatting.GREEN + "Hide Background" : ChatFormatting.RED + "Show Background");
public static final Function<Boolean, Component> MOVEMENT_YES_NO_TEXT = b -> InvMove.instance().literalComponent(b ? ChatFormatting.GREEN + "Allow Movement" : ChatFormatting.RED + "Disallow Movement");
public static final Function<Boolean, Component> BACKGROUND_YES_NO_TEXT = b -> InvMove.instance().literalComponent(b ? ChatFormatting.GREEN + "Hide Background" : ChatFormatting.RED + "Show Background");

public static final General GENERAL = new General();
public static final Movement MOVEMENT = new Movement();
Expand Down Expand Up @@ -79,43 +79,43 @@ public enum PauseScreenMode {
}

public static Screen setupCloth(Screen parent){
ConfigBuilder builder = ConfigBuilder.create().setParentScreen(parent).setTitle(InvMove.instance.translatableComponent("config.invmove.title"));
ConfigBuilder builder = ConfigBuilder.create().setParentScreen(parent).setTitle(InvMove.instance().translatableComponent("config.invmove.title"));
builder.setDefaultBackgroundTexture(new ResourceLocation("minecraft:textures/block/spruce_planks.png"));
builder.transparentBackground();

ConfigEntryBuilder eb = builder.entryBuilder();
ConfigCategory general = builder.getOrCreateCategory(InvMove.instance.translatableComponent("key.invmove.category.general"));
ConfigCategory general = builder.getOrCreateCategory(InvMove.instance().translatableComponent("key.invmove.category.general"));
GENERAL.cfg.addTo(general, eb, "config.invmove");

// movement

ConfigCategory movement = builder.getOrCreateCategory(InvMove.instance.translatableComponent("key.invmove.category.movement"));
ConfigCategory movement = builder.getOrCreateCategory(InvMove.instance().translatableComponent("key.invmove.category.movement"));
MOVEMENT.cfg.addTo(movement, eb, "config.invmove");

for (Module module : InvMove.instance.modules) {
SubCategoryBuilder cat = eb.startSubCategory(InvMove.instance.translatableComponent("key.invmove.module." + module.getId()));
for (Module module : InvMove.instance().modules) {
SubCategoryBuilder cat = eb.startSubCategory(InvMove.instance().translatableComponent("key.invmove.module." + module.getId()));
module.getMovementConfig().addTo(cat, eb, "config.invmove." + module.getId() + "");
movement.addEntry(cat.build());
}

// unrecognized

movement.addEntry(eb.startTextDescription(InvMove.instance.translatableComponent("key.invmove.unrecognized").withStyle(ChatFormatting.UNDERLINE)).build());
movement.addEntry(eb.startTextDescription(InvMove.instance().translatableComponent("key.invmove.unrecognized").withStyle(ChatFormatting.UNDERLINE)).build());
if (MOVEMENT.unrecognizedScreensAllowMovement.isEmpty())
movement.addEntry(eb.startTextDescription(InvMove.instance.translatableComponent("key.invmove.unrecognized.desc").withStyle(ChatFormatting.GRAY)).build());
movement.addEntry(eb.startTextDescription(InvMove.instance().translatableComponent("key.invmove.unrecognized.desc").withStyle(ChatFormatting.GRAY)).build());

for (String modid : MOVEMENT.unrecognizedScreensAllowMovement.keySet()) {
HashMap<Class<? extends Screen>, Boolean> screens = MOVEMENT.unrecognizedScreensAllowMovement.get(modid);

SubCategoryBuilder cat = eb.startSubCategory(modid.equals("?unknown") ? InvMove.instance.translatableComponent("key.invmove.unrecognized.nomod") : InvMove.instance.literalComponent(InvMove.instance.modNameFromModid(modid)));
cat.setTooltip(InvMove.instance.literalComponent(ChatFormatting.GRAY + "modid: " + modid));
SubCategoryBuilder cat = eb.startSubCategory(modid.equals("?unknown") ? InvMove.instance().translatableComponent("key.invmove.unrecognized.nomod") : InvMove.instance().literalComponent(InvMove.instance().modNameFromModid(modid)));
cat.setTooltip(InvMove.instance().literalComponent(ChatFormatting.GRAY + "modid: " + modid));

for (Class<? extends Screen> cl : screens.keySet()) {
cat.add(eb.startBooleanToggle(
InvMove.instance.literalComponent(cl.getSimpleName()),
InvMove.instance().literalComponent(cl.getSimpleName()),
screens.get(cl)
)
.setTooltip(InvMove.instance.literalComponent(ChatFormatting.GRAY + cl.getName()))
.setTooltip(InvMove.instance().literalComponent(ChatFormatting.GRAY + cl.getName()))
.setYesNoTextSupplier(MOVEMENT_YES_NO_TEXT)
.setSaveConsumer(v -> screens.put(cl, v))
.build());
Expand All @@ -126,33 +126,33 @@ public static Screen setupCloth(Screen parent){

// background

ConfigCategory background = builder.getOrCreateCategory(InvMove.instance.translatableComponent("key.invmove.category.background"));
ConfigCategory background = builder.getOrCreateCategory(InvMove.instance().translatableComponent("key.invmove.category.background"));
BACKGROUND.cfg.addTo(background, eb, "config.invmove");

for (Module module : InvMove.instance.modules) {
SubCategoryBuilder cat = eb.startSubCategory(InvMove.instance.translatableComponent("key.invmove.module." + module.getId()));
for (Module module : InvMove.instance().modules) {
SubCategoryBuilder cat = eb.startSubCategory(InvMove.instance().translatableComponent("key.invmove.module." + module.getId()));
module.getBackgroundConfig().addTo(cat, eb, "config.invmove." + module.getId() + "");
background.addEntry(cat.build());
}

// unrecognized

background.addEntry(eb.startTextDescription(InvMove.instance.translatableComponent("key.invmove.unrecognized").withStyle(ChatFormatting.UNDERLINE)).build());
background.addEntry(eb.startTextDescription(InvMove.instance().translatableComponent("key.invmove.unrecognized").withStyle(ChatFormatting.UNDERLINE)).build());
if (BACKGROUND.unrecognizedScreensHideBG.isEmpty())
background.addEntry(eb.startTextDescription(InvMove.instance.translatableComponent("key.invmove.unrecognized.desc").withStyle(ChatFormatting.GRAY)).build());
background.addEntry(eb.startTextDescription(InvMove.instance().translatableComponent("key.invmove.unrecognized.desc").withStyle(ChatFormatting.GRAY)).build());

for (String modid : BACKGROUND.unrecognizedScreensHideBG.keySet()) {
HashMap<Class<? extends Screen>, Boolean> screens = BACKGROUND.unrecognizedScreensHideBG.get(modid);

SubCategoryBuilder cat = eb.startSubCategory(modid.equals("?unknown") ? InvMove.instance.translatableComponent("key.invmove.unrecognized.nomod") : InvMove.instance.literalComponent(InvMove.instance.modNameFromModid(modid)));
cat.setTooltip(InvMove.instance.literalComponent(ChatFormatting.GRAY + "modid: " + modid));
SubCategoryBuilder cat = eb.startSubCategory(modid.equals("?unknown") ? InvMove.instance().translatableComponent("key.invmove.unrecognized.nomod") : InvMove.instance().literalComponent(InvMove.instance().modNameFromModid(modid)));
cat.setTooltip(InvMove.instance().literalComponent(ChatFormatting.GRAY + "modid: " + modid));

for (Class<? extends Screen> cl : screens.keySet()) {
cat.add(eb.startBooleanToggle(
InvMove.instance.literalComponent(cl.getSimpleName()),
InvMove.instance().literalComponent(cl.getSimpleName()),
screens.get(cl)
)
.setTooltip(InvMove.instance.literalComponent(ChatFormatting.GRAY + cl.getName()))
.setTooltip(InvMove.instance().literalComponent(ChatFormatting.GRAY + cl.getName()))
.setSaveConsumer(v -> screens.put(cl, v))
.setYesNoTextSupplier(BACKGROUND_YES_NO_TEXT)
.build());
Expand All @@ -172,7 +172,7 @@ public static Screen setupCloth(Screen parent){
*/
private static void moveOldConfig() {
try {
File configDir = InvMove.instance.configDir();
File configDir = InvMove.instance().configDir();
if (configDir != null) {
File invmoveDir = new File(configDir, "invMove/");
if (invmoveDir.exists()) {
Expand Down Expand Up @@ -213,7 +213,7 @@ private static void moveOldConfig() {

public static void save() {
try {
File configDir = InvMove.instance.configDir();
File configDir = InvMove.instance().configDir();
if (configDir != null) {

moveOldConfig();
Expand All @@ -235,7 +235,7 @@ public static void save() {

// module configs

for (Module module : InvMove.instance.modules) {
for (Module module : InvMove.instance().modules) {
File modFile = new File(configDir, "invmove/" + module.getId() + ".json");
if (!modFile.exists()) {
//noinspection ResultOfMethodCallIgnored
Expand Down Expand Up @@ -270,7 +270,7 @@ public static void save() {

public static void load() {
try {
File configDir = InvMove.instance.configDir();
File configDir = InvMove.instance().configDir();
if (configDir != null) {

moveOldConfig();
Expand All @@ -296,7 +296,7 @@ public static void load() {

// module configs

for (Module module : InvMove.instance.modules) {
for (Module module : InvMove.instance().modules) {
File modFile = new File(configDir, "invmove/" + module.getId() + ".json");
if (!modFile.exists()) {
//noinspection ResultOfMethodCallIgnored
Expand Down Expand Up @@ -436,7 +436,7 @@ private static void readUnrecognizedConfig(File file) throws IOException {
try {
Class<?> cl = Class.forName(entry.getKey(), false, InvMoveConfig.class.getClassLoader());
if (Screen.class.isAssignableFrom(cl)) {
String modid = InvMove.instance.modidFromClass(cl).orElse("?unknown");
String modid = InvMove.instance().modidFromClass(cl).orElse("?unknown");
InvMoveConfig.MOVEMENT.unrecognizedScreensAllowMovement.putIfAbsent(modid, new HashMap<>());
HashMap<Class<? extends Screen>, Boolean> hm = InvMoveConfig.MOVEMENT.unrecognizedScreensAllowMovement.get(modid);
//noinspection unchecked
Expand All @@ -454,7 +454,7 @@ private static void readUnrecognizedConfig(File file) throws IOException {
try {
Class<?> cl = Class.forName(entry.getKey());
if (Screen.class.isAssignableFrom(cl)) {
String modid = InvMove.instance.modidFromClass(cl).orElse("?unknown");
String modid = InvMove.instance().modidFromClass(cl).orElse("?unknown");
InvMoveConfig.BACKGROUND.unrecognizedScreensHideBG.putIfAbsent(modid, new HashMap<>());
HashMap<Class<? extends Screen>, Boolean> hm = InvMoveConfig.BACKGROUND.unrecognizedScreensHideBG.get(modid);
//noinspection unchecked
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package me.pieking1215.invmove;

import net.minecraft.client.KeyMapping;
import net.minecraft.network.chat.MutableComponent;

import java.io.File;
import java.util.Optional;

public class InvMoveNoOp extends InvMove {
@Override
public Optional<String> modidFromClass(Class<?> c) {
return Optional.empty();
}

@Override
public String modNameFromModid(String modid) {
return "???";
}

@Override
public File configDir() {
return null;
}

@Override
protected void registerKeybind(KeyMapping key) {

}

@Override
public MutableComponent translatableComponent(String key) {
return null;
}

@Override
public MutableComponent literalComponent(String text) {
return null;
}

@Override
public boolean optionToggleCrouch() {
return false;
}

@Override
public void setOptionToggleCrouch(boolean toggleCrouch) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class BackgroundMixin {
)
private void onRenderBackground(PoseStack poseStack, CallbackInfo ci){
//noinspection ConstantConditions
if(InvMove.instance.shouldDisableScreenBackground((Screen)(Object)this)) {
if(InvMove.instance().shouldDisableScreenBackground((Screen)(Object)this)) {
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private void keyPressed(int i, int j, int k, CallbackInfoReturnable<Boolean> cir
// but if the player can jump in this inventory we cancel that
// (fix for https://github.com/PieKing1215/InvMove/issues/2)
if (i == 32 && InvMoveConfig.GENERAL.ENABLED.get() && InvMoveConfig.MOVEMENT.ENABLED.get() && InvMoveConfig.MOVEMENT.JUMP.get()) {
if (InvMove.instance.allowMovementInScreen(Minecraft.getInstance().screen)) {
if (InvMove.instance().allowMovementInScreen(Minecraft.getInstance().screen)) {
cir.setReturnValue(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class MovementMixin {
)
private void onInput(CallbackInfo info) {
//noinspection ConstantConditions
InvMove.instance.onInputUpdate(((LocalPlayer)(Object)this).input);
InvMove.instance().onInputUpdate(((LocalPlayer)(Object)this).input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public VanillaModule16() {

@Override
public Movement shouldAllowMovement(Screen screen) {
if(screen.getTitle() != null && screen.getTitle().equals(InvMove.instance.translatableComponent("sign.edit"))) return Movement.SUGGEST_DISABLE;
if(screen.getTitle() != null && screen.getTitle().equals(InvMove.instance().translatableComponent("sign.edit"))) return Movement.SUGGEST_DISABLE;

if(InvMoveConfig.MOVEMENT.TEXT_FIELD_DISABLES.get()) {
// don't allow movement when focused on an active textfield
Expand Down Expand Up @@ -149,7 +149,7 @@ public Movement shouldAllowMovement(Screen screen) {
@Override
public Background shouldHideBackground(Screen screen) {

if(screen.getTitle() != null && screen.getTitle().equals(InvMove.instance.translatableComponent("sign.edit"))) return Background.SUGGEST_SHOW;
if(screen.getTitle() != null && screen.getTitle().equals(InvMove.instance().translatableComponent("sign.edit"))) return Background.SUGGEST_SHOW;

return super.shouldHideBackground(screen);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public ConfigBool textFn(Function<Boolean, Component> textFn) {

@Override
public void addTo(ConfigCategory category, ConfigEntryBuilder eb, String id) {
BooleanToggleBuilder b = eb.startBooleanToggle(InvMove.instance.translatableComponent(id), get()).setDefaultValue(getDefault()).setSaveConsumer(this::set);
BooleanToggleBuilder b = eb.startBooleanToggle(InvMove.instance().translatableComponent(id), get()).setDefaultValue(getDefault()).setSaveConsumer(this::set);
if (Language.getInstance().has("tooltip." + id)) {
b.setTooltip(InvMove.instance.translatableComponent("tooltip." + id));
b.setTooltip(InvMove.instance().translatableComponent("tooltip." + id));
}
if (this.textFn != null) {
b.setYesNoTextSupplier(this.textFn);
Expand All @@ -39,9 +39,9 @@ public void addTo(ConfigCategory category, ConfigEntryBuilder eb, String id) {

@Override
public void addTo(SubCategoryBuilder category, ConfigEntryBuilder eb, String id) {
BooleanToggleBuilder b = eb.startBooleanToggle(InvMove.instance.translatableComponent(id), get()).setDefaultValue(getDefault()).setSaveConsumer(this::set);
BooleanToggleBuilder b = eb.startBooleanToggle(InvMove.instance().translatableComponent(id), get()).setDefaultValue(getDefault()).setSaveConsumer(this::set);
if (Language.getInstance().has("tooltip." + id)) {
b.setTooltip(InvMove.instance.translatableComponent("tooltip." + id));
b.setTooltip(InvMove.instance().translatableComponent("tooltip." + id));
}
if (this.textFn != null) {
b.setYesNoTextSupplier(this.textFn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ public ConfigEnum<T> setMigrator(Function<JsonElement, Optional<T>> migrator) {
@Override
void addTo(ConfigCategory category, ConfigEntryBuilder eb, String id) {
//noinspection unchecked
EnumSelectorBuilder<T> esb = eb.startEnumSelector(InvMove.instance.translatableComponent(id), (Class<T>) this.defaultValue.getClass(), this.value)
EnumSelectorBuilder<T> esb = eb.startEnumSelector(InvMove.instance().translatableComponent(id), (Class<T>) this.defaultValue.getClass(), this.value)
.setDefaultValue(this.getDefault())
.setSaveConsumer(this::set)
.setTooltip(InvMove.instance.translatableComponent("tooltip." + id));
.setTooltip(InvMove.instance().translatableComponent("tooltip." + id));

category.addEntry(esb.build());
}

@Override
void addTo(SubCategoryBuilder category, ConfigEntryBuilder eb, String id) {
//noinspection unchecked
EnumSelectorBuilder<T> esb = eb.startEnumSelector(InvMove.instance.translatableComponent(id), (Class<T>) this.defaultValue.getClass(), this.value)
EnumSelectorBuilder<T> esb = eb.startEnumSelector(InvMove.instance().translatableComponent(id), (Class<T>) this.defaultValue.getClass(), this.value)
.setDefaultValue(this.getDefault())
.setSaveConsumer(this::set)
.setTooltip(InvMove.instance.translatableComponent("tooltip." + id));
.setTooltip(InvMove.instance().translatableComponent("tooltip." + id));
if (Language.getInstance().has("tooltip." + id)) {
esb.setTooltip(InvMove.instance.translatableComponent("tooltip." + id));
esb.setTooltip(InvMove.instance().translatableComponent("tooltip." + id));
}
category.add(esb.build());
}
Expand Down
Loading

0 comments on commit e487b37

Please sign in to comment.