Added Config and made the distance players no longer render configurable.
This commit is contained in:
parent
047dfcab72
commit
c7af5d198f
5 changed files with 142 additions and 2 deletions
|
@ -6,6 +6,7 @@ import net.hypixel.modapi.HypixelModAPI;
|
|||
import net.hypixel.modapi.packet.impl.clientbound.event.ClientboundLocationPacket;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import xyz.stachel.zombiesutils.config.Config;
|
||||
import xyz.stachel.zombiesutils.handlers.Location;
|
||||
import xyz.stachel.zombiesutils.handlers.Renderer;
|
||||
|
||||
|
@ -20,5 +21,6 @@ public class ZombiesUtils implements ModInitializer {
|
|||
HypixelModAPI.getInstance().createHandler(ClientboundLocationPacket.class, Location::onLocation);
|
||||
ZombiesUtils.LOGGER.info("initializing...");
|
||||
HudLayerRegistrationCallback.EVENT.register(new Renderer());
|
||||
Config.readFile();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package xyz.stachel.zombiesutils.config;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import net.minecraft.client.gui.tooltip.Tooltip;
|
||||
import net.minecraft.client.gui.widget.ClickableWidget;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
public abstract class AbstractConfig <T> {
|
||||
private final Text message;
|
||||
private final Tooltip tooltip;
|
||||
private T value;
|
||||
|
||||
public AbstractConfig(String key, T defaultValue) {
|
||||
value = defaultValue;
|
||||
message = Text.translatable(String.format("hardcover.config.%s", toSnakeCase(key)));
|
||||
tooltip = Tooltip.of(Text.translatable(String.format("hardcover.config.tooltip.%s", toSnakeCase(key))));
|
||||
}
|
||||
|
||||
private static String toSnakeCase(String text) {
|
||||
return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, text);
|
||||
}
|
||||
|
||||
public abstract ClickableWidget createWidget(Runnable callback);
|
||||
|
||||
public ClickableWidget createWidget() {
|
||||
return createWidget(() -> {});
|
||||
}
|
||||
|
||||
public Text getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public Tooltip getTooltip() {
|
||||
return tooltip;
|
||||
}
|
||||
|
||||
public T getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(T value) {
|
||||
this.value = value;
|
||||
Config.writeFile();
|
||||
}
|
||||
}
|
53
src/main/java/xyz/stachel/zombiesutils/config/Config.java
Normal file
53
src/main/java/xyz/stachel/zombiesutils/config/Config.java
Normal file
|
@ -0,0 +1,53 @@
|
|||
package xyz.stachel.zombiesutils.config;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonParser;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import xyz.stachel.zombiesutils.ZombiesUtils;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public final class Config {
|
||||
private static final Gson GSON = new GsonBuilder()
|
||||
.registerTypeAdapter(IntConfig.class, new IntConfig.Serializer())
|
||||
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
|
||||
.setPrettyPrinting()
|
||||
.create();
|
||||
private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve(String.format("%s.json", ZombiesUtils.MOD_ID));
|
||||
|
||||
public static void readFile() {
|
||||
if (Files.exists(PATH)) {
|
||||
try (var reader = Files.newBufferedReader(PATH)) {
|
||||
var jsonElement = JsonParser.parseReader(reader);
|
||||
|
||||
for (var field : Configs.class.getDeclaredFields()) {
|
||||
var value = jsonElement.getAsJsonObject().get(field.getName());
|
||||
|
||||
if (value != null) {
|
||||
var fieldType = field.getType();
|
||||
|
||||
if (IntConfig.class.isAssignableFrom(fieldType)) {
|
||||
var intValue = value.getAsInt();
|
||||
((IntConfig) field.get(null)).setValue(intValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
ZombiesUtils.LOGGER.error("Failed to load configs at '{}'. Using default values.", PATH, exception);
|
||||
}
|
||||
} else {
|
||||
writeFile();
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeFile() {
|
||||
try (var writer = Files.newBufferedWriter(PATH)) {
|
||||
GSON.toJson(new Configs(), writer);
|
||||
} catch (Exception exception) {
|
||||
ZombiesUtils.LOGGER.error("Failed to save configs to '{}'.", PATH, exception);
|
||||
}
|
||||
}
|
||||
}
|
39
src/main/java/xyz/stachel/zombiesutils/config/IntConfig.java
Normal file
39
src/main/java/xyz/stachel/zombiesutils/config/IntConfig.java
Normal file
|
@ -0,0 +1,39 @@
|
|||
package xyz.stachel.zombiesutils.config;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import net.minecraft.client.gui.widget.ClickableWidget;
|
||||
import net.minecraft.client.gui.widget.CyclingButtonWidget;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class IntConfig extends AbstractConfig<Integer> {
|
||||
|
||||
public IntConfig(String key, Integer defaultValue) {
|
||||
super(key, defaultValue);
|
||||
}
|
||||
|
||||
public static IntConfig of(String key, int defaultValue) {
|
||||
return new IntConfig(key, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClickableWidget createWidget(Runnable callback) {
|
||||
return CyclingButtonWidget.<Integer>builder((value) -> Text.of(getValue().toString())).initially(getValue())
|
||||
.tooltip(value -> getTooltip())
|
||||
.build(getMessage(), (button, value) -> {
|
||||
setValue(value);
|
||||
callback.run();
|
||||
});
|
||||
}
|
||||
|
||||
public static class Serializer implements JsonSerializer<IntConfig> {
|
||||
@Override
|
||||
public JsonElement serialize(IntConfig value, Type type, JsonSerializationContext context) {
|
||||
return new JsonPrimitive(value.getValue());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ import net.minecraft.client.network.OtherClientPlayerEntity;
|
|||
import net.minecraft.client.world.ClientWorld;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import xyz.stachel.zombiesutils.config.Configs;
|
||||
|
||||
@Mixin(OtherClientPlayerEntity.class)
|
||||
public abstract class OtherClientPlayerEntityMixin extends AbstractClientPlayerEntity {
|
||||
|
@ -17,8 +18,8 @@ public abstract class OtherClientPlayerEntityMixin extends AbstractClientPlayerE
|
|||
|
||||
@ModifyReturnValue(method = "shouldRender", at = @At(value = "RETURN"))
|
||||
public boolean shouldRender(boolean original, double distance) {
|
||||
int dist = 1;
|
||||
double d = this.getBoundingBox().getAverageSideLength() * 1.75 + ((double) dist * 2.5D);
|
||||
int dist = Configs.playerVisibleDistance.getValue();
|
||||
double d = this.getBoundingBox().getAverageSideLength() * 1.75 + (double) dist;
|
||||
if (Double.isNaN(d)) {
|
||||
d = 1.0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue