Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate the client configs #183

Draft
wants to merge 17 commits into
base: Multiloader-1.20
Choose a base branch
from
Draft
915 changes: 915 additions & 0 deletions common/src/main/java/org/vivecraft/client/ClientConfig.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package org.vivecraft.client.gui.settings;

import com.google.common.collect.ImmutableList;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import org.vivecraft.client.gui.widgets.SettingsList;
import org.vivecraft.common.ConfigBuilder;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.stream.Collectors;

public class GuiArrayValueEditScreen<T> extends GuiListScreen{
private final ConfigBuilder.ArrayValue<T> arrayValue;
private final Function<String, T> fromString;
private T[] elements;

public GuiArrayValueEditScreen(Component title, Screen lastScreen, ConfigBuilder.ArrayValue<T> arrayValue, Function<String, T> fromString) {
super(title, lastScreen);
this.arrayValue = arrayValue;
this.fromString = fromString;
}

@Override
protected void init() {
clearWidgets();
double scrollAmount = list != null? list.getScrollAmount() : 0.0D;

this.list = new SettingsList(this, minecraft, getEntries());
list.setScrollAmount(scrollAmount);
this.addWidget(this.list);

this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, button -> {
arrayValue.set(getCurrentValues());
this.minecraft.setScreen(this.lastScreen);
}).bounds(this.width / 2 - 155, this.height - 27, 150, 20).build());

this.addRenderableWidget(Button
.builder(CommonComponents.GUI_CANCEL, button -> this.minecraft.setScreen(this.lastScreen))
.bounds(this.width / 2 + 5, this.height - 27, 150, 20)
.build());
}

@Override
protected List<SettingsList.BaseEntry> getEntries() {
List<SettingsList.BaseEntry> entries = new LinkedList<>();
if (elements == null) {
elements = Arrays.copyOf(arrayValue.get(), arrayValue.get().length);
}
int i = 0;
for (T item : elements) {
EditBox box = new EditBox(Minecraft.getInstance().font, 0, 0, ArrayResetEntry.valueButtonWidth - 1, 20, Component.literal(item + ""));
box.setMaxLength(1000);
box.setValue(item + "");
int index = i++;
entries.add(new ArrayResetEntry(Component.empty(), box, () -> !fromString.apply(box.getValue()).equals(arrayValue.getDefault()[index]), button -> {elements[index] = arrayValue.getDefault()[index];reinit = true;}));
}
return entries;
}

private T[] getCurrentValues(){
return (T[]) list.children().stream().map(entry -> {
if (entry instanceof ArrayResetEntry arrayResetEntry) {
return fromString.apply(arrayResetEntry.getString());
} else {
return fromString.apply("");
}
}).collect(Collectors.toList()).toArray();
}

