Skip to content

Commit

Permalink
- Updates FibLib for HopperOptimizations compatibility, closing #14.
Browse files Browse the repository at this point in the history
- Adds several performance options
- Adds command to control aforementioned performance options
  • Loading branch information
Haven-King committed Mar 23, 2021
1 parent 7f238c9 commit f0508b6
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 99 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ yarn_mappings = 1.16.5+build.4
loader_version = 0.11.1

# Mod Properties
mod_version = 1.0.1
mod_version = 1.1.0
maven_group = dev.hephaestus
archives_base_name = sax

# Dependencies
fabric_version = 0.30.3+1.16
fiblib_version = 1.0.1
fiblib_version = 1.0.2
mod_menu_version = 1.14.6+build.31
81 changes: 77 additions & 4 deletions src/main/java/dev/hephaestus/sax/SAX.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
package dev.hephaestus.sax;

import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import dev.hephaestus.fiblib.api.BlockFib;
import dev.hephaestus.fiblib.api.BlockFibRegistry;
import dev.hephaestus.sax.server.Config;
import dev.hephaestus.sax.util.FastCaster;
import dev.hephaestus.sax.util.ObfuscatedWorld;
import dev.hephaestus.sax.util.Profiler;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.block.Block;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Identifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

public class SAX implements ModInitializer {
public static final String MODID = "sax";
Expand All @@ -36,11 +50,70 @@ public void onInitialize() {
);
}

ServerLifecycleEvents.SERVER_STOPPING.register(minecraftServer -> {
Profiler.dump(LOG);
});

ServerChunkEvents.CHUNK_LOAD.register(FastCaster::load);
ServerChunkEvents.CHUNK_UNLOAD.register(FastCaster::unload);

CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) ->
dispatcher.register(CommandManager.literal("sax")
.requires(source -> source.hasPermissionLevel(4))
.then(CommandManager.literal("lenient")
.then(RequiredArgumentBuilder.<ServerCommandSource, Boolean>argument("lenient", BoolArgumentType.bool())
.executes(SAX::lenient))
.executes(SAX::getLenient)
)
.then(CommandManager.literal("tickRate")
.then(RequiredArgumentBuilder.<ServerCommandSource, Integer>argument("tickRate", IntegerArgumentType.integer(1))
.executes(SAX::tickRate))
.executes(SAX::getTickrate)
)
.then(CommandManager.literal("chunkRadius")
.then(RequiredArgumentBuilder.<ServerCommandSource, Integer>argument("chunkRadius", IntegerArgumentType.integer(1, Byte.MAX_VALUE))
.executes(SAX::chunkRadius))
.executes(SAX::getChunkRadius)
)
));
}

private static int lenient(CommandContext<ServerCommandSource> context) {
Config.LENIENT = context.getArgument("lenient", Boolean.class);
Config.save();

return 0;
}

private static int getLenient(CommandContext<ServerCommandSource> context) {
context.getSource().sendFeedback(new LiteralText("Lenient: " + Config.LENIENT), false);

return 0;
}

private static int tickRate(CommandContext<ServerCommandSource> context) {
Config.TICK_RATE = context.getArgument("tickRate", Integer.class);
Config.save();

return 0;
}

private static int getTickrate(CommandContext<ServerCommandSource> context) {
context.getSource().sendFeedback(new LiteralText("Tick Rate: " + Config.TICK_RATE), false);

return 0;
}

private static int chunkRadius(CommandContext<ServerCommandSource> context) {
Config.CHUNK_RADIUS = (byte) (context.getArgument("chunkRadius", Integer.class) & 0xFF);
Config.save();

for (ServerWorld world : context.getSource().getMinecraftServer().getWorlds()) {
((ObfuscatedWorld) world).reset();
}

return 0;
}

private static int getChunkRadius(CommandContext<ServerCommandSource> context) {
context.getSource().sendFeedback(new LiteralText("Chunk Radius: " + Config.CHUNK_RADIUS), false);

return 0;
}
}
51 changes: 51 additions & 0 deletions src/main/java/dev/hephaestus/sax/mixin/MixinServerWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.hephaestus.sax.mixin;

