Skip to content

Commit

Permalink
Finish code base rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Dec 24, 2024
1 parent e31e76a commit ac388af
Show file tree
Hide file tree
Showing 62 changed files with 443 additions and 356 deletions.
4 changes: 4 additions & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ plugins {

dependencies {
compileOnly "com.viaversion:viaversion-common:5.2.1-SNAPSHOT"
compileOnly ("net.raphimc:ViaLoader:3.0.4") {
exclude group: "com.google.guava", module: "guava"
exclude group: "org.slf4j", module: "slf4j-api"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
package com.viaversion.viafabricplus.api;

import com.viaversion.viafabricplus.ViaFabricPlus;
import com.viaversion.viafabricplus.api.events.ChangeProtocolVersionCallback;
import com.viaversion.viafabricplus.api.events.LoadingCycleCallback;
import com.viaversion.viafabricplus.api.settings.SettingGroup;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import io.netty.channel.Channel;
import org.apache.logging.log4j.Logger;

import java.nio.file.Path;
import java.util.List;

/**
* General API point for mods. Get instance via {@link ViaFabricPlus#getImpl()}.
Expand Down Expand Up @@ -118,4 +122,26 @@ default int apiVersion() {
*/
int getMaxChatLength(final ProtocolVersion version);

/**
* All setting groups of the mod.
*
* @return The setting groups
*/
List<SettingGroup> settingGroups();

/**
* Add a setting group to the mod.
*
* @param group The setting group
*/
void addSettingGroup(final SettingGroup group);

/**
* Get a setting group by its translationKey.
*
* @param translationKey The translationKey of the setting group
* @return The setting group or null if it does not exist
*/
SettingGroup getSettingGroup(final String translationKey);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viafabricplus.api;
package com.viaversion.viafabricplus.api.events;

import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viafabricplus.api;
package com.viaversion.viafabricplus.api.events;

/**
* This event is fired in various loading stages of ViaFabricPlus. See {@link LoadingCycle} for more information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viafabricplus.settings.base;
package com.viaversion.viafabricplus.api.settings;

import com.google.gson.JsonObject;
import com.viaversion.viafabricplus.base.screen.VFPListEntry;
import com.viaversion.viafabricplus.util.ChatUtil;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;

import java.util.function.Supplier;

Expand All @@ -37,43 +33,38 @@
*/
public abstract class AbstractSetting<T> {

private final MutableText name;
private final String translationKey;
private final T defaultValue;

private T value;

private Supplier<Text> tooltip;
private Supplier<String> tooltip;

public AbstractSetting(final SettingGroup parent, final MutableText name, final T defaultValue) {
this.name = name;
public AbstractSetting(final SettingGroup parent, final String translationKey, final T defaultValue) {
this.translationKey = translationKey;
this.defaultValue = defaultValue;

this.value = defaultValue;

parent.getSettings().add(this);
}

/**
* This method is used to create a renderer for the setting.
* @return The renderer, see {@link VFPListEntry} for more details.
*/
public abstract VFPListEntry makeSettingRenderer();

public abstract void write(final JsonObject object);

public abstract void read(final JsonObject object);

public void onValueChanged() {
}

public MutableText getName() {
return name;
public String getTranslationKey() {
return translationKey;
}

/**
* @return The translation key of the name.
* @return a more readable name for the config.
*/
public String getTranslationKey() {
return mapTranslationKey(ChatUtil.uncoverTranslationKey(name));
public String getConfigName() {
return mapTranslationKey(getTranslationKey());
}

/**
Expand All @@ -99,19 +90,19 @@ public void setValue(T value) {
onValueChanged();
}

public Text getTooltip() {
public String getTooltip() {
if (tooltip == null) {
return null;
} else {
return tooltip.get();
}
}

public void setTooltip(Text tooltip) {
public void setTooltip(String tooltip) {
this.tooltip = () -> tooltip;
}

public void setTooltip(Supplier<Text> tooltip) {
public void setTooltip(Supplier<String> tooltip) {
this.tooltip = tooltip;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viafabricplus.settings.base;

import net.minecraft.text.Text;
package com.viaversion.viafabricplus.api.settings;

import java.util.ArrayList;
import java.util.List;

/**
* This class represents a group of settings. It is used to group settings in the settings screen.
*
* @see AbstractSetting
*/
public class SettingGroup {

private final List<AbstractSetting<?>> settings = new ArrayList<>();
private final Text name;
private final String translationKey;

public SettingGroup(Text name) {
this.name = name;
public SettingGroup(String translationKey) {
this.translationKey = translationKey;
}

/**
Expand All @@ -49,8 +48,23 @@ public List<AbstractSetting<?>> getSettings() {
return settings;
}

public Text getName() {
return name;
/**
* Returns a setting by its name.
*
* @param name The name of the setting.
* @return The setting or null if it does not exist.
*/
public AbstractSetting<?> getSetting(final String name) {
for (AbstractSetting<?> setting : settings) {
if (setting.getTranslationKey().equals(name)) {
return setting;
}
}
return null;
}

public String getTranslationKey() {
return translationKey;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viafabricplus.settings.base;
package com.viaversion.viafabricplus.api.settings.type;

import com.google.gson.JsonObject;
import com.viaversion.viafabricplus.base.screen.VFPListEntry;
import com.viaversion.viafabricplus.screen.settings.BooleanSettingRenderer;
import net.minecraft.text.MutableText;
import com.viaversion.viafabricplus.api.settings.AbstractSetting;
import com.viaversion.viafabricplus.api.settings.SettingGroup;

public class BooleanSetting extends AbstractSetting<Boolean> {

public BooleanSetting(SettingGroup parent, MutableText name, Boolean defaultValue) {
super(parent, name, defaultValue);
}

@Override
public VFPListEntry makeSettingRenderer() {
return new BooleanSettingRenderer(this);
public BooleanSetting(SettingGroup parent, String translationKey, Boolean defaultValue) {
super(parent, translationKey, defaultValue);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,34 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viafabricplus.settings.base;
package com.viaversion.viafabricplus.api.settings.type;

import com.google.gson.JsonObject;
import com.viaversion.viafabricplus.base.screen.VFPListEntry;
import com.viaversion.viafabricplus.screen.settings.ButtonSettingRenderer;
import net.minecraft.text.MutableText;
import com.viaversion.viafabricplus.api.settings.AbstractSetting;
import com.viaversion.viafabricplus.api.settings.SettingGroup;

public class ButtonSetting extends AbstractSetting<Runnable> {

public ButtonSetting(SettingGroup parent, MutableText name, Runnable onClick) {
super(parent, name, onClick);
public ButtonSetting(SettingGroup parent, String translationKey, Runnable onClick) {
super(parent, translationKey, onClick);
}

@Override
public VFPListEntry makeSettingRenderer() {
return new ButtonSettingRenderer(this);
}

public MutableText displayValue() {
return getName();
/**
* If overriden, this string is used as replacement for the provided translation key in the GUI. Note that this function
* does not support localization.
*
* @return The string to display in the GUI
*/
public String displayString() {
return null;
}

@Override
public void write(JsonObject object) {}
public void write(JsonObject object) {
}

@Override
public void read(JsonObject object) {}
public void read(JsonObject object) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,37 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.viaversion.viafabricplus.settings.base;
package com.viaversion.viafabricplus.api.settings.type;

import com.google.gson.JsonObject;
import com.viaversion.viafabricplus.base.screen.VFPListEntry;
import com.viaversion.viafabricplus.screen.settings.ModeSettingRenderer;
import com.viaversion.viafabricplus.util.ChatUtil;
import net.minecraft.text.MutableText;
import com.viaversion.viafabricplus.api.settings.AbstractSetting;
import com.viaversion.viafabricplus.api.settings.SettingGroup;

import java.util.Arrays;

public class ModeSetting extends AbstractSetting<MutableText> {
public class ModeSetting extends AbstractSetting<String> {

private final MutableText[] options;
private final String[] options;

public ModeSetting(SettingGroup parent, MutableText name, MutableText... options) {
this(parent, name, 0, options);
public ModeSetting(SettingGroup parent, String translationKey, String... options) {
this(parent, translationKey, 0, options);
}

public ModeSetting(SettingGroup parent, MutableText name, int defaultOption, MutableText... options) {
super(parent, name, options[defaultOption]);
public ModeSetting(SettingGroup parent, String translationKey, int defaultOption, String... options) {
super(parent, translationKey, options[defaultOption]);
this.options = options;
}

@Override
public VFPListEntry makeSettingRenderer() {
return new ModeSettingRenderer(this);
}

@Override
public void write(JsonObject object) {
object.addProperty(getTranslationKey(), mapTranslationKey(ChatUtil.uncoverTranslationKey(getValue())));
object.addProperty(getTranslationKey(), mapTranslationKey(getValue()));
}

@Override
public void read(JsonObject object) {
final String selected = object.get(getTranslationKey()).getAsString();
for (MutableText option : options) {
if (mapTranslationKey(ChatUtil.uncoverTranslationKey(option)).equals(selected)) {
for (String option : options) {
if (mapTranslationKey(option).equals(selected)) {
setValue(option);
break;
}
Expand All @@ -71,7 +64,7 @@ public int getIndex() {
return Arrays.stream(options).toList().indexOf(getValue());
}

public MutableText[] getOptions() {
public String[] getOptions() {
return options;
}

Expand Down
Loading

0 comments on commit ac388af

Please sign in to comment.