Skip to content

Commit

Permalink
Forge: Fix init world tick
Browse files Browse the repository at this point in the history
  • Loading branch information
pisaiah committed Aug 10, 2023
1 parent 971dfde commit 64a488f
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.Difficulty;
//import net.minecraft.registry.RegistryKey;
import net.minecraft.world.World;

//import me.isaiah.lib.IText;

Expand Down Expand Up @@ -156,6 +157,14 @@ public static int broadcast(ServerCommandSource source, Formatting formatting, S
e.printStackTrace();
}
}*/

if (args[0].equalsIgnoreCase("debugtick")) {
ServerWorld w = (ServerWorld) plr.getWorld();
Identifier id = w.getRegistryKey().getValue();
message(plr, "World ID: " + id.toString());
message(plr, "Players : " + w.getPlayers().size());
w.tick(() -> true);
}

if (args[0].equalsIgnoreCase("setspawn") && (ALL || Perm.has(plr, "multiworld.setspawn") )) {
return SetspawnCommand.run(mc, plr, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class Fantasy {
private final MinecraftServer server;
private final MinecraftServerAccess serverAccess;

private final RuntimeWorldManager worldManager;
public final RuntimeWorldManager worldManager;

private final Set<ServerWorld> deletionQueue = new ReferenceOpenHashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.event.lifecycle.*;
import net.minecraftforge.event.server.*;
import net.minecraftforge.event.server.ServerStartedEvent;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraft.server.MinecraftServer;
Expand All @@ -20,6 +21,8 @@
//@Mod("fantasy")
public final class FantasyInitializer {

public static boolean after_tick_start = false;

public MinecraftServer mc;
public FantasyInitializer() {
MinecraftForge.EVENT_BUS.register(this);
Expand Down Expand Up @@ -50,14 +53,21 @@ public void handleStart(ServerAboutToStartEvent event) {
mc = event.getServer();
}

@SubscribeEvent
public void handle_started(ServerStartedEvent event) {
after_tick_start = true;
}

@SubscribeEvent
public void handleTickEvent(TickEvent.ServerTickEvent event) {
/* if (event.phase == TickEvent.Phase.START) {
if (event.phase == TickEvent.Phase.START) {
//MinecraftServer server = event.getServer();
Fantasy fantasy = Fantasy.get(mc);
fantasy.tick();
System.out.println("TICK EVENT!");
}*/
for (ServerWorld w : fantasy.worldManager.worldss.values()) {
w.tick(() -> true);
}
}
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package xyz.nucleoid.fantasy;

import net.minecraft.registry.RegistryKey;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;

public interface IMC {

public void add_world(RegistryKey<World> key, ServerWorld value);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

final class RuntimeWorldManager {
private final MinecraftServer server;
private final MinecraftServerAccess serverAccess;

public Map<RegistryKey<World>, ServerWorld> worldss;

RuntimeWorldManager(MinecraftServer server) {
this.server = server;
this.worldss = new HashMap<>();
this.serverAccess = (MinecraftServerAccess) server;
}

Expand All @@ -41,7 +46,14 @@ RuntimeWorld add(RegistryKey<World> worldKey, RuntimeWorldConfig config, Runtime

RuntimeWorld world = new RuntimeWorld(this.server, worldKey, config, style);

this.serverAccess.getWorlds().put(world.getRegistryKey(), world);
IMC imc = (IMC) this.server;
imc.add_world(world.getRegistryKey(), world);

if (FantasyInitializer.after_tick_start) {
worldss.put(world.getRegistryKey(), world);
}

// this.serverAccess.getWorlds().put(world.getRegistryKey(), world);
// ServerWorldEvents.LOAD.invoker().onWorldLoad(this.server, world);

// tick the world to ensure it is ready for use right away
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
import java.util.function.BooleanSupplier;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.registry.RegistryKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;
import xyz.nucleoid.fantasy.Fantasy;
import xyz.nucleoid.fantasy.IMC;
import xyz.nucleoid.fantasy.util.SafeIterator;

@Mixin(MinecraftServer.class)
public class MixinMinecraftServer {
public class MixinMinecraftServer implements IMC {

// The locals you have to manage for an inject are insane. And do it twice. A redirect is much cleaner.
// Here is what it looks like with an inject: https://gist.github.com/i509VCB/f80077cc536eb4dba62b794eba5611c1
Expand All @@ -27,7 +31,7 @@ private <K, V> V onLoadWorld(Map<K, V> worlds, K registryKey, V serverWorld) {
final V result = worlds.put(registryKey, serverWorld);
// ServerWorldEvents.LOAD.invoker().onWorldLoad((MinecraftServer) (Object) this, (ServerWorld) serverWorld);

System.out.println("LOADING WORLD!: " + registryKey);
// System.out.println("LOADING WORLD!: " + registryKey);

return result;
}
Expand All @@ -48,5 +52,14 @@ private void before_shutdown_server(CallbackInfo info) {
Fantasy fantasy = Fantasy.get((MinecraftServer) (Object) this);
fantasy.onServerStopping();
}

@Shadow
private Map<RegistryKey<World>, ServerWorld> worlds;

@Override
public void add_world(RegistryKey<World> key, ServerWorld value) {
// TODO Auto-generated method stub
worlds.put(key, value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class ServerChunkManagerMixin {
@Inject(method = "executeQueuedTasks", at = @At("HEAD"), cancellable = true)
private void executeQueuedTasks(CallbackInfoReturnable<Boolean> ci) {
if (!((FantasyWorldAccess) this.world).fantasy$shouldTick()) {
System.out.println("NOT TICKING! " + world.getRegistryKey().getValue());
ci.setReturnValue(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ public abstract class ServerWorldMixin implements FantasyWorldAccess {
@Inject(method = "tick", at = @At("HEAD"), cancellable = true)
private void tick(BooleanSupplier shouldKeepTicking, CallbackInfo ci) {
boolean shouldTick = this.fantasy$tickWhenEmpty || !this.isWorldEmpty();

//ServerWorld sv = (ServerWorld) (Object) this;



//System.out.println(sv.getRegistryKey().getValue().toString() + getPlayers().size() + " - " + shouldTick);
if (shouldTick) {
this.fantasy$tickTimeout = TICK_TIMEOUT;
} else if (this.fantasy$tickTimeout-- <= 0) {
Expand Down
Binary file modified output/Multiworld-Fabric-1.18.2.jar
Binary file not shown.
Binary file modified output/Multiworld-Fabric-1.19.2.jar
Binary file not shown.
Binary file modified output/Multiworld-Fabric-1.19.4.jar
Binary file not shown.
Binary file modified output/Multiworld-Fabric-1.20.jar
Binary file not shown.
Binary file modified output/Multiworld-Fabric-bundle.jar
Binary file not shown.
Binary file modified output/Multiworld-Forge-1.19.4.jar
Binary file not shown.

0 comments on commit 64a488f

Please sign in to comment.