It loads the custom dimension!

Can't connect as a player though
Loading the dimensions on regular plugin enable
This commit is contained in:
Norbi Peti 2020-09-09 01:09:05 +02:00
parent 2d5b6e4492
commit c245d48033
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 79 additions and 42 deletions

View file

@ -1,17 +1,18 @@
package buttondevteam.customdimensions; package buttondevteam.customdimensions;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.mojang.serialization.Lifecycle;
import net.minecraft.server.v1_16_R2.*; import net.minecraft.server.v1_16_R2.*;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.WorldType;
import org.bukkit.craftbukkit.v1_16_R2.CraftServer; import org.bukkit.craftbukkit.v1_16_R2.CraftServer;
import org.bukkit.craftbukkit.v1_16_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_16_R2.SpigotTimings;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldInitEvent;
import org.bukkit.event.world.WorldLoadEvent; import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.*; import java.util.*;
public class CustomDimensions extends JavaPlugin implements Listener { public class CustomDimensions extends JavaPlugin implements Listener {
@ -27,20 +28,14 @@ public class CustomDimensions extends JavaPlugin implements Listener {
@Override @Override
public void onEnable() { public void onEnable() {
Bukkit.getPluginManager().registerEvents(this, this); //Bukkit.getPluginManager().registerEvents(this, this);
} getLogger().info("Loading custom dimensions...");
@EventHandler
public void worldLoad(WorldLoadEvent event) {
System.out.println("World loaded: " + event.getWorld().getName());
if (!event.getWorld().getName().equals(Bukkit.getWorlds().get(0).getName()))
return;
System.out.println("Main world");
try { try {
load(); load();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
getLogger().info("Finished loading custom dimensions!");
} }
public void load() throws Exception { public void load() throws Exception {
@ -49,7 +44,7 @@ public class CustomDimensions extends JavaPlugin implements Listener {
var field = console.getClass().getSuperclass().getDeclaredField("saveData"); var field = console.getClass().getSuperclass().getDeclaredField("saveData");
field.setAccessible(true); field.setAccessible(true);
var saveData = (SaveData) field.get(console); var saveData = (SaveData) field.get(console);
IWorldDataServer iworlddataserver = saveData.H(); //IWorldDataServer iworlddataserver = saveData.H();
GeneratorSettings generatorsettings = saveData.getGeneratorSettings(); GeneratorSettings generatorsettings = saveData.getGeneratorSettings();
RegistryMaterials<WorldDimension> registrymaterials = generatorsettings.d(); RegistryMaterials<WorldDimension> registrymaterials = generatorsettings.d();
var worldloadlistener = console.worldLoadListenerFactory.create(11); var worldloadlistener = console.worldLoadListenerFactory.create(11);
@ -58,23 +53,84 @@ public class CustomDimensions extends JavaPlugin implements Listener {
var mainWorld = Bukkit.getWorlds().get(0); var mainWorld = Bukkit.getWorlds().get(0);
var convertable = Convertable.a(Bukkit.getWorldContainer().toPath());
while (iterator.hasNext()) { while (iterator.hasNext()) {
Map.Entry<ResourceKey<WorldDimension>, WorldDimension> entry = iterator.next(); Map.Entry<ResourceKey<WorldDimension>, WorldDimension> entry = iterator.next();
ResourceKey<WorldDimension> resourcekey = entry.getKey(); ResourceKey<WorldDimension> resourcekey = entry.getKey();
if (resourcekey != WorldDimension.OVERWORLD) { if (resourcekey != WorldDimension.OVERWORLD
&& resourcekey != WorldDimension.THE_NETHER
&& resourcekey != WorldDimension.THE_END) {
ResourceKey<World> resourcekey1 = ResourceKey.a(IRegistry.L, resourcekey.a()); ResourceKey<World> resourcekey1 = ResourceKey.a(IRegistry.L, resourcekey.a());
DimensionManager dimensionmanager1 = entry.getValue().b(); DimensionManager dimensionmanager = entry.getValue().b();
ChunkGenerator chunkgenerator = entry.getValue().c(); //TODO: Shade ChunkGenerator chunkgenerator = entry.getValue().c(); //TODO: Shade
var data = DimensionWorldData.create((WorldDataServer) iworlddataserver, "dream_dimension", EnumGamemode.CREATIVE); var name = resourcekey.a().getKey();
WorldServer worldserver1 = new WorldServer(console, console.executorService, console.convertable, var session = convertable.new ConversionSession(name, resourcekey) { //The original session isn't prepared for custom dimensions
data, resourcekey1, dimensionmanager1, worldloadlistener, chunkgenerator, @Override
public File a(ResourceKey<World> resourcekey) {
return new File(this.folder.toFile(), "custom");
}
};
MinecraftServer.convertWorld(session);
//Load world settings and create a custom
RegistryReadOps<NBTBase> registryreadops = RegistryReadOps.a(DynamicOpsNBT.a, console.dataPackResources.h(), console.f);
WorldDataServer worlddata = (WorldDataServer) session.a(registryreadops, console.datapackconfiguration);
if (worlddata == null) {
Properties properties = new Properties();
properties.put("level-seed", Objects.toString(mainWorld.getSeed()));
properties.put("generate-structures", Objects.toString(true));
properties.put("level-type", WorldType.NORMAL.getName());
GeneratorSettings generatorsettings2 = GeneratorSettings.a(console.aX(), properties);
WorldSettings worldSettings = new WorldSettings(name,
EnumGamemode.getById(Bukkit.getDefaultGameMode().getValue()),
false, //Hardcore
EnumDifficulty.EASY, false, new GameRules(), console.datapackconfiguration);
worlddata = new WorldDataServer(worldSettings, generatorsettings2, Lifecycle.stable());
}
var data = DimensionWorldData.create(worlddata, name, EnumGamemode.CREATIVE);
worlddata.checkName(name);
worlddata.a(console.getServerModName(), console.getModded().isPresent());
if (console.options.has("forceUpgrade")) {
net.minecraft.server.v1_16_R2.Main.convertWorld(session, DataConverterRegistry.a(),
console.options.has("eraseCache"), () -> true,
worlddata.getGeneratorSettings().d().d().stream()
.map((entry2) -> ResourceKey.a(IRegistry.K, entry2.getKey().a()))
.collect(ImmutableSet.toImmutableSet()));
}
List<MobSpawner> list = ImmutableList.of(new MobSpawnerPhantom(), new MobSpawnerPatrol(), new MobSpawnerCat(), new VillageSiege(), new MobSpawnerTrader(worlddata));
//Register dimension manager in registry
ResourceKey<DimensionManager> dimManResKey = ResourceKey.a(IRegistry.K, resourcekey.a());
((RegistryMaterials<DimensionManager>) console.f.a()).a(dimManResKey, dimensionmanager, Lifecycle.stable());
//RegistryMaterials<WorldDimension> registrymaterials = worlddata.getGeneratorSettings().d();
//WorldDimension worlddimension = (WorldDimension) registrymaterials2.a(actualDimension);
//Use the main world's dimension data
WorldServer worldserver = new WorldServer(console, console.executorService, session,
data, resourcekey1, dimensionmanager, worldloadlistener, chunkgenerator,
false, //isDebugWorld false, //isDebugWorld
BiomeManager.a(mainWorld.getSeed()), //Biome seed BiomeManager.a(mainWorld.getSeed()), //Biome seed
ImmutableList.of(), false, org.bukkit.World.Environment.NORMAL, null); list, false, org.bukkit.World.Environment.NORMAL, null);
((CraftWorld) mainWorld).getHandle().getWorldBorder().a(new IWorldBorderListener.a(worldserver1.getWorldBorder())); //((CraftWorld) mainWorld).getHandle().getWorldBorder().a(new IWorldBorderListener.a(worldserver.getWorldBorder()));
console.worldServer.put(resourcekey1, worldserver1);
if (Bukkit.getWorld(name.toLowerCase(Locale.ENGLISH)) == null) {
getLogger().warning("Failed to load custom dimension " + name);
} else {
console.initWorld(worldserver, worlddata, worlddata, worlddata.getGeneratorSettings());
worldserver.setSpawnFlags(true, true);
console.worldServer.put(worldserver.getDimensionKey(), worldserver);
Bukkit.getPluginManager().callEvent(new WorldInitEvent(worldserver.getWorld()));
console.loadSpawn(worldserver.getChunkProvider().playerChunkMap.worldLoadListener, worldserver);
Bukkit.getPluginManager().callEvent(new WorldLoadEvent(worldserver.getWorld()));
}
} }
} }
System.out.println("Loading finished!"); System.out.println("Loading finished!");

View file

@ -2,38 +2,20 @@ package buttondevteam.customdimensions;
import com.mojang.serialization.Lifecycle; import com.mojang.serialization.Lifecycle;
import net.minecraft.server.v1_16_R2.*; import net.minecraft.server.v1_16_R2.*;
import org.mockito.Answers;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
public class DimensionWorldData extends WorldDataServer { public class DimensionWorldData extends WorldDataServer {
public DimensionWorldData(WorldSettings worldsettings, GeneratorSettings generatorsettings, Lifecycle lifecycle) { public DimensionWorldData(WorldSettings worldsettings, GeneratorSettings generatorsettings, Lifecycle lifecycle) {
super(worldsettings, generatorsettings, lifecycle); super(worldsettings, generatorsettings, lifecycle);
} }
private String name;
private EnumGamemode gamemode;
@Override
public String getName() {
return name;
}
@Override
public EnumGamemode getGameType() {
return gamemode;
}
public static DimensionWorldData create(WorldDataServer data, String name, EnumGamemode gamemode) { public static DimensionWorldData create(WorldDataServer data, String name, EnumGamemode gamemode) {
var mock = Mockito.mock(DimensionWorldData.class, invocation -> { var mock = Mockito.mock(DimensionWorldData.class, invocation -> {
if (invocation.getMethod().getDeclaringClass() == DimensionWorldData.class) if (invocation.getMethod().getDeclaringClass() == DimensionWorldData.class)
return invocation.callRealMethod(); return invocation.callRealMethod();
return invocation.getMethod().invoke(data, invocation.getArguments()); return invocation.getMethod().invoke(data, invocation.getArguments());
}); });
mock.name = name; mock.b = new WorldSettings(name, gamemode, false, EnumDifficulty.EASY, true, data.q(), data.D());
mock.gamemode = gamemode; //There are a couple things that come from SaveData, but that's the same
mock.b = data.b;
return mock; return mock;
} }
} }

View file

@ -1,5 +1,4 @@
name: CustomDimensions name: CustomDimensions
main: buttondevteam.customdimensions.CustomDimensions main: buttondevteam.customdimensions.CustomDimensions
version: '1.0' version: '1.0'
load: STARTUP
api-version: '1.16' api-version: '1.16'