Skip to content

Commit f0508b6

Browse files
committed
- Updates FibLib for HopperOptimizations compatibility, closing #14.
- Adds several performance options - Adds command to control aforementioned performance options
1 parent 7f238c9 commit f0508b6

File tree

11 files changed

+223
-99
lines changed

11 files changed

+223
-99
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ yarn_mappings = 1.16.5+build.4
77
loader_version = 0.11.1
88

99
# Mod Properties
10-
mod_version = 1.0.1
10+
mod_version = 1.1.0
1111
maven_group = dev.hephaestus
1212
archives_base_name = sax
1313

1414
# Dependencies
1515
fabric_version = 0.30.3+1.16
16-
fiblib_version = 1.0.1
16+
fiblib_version = 1.0.2
1717
mod_menu_version = 1.14.6+build.31

src/main/java/dev/hephaestus/sax/SAX.java

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
package dev.hephaestus.sax;
22

3+
import com.mojang.brigadier.arguments.BoolArgumentType;
4+
import com.mojang.brigadier.arguments.IntegerArgumentType;
5+
import com.mojang.brigadier.arguments.StringArgumentType;
6+
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
7+
import com.mojang.brigadier.context.CommandContext;
8+
import com.mojang.brigadier.suggestion.SuggestionProvider;
39
import dev.hephaestus.fiblib.api.BlockFib;
410
import dev.hephaestus.fiblib.api.BlockFibRegistry;
511
import dev.hephaestus.sax.server.Config;
612
import dev.hephaestus.sax.util.FastCaster;
13+
import dev.hephaestus.sax.util.ObfuscatedWorld;
714
import dev.hephaestus.sax.util.Profiler;
815
import net.fabricmc.api.ModInitializer;
16+
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
917
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents;
18+
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
1019
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
1120
import net.minecraft.block.Block;
21+
import net.minecraft.server.command.CommandManager;
22+
import net.minecraft.server.command.ServerCommandSource;
23+
import net.minecraft.server.world.ServerWorld;
24+
import net.minecraft.text.LiteralText;
1225
import net.minecraft.util.Identifier;
1326
import org.apache.logging.log4j.LogManager;
1427
import org.apache.logging.log4j.Logger;
1528

1629
import java.util.Map;
30+
import java.util.concurrent.CompletableFuture;
1731

1832
public class SAX implements ModInitializer {
1933
public static final String MODID = "sax";
@@ -36,11 +50,70 @@ public void onInitialize() {
3650
);
3751
}
3852

