Skip to content

Commit

Permalink
Rewrite gamerule toggle system to use Gson reading from `vanilla_disa…
Browse files Browse the repository at this point in the history
…ble.mixins.json` file, to fix crash in prod
  • Loading branch information
DragonEggBedrockBreaking committed Jul 31, 2022
1 parent c6e587d commit 551dba9
Showing 1 changed file with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package uk.debb.vanilla_disable.util.gamerules;

import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath;
import com.google.gson.stream.JsonReader;
import net.fabricmc.fabric.api.gamerule.v1.CustomGameRuleCategory;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import uk.debb.vanilla_disable.mixin_plugins.CaffeineConfigMixinConfigPlugin;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public enum GameruleCategories {
VD_DAMAGE,
Expand Down Expand Up @@ -57,12 +59,30 @@ private CustomGameRuleCategory createCustomGameRuleCategory(String name) {
}

public void toggle() throws IOException {
ClassPath cp = ClassPath.from(Thread.currentThread().getContextClassLoader());
String subpackage = this.name().substring(3).toLowerCase();
ImmutableSet<ClassPath.ClassInfo> set = cp.getTopLevelClasses("uk.debb.vanilla_disable.mixin." + subpackage);
enabled = CaffeineConfigMixinConfigPlugin.caffeineConfig.getEffectiveOptionForMixin(
set.iterator().next().toString().split("mixin\\.")[1]
).isEnabled();
InputStream inputStream = GameruleCategories.class.getResourceAsStream("/vanilla_disable.mixins.json");
if (inputStream != null) {
Reader reader = new InputStreamReader(inputStream);
JsonReader jsonReader = new JsonReader(reader);
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (name.equals("mixins")) {
jsonReader.beginArray();
while (jsonReader.hasNext()) {
String mixin = jsonReader.nextString();
if (mixin.startsWith(subpackage + ".")) {
enabled = CaffeineConfigMixinConfigPlugin.caffeineConfig.getEffectiveOptionForMixin(mixin).isEnabled();
break;
}
}
break;
} else {
jsonReader.skipValue();
}
}
reader.close();
}
}

public CustomGameRuleCategory get() {
Expand Down

0 comments on commit 551dba9

Please sign in to comment.