private static class ArrayResetEntry extends SettingsList.WidgetEntry {
public static final int valueButtonWidth = 280;
private final BooleanSupplier canReset;
private final Button resetButton;

public ArrayResetEntry(Component name, EditBox valueWidget, BooleanSupplier canReset, Button.OnPress resetAction) {
super(name, valueWidget);
this.canReset = canReset;

this.resetButton = Button.builder(Component.literal("X"), resetAction)
.tooltip(Tooltip.create(Component.translatable("controls.reset")))
.bounds(0, 0, 20, 20).build();
}

@Override
public void render(GuiGraphics guiGraphics, int i, int j, int k, int l, int m, int n, int o, boolean bl, float f) {
this.valueWidget.setX(k - 50);
this.valueWidget.setY(j);
this.valueWidget.render(guiGraphics, n, o, f);
this.resetButton.setX(k + 230);
this.resetButton.setY(j);
this.resetButton.active = canReset.getAsBoolean();
this.resetButton.render(guiGraphics, n, o, f);
}

@Override
public List<? extends GuiEventListener> children() {
return ImmutableList.of(this.valueWidget, this.resetButton);
}

@Override
public List<? extends NarratableEntry> narratables() {
return ImmutableList.of(this.valueWidget, this.resetButton);
}

public String getString() {
return ((EditBox)valueWidget).getValue();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.vivecraft.client.gui.settings;

import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.vivecraft.client.ClientConfig;
import org.vivecraft.client.gui.widgets.SettingsList;
import org.vivecraft.common.ConfigBuilder;

import java.util.LinkedList;
import java.util.List;

public class GuiClientSettings extends GuiListScreen {
public GuiClientSettings(Screen lastScreen) {
super(Component.translatable("vivecraft.options.screen.client"), lastScreen);
}

@Override
protected List<SettingsList.BaseEntry> getEntries() {
List<SettingsList.BaseEntry> entries = new LinkedList<>();
String lastCategory = null;
for (ConfigBuilder.ConfigValue cv : ClientConfig.getConfigValues()) {
String path = cv.getPath();
String category = path.substring(0, path.lastIndexOf("."));
String name = path.substring(path.lastIndexOf(".") + 1);
if (!category.equals(lastCategory)) {
lastCategory = category;
entries.add(new SettingsList.CategoryEntry(Component.literal(category)));
}
entries.add(SettingsList.ConfigToEntry(cv, Component.literal(name)));
}
return entries;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.vivecraft.client.gui.settings;

import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.vivecraft.client.gui.widgets.SettingsList;
import org.vivecraft.common.ConfigBuilder;

import java.util.LinkedList;
import java.util.List;

public class GuiConfigListScreen extends GuiListScreen{
private final ConfigBuilder.ConfigValue<?>[] config;

public GuiConfigListScreen(Component title, Screen lastScreen, ConfigBuilder.ConfigValue<?> ... config) {
super(title, lastScreen);
this.config = config;
}

@Override
protected List<SettingsList.BaseEntry> getEntries() {
List<SettingsList.BaseEntry> entries = new LinkedList<>();
for (ConfigBuilder.ConfigValue<?> value : config) {
String path = value.getPath();
String name = path.substring(path.lastIndexOf(".") + 1);
entries.add(SettingsList.ConfigToEntry(value, Component.literal(name)));
}
return entries;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import org.vivecraft.client.gui.widgets.SettingsList;
import org.vivecraft.server.config.ConfigBuilder;
import org.vivecraft.common.ConfigBuilder;

import java.util.ArrayList;
import java.util.LinkedList;
Expand Down Expand Up @@ -100,7 +100,7 @@ public ListValueEntry(Component name, EditBox valueWidget, Button.OnPress delete

@Override
public void render(GuiGraphics guiGraphics, int i, int j, int k, int l, int m, int n, int o, boolean bl, float f) {
this.valueWidget.setX(k + -50);
this.valueWidget.setX(k - 50);
this.valueWidget.setY(j);
this.valueWidget.render(guiGraphics, n, o, f);
this.deleteButton.setX(k + 230);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.vivecraft.client.gui.widgets.SettingsList;
import org.vivecraft.server.config.ConfigBuilder;
import org.vivecraft.server.config.ServerConfig;
import org.vivecraft.common.ConfigBuilder;
import org.vivecraft.server.ServerConfig;

import java.util.LinkedList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.vivecraft.client.ClientConfig;
import org.vivecraft.client.gui.widgets.SettingsList;
import org.vivecraft.client.network.ClientNetworking;
import org.vivecraft.client_vr.ClientDataHolderVR;
Expand Down Expand Up @@ -47,6 +48,10 @@ protected List<SettingsList.BaseEntry> getEntries() {
Component.translatable("vivecraft.options.screen.server"),
Button.builder(Component.translatable("vivecraft.options.screen.server"), button -> this.minecraft.setScreen(new GuiServerSettings(this))).size(SettingsList.WidgetEntry.valueButtonWidth, 20).build()));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.client"),
Button.builder(Component.translatable("vivecraft.options.screen.client"), button -> this.minecraft.setScreen(new GuiClientSettings(this))).size(SettingsList.WidgetEntry.valueButtonWidth, 20).build()));

entries.add(new SettingsList.CategoryEntry(Component.literal("Vivecraft Buttons")));

entries.add(new SettingsList.WidgetEntry(
Expand All @@ -55,18 +60,99 @@ protected List<SettingsList.BaseEntry> getEntries() {
ClientDataHolderVR.getInstance().vrSettings.vrToggleButtonEnabled = !ClientDataHolderVR.getInstance().vrSettings.vrToggleButtonEnabled;
ClientDataHolderVR.getInstance().vrSettings.saveOptions();
})));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.VR_SETTINGS_BUTTON_VISIBLE"),
CycleButton.onOffBuilder(ClientDataHolderVR.getInstance().vrSettings.vrSettingsButtonEnabled).displayOnlyValue().create(0, 0, SettingsList.WidgetEntry.valueButtonWidth, 20, Component.empty(), (cycleButton, object) -> {
ClientDataHolderVR.getInstance().vrSettings.vrSettingsButtonEnabled = !ClientDataHolderVR.getInstance().vrSettings.vrSettingsButtonEnabled;
ClientDataHolderVR.getInstance().vrSettings.saveOptions();
})));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.VR_SETTINGS_BUTTON_POSITION"),
new CycleButton.Builder<Boolean>(bool -> bool ? Component.translatable("vivecraft.options.left") : Component.translatable("vivecraft.options.right")).withValues(ImmutableList.of(Boolean.TRUE, Boolean.FALSE)).withInitialValue(ClientDataHolderVR.getInstance().vrSettings.vrSettingsButtonPositionLeft).displayOnlyValue().create(0, 0, SettingsList.WidgetEntry.valueButtonWidth, 20, Component.empty(), (cycleButton, object) -> {
ClientDataHolderVR.getInstance().vrSettings.vrSettingsButtonPositionLeft = !ClientDataHolderVR.getInstance().vrSettings.vrSettingsButtonPositionLeft;
ClientDataHolderVR.getInstance().vrSettings.saveOptions();
})));

