forked from NucleoidMC/fantasy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport: Add support for world unloading, fix world data being kept …
…in level.dat (again), fix random crashes when adding/removing a world
- Loading branch information
Showing
11 changed files
with
212 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/xyz/nucleoid/fantasy/mixin/MinecraftServerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package xyz.nucleoid.fantasy.mixin; | ||
|
||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.world.ServerWorld; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
import xyz.nucleoid.fantasy.util.SafeIterator; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
@Mixin(MinecraftServer.class) | ||
public class MinecraftServerMixin { | ||
@Redirect(method = "tickWorlds", at = @At(value = "INVOKE", target = "Ljava/lang/Iterable;iterator()Ljava/util/Iterator;", ordinal = 0), require = 0) | ||
private Iterator<ServerWorld> fantasy$copyBeforeTicking(Iterable<ServerWorld> instance) { | ||
return new SafeIterator<>((Collection<ServerWorld>) instance); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/xyz/nucleoid/fantasy/mixin/registry/DimensionOptionsRegistryHolderMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package xyz.nucleoid.fantasy.mixin.registry; | ||
|
||
// import net.minecraft.registry.Registry; | ||
import net.minecraft.world.dimension.DimensionOptions; | ||
// import net.minecraft.world.dimension.DimensionOptionsRegistryHolder; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.ModifyArg; | ||
import xyz.nucleoid.fantasy.FantasyDimensionOptions; | ||
import xyz.nucleoid.fantasy.util.FilteredRegistry; | ||
|
||
import java.util.function.Function; | ||
|
||
// @Mixin(DimensionOptionsRegistryHolder.class) | ||
public class DimensionOptionsRegistryHolderMixin { | ||
//@ModifyArg(method = "method_45516", at = @At(value = "INVOKE", target = "Lcom/mojang/serialization/MapCodec;forGetter(Ljava/util/function/Function;)Lcom/mojang/serialization/codecs/RecordCodecBuilder;")) | ||
//private static Function<Object, Registry<DimensionOptions>> fantasy$swapRegistryGetter(Function<Object, Registry<DimensionOptions>> getter) { | ||
// return (x) -> new FilteredRegistry<>(getter.apply(x), FantasyDimensionOptions.SAVE_PROPERTIES_PREDICATE); | ||
//} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package xyz.nucleoid.fantasy.util; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
public final class SafeIterator<T> implements Iterator<T> { | ||
private final Object[] values; | ||
private int index = 0; | ||
|
||
public SafeIterator(Collection<T> source) { | ||
this.values = source.toArray(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return this.values.length > this.index; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
return (T) this.values[this.index++]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 73 additions & 8 deletions
81
src/testmod/java/xyz/nucleoid/fantasy/test/FantasyInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,92 @@ | ||
package xyz.nucleoid.fantasy.test; | ||
|
||
import com.mojang.brigadier.CommandDispatcher; | ||
import net.fabricmc.api.ModInitializer; | ||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; | ||
import net.fabricmc.fabric.api.dimension.v1.FabricDimensions; | ||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import net.minecraft.command.argument.IdentifierArgumentType; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.math.Vec3d; | ||
import net.minecraft.util.profiler.Profiler; | ||
import net.minecraft.util.registry.Registry; | ||
import net.minecraft.world.TeleportTarget; | ||
import xyz.nucleoid.fantasy.Fantasy; | ||
import xyz.nucleoid.fantasy.RuntimeWorldConfig; | ||
import xyz.nucleoid.fantasy.RuntimeWorldHandle; | ||
import xyz.nucleoid.fantasy.util.VoidChunkGenerator; | ||
|
||
import java.util.HashMap; | ||
import java.util.WeakHashMap; | ||
|
||
import static net.minecraft.server.command.CommandManager.argument; | ||
import static net.minecraft.server.command.CommandManager.literal; | ||
|
||
public final class FantasyInitializer implements ModInitializer { | ||
private HashMap<Identifier, RuntimeWorldHandle> worlds = new HashMap<>(); | ||
|
||
@Override | ||
public void onInitialize() { | ||
ServerLifecycleEvents.SERVER_STARTED.register((s) -> { | ||
Fantasy.get(s).openTemporaryWorld(new RuntimeWorldConfig().setGenerator(new VoidChunkGenerator(s.getRegistryManager().get(Registry.BIOME_KEY).getEntry(0).get()))); | ||
Fantasy.get(s).getOrOpenPersistentWorld( | ||
new Identifier("fantasytest:test"), | ||
new RuntimeWorldConfig() | ||
.setGenerator(s.getOverworld().getChunkManager().getChunkGenerator()) | ||
); | ||
}); | ||
} | ||
|
||
CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> { | ||
dispatcher.register(literal("fantasy_open").then( | ||
argument("name", IdentifierArgumentType.identifier()) | ||
.executes((context -> { | ||
try { | ||
var t = System.currentTimeMillis(); | ||
var id = IdentifierArgumentType.getIdentifier(context, "name"); | ||
|
||
var x = Fantasy.get(context.getSource().getServer()).getOrOpenPersistentWorld( | ||
id, | ||
new RuntimeWorldConfig() | ||
.setGenerator(context.getSource().getServer().getOverworld().getChunkManager().getChunkGenerator()) | ||
.setSeed(id.hashCode()) | ||
); | ||
context.getSource().sendFeedback(Text.literal("WorldCreate: " + (System.currentTimeMillis() - t)), false); | ||
|
||
worlds.put(id, x); | ||
t = System.currentTimeMillis(); | ||
FabricDimensions.teleport(context.getSource().getEntity(), x.asWorld(), new TeleportTarget(new Vec3d(0, 100 ,0) , Vec3d.ZERO, 0, 0)); | ||
context.getSource().sendFeedback(Text.literal("Teleport: " + (System.currentTimeMillis() - t)), false); | ||
} catch (Throwable e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return 0; | ||
})) | ||
)); | ||
|
||
dispatcher.register(literal("fantasy_delete").then( | ||
argument("name", IdentifierArgumentType.identifier()) | ||
.executes((context -> { | ||
try { | ||
var id = IdentifierArgumentType.getIdentifier(context, "name"); | ||
worlds.get(id).delete(); | ||
worlds.remove(id); | ||
} catch (Throwable e) { | ||
e.printStackTrace(); | ||
} | ||
return 0; | ||
})) | ||
)); | ||
|
||
dispatcher.register(literal("fantasy_unload").then( | ||
argument("name", IdentifierArgumentType.identifier()) | ||
.executes((context -> { | ||
try { | ||
var id = IdentifierArgumentType.getIdentifier(context, "name"); | ||
worlds.get(id).unload(); | ||
worlds.remove(id); | ||
} catch (Throwable e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return 0; | ||
})) | ||
)); | ||
})); | ||
} | ||
} |