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

Json5 Config! #102

Open
wants to merge 3 commits into
base: 1.20.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

## License Notes
Some code is used from other projects with other licenses.
* The package `io.github.xypercode.craftyconfig` and all it's subpackages are licensed under AGPL v3.
Originates in [Ultracraft](https://github.com/Ultreon/ultracraft)
* The package `com.jab125` and all it's subpackages are licensed the GPL 3.0.
* Everything in [updater/](updater) also is licensed under the GPL 3.0.

Expand Down
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ subprojects {
url 'https://maven.quiltmc.org/repository/release/'
}

maven {
name "Jitpack"
url "https://jitpack.io"
}


maven {
url "https://cursemaven.com"
Expand All @@ -78,6 +83,8 @@ subprojects {
parchment("org.parchmentmc.data:parchment-1.20.1:2023.09.03@zip")
}

implementation "com.github.Ultreon:json5-api:d0a559bc9b"

// layered
// The following line declares the yarn mappings you may select this one as well.
// mappings "net.fabricmc:yarn:@YARN_MAPPINGS@:v2"
Expand Down
124 changes: 40 additions & 84 deletions common/src/main/java/com/ultreon/devices/DeviceConfig.java
Original file line number Diff line number Diff line change
@@ -1,99 +1,55 @@
package com.ultreon.devices;

import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.architectury.platform.Platform;
import io.github.xypercode.craftyconfig.ConfigEntry;
import io.github.xypercode.craftyconfig.ConfigInfo;
import io.github.xypercode.craftyconfig.CraftyConfig;
import io.github.xypercode.craftyconfig.Ranged;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraftforge.common.ForgeConfigSpec;

public class DeviceConfig {
private static final String CATEGORY_LAPTOP = "laptopSettings";
public static final ForgeConfigSpec.IntValue PING_RATE;

private static final String CATEGORY_ROUTER = "routerSettings";
public static final ForgeConfigSpec.IntValue SIGNAL_RANGE;
public static final ForgeConfigSpec.IntValue BEACON_INTERVAL;
public static final ForgeConfigSpec.IntValue MAX_DEVICES;

private static final String CATEGORY_PRINTING = "printerSettings";
public static final ForgeConfigSpec.BooleanValue OVERRIDE_PRINT_SPEED;
public static final ForgeConfigSpec.IntValue CUSTOM_PRINT_SPEED;
public static final ForgeConfigSpec.IntValue MAX_PAPER_COUNT;

private static final String CATEGORY_PIXEL_PAINTER = "pixelPainter";
public static final ForgeConfigSpec.BooleanValue PIXEL_PAINTER_ENABLE;
public static final ForgeConfigSpec.BooleanValue RENDER_PRINTED_3D;

public static final String CATEGORY_DEBUG = "debug";
public static final ForgeConfigSpec.BooleanValue DEBUG_BUTTON;

public static final ForgeConfigSpec CONFIG;

static {
ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
PING_RATE = builder.comment("The amount of ticks the laptop waits until sending another ping to it's connected router.")
.defineInRange(CATEGORY_LAPTOP + ".pingRate", 20, 1, 200);

SIGNAL_RANGE = builder.comment("The range that routers can produce a signal to devices. This is the radius in blocks. Be careful when increasing this value, the performance is O(n^3) and larger numbers will have a bigger impact on the server")
.defineInRange(CATEGORY_ROUTER + ".signalRange", 20, 10, 100);
BEACON_INTERVAL = builder.comment("The amount of ticks the router waits before sending out a beacon signal. Higher number will increase performance but devices won't know as quick if they lost connection.")
.defineInRange(CATEGORY_ROUTER + ".beaconInterval", 20, 1, 200);
MAX_DEVICES = builder.comment("The maximum amount of devices that can be connected to the router.")
.defineInRange(CATEGORY_ROUTER + ".maxDevices", 16, 1, 64);

OVERRIDE_PRINT_SPEED = builder.comment("If enable, overrides all printing times with customPrintSpeed property")
.define(CATEGORY_PRINTING + ".overridePrintSpeed", false);
CUSTOM_PRINT_SPEED = builder.comment("The amount of seconds it takes to print a page. This is overridden if overridePrintSpeed is enabled.")
.defineInRange(CATEGORY_PRINTING + ".customPrintSpeed", 20, 1, 600);
MAX_PAPER_COUNT = builder.comment("The maximum amount of paper that can be used in the printer.")
.defineInRange(CATEGORY_PRINTING + ".maxPaperCount", 64, 0, 99);

PIXEL_PAINTER_ENABLE = builder.comment("Enable or disable the Pixel Painter app.")
.define(CATEGORY_PIXEL_PAINTER + ".enabled", true);
RENDER_PRINTED_3D = builder.comment("Should the pixels on printed pictures be render in 3D? Warning, this will decrease the performance of the game. You shouldn't enable it if you have a slow computer.")
.define(CATEGORY_PIXEL_PAINTER + ".renderPrintedIn3d", false);

DEBUG_BUTTON = builder.comment("Display a button to access a worldless laptop")
.define(CATEGORY_DEBUG + ".debugButton", Platform.isDevelopmentEnvironment());

CONFIG = builder.build();
}

// TODO *** Add read/write of synchronization tags of the config file if needed ***
@ConfigInfo(fileName = "devices")
public class DeviceConfig extends CraftyConfig {
@ConfigEntry(path = "laptop.pingRate", comment = "The amount of ticks the laptop waits until sending another ping to it's connected router.")
@Ranged(min = 1, max = 200)
public static int PING_RATE = 20;

@ConfigEntry(path = "router.signalRange", comment = "The range of the router's signal.")
@Ranged(min = 10, max = 100)
public static int SIGNAL_RANGE = 20;
@ConfigEntry(path = "router.beaconInterval", comment = "The amount of ticks between router beacons.")
@Ranged(min = 1, max = 200)
public static int BEACON_INTERVAL = 20;
@ConfigEntry(path = "router.maxDevices", comment = "The maximum amount of devices that can be connected to the router.")
@Ranged(min = 1, max = 64)
public static int MAX_DEVICES = 16;

@ConfigEntry(path = "printer.overridePrintSpeed", comment = "If the printer should override the print speed.")
public static boolean OVERRIDE_PRINT_SPEED = false;
@ConfigEntry(path = "printer.customPrintSpeed", comment = "The custom print speed.")
@Ranged(min = 1, max = 600)
public static int CUSTOM_PRINT_SPEED = 20;
@ConfigEntry(path = "printer.maxPaperCount", comment = "The maximum amount of paper that can be printed.")
@Ranged(min = 1, max = 99)
public static int MAX_PAPER_COUNT = 64;

@ConfigEntry(path = "pixelPainter.enable", comment = "If the pixel painter should be enabled.")
public static boolean PIXEL_PAINTER_ENABLE = true;
@ConfigEntry(path = "pixelPainter.renderPrinted3D", comment = "If the pixel painter should render printed 3D.")
public static boolean RENDER_PRINTED_3D = true;

@ConfigEntry(path = "debug.debugButton", comment = "If the debug button should be enabled.")
public static boolean DEBUG_BUTTON = Platform.isDevelopmentEnvironment();

public static void readSyncTag(CompoundTag tag) {
if (tag.contains("pingRate", Tag.TAG_INT)) PING_RATE.set(tag.getInt("pingRate"));
if (tag.contains("signalRange", Tag.TAG_INT)) SIGNAL_RANGE.set(tag.getInt("signalRange"));
if (tag.contains("pingRate", Tag.TAG_INT)) PING_RATE = tag.getInt("pingRate");
if (tag.contains("signalRange", Tag.TAG_INT)) SIGNAL_RANGE = tag.getInt("signalRange");
}

public static CompoundTag writeSyncTag() {
CompoundTag tag = new CompoundTag();
tag.putInt("pingRate", PING_RATE.get());
tag.putInt("signalRange", SIGNAL_RANGE.get());
tag.putInt("pingRate", PING_RATE);
tag.putInt("signalRange", SIGNAL_RANGE);
return tag;
}

public static void init() {
// NO-OP
}

// @ExpectPlatform
// @PlatformOnly("fabric")
// public static void.json register(ModLoadingContext context) {
// throw new AssertionError();
// //context.registerConfig(ModConfig.Type.CLIENT, CONFIG);
// }

public static void restore() {
// NO-OP
}

public void save() {
CONFIG.save();
}

// @SubscribeEvent
// public static void.json onConfigChanged(ModConfigEvent.Reloading event) {
// // TODO // Implement config reloading if needed.
// }
}
98 changes: 98 additions & 0 deletions common/src/main/java/com/ultreon/devices/DeviceConfigOld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.ultreon.devices;

import dev.architectury.platform.Platform;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraftforge.common.ForgeConfigSpec;

public class DeviceConfigOld {
private static final String CATEGORY_LAPTOP = "laptopSettings";
public static final ForgeConfigSpec.IntValue PING_RATE;

private static final String CATEGORY_ROUTER = "routerSettings";
public static final ForgeConfigSpec.IntValue SIGNAL_RANGE;
public static final ForgeConfigSpec.IntValue BEACON_INTERVAL;
public static final ForgeConfigSpec.IntValue MAX_DEVICES;

private static final String CATEGORY_PRINTING = "printerSettings";
public static final ForgeConfigSpec.BooleanValue OVERRIDE_PRINT_SPEED;
public static final ForgeConfigSpec.IntValue CUSTOM_PRINT_SPEED;
public static final ForgeConfigSpec.IntValue MAX_PAPER_COUNT;

private static final String CATEGORY_PIXEL_PAINTER = "pixelPainter";
public static final ForgeConfigSpec.BooleanValue PIXEL_PAINTER_ENABLE;
public static final ForgeConfigSpec.BooleanValue RENDER_PRINTED_3D;

public static final String CATEGORY_DEBUG = "debug";
public static final ForgeConfigSpec.BooleanValue DEBUG_BUTTON;

public static final ForgeConfigSpec CONFIG;

static {
ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
PING_RATE = builder.comment("The amount of ticks the laptop waits until sending another ping to it's connected router.")
.defineInRange(CATEGORY_LAPTOP + ".pingRate", 20, 1, 200);

SIGNAL_RANGE = builder.comment("The range that routers can produce a signal to devices. This is the radius in blocks. Be careful when increasing this value, the performance is O(n^3) and larger numbers will have a bigger impact on the server")
.defineInRange(CATEGORY_ROUTER + ".signalRange", 20, 10, 100);
BEACON_INTERVAL = builder.comment("The amount of ticks the router waits before sending out a beacon signal. Higher number will increase performance but devices won't know as quick if they lost connection.")
.defineInRange(CATEGORY_ROUTER + ".beaconInterval", 20, 1, 200);
MAX_DEVICES = builder.comment("The maximum amount of devices that can be connected to the router.")
.defineInRange(CATEGORY_ROUTER + ".maxDevices", 16, 1, 64);

OVERRIDE_PRINT_SPEED = builder.comment("If enable, overrides all printing times with customPrintSpeed property")
.define(CATEGORY_PRINTING + ".overridePrintSpeed", false);
CUSTOM_PRINT_SPEED = builder.comment("The amount of seconds it takes to print a page. This is overridden if overridePrintSpeed is enabled.")
.defineInRange(CATEGORY_PRINTING + ".customPrintSpeed", 20, 1, 600);
MAX_PAPER_COUNT = builder.comment("The maximum amount of paper that can be used in the printer.")
.defineInRange(CATEGORY_PRINTING + ".maxPaperCount", 64, 0, 99);

PIXEL_PAINTER_ENABLE = builder.comment("Enable or disable the Pixel Painter app.")
.define(CATEGORY_PIXEL_PAINTER + ".enabled", true);
RENDER_PRINTED_3D = builder.comment("Should the pixels on printed pictures be render in 3D? Warning, this will decrease the performance of the game. You shouldn't enable it if you have a slow computer.")
.define(CATEGORY_PIXEL_PAINTER + ".renderPrintedIn3d", false);

DEBUG_BUTTON = builder.comment("Display a button to access a worldless laptop")
.define(CATEGORY_DEBUG + ".debugButton", Platform.isDevelopmentEnvironment());

CONFIG = builder.build();
}

// TODO *** Add read/write of synchronization tags of the config file if needed ***

public static void readSyncTag(CompoundTag tag) {
if (tag.contains("pingRate", Tag.TAG_INT)) PING_RATE.set(tag.getInt("pingRate"));
if (tag.contains("signalRange", Tag.TAG_INT)) SIGNAL_RANGE.set(tag.getInt("signalRange"));
}

public static CompoundTag writeSyncTag() {
CompoundTag tag = new CompoundTag();
tag.putInt("pingRate", PING_RATE.get());
tag.putInt("signalRange", SIGNAL_RANGE.get());
return tag;
}

public static void init() {
// NO-OP
}

// @ExpectPlatform
// @PlatformOnly("fabric")
// public static void.json register(ModLoadingContext context) {
// throw new AssertionError();
// //context.registerConfig(ModConfig.Type.CLIENT, CONFIG);
// }

public static void restore() {
// NO-OP
}

public void save() {
CONFIG.save();
}

// @SubscribeEvent
// public static void.json onConfigChanged(ModConfigEvent.Reloading event) {
// // TODO // Implement config reloading if needed.
// }
}
10 changes: 8 additions & 2 deletions common/src/main/java/com/ultreon/devices/Devices.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public abstract class Devices {
static List<AppInfo> allowedApps = new ArrayList<>();
private static List<Vulnerability> vulnerabilities = new ArrayList<>();
private static Devices instance;
private static DeviceConfig config;

public static List<Vulnerability> getVulnerabilities() {
return vulnerabilities;
Expand All @@ -115,6 +116,10 @@ public static Devices getInstance() {
return instance;
}

public static DeviceConfig getConfig() {
return config;
}

public void init() {
if (ArchitecturyTarget.getCurrentTarget().equals("fabric")) {
preInit();
Expand Down Expand Up @@ -157,7 +162,8 @@ public static void preInit() {
throw new LaunchException();
}

DeviceConfig.init();
config = new DeviceConfig();
config.load();
}


Expand Down Expand Up @@ -374,7 +380,7 @@ private static void setupClientEvents() {
LOGGER.debug("Client disconnected from server");

allowedApps = null;
DeviceConfig.restore();
Devices.config.load();
}));
}

Expand Down
7 changes: 6 additions & 1 deletion common/src/main/java/com/ultreon/devices/Reference.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.ultreon.devices;

import dev.architectury.injectables.annotations.ExpectPlatform;
import de.marhali.json5.Json5;
import dev.architectury.platform.Platform;

public class Reference {
public static final String MOD_ID = "devices";
public static final String VERSION;
public static final Json5 JSON5 = Json5.builder(builder -> {
builder.quoteless();
builder.indentFactor(4);
return builder.build();
});
private static String[] verInfo;
static {
VERSION = getVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void tick() {
return;

if (connection != null) {
if (++counter >= DeviceConfig.BEACON_INTERVAL.get() * 2) {
if (++counter >= DeviceConfig.BEACON_INTERVAL * 2) {
connection.setRouterPos(null);
counter = 0;
}
Expand Down Expand Up @@ -71,7 +71,7 @@ public boolean isConnected() {
}

public boolean receiveBeacon(Router router) {
if (counter >= DeviceConfig.BEACON_INTERVAL.get() * 2) {
if (counter >= DeviceConfig.BEACON_INTERVAL * 2) {
connect(router);
return true;
}
Expand All @@ -87,7 +87,7 @@ public int getSignalStrength() {
BlockPos routerPos = connection != null ? connection.getRouterPos() : null;
if (routerPos != null) {
double distance = Math.sqrt(worldPosition.distToCenterSqr(routerPos.getX() + 0.5, routerPos.getY() + 0.5, routerPos.getZ() + 0.5));
double level = DeviceConfig.SIGNAL_RANGE.get() / 3d;
double level = DeviceConfig.SIGNAL_RANGE / 3d;
return distance > level * 2 ? 2 : distance > level ? 1 : 0;
}
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ public void setState(State newState) {

state = newState;
if (state == PRINTING) {
if (DeviceConfig.OVERRIDE_PRINT_SPEED.get()) {
remainingPrintTime = DeviceConfig.CUSTOM_PRINT_SPEED.get() * 20;
if (DeviceConfig.OVERRIDE_PRINT_SPEED) {
remainingPrintTime = DeviceConfig.CUSTOM_PRINT_SPEED * 20;
} else {
remainingPrintTime = currentPrint.speed() * 20;
}
Expand Down Expand Up @@ -185,7 +185,7 @@ public int getRemainingPrintTime() {
}

public boolean addPaper(ItemStack stack, boolean addAll) {
if (!stack.isEmpty() && stack.getItem() == Items.PAPER && paperCount < DeviceConfig.MAX_PAPER_COUNT.get()) {
if (!stack.isEmpty() && stack.getItem() == Items.PAPER && paperCount < DeviceConfig.MAX_PAPER_COUNT) {
if (!addAll) {
paperCount++;
stack.shrink(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ public void render(PaperBlockEntity blockEntity, float partialTick, @NotNull Pos
CompoundTag data = print.toTag();
if (data.contains("pixels", Tag.TAG_INT_ARRAY) && data.contains("resolution", Tag.TAG_INT)) {
RenderSystem.setShaderTexture(0, PrinterRenderer.PaperModel.TEXTURE);
if (DeviceConfig.RENDER_PRINTED_3D.get() && !data.getBoolean("cut")) {
if (DeviceConfig.RENDER_PRINTED_3D && !data.getBoolean("cut")) {
// drawCuboid(0, 0, 0, 16, 16, 1, bufferSource);
}

pose.translate(0, 0, DeviceConfig.RENDER_PRINTED_3D.get() ? 0.0625 : 0.001);
pose.translate(0, 0, DeviceConfig.RENDER_PRINTED_3D ? 0.0625 : 0.001);

pose.pushPose();
{
Expand All @@ -124,7 +124,7 @@ public void render(PaperBlockEntity blockEntity, float partialTick, @NotNull Pos

pose.pushPose();
{
if (DeviceConfig.RENDER_PRINTED_3D.get() && data.getBoolean("cut")) {
if (DeviceConfig.RENDER_PRINTED_3D && data.getBoolean("cut")) {
CompoundTag tag = print.toTag();
drawPixels(pose, tag.getIntArray("pixels"), tag.getInt("resolution"), tag.getBoolean("cut"), packedLight, bufferSource);
}
Expand Down
Loading
Loading