entries.add(new SettingsList.CategoryEntry(Component.literal("Client Settings")));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.general"),
Button.builder(Component.translatable("vivecraft.options.screen.general"), button -> this.minecraft.setScreen(new GuiConfigListScreen(Component.translatable("vivecraft.options.screen.general"), this, ClientConfig.generalConfig())))
.size(SettingsList.WidgetEntry.valueButtonWidth, 20)
.build()));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.update"),
Button.builder(Component.translatable("vivecraft.options.screen.update"), button -> this.minecraft.setScreen(new GuiConfigListScreen(Component.translatable("vivecraft.options.screen.update"), this, ClientConfig.updateConfig())))
.size(SettingsList.WidgetEntry.valueButtonWidth, 20)
.build()));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.roomscale"),
Button.builder(Component.translatable("vivecraft.options.screen.roomscale"), button -> this.minecraft.setScreen(new GuiConfigListScreen(Component.translatable("vivecraft.options.screen.roomscale"), this, ClientConfig.roomScaleConfig())))
.size(SettingsList.WidgetEntry.valueButtonWidth, 20)
.build()));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.seated"),
Button.builder(Component.translatable("vivecraft.options.screen.seated"), button -> this.minecraft.setScreen(new GuiConfigListScreen(Component.translatable("vivecraft.options.screen.seated"), this, ClientConfig.seatedConfig())))
.size(SettingsList.WidgetEntry.valueButtonWidth, 20)
.build()));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.teleport"),
Button.builder(Component.translatable("vivecraft.options.screen.teleport"), button -> this.minecraft.setScreen(new GuiConfigListScreen(Component.translatable("vivecraft.options.screen.teleport"), this, ClientConfig.teleportConfig())))
.size(SettingsList.WidgetEntry.valueButtonWidth, 20)
.build()));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.display"),
Button.builder(Component.translatable("vivecraft.options.screen.display"), button -> this.minecraft.setScreen(new GuiConfigListScreen(Component.translatable("vivecraft.options.screen.display"), this, ClientConfig.displayConfig())))
.size(SettingsList.WidgetEntry.valueButtonWidth, 20)
.build()));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.hud"),
Button.builder(Component.translatable("vivecraft.options.screen.hud"), button -> this.minecraft.setScreen(new GuiConfigListScreen(Component.translatable("vivecraft.options.screen.hud"), this, ClientConfig.hudConfig())))
.size(SettingsList.WidgetEntry.valueButtonWidth, 20)
.build()));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.fov"),
Button.builder(Component.translatable("vivecraft.options.screen.fov"), button -> this.minecraft.setScreen(new GuiConfigListScreen(Component.translatable("vivecraft.options.screen.fov"), this, ClientConfig.fovConfig())))
.size(SettingsList.WidgetEntry.valueButtonWidth, 20)
.build()));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.camera"),
Button.builder(Component.translatable("vivecraft.options.screen.camera"), button -> this.minecraft.setScreen(new GuiConfigListScreen(Component.translatable("vivecraft.options.screen.camera"), this, ClientConfig.cameraConfig())))
.size(SettingsList.WidgetEntry.valueButtonWidth, 20)
.build()));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.mr"),
Button.builder(Component.translatable("vivecraft.options.screen.mr"), button -> this.minecraft.setScreen(new GuiConfigListScreen(Component.translatable("vivecraft.options.screen.mr"), this, ClientConfig.mrConfig())))
.size(SettingsList.WidgetEntry.valueButtonWidth, 20)
.build()));

entries.add(new SettingsList.WidgetEntry(
Component.translatable("vivecraft.options.screen.keyboard"),
Button.builder(Component.translatable("vivecraft.options.screen.keyboard"), button -> this.minecraft.setScreen(new GuiConfigListScreen(Component.translatable("vivecraft.options.screen.keyboard"), this, ClientConfig.keyboardConfig())))
.size(SettingsList.WidgetEntry.valueButtonWidth, 20)
.build()));

entries.add(new SettingsList.CategoryEntry(Component.literal("Radial")));

entries.add(SettingsList.ConfigToEntry(ClientConfig.radialModeHold, Component.literal(ClientConfig.radialModeHold.getPath())));
entries.add(SettingsList.ConfigToEntry(ClientConfig.main, Component.literal(ClientConfig.main.getPath())));
entries.add(SettingsList.ConfigToEntry(ClientConfig.alt, Component.literal(ClientConfig.alt.getPath())));

entries.add(new SettingsList.CategoryEntry(Component.literal("Quick Commands")));

entries.add(SettingsList.ConfigToEntry(ClientConfig.commands, Component.literal(ClientConfig.commands.getPath())));

return entries;
}
}
Loading