2015-12-08 06:47:58 -08:00
|
|
|
package amidst.mojangapi.minecraftinterface.local;
|
2014-01-01 20:17:01 -08:00
|
|
|
|
2015-12-14 04:27:04 -08:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
2015-09-01 16:53:04 -07:00
|
|
|
import amidst.clazz.symbolic.SymbolicClass;
|
|
|
|
import amidst.clazz.symbolic.SymbolicObject;
|
2017-05-27 14:34:21 -07:00
|
|
|
import amidst.clazz.translator.ClassTranslator;
|
2015-12-10 18:04:10 -08:00
|
|
|
import amidst.documentation.ThreadSafe;
|
2018-03-09 23:48:03 -08:00
|
|
|
import amidst.gameengineabstraction.GameEngineType;
|
2016-11-20 14:48:53 -08:00
|
|
|
import amidst.logging.AmidstLogger;
|
2017-06-01 11:56:03 -07:00
|
|
|
import amidst.mojangapi.file.LauncherProfile;
|
2015-12-08 07:04:05 -08:00
|
|
|
import amidst.mojangapi.minecraftinterface.MinecraftInterface;
|
2015-12-14 17:30:50 -08:00
|
|
|
import amidst.mojangapi.minecraftinterface.MinecraftInterfaceException;
|
2015-12-08 06:47:58 -08:00
|
|
|
import amidst.mojangapi.minecraftinterface.RecognisedVersion;
|
|
|
|
import amidst.mojangapi.world.WorldType;
|
2014-01-01 20:17:01 -08:00
|
|
|
|
2015-12-10 18:04:10 -08:00
|
|
|
@ThreadSafe
|
2015-12-08 07:04:05 -08:00
|
|
|
public class LocalMinecraftInterface implements MinecraftInterface {
|
2017-05-31 15:47:12 -07:00
|
|
|
public static LocalMinecraftInterface create(ClassTranslator translator, LauncherProfile launcherProfile)
|
|
|
|
throws LocalMinecraftInterfaceCreationException {
|
|
|
|
return new LocalMinecraftInterfaceBuilder(translator).create(launcherProfile);
|
2017-05-27 14:34:21 -07:00
|
|
|
}
|
|
|
|
|
2015-05-01 22:43:56 -07:00
|
|
|
/**
|
2015-08-31 18:48:21 -07:00
|
|
|
* A GenLayer instance, at quarter scale to the final biome layer (i.e. both
|
|
|
|
* axis are divided by 4). Minecraft calculates biomes at
|
|
|
|
* quarter-resolution, then noisily interpolates the biome-map up to 1:1
|
|
|
|
* resolution when needed, this is the biome GenLayer before it is
|
|
|
|
* interpolated.
|
2015-05-01 22:43:56 -07:00
|
|
|
*/
|
2015-12-09 10:18:59 -08:00
|
|
|
private volatile SymbolicObject quarterResolutionBiomeGenerator;
|
2015-08-31 21:48:13 -07:00
|
|
|
|
2015-05-01 22:43:56 -07:00
|
|
|
/**
|
2015-08-31 18:48:21 -07:00
|
|
|
* A GenLayer instance, the biome layer. (1:1 scale) Minecraft calculates
|
|
|
|
* biomes at quarter-resolution, then noisily interpolates the biome-map up
|
|
|
|
* to 1:1 resolution when needed, this is the interpolated biome GenLayer.
|
2015-05-01 22:43:56 -07:00
|
|
|
*/
|
2015-12-09 10:18:59 -08:00
|
|
|
private volatile SymbolicObject fullResolutionBiomeGenerator;
|
2015-08-31 21:48:13 -07:00
|
|
|
|
2015-12-08 05:45:43 -08:00
|
|
|
private final SymbolicClass intCacheClass;
|
|
|
|
private final SymbolicClass blockInitClass;
|
|
|
|
private final SymbolicClass genLayerClass;
|
|
|
|
private final SymbolicClass worldTypeClass;
|
2016-11-17 13:51:24 -08:00
|
|
|
private final SymbolicClass genOptionsFactoryClass;
|
2015-12-08 05:45:43 -08:00
|
|
|
private final RecognisedVersion recognisedVersion;
|
2015-08-31 18:48:21 -07:00
|
|
|
|
2016-04-11 05:29:06 -07:00
|
|
|
LocalMinecraftInterface(
|
|
|
|
SymbolicClass intCacheClass,
|
|
|
|
SymbolicClass blockInitClass,
|
|
|
|
SymbolicClass genLayerClass,
|
|
|
|
SymbolicClass worldTypeClass,
|
2016-11-17 13:51:24 -08:00
|
|
|
SymbolicClass genOptionsFactoryClass,
|
2016-04-11 05:29:06 -07:00
|
|
|
RecognisedVersion recognisedVersion) {
|
2015-12-10 18:04:10 -08:00
|
|
|
this.intCacheClass = intCacheClass;
|
|
|
|
this.blockInitClass = blockInitClass;
|
|
|
|
this.genLayerClass = genLayerClass;
|
|
|
|
this.worldTypeClass = worldTypeClass;
|
2016-11-17 13:51:24 -08:00
|
|
|
this.genOptionsFactoryClass = genOptionsFactoryClass;
|
2015-12-08 04:00:22 -08:00
|
|
|
this.recognisedVersion = recognisedVersion;
|
2014-01-01 20:17:01 -08:00
|
|
|
}
|
2015-08-31 18:48:21 -07:00
|
|
|
|
2014-01-01 20:17:01 -08:00
|
|
|
@Override
|
2015-12-14 17:30:50 -08:00
|
|
|
public synchronized int[] getBiomeData(int x, int y, int width, int height, boolean useQuarterResolution)
|
|
|
|
throws MinecraftInterfaceException {
|
2015-12-14 04:27:04 -08:00
|
|
|
try {
|
2015-12-14 15:45:53 -08:00
|
|
|
intCacheClass.callStaticMethod(SymbolicNames.METHOD_INT_CACHE_RESET_INT_CACHE);
|
2016-11-20 13:07:14 -08:00
|
|
|
return (int[]) getBiomeGenerator(useQuarterResolution)
|
|
|
|
.callMethod(SymbolicNames.METHOD_GEN_LAYER_GET_INTS, x, y, width, height);
|
2015-12-23 11:16:06 -08:00
|
|
|
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
2015-12-14 17:30:50 -08:00
|
|
|
throw new MinecraftInterfaceException("unable to get biome data", e);
|
2015-12-14 04:27:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-08 05:45:43 -08:00
|
|
|
private SymbolicObject getBiomeGenerator(boolean useQuarterResolution) {
|
|
|
|
if (useQuarterResolution) {
|
|
|
|
return quarterResolutionBiomeGenerator;
|
2015-08-31 21:48:13 -07:00
|
|
|
} else {
|
2015-12-08 05:45:43 -08:00
|
|
|
return fullResolutionBiomeGenerator;
|
2015-08-31 21:48:13 -07:00
|
|
|
}
|
2014-01-01 20:17:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-14 17:30:50 -08:00
|
|
|
public synchronized void createWorld(long seed, WorldType worldType, String generatorOptions)
|
|
|
|
throws MinecraftInterfaceException {
|
2015-12-14 04:27:04 -08:00
|
|
|
try {
|
2016-11-20 14:48:53 -08:00
|
|
|
AmidstLogger.info("Creating world with seed '" + seed + "' and type '" + worldType.getName() + "'");
|
|
|
|
AmidstLogger.info("Using the following generator options: " + generatorOptions);
|
2015-12-14 15:45:53 -08:00
|
|
|
initializeBlock();
|
|
|
|
Object[] genLayers = getGenLayers(seed, worldType, generatorOptions);
|
|
|
|
quarterResolutionBiomeGenerator = new SymbolicObject(genLayerClass, genLayers[0]);
|
|
|
|
fullResolutionBiomeGenerator = new SymbolicObject(genLayerClass, genLayers[1]);
|
2015-12-23 11:16:06 -08:00
|
|
|
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
2015-12-14 17:30:50 -08:00
|
|
|
throw new MinecraftInterfaceException("unable to create world", e);
|
2015-12-14 04:27:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 10:18:59 -08:00
|
|
|
/**
|
|
|
|
* Minecraft 1.8 and higher require block initialization to be called before
|
|
|
|
* creating a biome generator.
|
|
|
|
*/
|
2016-11-20 13:07:14 -08:00
|
|
|
private void initializeBlock() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
2015-08-31 21:48:13 -07:00
|
|
|
if (blockInitClass != null) {
|
2015-12-14 15:45:53 -08:00
|
|
|
blockInitClass.callStaticMethod(SymbolicNames.METHOD_BLOCK_INIT_INITIALIZE);
|
2015-08-31 21:48:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-14 15:45:53 -08:00
|
|
|
private Object[] getGenLayers(long seed, WorldType worldType, String generatorOptions)
|
2016-11-20 13:07:14 -08:00
|
|
|
throws IllegalAccessException,
|
|
|
|
IllegalArgumentException,
|
|
|
|
InvocationTargetException {
|
2014-01-01 20:17:01 -08:00
|
|
|
if (worldTypeClass == null) {
|
2016-11-20 13:07:14 -08:00
|
|
|
return (Object[]) genLayerClass
|
|
|
|
.callStaticMethod(SymbolicNames.METHOD_GEN_LAYER_INITIALIZE_ALL_BIOME_GENERATORS_1, seed);
|
2016-11-20 08:53:53 -08:00
|
|
|
} else if (genLayerClass.hasMethod(SymbolicNames.METHOD_GEN_LAYER_INITIALIZE_ALL_BIOME_GENERATORS_4)) {
|
2016-11-20 13:07:14 -08:00
|
|
|
return (Object[]) genLayerClass.callStaticMethod(
|
|
|
|
SymbolicNames.METHOD_GEN_LAYER_INITIALIZE_ALL_BIOME_GENERATORS_4,
|
|
|
|
seed,
|
|
|
|
getWorldType(worldType).getObject(),
|
|
|
|
getGeneratorOptions(generatorOptions).getObject());
|
2015-12-14 15:45:53 -08:00
|
|
|
} else if (genLayerClass.hasMethod(SymbolicNames.METHOD_GEN_LAYER_INITIALIZE_ALL_BIOME_GENERATORS_3)) {
|
2016-11-20 13:07:14 -08:00
|
|
|
return (Object[]) genLayerClass.callStaticMethod(
|
|
|
|
SymbolicNames.METHOD_GEN_LAYER_INITIALIZE_ALL_BIOME_GENERATORS_3,
|
|
|
|
seed,
|
|
|
|
getWorldType(worldType).getObject(),
|
|
|
|
generatorOptions);
|
2014-01-01 20:17:01 -08:00
|
|
|
} else {
|
2016-11-20 13:07:14 -08:00
|
|
|
return (Object[]) genLayerClass.callStaticMethod(
|
|
|
|
SymbolicNames.METHOD_GEN_LAYER_INITIALIZE_ALL_BIOME_GENERATORS_2,
|
|
|
|
seed,
|
|
|
|
getWorldType(worldType).getObject());
|
2014-01-01 20:17:01 -08:00
|
|
|
}
|
2015-08-31 21:48:13 -07:00
|
|
|
}
|
2016-11-20 13:07:14 -08:00
|
|
|
|
2016-11-17 13:51:24 -08:00
|
|
|
private SymbolicObject getGeneratorOptions(String generatorOptions)
|
2016-11-20 13:07:14 -08:00
|
|
|
throws IllegalAccessException,
|
|
|
|
IllegalArgumentException,
|
|
|
|
InvocationTargetException {
|
|
|
|
SymbolicObject factory = (SymbolicObject) genOptionsFactoryClass
|
|
|
|
.callStaticMethod(SymbolicNames.METHOD_GEN_OPTIONS_FACTORY_JSON_TO_FACTORY, generatorOptions);
|
2016-11-17 13:51:24 -08:00
|
|
|
return (SymbolicObject) factory.callMethod(SymbolicNames.METHOD_GEN_OPTIONS_FACTORY_BUILD);
|
|
|
|
}
|
2015-08-31 21:48:13 -07:00
|
|
|
|
2016-11-20 13:07:14 -08:00
|
|
|
private SymbolicObject getWorldType(WorldType worldType) throws IllegalArgumentException, IllegalAccessException {
|
2015-12-16 08:05:58 -08:00
|
|
|
return (SymbolicObject) worldTypeClass.getStaticFieldValue(worldType.getSymbolicFieldName());
|
2014-01-01 20:17:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-12-08 04:00:22 -08:00
|
|
|
public RecognisedVersion getRecognisedVersion() {
|
|
|
|
return recognisedVersion;
|
2014-01-01 20:17:01 -08:00
|
|
|
}
|
2018-03-09 23:48:03 -08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public GameEngineType getGameEngineType() {
|
|
|
|
return GameEngineType.MINECRAFT;
|
|
|
|
}
|
2014-01-01 20:17:01 -08:00
|
|
|
}
|