Flywheel/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java

100 lines
3.3 KiB
Java
Raw Normal View History

package com.jozufozu.flywheel.config;
import java.util.function.Consumer;
import java.util.function.Supplier;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.OptifineHandler;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
public enum BooleanConfig {
ENGINE(() -> BooleanConfig::enabled),
NORMAL_OVERLAY(() -> BooleanConfig::normalOverlay),
;
final Supplier<Consumer<BooleanDirective>> receiver;
BooleanConfig(Supplier<Consumer<BooleanDirective>> receiver) {
this.receiver = receiver;
}
public SConfigureBooleanPacket packet(BooleanDirective directive) {
return new SConfigureBooleanPacket(this, directive);
}
/**
* Encode a variant of BooleanConfig. Symmetrical function to {@link #decode}
*/
public void encode(FriendlyByteBuf buffer) {
buffer.writeByte(this.ordinal());
}
/**
* Safely decode a variant of BooleanConfig. Symmetrical function to {@link #encode}
*/
public static BooleanConfig decode(FriendlyByteBuf buffer) {
byte t = buffer.readByte();
BooleanConfig[] values = values();
// Protects against version differences.
// Shouldn't ever happen but do a sanity check for safety.
if (t >= 0 && t < values.length)
return values[t];
else
return null;
}
@OnlyIn(Dist.CLIENT)
private static void enabled(BooleanDirective state) {
LocalPlayer player = Minecraft.getInstance().player;
if (player == null || state == null) return;
if (state == BooleanDirective.DISPLAY) {
2021-11-09 05:48:02 +01:00
Component text = new TextComponent("Flywheel renderer is currently: ").append(boolToText(FlwConfig.get().enabled()));
2021-07-15 20:36:24 +02:00
player.displayClientMessage(text, false);
return;
}
boolean enabled = state.get();
boolean cannotUse = OptifineHandler.usingShaders() && enabled;
FlwConfig.get().client.enabled.set(enabled);
2021-11-09 05:48:02 +01:00
Component text = boolToText(FlwConfig.get().enabled()).append(new TextComponent(" Flywheel renderer").withStyle(ChatFormatting.WHITE));
Component error = new TextComponent("Flywheel renderer does not support Optifine Shaders").withStyle(ChatFormatting.RED);
player.displayClientMessage(cannotUse ? error : text, false);
Backend.reloadWorldRenderers();
}
@OnlyIn(Dist.CLIENT)
private static void normalOverlay(BooleanDirective state) {
LocalPlayer player = Minecraft.getInstance().player;
if (player == null || state == null) return;
if (state == BooleanDirective.DISPLAY) {
2021-11-09 05:48:02 +01:00
Component text = new TextComponent("Normal debug mode is currently: ").append(boolToText(FlwConfig.get().debugNormals()));
2021-07-15 20:36:24 +02:00
player.displayClientMessage(text, false);
return;
}
FlwConfig.get().client.debugNormals.set(state.get());
2021-11-09 05:48:02 +01:00
Component text = boolToText(FlwConfig.get().debugNormals()).append(new TextComponent(" normal debug mode").withStyle(ChatFormatting.WHITE));
2021-07-15 20:36:24 +02:00
player.displayClientMessage(text, false);
}
private static MutableComponent boolToText(boolean b) {
return b ? new TextComponent("enabled").withStyle(ChatFormatting.DARK_GREEN) : new TextComponent("disabled").withStyle(ChatFormatting.RED);
}
}