Skip to content

Commit 688a20a

Browse files
committed
Fix crashes on dedicated servers; slightly improve logs
1 parent 3bf52ff commit 688a20a

9 files changed

Lines changed: 82 additions & 22 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ yarn_mappings=1.21.1+build.3
77
loader_version=0.16.10
88
fabric_version=0.115.1+1.21.1
99

10-
mod_version=1.0.0-alpha.18
10+
mod_version=1.0.0-alpha.19
1111
maven_group=falseresync.vivatech
1212
archives_base_name=vivatech
1313

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package falseresync.lib.logging;
2+
3+
import org.slf4j.Logger;
4+
5+
public class BetterLogger {
6+
private final Logger delegate;
7+
private final String prefix;
8+
9+
public BetterLogger(Logger logger, String prefix) {
10+
this.delegate = logger;
11+
this.prefix = "[%s]".formatted(prefix);
12+
}
13+
14+
public Logger getDelegate() {
15+
return delegate;
16+
}
17+
18+
public String getPrefix() {
19+
return prefix;
20+
}
21+
22+
public void trace(Object msg) {
23+
delegate.trace("{} {}", prefix, msg);
24+
}
25+
26+
public void debug(Object msg) {
27+
delegate.debug("{} {}", prefix, msg);
28+
}
29+
30+
public void info(Object msg) {
31+
delegate.info("{} {}", prefix, msg);
32+
}
33+
34+
public void warn(Object msg) {
35+
delegate.warn("{} {}", prefix, msg);
36+
}
37+
38+
public void error(Object msg) {
39+
delegate.error("{} {}", prefix, msg);
40+
}
41+
}

src/main/java/falseresync/lib/registry/AutoRegistry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ public <T> AutoRegistry link(Registry<T> registry, Class<?>... holderClasses) {
5555
try {
5656
var registryObject = field.get(holderClass);
5757
if (registryObject == null) {
58-
logger.warn("Found a null @RegistryObject field, discarding: %s at %s".formatted(field.getName(), holderClass.getCanonicalName()));
58+
logger.warn("[AutoRegistry / %s] Found a null @RegistryObject field, discarding: %s at %s".formatted(modId, field.getName(), holderClass.getCanonicalName()));
5959
continue;
6060
}
6161

6262
//noinspection unchecked
6363
Registry.register(registry, Identifier.of(modId, field.getName().toLowerCase()), (T) registryObject);
6464
} catch (IllegalAccessException e) {
65-
throw new InaccessibleObjectException("Couldn't read a @RegistryObject field: %s at %s".formatted(field.getName(), holderClass.getCanonicalName()));
65+
throw new InaccessibleObjectException("[AutoRegistry / %s] Couldn't read a @RegistryObject field: %s at %s".formatted(modId, field.getName(), holderClass.getCanonicalName()));
6666
} catch (ClassCastException e) {
67-
throw new IllegalArgumentException("A @RegistryObject field's type doesn't match the provided registry: %s at %s".formatted(field.getName(), holderClass.getCanonicalName()));
67+
throw new IllegalArgumentException("[AutoRegistry / %s] A @RegistryObject field's type doesn't match the provided registry: %s at %s".formatted(modId, field.getName(), holderClass.getCanonicalName()));
6868
}
6969
}
7070
}

src/main/java/falseresync/vivatech/client/VivatechClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package falseresync.vivatech.client;
22

3+
import falseresync.lib.logging.BetterLogger;
34
import falseresync.vivatech.client.gui.VivatechGui;
45
import falseresync.vivatech.client.hud.VivatechHud;
56
import falseresync.vivatech.client.particle.VivatechParticleFactories;
67
import falseresync.vivatech.client.rendering.VivatechRendering;
8+
import falseresync.vivatech.common.Vivatech;
79
import falseresync.vivatech.common.config.TranslatableEnum;
810
import falseresync.vivatech.common.config.TranslatableEnumGuiProvider;
911
import falseresync.vivatech.common.config.VivatechConfig;
@@ -13,8 +15,10 @@
1315
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientChunkEvents;
1416
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
1517
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
18+
import org.slf4j.LoggerFactory;
1619

1720
public class VivatechClient implements ClientModInitializer {
21+
public static final BetterLogger LOGGER = new BetterLogger(LoggerFactory.getLogger(Vivatech.MOD_ID), "Vivatech / Client");
1822
private static ClientWireManager clientWireManager;
1923
private static VivatechHud hud;
2024
private static ToolManager toolManager;
@@ -42,6 +46,8 @@ public void onInitializeClient() {
4246
ClientTickEvents.END_CLIENT_TICK.register(client -> {
4347
clientWireManager.tick();
4448
});
49+
50+
LOGGER.info("Initialized");
4551
}
4652

4753
public static ClientWireManager getClientWireManager() {

src/main/java/falseresync/vivatech/client/rendering/world/WireRenderer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ public void afterEntities(WorldRenderContext context) {
7272
var cameraPos = context.camera().getPos();
7373

7474
for (var wire : wires) {
75-
var wireEnd = wire.start().relativize(wire.end()).toVector3f();
76-
var light = WorldRenderer.getLightmapCoordinates(context.world(), BlockPos.ofFloored(wire.middle()));
75+
var wireEnd = wire.end().sub(wire.start(), new Vector3f());
76+
var light = WorldRenderer.getLightmapCoordinates(context.world(), BlockPos.ofFloored(wire.middle().x, wire.middle().y, wire.middle().z));
7777

7878
matrices.push();
7979

80-
var cameraAdjustment = cameraPos.relativize(wire.start());
80+
var cameraAdjustment = wire.start().sub(cameraPos.toVector3f(), new Vector3f());
8181
matrices.translate(cameraAdjustment.x, cameraAdjustment.y, cameraAdjustment.z);
8282

8383
var positionMatrix = matrices.peek().getPositionMatrix();

src/main/java/falseresync/vivatech/common/Vivatech.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package falseresync.vivatech.common;
22

3+
import falseresync.lib.logging.BetterLogger;
34
import falseresync.lib.registry.AutoRegistry;
45
import falseresync.vivatech.common.block.VivatechBlocks;
56
import falseresync.vivatech.common.blockentity.VivatechBlockEntities;
@@ -10,7 +11,6 @@
1011
import falseresync.vivatech.common.item.VivatechItemGroups;
1112
import falseresync.vivatech.common.item.VivatechItems;
1213
import falseresync.vivatech.common.item.focus.TransmutationFocusBehavior;
13-
import falseresync.vivatech.common.power.Grid;
1414
import falseresync.vivatech.common.power.PowerSystem;
1515
import falseresync.vivatech.common.power.ServerGridsLoader;
1616
import falseresync.vivatech.common.power.WireType;
@@ -25,12 +25,11 @@
2525
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
2626
import net.minecraft.registry.Registries;
2727
import net.minecraft.util.Identifier;
28-
import org.slf4j.Logger;
2928
import org.slf4j.LoggerFactory;
3029

3130
public class Vivatech implements ModInitializer {
3231
public static final String MOD_ID = "vivatech";
33-
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
32+
public static final BetterLogger LOGGER = new BetterLogger(LoggerFactory.getLogger(MOD_ID), "Vivatech");
3433
private static ChargeManager chargeManager;
3534
private static VivatechConfig config;
3635
private static ServerGridsLoader serverGridsLoader;
@@ -53,11 +52,14 @@ public static Identifier vtId(String path) {
5352

5453
@Override
5554
public void onInitialize() {
55+
LOGGER.warn("This is an alpha version of the mod! It is very unstable and might break your world, especially when updating.");
56+
LOGGER.warn("Use at your own risk, but I will appreciate a bug report should anything happen");
57+
5658
config = AutoConfig.register(VivatechConfig.class, JanksonConfigSerializer::new).getConfig();
5759

5860
VivatechBlocks.registerAll();
5961
VivatechItems.registerAll();
60-
new AutoRegistry(MOD_ID, LOGGER)
62+
new AutoRegistry(MOD_ID, LOGGER.getDelegate())
6163
.link(Registries.BLOCK_ENTITY_TYPE, VivatechBlockEntities.class)
6264
.link(Registries.ITEM_GROUP, VivatechItemGroups.class)
6365
.link(Registries.DATA_COMPONENT_TYPE, VivatechComponents.class)
@@ -92,5 +94,7 @@ public void onInitialize() {
9294
ServerTickEvents.START_WORLD_TICK.register(world -> {
9395
serverGridsLoader.tick(world);
9496
});
97+
98+
LOGGER.info("Initialized");
9599
}
96100
}

src/main/java/falseresync/vivatech/common/power/ServerGridsLoader.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ private List<GridSnapshot> loadSnapshots(World world) {
7575
throw eOuter;
7676
}
7777
} catch (IOException e) {
78-
LOGGER.error("Couldn't access power systems in {}", filePath, e);
78+
LOGGER.error("Couldn't access power systems in %s".formatted(filePath));
79+
LOGGER.error(e);
7980
} catch (IllegalStateException e) {
80-
LOGGER.error("Couldn't parse power systems in {}", filePath, e);
81+
LOGGER.error("Couldn't parse power systems in %s".formatted(filePath));
82+
LOGGER.error(e);
8183
}
8284
}
8385

@@ -111,9 +113,11 @@ public void save(ServerWorld world) {
111113

112114
fileOutputStream.close();
113115
} catch (IOException e) {
114-
LOGGER.error("Couldn't save power systems to {}", filePath, e);
116+
LOGGER.error("Couldn't save power systems to %s".formatted(filePath));
117+
LOGGER.error(e);
115118
} catch (IllegalStateException e) {
116-
LOGGER.error("Couldn't serialize power systems for {}", filePath, e);
119+
LOGGER.error("Couldn't serialize power systems for %s".formatted(filePath));
120+
LOGGER.error(e);
117121
}
118122
}
119123

src/main/java/falseresync/vivatech/common/power/Wire.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
import net.minecraft.util.ItemScatterer;
1010
import net.minecraft.util.math.*;
1111
import net.minecraft.world.World;
12+
import org.joml.Vector3f;
1213

1314
import java.util.Objects;
1415

1516
public record Wire(
1617
ImmutableSet<BlockPos> positions,
1718
BlockPos u,
1819
BlockPos v,
19-
@Environment(EnvType.CLIENT) Vec3d start,
20-
@Environment(EnvType.CLIENT) Vec3d end,
21-
Vec3d middle,
20+
Vector3f start,
21+
Vector3f end,
22+
Vector3f middle,
2223
ChunkPos chunkPos
2324
) {
2425
public static final PacketCodec<RegistryByteBuf, Wire> PACKET_CODEC = PacketCodec.tuple(
@@ -28,15 +29,19 @@ public record Wire(
2829
);
2930

3031
public static Wire createServerWire(BlockPos u, BlockPos v) {
31-
var middle = u.add(v).toCenterPos().multiply(0.5f);
32+
var middle = toCenterPos(u.add(v)).mul(0.5f);
3233
var chunkPos = new ChunkPos(ChunkSectionPos.getSectionCoordFloored(middle.x), ChunkSectionPos.getSectionCoordFloored(middle.z));
3334
return new Wire(ImmutableSet.of(u, v), u, v, null, null, middle, chunkPos);
3435
}
3536

3637
public static Wire createClientWire(BlockPos u, BlockPos v) {
37-
var middle = u.add(v).toCenterPos().multiply(0.5f);
38+
var middle = toCenterPos(u.add(v)).mul(0.5f);
3839
var chunkPos = new ChunkPos(ChunkSectionPos.getSectionCoordFloored(middle.x), ChunkSectionPos.getSectionCoordFloored(middle.z));
39-
return new Wire(ImmutableSet.of(u, v), u, v, u.toCenterPos(), v.toCenterPos(), middle, chunkPos);
40+
return new Wire(ImmutableSet.of(u, v), u, v, toCenterPos(u), toCenterPos(v), middle, chunkPos);
41+
}
42+
43+
private static Vector3f toCenterPos(BlockPos pos) {
44+
return new Vector3f(pos.getX() + 0.5f, pos.getY() + 0.5f, pos.getZ() + 0.5f);
4045
}
4146

4247
public void drop(World world, WireType type) {

src/main/resources/fabric.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
],
3636
"depends": {
3737
"fabricloader": ">=0.16.9",
38-
"minecraft": "~1.21.1",
38+
"minecraft": "1.21.1",
3939
"java": ">=21",
4040
"fabric-api": "*",
4141
"cloth-config": "*",

0 commit comments

Comments
 (0)