39-
ServerLifecycleEvents.SERVER_STOPPING.register(minecraftServer -> {
40-
Profiler.dump(LOG);
41-
});
42-
4353
ServerChunkEvents.CHUNK_LOAD.register(FastCaster::load);
4454
ServerChunkEvents.CHUNK_UNLOAD.register(FastCaster::unload);
55+
56+
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) ->
57+
dispatcher.register(CommandManager.literal("sax")
58+
.requires(source -> source.hasPermissionLevel(4))
59+
.then(CommandManager.literal("lenient")
60+
.then(RequiredArgumentBuilder.<ServerCommandSource, Boolean>argument("lenient", BoolArgumentType.bool())
61+
.executes(SAX::lenient))
62+
.executes(SAX::getLenient)
63+
)
64+
.then(CommandManager.literal("tickRate")
65+
.then(RequiredArgumentBuilder.<ServerCommandSource, Integer>argument("tickRate", IntegerArgumentType.integer(1))
66+
.executes(SAX::tickRate))
67+
.executes(SAX::getTickrate)
68+
)
69+
.then(CommandManager.literal("chunkRadius")
70+
.then(RequiredArgumentBuilder.<ServerCommandSource, Integer>argument("chunkRadius", IntegerArgumentType.integer(1, Byte.MAX_VALUE))
71+
.executes(SAX::chunkRadius))
72+
.executes(SAX::getChunkRadius)
73+
)
74+
));
75+
}
76+
77+
private static int lenient(CommandContext<ServerCommandSource> context) {
78+
Config.LENIENT = context.getArgument("lenient", Boolean.class);
79+
Config.save();
80+
81+
return 0;
82+
}
83+
84+
private static int getLenient(CommandContext<ServerCommandSource> context) {
85+
context.getSource().sendFeedback(new LiteralText("Lenient: " + Config.LENIENT), false);
86+
87+
return 0;
88+
}
89+
90+
private static int tickRate(CommandContext<ServerCommandSource> context) {
91+
Config.TICK_RATE = context.getArgument("tickRate", Integer.class);
92+
Config.save();
93+
94+
return 0;
95+
}
96+
97+
private static int getTickrate(CommandContext<ServerCommandSource> context) {
98+
context.getSource().sendFeedback(new LiteralText("Tick Rate: " + Config.TICK_RATE), false);
99+
100+
return 0;
101+
}
102+
103+
private static int chunkRadius(CommandContext<ServerCommandSource> context) {
104+
Config.CHUNK_RADIUS = (byte) (context.getArgument("chunkRadius", Integer.class) & 0xFF);
105+
Config.save();
106+
107+
for (ServerWorld world : context.getSource().getMinecraftServer().getWorlds()) {
108+
((ObfuscatedWorld) world).reset();
109+
}
110+
111+
return 0;
112+
}
113+
114+
private static int getChunkRadius(CommandContext<ServerCommandSource> context) {
115+
context.getSource().sendFeedback(new LiteralText("Chunk Radius: " + Config.CHUNK_RADIUS), false);
116+
117+
return 0;
45118
}
46119
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package dev.hephaestus.sax.mixin;
2+
3+
import dev.hephaestus.sax.server.DeObfuscator;
4+
import dev.hephaestus.sax.util.ObfuscatedWorld;
5+
import net.minecraft.entity.Entity;
6+
import net.minecraft.server.network.ServerPlayerEntity;
7+
import net.minecraft.server.world.ServerWorld;
8+
import org.spongepowered.asm.mixin.Final;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.Shadow;
11+
import org.spongepowered.asm.mixin.Unique;
12+
import org.spongepowered.asm.mixin.injection.At;
13+
import org.spongepowered.asm.mixin.injection.Inject;
14+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
15+
16+
import java.util.HashMap;
17+
import java.util.List;
18+
import java.util.Map;
19+
import java.util.UUID;
20+
21+
@Mixin(ServerWorld.class)
22+
public class MixinServerWorld implements ObfuscatedWorld {
23+
@Shadow @Final private List<ServerPlayerEntity> players;
24+
@Unique private final Map<UUID, DeObfuscator> deObfuscatorMap = new HashMap<>();
25+
26+
@Inject(method = "addPlayer", at = @At("TAIL"))
27+
private void addDeobfuscator(ServerPlayerEntity player, CallbackInfo ci) {
28+
this.deObfuscatorMap.put(player.getUuid(), new DeObfuscator(player));
29+
}
30+
31+
@Inject(method = "removePlayer", at = @At("TAIL"))
32+
private void removeDeobfuscator(ServerPlayerEntity player, CallbackInfo ci) {
33+
this.deObfuscatorMap.remove(player.getUuid()).remove();
34+
}
35+
36+
@Inject(method = "tickEntity", at = @At("TAIL"))
37+
private void tickDeObfuscator(Entity entity, CallbackInfo ci) {
38+
if (entity instanceof ServerPlayerEntity) {
39+
this.deObfuscatorMap.get(entity.getUuid()).tick();
40+
}
41+
}
42+
43+
@Override
44+
public void reset() {
45+
this.deObfuscatorMap.clear();
46+
47+
for (ServerPlayerEntity player : this.players) {
48+
this.deObfuscatorMap.put(player.getUuid(), new DeObfuscator(player));
49+
}
50+
}
51+
}

src/main/java/dev/hephaestus/sax/mixin/world/ImplementOreChunk.java renamed to src/main/java/dev/hephaestus/sax/mixin/MixinWorldChunk.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dev.hephaestus.sax.mixin.world;
1+
package dev.hephaestus.sax.mixin;
22

33
import dev.hephaestus.sax.server.Config;
44
import dev.hephaestus.sax.util.ListView;
@@ -30,7 +30,7 @@
3030
import java.util.function.Consumer;
3131

