Cleanup, dropped Mockito

This commit is contained in:
Norbi Peti 2020-09-09 18:14:28 +02:00
parent 82cefec71a
commit b8f157bec3
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 8 additions and 187 deletions

View file

@ -34,11 +34,6 @@
<version>1.16.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</project>

View file

@ -29,7 +29,6 @@ public class CustomDimensions extends JavaPlugin implements Listener {
}
public void load() throws Exception {
getLogger().info("Loading...");
var console = ((CraftServer) Bukkit.getServer()).getServer();
var field = console.getClass().getSuperclass().getDeclaredField("saveData");
field.setAccessible(true);
@ -47,22 +46,14 @@ public class CustomDimensions extends JavaPlugin implements Listener {
Map.Entry<ResourceKey<WorldDimension>, WorldDimension> dimEntry = dimIterator.next();
ResourceKey<WorldDimension> dimKey = dimEntry.getKey();
if (dimKey != WorldDimension.OVERWORLD
if (dimKey != WorldDimension.OVERWORLD //The default dimensions are already loaded
&& dimKey != WorldDimension.THE_NETHER
&& dimKey != WorldDimension.THE_END) {
System.out.println("First check");
for (var dimMan : console.f.a()) {
try {
System.out.println("Found dim man for key: " + console.f.a().getKey(dimMan));
System.out.println("Resource key: " + console.f.a().c(dimMan));
} catch (Exception e) {
e.printStackTrace();
}
}
ResourceKey<World> worldKey = ResourceKey.a(IRegistry.L, dimKey.a());
DimensionManager dimensionmanager = dimEntry.getValue().b();
ChunkGenerator chunkgenerator = dimEntry.getValue().c(); //TODO: Shade
ChunkGenerator chunkgenerator = dimEntry.getValue().c();
var name = dimKey.a().getKey();
getLogger().info("Loading " + name);
var session = convertable.new ConversionSession(name, dimKey) { //The original session isn't prepared for custom dimensions
@Override
public File a(ResourceKey<World> resourcekey) {
@ -87,9 +78,6 @@ public class CustomDimensions extends JavaPlugin implements Listener {
worlddata = new WorldDataServer(worldSettings, dimGenSettings, Lifecycle.stable());
}
//Create a custom WorldDataServer that is aware that it's a dimension and delegates most calls to the main world
var data = DimensionWorldData.create(worlddata, name);
worlddata.checkName(name);
worlddata.a(console.getServerModName(), console.getModded().isPresent());
if (console.options.has("forceUpgrade")) {
@ -102,62 +90,17 @@ public class CustomDimensions extends JavaPlugin implements Listener {
List<MobSpawner> spawners = ImmutableList.of(new MobSpawnerPhantom(), new MobSpawnerPatrol(), new MobSpawnerCat(), new VillageSiege(), new MobSpawnerTrader(worlddata));
//in bf but not in bi
System.out.println("Second check");
ResourceKey<DimensionManager> dimManKey = null;
int lastID = -1;
for (var dimMan : console.f.a()) {
try {
System.out.println("Found dim man for key: " + console.f.a().getKey(dimMan));
var key = console.f.a().c(dimMan);
System.out.println("Resource key: " + key);
int id = console.f.a().a(dimensionmanager);
System.out.println("ID: " + id);
if (id > lastID) lastID = id;
if (key.isPresent() && key.get().a().getKey().equals(name)) {
//Register dimension manager in registry
//var originalManagerID = console.f.a().a(dimensionmanager); - -1
//System.out.println("Replacing dimension manager with ID " + originalManagerID);
dimManKey = key.get();
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (dimManKey != null) {
ResourceKey<DimensionManager> dimManResKey = ResourceKey.a(IRegistry.K, dimKey.a());
System.out.println("Replacing with " + dimManResKey);
((RegistryMaterials<DimensionManager>) console.f.a()).a(OptionalInt.empty(), dimManKey, dimensionmanager, Lifecycle.stable());
}
//in bf but not in bi
System.out.println("Third check");
for (var dimMan : console.f.a()) {
try {
System.out.println("Found dim man for key: " + console.f.a().getKey(dimMan));
System.out.println("Resource key: " + console.f.a().c(dimMan));
} catch (Exception e) {
e.printStackTrace();
}
}
ResourceKey<DimensionManager> dimManResKey = ResourceKey.a(IRegistry.K, dimKey.a());
//Replace existing dimension manager, correctly setting the ID up (which is -1 for default worlds...)
((RegistryMaterials<DimensionManager>) console.f.a()).a(OptionalInt.empty(), dimManResKey, dimensionmanager, Lifecycle.stable());
var worldloadlistener = console.worldLoadListenerFactory.create(11);
//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, worldKey, dimensionmanager, worldloadlistener, chunkgenerator,
worlddata, worldKey, dimensionmanager, worldloadlistener, chunkgenerator,
false, //isDebugWorld
BiomeManager.a(mainWorld.getSeed()), //Biome seed
BiomeManager.a(worlddata.getGeneratorSettings().getSeed()), //Biome seed
spawners, false, org.bukkit.World.Environment.NORMAL, null);
data.world = worldserver; //Mocked world data
worlddata.world = worldserver; //Inner world data
//((CraftWorld) mainWorld).getHandle().getWorldBorder().a(new IWorldBorderListener.a(worldserver.getWorldBorder()));
if (Bukkit.getWorld(name.toLowerCase(Locale.ENGLISH)) == null) {
getLogger().warning("Failed to load custom dimension " + name);
@ -171,99 +114,5 @@ public class CustomDimensions extends JavaPlugin implements Listener {
}
}
}
getLogger().info("Loading finished!");
}
/*private WorldServer createWorldServer(WorldDataMutable worldData, String name) throws Exception {
var obj = new ObjenesisStd();
var ws = obj.newInstance(WorldServer.class);
ws.spigotConfig = new SpigotWorldConfig(name);
//ws.generator = gen;
var field = ws.getClass().getDeclaredField("world");
field.setAccessible(true);
field.set(ws, new CraftWorld(ws, null, org.bukkit.World.Environment.NORMAL));
ws.ticksPerAnimalSpawns = ws.getServer().getTicksPerAnimalSpawns();
ws.ticksPerMonsterSpawns = ws.getServer().getTicksPerMonsterSpawns();
...
}*/
/*public World load(String name) {
File folder = new File(Bukkit.getWorldContainer(), name);
World world = Bukkit.getWorld(name);
if (world != null) {
return world;
} else if (folder.exists() && !folder.isDirectory()) {
throw new IllegalArgumentException("File exists with the name '" + name + "' and isn't a folder");
} else {
ResourceKey<WorldDimension> actualDimension = ResourceKey.a(IRegistry.M, new MinecraftKey(name));
Convertable.ConversionSession worldSession;
try {
worldSession = Convertable.a(Bukkit.getWorldContainer().toPath()).c(name, actualDimension);
} catch (IOException var21) {
throw new RuntimeException(var21);
}
var server = (CraftServer) Bukkit.getServer();
var console = server.getServer();
MinecraftServer.convertWorld(worldSession);
RegistryReadOps<NBTBase> registryreadops = RegistryReadOps.a(DynamicOpsNBT.a, console.dataPackResources.h(), console.f);
WorldDataServer worlddata = (WorldDataServer) worldSession.a(registryreadops, console.datapackconfiguration);
if (worlddata == null) {
System.out.println("No world data found in datapacks!");
System.out.println("folder: "+worldSession.folder);
System.out.println("level name: "+worldSession.getLevelName());
Bukkit.getWorlds()
//World folder: Minecraft only supports the three default types for the main world
//Therefore we can't load a separate world with a custom dimension
//How about just pretending we did
return null;
}
worlddata.checkName(name);
worlddata.a(console.getServerModName(), console.getModded().isPresent());
if (console.options.has("forceUpgrade")) {
Main.convertWorld(worldSession, DataConverterRegistry.a(), console.options.has("eraseCache"),
() -> true, worlddata.getGeneratorSettings().d().d().stream().map((entry) -> ResourceKey.a(IRegistry.K,
entry.getKey().a())).collect(ImmutableSet.toImmutableSet()));
}
long j = BiomeManager.a(Bukkit.getWorlds().get(0).getSeed());
List<MobSpawner> list = ImmutableList.of(new MobSpawnerPhantom(), new MobSpawnerPatrol(), new MobSpawnerCat(), new VillageSiege(), new MobSpawnerTrader(worlddata));
RegistryMaterials<WorldDimension> registrymaterials = worlddata.getGeneratorSettings().d();
WorldDimension worlddimension = registrymaterials.a(actualDimension);
DimensionManager dimensionmanager;
ChunkGenerator chunkgenerator;
if (worlddimension == null) {
//dimensionmanager = console.f.a().d(DimensionManager.OVERWORLD);
//chunkgenerator = GeneratorSettings.a(console.f.b(IRegistry.ay), console.f.b(IRegistry.ar), (new Random()).nextLong());
System.out.println("World dimension is null!");
return null;
} else {
dimensionmanager = worlddimension.b();
chunkgenerator = worlddimension.c();
}
var environment = World.Environment.NORMAL;
ResourceKey<net.minecraft.server.v1_16_R2.World> worldKey = ResourceKey.a(IRegistry.L, new MinecraftKey(name.toLowerCase(Locale.ENGLISH)));
WorldServer internal = new WorldServer(console, console.executorService, worldSession, worlddata, worldKey,
dimensionmanager, console.worldLoadListenerFactory.create(11), chunkgenerator,
worlddata.getGeneratorSettings().isDebugWorld(), j, environment == World.Environment.NORMAL ? list : ImmutableList.of(),
true, environment, null);
if (Bukkit.getWorld(name.toLowerCase(Locale.ENGLISH)) == null) {
System.out.println("Newly created world not found!");
return null;
} else {
console.initWorld(internal, worlddata, worlddata, worlddata.getGeneratorSettings());
internal.setSpawnFlags(true, true);
console.worldServer.put(internal.getDimensionKey(), internal);
server.getPluginManager().callEvent(new WorldInitEvent(internal.getWorld()));
console.loadSpawn(internal.getChunkProvider().playerChunkMap.worldLoadListener, internal);
server.getPluginManager().callEvent(new WorldLoadEvent(internal.getWorld()));
return internal.getWorld();
}
}
}*/
}

View file

@ -1,23 +0,0 @@
package buttondevteam.customdimensions;
import com.mojang.serialization.Lifecycle;
import net.minecraft.server.v1_16_R2.GeneratorSettings;
import net.minecraft.server.v1_16_R2.WorldDataServer;
import net.minecraft.server.v1_16_R2.WorldSettings;
import org.mockito.Mockito;
public class DimensionWorldData extends WorldDataServer {
public DimensionWorldData(WorldSettings worldsettings, GeneratorSettings generatorsettings, Lifecycle lifecycle) {
super(worldsettings, generatorsettings, lifecycle);
}
public static DimensionWorldData create(WorldDataServer data, String name) {
var mock = Mockito.mock(DimensionWorldData.class, Mockito.withSettings().defaultAnswer(invocation -> {
if (invocation.getMethod().getDeclaringClass() == DimensionWorldData.class)
return invocation.callRealMethod();
return invocation.getMethod().invoke(data, invocation.getArguments());
}).stubOnly());
mock.b = new WorldSettings(name, data.getGameType(), false, data.getDifficulty(), true, data.q(), data.D());
return mock;
}
}