created the class WorldSeed
parent
ef3e5e0aff
commit
51f57cb5a0
|
@ -149,7 +149,7 @@ public class MainWindow {
|
|||
*/
|
||||
public void setWorldSurroundings(WorldSurroundings worldSurroundings) {
|
||||
clearWorldSurroundings();
|
||||
seedHistoryLogger.log(worldSurroundings.getSeed());
|
||||
seedHistoryLogger.log(worldSurroundings.getWorldSeed());
|
||||
contentPane.add(worldSurroundings.getComponent(), BorderLayout.CENTER);
|
||||
menuBar.setWorldMenuEnabled(true);
|
||||
menuBar.setPlayerLocationMenusEnabled(worldSurroundings
|
||||
|
|
|
@ -18,6 +18,7 @@ import amidst.gui.worldsurroundings.WorldSurroundings;
|
|||
import amidst.logging.Log;
|
||||
import amidst.mojangapi.MojangApi;
|
||||
import amidst.mojangapi.world.CoordinatesInWorld;
|
||||
import amidst.mojangapi.world.WorldSeed;
|
||||
import amidst.mojangapi.world.WorldType;
|
||||
import amidst.mojangapi.world.icon.WorldIcon;
|
||||
import amidst.preferences.BiomeColorProfile;
|
||||
|
@ -72,29 +73,26 @@ public class MenuActions {
|
|||
}
|
||||
|
||||
public void newFromSeed() {
|
||||
String seed = mainWindow.askForSeed();
|
||||
if (seed != null) {
|
||||
if (seed.isEmpty()) {
|
||||
newFromRandom();
|
||||
String input = mainWindow.askForSeed();
|
||||
if (input != null) {
|
||||
if (input.isEmpty()) {
|
||||
newFromSeed(WorldSeed.random());
|
||||
} else {
|
||||
WorldType worldType = mainWindow.askForWorldType();
|
||||
if (worldType != null) {
|
||||
try {
|
||||
mainWindow.setWorld(mojangApi.createWorldFromSeed(seed,
|
||||
worldType));
|
||||
} catch (IllegalStateException e) {
|
||||
mainWindow.displayException(e);
|
||||
}
|
||||
}
|
||||
newFromSeed(WorldSeed.fromUserInput(input));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void newFromRandom() {
|
||||
newFromSeed(WorldSeed.random());
|
||||
}
|
||||
|
||||
private void newFromSeed(WorldSeed seed) {
|
||||
WorldType worldType = mainWindow.askForWorldType();
|
||||
if (worldType != null) {
|
||||
try {
|
||||
mainWindow.setWorld(mojangApi.createRandomWorld(worldType));
|
||||
mainWindow.setWorld(mojangApi.createWorldFromSeed(seed,
|
||||
worldType));
|
||||
} catch (IllegalStateException e) {
|
||||
mainWindow.displayException(e);
|
||||
}
|
||||
|
@ -170,7 +168,7 @@ public class MenuActions {
|
|||
public void copySeedToClipboard() {
|
||||
WorldSurroundings worldSurroundings = this.worldSurroundings.get();
|
||||
if (worldSurroundings != null) {
|
||||
String seed = "" + worldSurroundings.getSeed();
|
||||
String seed = "" + worldSurroundings.getWorldSeed().getLong();
|
||||
StringSelection selection = new StringSelection(seed);
|
||||
Toolkit.getDefaultToolkit().getSystemClipboard()
|
||||
.setContents(selection, selection);
|
||||
|
|
|
@ -3,24 +3,18 @@ package amidst.gui.widget;
|
|||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
|
||||
import amidst.mojangapi.world.WorldSeed;
|
||||
|
||||
public class SeedWidget extends Widget {
|
||||
private final String text;
|
||||
|
||||
public SeedWidget(CornerAnchorPoint anchor, long seed, String seedText) {
|
||||
public SeedWidget(CornerAnchorPoint anchor, WorldSeed seed) {
|
||||
super(anchor);
|
||||
this.text = createText(seed, seedText);
|
||||
this.text = "Seed: " + seed.getLabel();
|
||||
setWidth(20);
|
||||
setHeight(30);
|
||||
}
|
||||
|
||||
private String createText(long seed, String seedText) {
|
||||
if (seedText == null) {
|
||||
return "Seed: " + seed;
|
||||
} else {
|
||||
return "Seed: " + seedText + " (" + seed + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, FontMetrics fontMetrics, float time) {
|
||||
setWidth(fontMetrics.stringWidth(text) + 20);
|
||||
|
|
|
@ -47,7 +47,7 @@ public class WidgetBuilder {
|
|||
return Arrays.asList(
|
||||
new FpsWidget( CornerAnchorPoint.BOTTOM_LEFT, new FramerateTimer(2), options.showFPS),
|
||||
new ScaleWidget( CornerAnchorPoint.BOTTOM_CENTER, zoom, options.showScale),
|
||||
new SeedWidget( CornerAnchorPoint.TOP_LEFT, world.getSeed(), world.getSeedText()),
|
||||
new SeedWidget( CornerAnchorPoint.TOP_LEFT, world.getWorldSeed()),
|
||||
new DebugWidget( CornerAnchorPoint.BOTTOM_RIGHT, graph, fragmentManager, options.showDebug),
|
||||
new SelectedIconWidget( CornerAnchorPoint.TOP_LEFT, worldIconSelection),
|
||||
new CursorInformationWidget(CornerAnchorPoint.TOP_RIGHT, graph, translator),
|
||||
|
|
|
@ -12,6 +12,7 @@ import amidst.fragment.layer.LayerReloader;
|
|||
import amidst.gui.SkinLoader;
|
||||
import amidst.mojangapi.world.CoordinatesInWorld;
|
||||
import amidst.mojangapi.world.World;
|
||||
import amidst.mojangapi.world.WorldSeed;
|
||||
import amidst.mojangapi.world.icon.WorldIcon;
|
||||
|
||||
public class WorldSurroundings {
|
||||
|
@ -88,8 +89,8 @@ public class WorldSurroundings {
|
|||
zoom.adjustZoom(viewer.getMousePositionOrCenter(), notches);
|
||||
}
|
||||
|
||||
public long getSeed() {
|
||||
return world.getSeed();
|
||||
public WorldSeed getWorldSeed() {
|
||||
return world.getWorldSeed();
|
||||
}
|
||||
|
||||
// TODO: use this
|
||||
|
|
|
@ -12,6 +12,7 @@ import amidst.mojangapi.file.directory.VersionDirectory;
|
|||
import amidst.mojangapi.file.json.versionlist.VersionListJson;
|
||||
import amidst.mojangapi.minecraftinterface.MinecraftInterface;
|
||||
import amidst.mojangapi.minecraftinterface.RecognisedVersion;
|
||||
import amidst.mojangapi.world.WorldSeed;
|
||||
import amidst.mojangapi.world.World;
|
||||
import amidst.mojangapi.world.WorldBuilder;
|
||||
import amidst.mojangapi.world.WorldType;
|
||||
|
@ -94,26 +95,9 @@ public class MojangApi {
|
|||
* one world at a time. Creating a new world will make all previous world
|
||||
* object unusable.
|
||||
*/
|
||||
public World createRandomWorld(WorldType worldType)
|
||||
throws IllegalStateException {
|
||||
public World createWorldFromSeed(WorldSeed seed, WorldType worldType) {
|
||||
if (canCreateWorld()) {
|
||||
return worldBuilder.random(minecraftInterface, worldType);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"cannot create a world without a minecraft interface");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Due to the limitation of the minecraft interface, you can only world with
|
||||
* one world at a time. Creating a new world will make all previous world
|
||||
* object unusable.
|
||||
*/
|
||||
public World createWorldFromSeed(String seedText, WorldType worldType)
|
||||
throws IllegalStateException {
|
||||
if (canCreateWorld()) {
|
||||
return worldBuilder.fromSeed(minecraftInterface, seedText,
|
||||
worldType);
|
||||
return worldBuilder.fromSeed(minecraftInterface, seed, worldType);
|
||||
} else {
|
||||
throw new IllegalStateException(
|
||||
"cannot create a world without a minecraft interface");
|
||||
|
|
|
@ -10,8 +10,7 @@ import amidst.mojangapi.world.oracle.BiomeDataOracle;
|
|||
import amidst.mojangapi.world.oracle.SlimeChunkOracle;
|
||||
|
||||
public class World {
|
||||
private final long seed;
|
||||
private final String seedText;
|
||||
private final WorldSeed seed;
|
||||
private final WorldType worldType;
|
||||
private final String generatorOptions;
|
||||
private final MovablePlayerList movablePlayerList;
|
||||
|
@ -26,8 +25,8 @@ public class World {
|
|||
private final WorldIconProducer oceanMonumentProducer;
|
||||
private final WorldIconProducer netherFortressProducer;
|
||||
|
||||
World(long seed, String seedText, WorldType worldType,
|
||||
String generatorOptions, MovablePlayerList movablePlayerList,
|
||||
World(WorldSeed seed, WorldType worldType, String generatorOptions,
|
||||
MovablePlayerList movablePlayerList,
|
||||
BiomeDataOracle biomeDataOracle, SlimeChunkOracle slimeChunkOracle,
|
||||
CachedWorldIconProducer playerProducer,
|
||||
CachedWorldIconProducer strongholdProducer,
|
||||
|
@ -37,7 +36,6 @@ public class World {
|
|||
WorldIconProducer oceanMonumentProducer,
|
||||
WorldIconProducer netherFortressProducer) {
|
||||
this.seed = seed;
|
||||
this.seedText = seedText;
|
||||
this.worldType = worldType;
|
||||
this.generatorOptions = generatorOptions;
|
||||
this.movablePlayerList = movablePlayerList;
|
||||
|
@ -53,15 +51,10 @@ public class World {
|
|||
}
|
||||
|
||||
@CalledByAny
|
||||
public long getSeed() {
|
||||
public WorldSeed getWorldSeed() {
|
||||
return seed;
|
||||
}
|
||||
|
||||
@CalledByAny
|
||||
public String getSeedText() {
|
||||
return seedText;
|
||||
}
|
||||
|
||||
@CalledByAny
|
||||
public WorldType getWorldType() {
|
||||
return worldType;
|
||||
|
|
|
@ -2,8 +2,8 @@ package amidst.mojangapi.world;
|
|||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
|
||||
import amidst.documentation.ThreadSafe;
|
||||
import amidst.logging.Log;
|
||||
import amidst.mojangapi.file.directory.SaveDirectory;
|
||||
import amidst.mojangapi.file.nbt.LevelDat;
|
||||
|
@ -24,56 +24,24 @@ import amidst.mojangapi.world.icon.TempleProducer;
|
|||
import amidst.mojangapi.world.icon.VillageProducer;
|
||||
import amidst.mojangapi.world.oracle.BiomeDataOracle;
|
||||
import amidst.mojangapi.world.oracle.SlimeChunkOracle;
|
||||
import amidst.utilities.Google;
|
||||
|
||||
@ThreadSafe
|
||||
public class WorldBuilder {
|
||||
public World random(MinecraftInterface minecraftInterface,
|
||||
WorldType worldType) {
|
||||
// TODO: no Google.track(), because a random seed is not interesting?
|
||||
long seed = new Random().nextLong();
|
||||
return simple(minecraftInterface, seed, null, worldType);
|
||||
}
|
||||
|
||||
public World fromSeed(MinecraftInterface minecraftInterface,
|
||||
String seedText, WorldType worldType) {
|
||||
long seed = getSeedFromString(seedText);
|
||||
Google.track("seed/" + seedText + "/" + seed);
|
||||
if (isNumericSeed(seedText, seed)) {
|
||||
return simple(minecraftInterface, seed, null, worldType);
|
||||
} else {
|
||||
return simple(minecraftInterface, seed, seedText, worldType);
|
||||
}
|
||||
}
|
||||
|
||||
private long getSeedFromString(String seed) {
|
||||
try {
|
||||
return Long.parseLong(seed);
|
||||
} catch (NumberFormatException err) {
|
||||
return seed.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNumericSeed(String seedText, long seed) {
|
||||
return ("" + seed).equals(seedText);
|
||||
WorldSeed seed, WorldType worldType) {
|
||||
return create(minecraftInterface, seed, worldType, "",
|
||||
MovablePlayerList.empty());
|
||||
}
|
||||
|
||||
public World fromFile(MinecraftInterface minecraftInterface,
|
||||
SaveDirectory saveDirectory) throws FileNotFoundException,
|
||||
IOException {
|
||||
World world = createFromFile(minecraftInterface, saveDirectory);
|
||||
Google.track("seed/file/" + world.getSeed());
|
||||
return world;
|
||||
}
|
||||
|
||||
private World createFromFile(MinecraftInterface minecraftInterface,
|
||||
SaveDirectory saveDirectory) throws FileNotFoundException,
|
||||
IOException {
|
||||
LevelDat levelDat = saveDirectory.createLevelDat();
|
||||
if (saveDirectory.isMultiPlayer()) {
|
||||
Log.i("Multiplayer world detected.");
|
||||
return file(
|
||||
return create(
|
||||
minecraftInterface,
|
||||
levelDat.getSeed(),
|
||||
WorldSeed.fromFile(levelDat.getSeed()),
|
||||
levelDat.getWorldType(),
|
||||
levelDat.getGeneratorOptions(),
|
||||
createMovablePlayerList(minecraftInterface,
|
||||
|
@ -81,9 +49,9 @@ public class WorldBuilder {
|
|||
new MultiPlayerPlayerMover(saveDirectory)));
|
||||
} else {
|
||||
Log.i("Singleplayer world detected.");
|
||||
return file(
|
||||
return create(
|
||||
minecraftInterface,
|
||||
levelDat.getSeed(),
|
||||
WorldSeed.fromFile(levelDat.getSeed()),
|
||||
levelDat.getWorldType(),
|
||||
levelDat.getGeneratorOptions(),
|
||||
createMovablePlayerList(minecraftInterface,
|
||||
|
@ -102,41 +70,28 @@ public class WorldBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
private World simple(MinecraftInterface minecraftInterface, long seed,
|
||||
String seedText, WorldType worldType) {
|
||||
return create(minecraftInterface, seed, seedText, worldType, "",
|
||||
MovablePlayerList.empty());
|
||||
}
|
||||
|
||||
public World file(MinecraftInterface minecraftInterface, long seed,
|
||||
private World create(MinecraftInterface minecraftInterface, WorldSeed seed,
|
||||
WorldType worldType, String generatorOptions,
|
||||
MovablePlayerList movablePlayerList) {
|
||||
return create(minecraftInterface, seed, null, worldType,
|
||||
generatorOptions, movablePlayerList);
|
||||
}
|
||||
|
||||
private World create(MinecraftInterface minecraftInterface, long seed,
|
||||
String seedText, WorldType worldType, String generatorOptions,
|
||||
MovablePlayerList movablePlayerList) {
|
||||
// @formatter:off
|
||||
minecraftInterface.createWorld(seed, worldType, generatorOptions);
|
||||
seed.trackIfNecessary();
|
||||
minecraftInterface.createWorld(seed.getLong(), worldType, generatorOptions);
|
||||
RecognisedVersion recognisedVersion = minecraftInterface.getRecognisedVersion();
|
||||
BiomeDataOracle biomeDataOracle = new BiomeDataOracle(minecraftInterface);
|
||||
return new World(
|
||||
seed,
|
||||
seedText,
|
||||
worldType,
|
||||
generatorOptions,
|
||||
movablePlayerList,
|
||||
biomeDataOracle,
|
||||
new SlimeChunkOracle( seed),
|
||||
new SlimeChunkOracle( seed.getLong()),
|
||||
new PlayerProducer( recognisedVersion, movablePlayerList),
|
||||
new SpawnProducer( recognisedVersion, seed, biomeDataOracle),
|
||||
new StrongholdProducer( recognisedVersion, seed, biomeDataOracle),
|
||||
new TempleProducer( recognisedVersion, seed, biomeDataOracle),
|
||||
new VillageProducer( recognisedVersion, seed, biomeDataOracle),
|
||||
new OceanMonumentProducer( recognisedVersion, seed, biomeDataOracle),
|
||||
new NetherFortressProducer(recognisedVersion, seed, biomeDataOracle));
|
||||
new SpawnProducer( recognisedVersion, seed.getLong(), biomeDataOracle),
|
||||
new StrongholdProducer( recognisedVersion, seed.getLong(), biomeDataOracle),
|
||||
new TempleProducer( recognisedVersion, seed.getLong(), biomeDataOracle),
|
||||
new VillageProducer( recognisedVersion, seed.getLong(), biomeDataOracle),
|
||||
new OceanMonumentProducer( recognisedVersion, seed.getLong(), biomeDataOracle),
|
||||
new NetherFortressProducer(recognisedVersion, seed.getLong(), biomeDataOracle));
|
||||
// @formatter:on
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
package amidst.mojangapi.world;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import amidst.documentation.Immutable;
|
||||
import amidst.utilities.Google;
|
||||
|
||||
@Immutable
|
||||
public class WorldSeed {
|
||||
public static WorldSeed random() {
|
||||
return new WorldSeed(new Random().nextLong(), null, null);
|
||||
}
|
||||
|
||||
public static WorldSeed fromUserInput(String input) {
|
||||
try {
|
||||
long seed = Long.parseLong(input);
|
||||
return new WorldSeed(seed, null, "seed/" + input + "/" + seed);
|
||||
} catch (NumberFormatException err) {
|
||||
int seed = input.hashCode();
|
||||
return new WorldSeed(seed, input, "seed/" + input + "/" + seed);
|
||||
}
|
||||
}
|
||||
|
||||
public static WorldSeed fromFile(long seed) {
|
||||
return new WorldSeed(seed, null, "seed/file/" + seed);
|
||||
}
|
||||
|
||||
private final long seed;
|
||||
private final String text;
|
||||
private final String label;
|
||||
private final String trackingString;
|
||||
|
||||
private WorldSeed(long seed, String text, String trackingString) {
|
||||
this.seed = seed;
|
||||
this.text = text;
|
||||
this.label = createLabel();
|
||||
this.trackingString = trackingString;
|
||||
}
|
||||
|
||||
private String createLabel() {
|
||||
if (text != null) {
|
||||
return text + " (" + seed + ")";
|
||||
} else {
|
||||
return "" + seed;
|
||||
}
|
||||
}
|
||||
|
||||
public long getLong() {
|
||||
return seed;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
// TODO: make google tracking non-static
|
||||
@Deprecated
|
||||
public void trackIfNecessary() {
|
||||
if (trackingString != null) {
|
||||
Google.track(trackingString);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ import java.sql.Timestamp;
|
|||
import java.util.Date;
|
||||
|
||||
import amidst.logging.Log;
|
||||
import amidst.mojangapi.world.WorldSeed;
|
||||
|
||||
public class SeedHistoryLogger {
|
||||
private File file;
|
||||
|
@ -15,7 +16,7 @@ public class SeedHistoryLogger {
|
|||
this.file = getHistoryFile(filename);
|
||||
}
|
||||
|
||||
public void log(long seed) {
|
||||
public void log(WorldSeed seed) {
|
||||
if (file != null) {
|
||||
if (!file.exists()) {
|
||||
tryCreateFile();
|
||||
|
@ -43,7 +44,7 @@ public class SeedHistoryLogger {
|
|||
}
|
||||
}
|
||||
|
||||
private void writeLine(long seed) {
|
||||
private void writeLine(WorldSeed seed) {
|
||||
FileWriter writer = null;
|
||||
try {
|
||||
writer = new FileWriter(file, true);
|
||||
|
@ -56,8 +57,9 @@ public class SeedHistoryLogger {
|
|||
}
|
||||
}
|
||||
|
||||
private String createLine(long seed) {
|
||||
return new Timestamp(new Date().getTime()) + " " + seed + "\r\n";
|
||||
private String createLine(WorldSeed seed) {
|
||||
return new Timestamp(new Date().getTime()) + " " + seed.getLong()
|
||||
+ "\r\n";
|
||||
}
|
||||
|
||||
private void closeWriter(FileWriter writer) {
|
||||
|
|
Loading…
Reference in New Issue