Skip to content

Commit

Permalink
Merge pull request #101 from mircokroon/dimensions
Browse files Browse the repository at this point in the history
Added support for custom dimensions
  • Loading branch information
mircokroon authored Jan 31, 2021
2 parents 717bebd + d6ed874 commit e3326c0
Show file tree
Hide file tree
Showing 17 changed files with 1,007 additions and 552 deletions.
15 changes: 1 addition & 14 deletions src/main/java/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import game.data.Coordinate2D;
import game.data.Coordinate3D;
import game.data.Dimension;
import game.data.dimension.Dimension;
import game.data.registries.RegistryLoader;
import game.data.WorldManager;
import game.data.chunk.ChunkFactory;
Expand Down Expand Up @@ -98,8 +98,6 @@ public static void init(Namespace args) {

Palette.setMaskBedrock(args.getBoolean("mask-bedrock"));

initFolders();

WorldManager.setSaveServiceVariables(args.getBoolean("mark-new-chunks"), args.getBoolean("write-chunks"));
if (args.getBoolean("gui")) {
GuiManager.showGui();
Expand All @@ -110,17 +108,6 @@ public static void init(Namespace args) {
versionHandler = ProtocolVersionHandler.getInstance();
}

public static void initFolders() {
File dirOverworld = Paths.get(getExportDirectory(), Dimension.OVERWORLD.getPath(),"region").toFile();
if (!dirOverworld.isDirectory()) { dirOverworld.mkdirs(); }

File dirNether = Paths.get(getExportDirectory(), Dimension.NETHER.getPath(), "region").toFile();
if (!dirNether.isDirectory()) { dirNether.mkdirs(); }

File dirEnd = Paths.get(getExportDirectory(),Dimension.END.getPath(), "region").toFile();
if (!dirEnd.isDirectory()) { dirEnd.mkdirs(); }
}

public static String getExportDirectory() {
return args.getString("output");
}
Expand Down
200 changes: 101 additions & 99 deletions src/main/java/game/data/Coordinate2D.java
Original file line number Diff line number Diff line change
@@ -1,99 +1,101 @@
package game.data;

import java.util.Objects;

public class Coordinate2D {
private static int offsetX = 0;
private static int offsetZ = 0;

int x;
int z;

public Coordinate2D(int x, int z) {
this.x = x;
this.z = z;
}

public Coordinate2D(double x, double z) {
this.x = (int) x;
this.z = (int) z;
}

public void offset(int x, int z) {
this.x += x;
this.z += z;
}

public static void setOffset(int x, int z) {
offsetX = x >> 4 << 4;
offsetZ = z >> 4 << 4;
}

public void offsetGlobal() {
this.x += offsetX;
this.z += offsetZ;
}

public void offsetChunk() {
this.x += offsetX >> 4;
this.z += offsetZ >> 4;
}

public boolean isInRange(Coordinate2D other, int distanceX, int distanceZ) {
return Math.abs(this.x - other.x) <= distanceX && Math.abs(this.z - other.z) <= distanceZ;
}

public boolean isInRange(Coordinate2D other, int distance) {
return isInRange(other, distance, distance);
}

public Coordinate2D globalToChunk() {
return new Coordinate2D(x >> 4, z >> 4);
}
public Coordinate2D chunkToRegion() {
return new Coordinate2D(x >> 5, z >> 5);
}

public int getX() {
return x;
}

public int getZ() {
return z;
}

@Override
public int hashCode() {
return Objects.hash(x, z);
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
Coordinate2D that = (Coordinate2D) o;
return x == that.x &&
z == that.z;
}

@Override
public String toString() {
return "(" + x + ", " + z + ")";
}

public Coordinate2D toRegionLocal() {
return new Coordinate2D(toLocal(x), toLocal(z));
}

private int toLocal(int pos) {
pos = pos % 32;
if (pos < 0) {
return pos + 32;
}
return pos;
}

public CoordinateDim2D addDimension(Dimension dimension) {
return new CoordinateDim2D(this, dimension);
}
}
package game.data;

import game.data.dimension.Dimension;

import java.util.Objects;

public class Coordinate2D {
private static int offsetX = 0;
private static int offsetZ = 0;

int x;
int z;

public Coordinate2D(int x, int z) {
this.x = x;
this.z = z;
}

public Coordinate2D(double x, double z) {
this.x = (int) x;
this.z = (int) z;
}

public void offset(int x, int z) {
this.x += x;
this.z += z;
}

public static void setOffset(int x, int z) {
offsetX = x >> 4 << 4;
offsetZ = z >> 4 << 4;
}

public void offsetGlobal() {
this.x += offsetX;
this.z += offsetZ;
}

public void offsetChunk() {
this.x += offsetX >> 4;
this.z += offsetZ >> 4;
}

public boolean isInRange(Coordinate2D other, int distanceX, int distanceZ) {
return Math.abs(this.x - other.x) <= distanceX && Math.abs(this.z - other.z) <= distanceZ;
}

public boolean isInRange(Coordinate2D other, int distance) {
return isInRange(other, distance, distance);
}

public Coordinate2D globalToChunk() {
return new Coordinate2D(x >> 4, z >> 4);
}
public Coordinate2D chunkToRegion() {
return new Coordinate2D(x >> 5, z >> 5);
}

public int getX() {
return x;
}

public int getZ() {
return z;
}

@Override
public int hashCode() {
return Objects.hash(x, z);
}

@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
Coordinate2D that = (Coordinate2D) o;
return x == that.x &&
z == that.z;
}

@Override
public String toString() {
return "(" + x + ", " + z + ")";
}

public Coordinate2D toRegionLocal() {
return new Coordinate2D(toLocal(x), toLocal(z));
}

private int toLocal(int pos) {
pos = pos % 32;
if (pos < 0) {
return pos + 32;
}
return pos;
}

public CoordinateDim2D addDimension(Dimension dimension) {
return new CoordinateDim2D(this, dimension);
}
}
144 changes: 73 additions & 71 deletions src/main/java/game/data/Coordinate3D.java
Original file line number Diff line number Diff line change
@@ -1,71 +1,73 @@
package game.data;

public class Coordinate3D extends Coordinate2D {
private int y;

public Coordinate3D(double x, double y, double z) {
this((int) x, (int) y, (int) z);
}

public Coordinate3D(int x, int y, int z) {
super(x, z);
this.y = y;
}

public Coordinate3D add(Coordinate2D change) {
return new Coordinate3D(this.x + change.x, this.y, this.z + change.z);
}

public void setTo(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}

public int getY() {
return y;
}

public Coordinate3D withinChunk() {
return new Coordinate3D(withinChunk(x), y, withinChunk(z));
}

private int withinChunk(int val) {
int newVal = val % 16;
if (newVal < 0) { return newVal + 16; }
return newVal;
}

public CoordinateDim3D addDimension3D(Dimension dimension) {
return new CoordinateDim3D(this, dimension);
}

@Override
public String toString() {
return "(" + x + ", " + y + ", " + z + ")";
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}

Coordinate3D that = (Coordinate3D) o;

return y == that.y;
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + y;
return result;
}
}
package game.data;

import game.data.dimension.Dimension;

public class Coordinate3D extends Coordinate2D {
private int y;

public Coordinate3D(double x, double y, double z) {
this((int) x, (int) y, (int) z);
}

public Coordinate3D(int x, int y, int z) {
super(x, z);
this.y = y;
}

public Coordinate3D add(Coordinate2D change) {
return new Coordinate3D(this.x + change.x, this.y, this.z + change.z);
}

public void setTo(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}

public int getY() {
return y;
}

public Coordinate3D withinChunk() {
return new Coordinate3D(withinChunk(x), y, withinChunk(z));
}

private int withinChunk(int val) {
int newVal = val % 16;
if (newVal < 0) { return newVal + 16; }
return newVal;
}

public CoordinateDim3D addDimension3D(Dimension dimension) {
return new CoordinateDim3D(this, dimension);
}

@Override
public String toString() {
return "(" + x + ", " + y + ", " + z + ")";
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}

Coordinate3D that = (Coordinate3D) o;

return y == that.y;
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + y;
return result;
}
}
Loading

0 comments on commit e3326c0

Please sign in to comment.