3232
@Mixin(WorldChunk.class)
33-
public abstract class ImplementOreChunk implements OreChunk {
33+
public abstract class MixinWorldChunk implements OreChunk {
3434
@Shadow @Final private ChunkSection[] sections;
3535
@Shadow @Final private ChunkPos pos;
3636

src/main/java/dev/hephaestus/sax/mixin/server/DeObfuscateTargetedBlock.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main/java/dev/hephaestus/sax/server/Config.java

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.gson.JsonElement;
66
import com.google.gson.JsonObject;
77
import net.fabricmc.loader.api.FabricLoader;
8+
import net.fabricmc.loader.lib.gson.JsonReader;
89
import net.minecraft.block.Block;
910
import net.minecraft.block.Blocks;
1011
import net.minecraft.util.Identifier;
@@ -20,10 +21,15 @@
2021

2122
// Simple config for now
2223
public class Config {
24+
private static final Path OPTIONS = FabricLoader.getInstance().getConfigDir().resolve("sax").resolve("options.json");
25+
private static final Path BLOCKS = FabricLoader.getInstance().getConfigDir().resolve("sax").resolve("blocks.json");
26+
2327
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
24-
public static final HashMap<Block, Block> HIDDEN = new HashMap<>();
2528

26-
public static byte CHUNK_RADIUS = 8;
29+
public static final HashMap<Block, Block> HIDDEN = new HashMap<>();
30+
public static byte CHUNK_RADIUS = 4;
31+
public static boolean LENIENT = false;
32+
public static int TICK_RATE = 10;
2733

2834
static {
2935
HIDDEN.put(Blocks.DIAMOND_ORE, Blocks.STONE);
@@ -41,40 +47,58 @@ public class Config {
4147
}
4248

4349
public static void load() {
44-
Path configDir = FabricLoader.getInstance().getConfigDir().normalize().resolve("sax");
45-
loadOptions(configDir, configDir.resolve("options.json"));
46-
loadBlocks(configDir, configDir.resolve("blocks.json"));
50+
loadOptions();
51+
loadBlocks();
4752
}
4853

49-
private static void loadOptions(Path dir, Path file) {
54+
private static void loadOptions() {
5055
try {
51-
if (!Files.exists(file)) {
52-
Files.createDirectories(dir);
53-
54-
JsonObject options = new JsonObject();
55-
56-
options.addProperty("chunk_radius", CHUNK_RADIUS);
57-
58-
Writer writer = Files.newBufferedWriter(file);
59-
writer.write(GSON.toJson(options));
60-
writer.close();
61-
} else {
62-
JsonObject options = JsonHelper.deserialize(Files.newBufferedReader(file));
56+
if (Files.exists(Config.OPTIONS)) {
57+
JsonObject options = JsonHelper.deserialize(Files.newBufferedReader(Config.OPTIONS));
6358

6459
if (options.has("chunk_radius")) {
6560
CHUNK_RADIUS = options.get("chunk_radius").getAsByte();
6661
}
62+
63+
if (options.has("lenient")) {
64+
LENIENT = options.get("lenient").getAsBoolean();
65+
}
66+
67+
if (options.has("tick_rate")) {
68+
TICK_RATE = options.get("tick_rate").getAsInt();
69+
}
6770
}
71+
72+
save();
6873
} catch (IOException e) {
6974
e.printStackTrace();
7075
}
76+
}
77+
78+
public static void save() {
79+
try {
80+
if (!Files.exists(Config.OPTIONS.getParent())) {
81+
Files.createDirectories(Config.OPTIONS.getParent());
82+
}
7183

84+
JsonObject options = new JsonObject();
85+
86+
options.addProperty("chunk_radius", CHUNK_RADIUS);
87+
options.addProperty("lenient", LENIENT);
88+
options.addProperty("tick_rate", TICK_RATE);
89+
90+
Writer writer = Files.newBufferedWriter(Config.OPTIONS);
91+
writer.write(GSON.toJson(options));
92+
writer.close();
93+
} catch (IOException e) {
94+
e.printStackTrace();
95+
}
7296
}
7397

74-
private static void loadBlocks(Path dir, Path file) {
98+
private static void loadBlocks() {
7599
try {
76-
if (!Files.exists(file)) {
77-
Files.createDirectories(dir);
100+
if (!Files.exists(Config.BLOCKS)) {
101+
Files.createDirectories(Config.BLOCKS.getParent());
78102

79103
JsonObject blocks = new JsonObject();
80104

@@ -85,13 +109,13 @@ private static void loadBlocks(Path dir, Path file) {
85109
);
86110
}
87111

88-
Writer writer = Files.newBufferedWriter(file);
112+
Writer writer = Files.newBufferedWriter(Config.BLOCKS);
89113
writer.write(GSON.toJson(blocks));
90114
writer.close();
91115
} else {
92116
HIDDEN.clear();
93117

94-
for (Map.Entry<String, JsonElement> element : JsonHelper.deserialize(Files.newBufferedReader(file)).entrySet()) {
118+
for (Map.Entry<String, JsonElement> element : JsonHelper.deserialize(Files.newBufferedReader(Config.BLOCKS)).entrySet()) {
95119
HIDDEN.put(
96120
Registry.BLOCK.get(new Identifier(element.getKey())),
97121
Registry.BLOCK.get(new Identifier(element.getValue().getAsString()))

0 commit comments

Comments
 (0)