Skip to content

Add Vec3iTuple Config #145

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

Merged
merged 14 commits into from
Jan 8, 2025
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package top.hendrixshen.magiclib.util.serializable;

import com.google.gson.JsonElement;
import org.jetbrains.annotations.NotNull;
import top.hendrixshen.magiclib.MagicLib;

public interface JsonSerializable<O, J extends JsonElement> {
J serialize(O object);

O deserialize(@NotNull J jsonElement);

default O deserializeSafe(@NotNull J jsonElement, @NotNull O defaultValue) {
try {
return this.deserialize(jsonElement);
} catch (Exception e) {
MagicLib.getLogger().warn("Failed to load data of {} from json object {}: {}",
this.getClass().getSimpleName(), jsonElement, e);
return defaultValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public interface ConfigVec3i extends IConfigResettable, MagicIConfigBase {
default Vec3i getVec3i() {
return new Vec3i(getX(), getY(), getZ());
return new Vec3i(this.getX(), this.getY(), this.getZ());
}

default void setVec3i(Vec3i value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package top.hendrixshen.magiclib.api.malilib.config.option;

import net.minecraft.core.Vec3i;

public interface ConfigVec3iTuple extends ConfigVec3i {
default Vec3i getFirstVec3i() {
return this.getVec3i();
}

default Vec3i getSecondVec3i() {
return new Vec3i(this.getSecondX(), this.getSecondY(), this.getSecondZ());
}

default void setFirstVec3i(Vec3i value) {
this.setVec3i(value);
}

default void setSecondVec3i(Vec3i value) {
this.setSecondX(value.getX());
this.setSecondY(value.getY());
this.setSecondZ(value.getZ());
}

default Vec3i getDefaultFirstVec3iValue() {
return this.getDefaultVec3iValue();
}

Vec3i getDefaultSecondVec3iValue();

default int getFirstX() {
return this.getX();
}

default int getFirstY() {
return this.getY();
}

default int getFirstZ() {
return this.getZ();
}

int getSecondX();

int getSecondY();

int getSecondZ();

default void setFirstX(int x) {
this.setX(x);
}

default void setFirstY(int y) {
this.setY(y);
}

default void setFirstZ(int z) {
this.setZ(z);
}

void setSecondX(int x);

void setSecondY(int y);

void setSecondZ(int z);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package top.hendrixshen.magiclib.api.malilib.config.option;

import com.google.common.collect.ImmutableList;
import fi.dy.masa.malilib.config.IConfigResettable;
import lombok.*;
import net.minecraft.core.Vec3i;

import java.util.List;

public interface ConfigVec3iTupleList extends IConfigResettable, MagicIConfigBase {
List<Entry> getVec3iTupleList();

void setVec3iTupleList(List<Entry> vec3iTuples);

ImmutableList<Entry> getDefaultVec3iTupleList();

@Getter
@ToString
@EqualsAndHashCode
@AllArgsConstructor
class Entry {
public static Entry ZERO = new Entry(Vec3i.ZERO, Vec3i.ZERO);

private final Vec3i firstVec3i;
private final Vec3i secondVec3i;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import top.hendrixshen.magiclib.api.malilib.annotation.Config;
import top.hendrixshen.magiclib.api.malilib.annotation.Statistic;
import top.hendrixshen.magiclib.api.malilib.config.MagicConfigManager;
import top.hendrixshen.magiclib.api.malilib.config.option.ConfigVec3iTupleList;
import top.hendrixshen.magiclib.api.malilib.config.option.EnumOptionEntry;
import top.hendrixshen.magiclib.impl.malilib.SharedConstants;
import top.hendrixshen.magiclib.impl.malilib.config.MagicConfigFactory;
Expand Down Expand Up @@ -78,6 +79,12 @@ public class Configs {
@Config(category = ConfigCategory.TEST, debugOnly = true)
public static final MagicConfigVec3iList testConfigVec3iList = Configs.cf.newConfigVec3iList("testConfigVec3iList", ImmutableList.of(new Vec3i(1, 2, 3), new Vec3i(-1, -2, -3)));

@Config(category = ConfigCategory.TEST, debugOnly = true)
public static final MagicConfigVec3iTuple testConfigVec3iTuple = Configs.cf.newConfigVec3iTuple("testConfigVec3iTuple", new Vec3i(1, 0, -1), new Vec3i(5, 0, -5));

@Config(category = ConfigCategory.TEST, debugOnly = true)
public static final MagicConfigVec3iTupleList testConfigVec3iTupleList = Configs.cf.newConfigVec3iTupleList("testConfigVec3iTupleList", ImmutableList.of(new ConfigVec3iTupleList.Entry(new Vec3i(1, 2, 3), new Vec3i(-1, -2, -3))));

@Dependencies(
conflict = @Dependency(dependencyType = DependencyType.MOD_ID, value = "minecraft", versionPredicates = "<2.0"),
require = @Dependency(dependencyType = DependencyType.MOD_ID, value = "dummy-lib", versionPredicates = "*")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import fi.dy.masa.malilib.config.IConfigOptionListEntry;
import fi.dy.masa.malilib.hotkeys.KeybindSettings;
import net.minecraft.core.Vec3i;
import top.hendrixshen.magiclib.api.malilib.config.option.ConfigVec3iTupleList;
import top.hendrixshen.magiclib.impl.malilib.config.option.*;

/**
Expand Down Expand Up @@ -160,4 +161,20 @@ public MagicConfigVec3iList newConfigVec3iList(String name) {
public MagicConfigVec3iList newConfigVec3iList(String name, ImmutableList<Vec3i> defaultValue) {
return new MagicConfigVec3iList(this.identifier, name, defaultValue);
}

public MagicConfigVec3iTuple newConfigVec3iTuple(String name) {
return new MagicConfigVec3iTuple(this.identifier, name);
}

public MagicConfigVec3iTuple newConfigVec3iTuple(String name, Vec3i defaultFirstValue, Vec3i defaultSecondValue) {
return new MagicConfigVec3iTuple(this.identifier, name, defaultFirstValue, defaultSecondValue);
}

public MagicConfigVec3iTupleList newConfigVec3iTupleList(String name) {
return new MagicConfigVec3iTupleList(this.identifier, name, ImmutableList.of());
}

public MagicConfigVec3iTupleList newConfigVec3iTupleList(String name, ImmutableList<ConfigVec3iTupleList.Entry> defaultValue) {
return new MagicConfigVec3iTupleList(this.identifier, name, defaultValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.mojang.blaze3d.vertex.PoseStack;
//#endif

public class GuiVec3Edit extends GuiBase {
public class GuiVec3iEdit extends GuiBase {
@Getter
protected final ConfigVec3i config;
protected final MagicConfigGui configGui;
Expand All @@ -31,7 +31,7 @@ public class GuiVec3Edit extends GuiBase {
protected final IDialogHandler dialogHandler;
protected WidgetVec3iEdit widget;

public GuiVec3Edit(ConfigVec3i config, MagicConfigGui configGui, @Nullable IDialogHandler dialogHandler, Screen parent) {
public GuiVec3iEdit(ConfigVec3i config, MagicConfigGui configGui, @Nullable IDialogHandler dialogHandler, Screen parent) {
super();
this.config = config;
this.configGui = configGui;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package top.hendrixshen.magiclib.impl.malilib.config.gui;

import fi.dy.masa.malilib.config.ConfigManager;
import fi.dy.masa.malilib.gui.GuiBase;
import fi.dy.masa.malilib.gui.interfaces.IDialogHandler;
import fi.dy.masa.malilib.render.RenderUtils;
import lombok.Getter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;
import top.hendrixshen.magiclib.api.i18n.I18n;
import top.hendrixshen.magiclib.api.malilib.config.option.ConfigVec3iTuple;
import top.hendrixshen.magiclib.impl.malilib.config.gui.widget.WidgetVec3iTupleEdit;

//#if MC > 11904
//$$ import net.minecraft.client.gui.GuiGraphics;
//#elseif MC > 11502
import com.mojang.blaze3d.vertex.PoseStack;
//#endif

public class GuiVec3iTupleEdit extends GuiBase {
@Getter
protected final ConfigVec3iTuple config;
protected final MagicConfigGui configGui;
protected int dialogWidth;
protected int dialogHeight;
protected int dialogLeft;
protected int dialogTop;
@Nullable
protected final IDialogHandler dialogHandler;
protected WidgetVec3iTupleEdit widget;

public GuiVec3iTupleEdit(ConfigVec3iTuple config, MagicConfigGui configGui, @Nullable IDialogHandler dialogHandler, Screen parent) {
this.config = config;
this.configGui = configGui;
this.dialogHandler = dialogHandler;
this.title = I18n.tr("magiclib.config.gui.title.vec3i_tuple_edit", config.getName());

if (this.dialogHandler == null) {
this.setParent(parent);
}
}

protected void setWidthAndHeight() {
this.dialogWidth = 300;
this.dialogHeight = 70;
}

protected void centerOnScreen() {
if (this.getParent() != null) {
this.dialogLeft = this.getParent().width / 2 - this.dialogWidth / 2;
this.dialogTop = this.getParent().height / 2 - this.dialogHeight / 2;
} else {
this.dialogLeft = 20;
this.dialogTop = 20;
}
}

protected void reCreateWidget() {
this.widget = this.createListWidget();
this.clearWidgets();
this.addWidget(this.widget);
}

//#if MC > 11605
//$$ @Override
//$$ public void initGui() {
//$$ this.setWidthAndHeight();
//$$ this.centerOnScreen();
//$$ super.initGui();
//$$ this.reCreateWidget();
//$$ }
//#else
@Override
public void init(Minecraft mc, int width, int height) {
if (this.getParent() != null) {
this.getParent().init(mc, width, height);
}

super.init(mc, width, height);
this.setWidthAndHeight();
this.centerOnScreen();
this.initGui();
this.reCreateWidget();
}
//#endif

protected WidgetVec3iTupleEdit createListWidget() {
return new WidgetVec3iTupleEdit(this.dialogLeft + 10, this.dialogTop + 20,
this.dialogWidth - 14, this.dialogHeight - 30,
this.config.getFirstVec3i(), this.config.getDefaultFirstVec3iValue(),
this.config.getSecondVec3i(), this.config.getDefaultSecondVec3iValue(),
(firstVec3i, secondVec3i) -> {
this.config.setFirstVec3i(firstVec3i);
this.config.setSecondVec3i(secondVec3i);
});
}

@Override
public void removed() {
if (this.widget.wasConfigModified()) {
this.widget.applyNewValueToConfig();
ConfigManager.getInstance().onConfigsChanged(this.configGui.getModId());
}

super.removed();
}

@Override
public void render(
//#if MC > 11904
//$$ GuiGraphics poseStackOrGuiGraphics,
//#elseif MC > 11502
PoseStack poseStackOrGuiGraphics,
//#endif
int mouseX,
int mouseY,
float partialTicks
) {
if (this.getParent() != null) {
this.getParent().render(
//#if MC > 11502
poseStackOrGuiGraphics,
//#endif
mouseX,
mouseY,
partialTicks
);
}

super.render(
//#if MC > 11502
poseStackOrGuiGraphics,
//#endif
mouseX,
mouseY,
partialTicks
);
}

@Override
protected void drawScreenBackground(int mouseX, int mouseY) {
RenderUtils.drawOutlinedBox(this.dialogLeft, this.dialogTop, this.dialogWidth, this.dialogHeight, 0xFF000000, 0xFF999999);
}

@Override
protected void drawTitle(
//#if MC > 11904
//$$ GuiGraphics poseStackOrGuiGraphics,
//#elseif MC > 11502
PoseStack poseStackOrGuiGraphics,
//#endif
int mouseX,
int mouseY,
float partialTicks
) {
this.drawStringWithShadow(
//#if MC > 11502
poseStackOrGuiGraphics,
//#endif
this.title,
this.dialogLeft + 10,
this.dialogTop + 6,
-1
);
}

@Override
public boolean onKeyTyped(int keyCode, int scanCode, int modifiers) {
if (keyCode == GLFW.GLFW_KEY_ESCAPE && this.dialogHandler != null) {
this.dialogHandler.closeDialog();
return true;
} else {
return super.onKeyTyped(keyCode, scanCode, modifiers);
}
}
}
Loading