import dev.hephaestus.sax.server.DeObfuscator;
import dev.hephaestus.sax.util.ObfuscatedWorld;
import net.minecraft.entity.Entity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Mixin(ServerWorld.class)
public class MixinServerWorld implements ObfuscatedWorld {
@Shadow @Final private List<ServerPlayerEntity> players;
@Unique private final Map<UUID, DeObfuscator> deObfuscatorMap = new HashMap<>();

@Inject(method = "addPlayer", at = @At("TAIL"))
private void addDeobfuscator(ServerPlayerEntity player, CallbackInfo ci) {
this.deObfuscatorMap.put(player.getUuid(), new DeObfuscator(player));
}

@Inject(method = "removePlayer", at = @At("TAIL"))
private void removeDeobfuscator(ServerPlayerEntity player, CallbackInfo ci) {
this.deObfuscatorMap.remove(player.getUuid()).remove();
}

@Inject(method = "tickEntity", at = @At("TAIL"))
private void tickDeObfuscator(Entity entity, CallbackInfo ci) {
if (entity instanceof ServerPlayerEntity) {
this.deObfuscatorMap.get(entity.getUuid()).tick();
}
}

@Override
public void reset() {
this.deObfuscatorMap.clear();

for (ServerPlayerEntity player : this.players) {
this.deObfuscatorMap.put(player.getUuid(), new DeObfuscator(player));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hephaestus.sax.mixin.world;
package dev.hephaestus.sax.mixin;

import dev.hephaestus.sax.server.Config;
import dev.hephaestus.sax.util.ListView;
Expand Down Expand Up @@ -30,7 +30,7 @@
import java.util.function.Consumer;

@Mixin(WorldChunk.class)
public abstract class ImplementOreChunk implements OreChunk {
public abstract class MixinWorldChunk implements OreChunk {
@Shadow @Final private ChunkSection[] sections;
@Shadow @Final private ChunkPos pos;

Expand Down

This file was deleted.

70 changes: 47 additions & 23 deletions src/main/java/dev/hephaestus/sax/server/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.lib.gson.JsonReader;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.util.Identifier;
Expand All @@ -20,10 +21,15 @@

// Simple config for now
public class Config {
private static final Path OPTIONS = FabricLoader.getInstance().getConfigDir().resolve("sax").resolve("options.json");
private static final Path BLOCKS = FabricLoader.getInstance().getConfigDir().resolve("sax").resolve("blocks.json");

private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
public static final HashMap<Block, Block> HIDDEN = new HashMap<>();

public static byte CHUNK_RADIUS = 8;
public static final HashMap<Block, Block> HIDDEN = new HashMap<>();
public static byte CHUNK_RADIUS = 4;
public static boolean LENIENT = false;
public static int TICK_RATE = 10;

static {
HIDDEN.put(Blocks.DIAMOND_ORE, Blocks.STONE);
Expand All @@ -41,40 +47,58 @@ public class Config {
}

public static void load() {
Path configDir = FabricLoader.getInstance().getConfigDir().normalize().resolve("sax");
loadOptions(configDir, configDir.resolve("options.json"));
loadBlocks(configDir, configDir.resolve("blocks.json"));
loadOptions();
loadBlocks();
}

private static void loadOptions(Path dir, Path file) {
private static void loadOptions() {
try {
if (!Files.exists(file)) {
Files.createDirectories(dir);

JsonObject options = new JsonObject();

options.addProperty("chunk_radius", CHUNK_RADIUS);

Writer writer = Files.newBufferedWriter(file);
writer.write(GSON.toJson(options));
writer.close();
} else {
JsonObject options = JsonHelper.deserialize(Files.newBufferedReader(file));
if (Files.exists(Config.OPTIONS)) {
JsonObject options = JsonHelper.deserialize(Files.newBufferedReader(Config.OPTIONS));

if (options.has("chunk_radius")) {
CHUNK_RADIUS = options.get("chunk_radius").getAsByte();
}

if (options.has("lenient")) {
LENIENT = options.get("lenient").getAsBoolean();
}

if (options.has("tick_rate")) {
TICK_RATE = options.get("tick_rate").getAsInt();
}
}

save();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void save() {
try {
if (!Files.exists(Config.OPTIONS.getParent())) {
Files.createDirectories(Config.OPTIONS.getParent());
}

JsonObject options = new JsonObject();

options.addProperty("chunk_radius", CHUNK_RADIUS);
options.addProperty("lenient", LENIENT);
options.addProperty("tick_rate", TICK_RATE);

Writer writer = Files.newBufferedWriter(Config.OPTIONS);
writer.write(GSON.toJson(options));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private static void loadBlocks(Path dir, Path file) {
private static void loadBlocks() {
try {
if (!Files.exists(file)) {
Files.createDirectories(dir);
if (!Files.exists(Config.BLOCKS)) {
Files.createDirectories(Config.BLOCKS.getParent());

JsonObject blocks = new JsonObject();

Expand All @@ -85,13 +109,13 @@ private static void loadBlocks(Path dir, Path file) {
);
}

Writer writer = Files.newBufferedWriter(file);
Writer writer = Files.newBufferedWriter(Config.BLOCKS);
writer.write(GSON.toJson(blocks));
writer.close();
} else {
HIDDEN.clear();

for (Map.Entry<String, JsonElement> element : JsonHelper.deserialize(Files.newBufferedReader(file)).entrySet()) {
for (Map.Entry<String, JsonElement> element : JsonHelper.deserialize(Files.newBufferedReader(Config.BLOCKS)).entrySet()) {
HIDDEN.put(
Registry.BLOCK.get(new Identifier(element.getKey())),
Registry.BLOCK.get(new Identifier(element.getValue().getAsString()))
Expand Down
Loading

0 comments on commit f0508b6

Please sign in to comment.