Skip to content

Commit

Permalink
Change portal coordinate system and adjust yaw (#4)
Browse files Browse the repository at this point in the history
* Change coordinate system for portals and calculate yaw

* Ensure backwards compatibility with old coordinate system

* spotlessApply

* Do not regenerate a portal
  • Loading branch information
xchgeax authored Aug 16, 2022
1 parent 86d93c8 commit cf086fe
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 34 deletions.
73 changes: 46 additions & 27 deletions src/main/java/xyz/kubasz/personalspace/block/PortalTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public class PortalTileEntity extends TileEntity {

public boolean active = false;
public int targetDimId = 0;
public int targetX = 8;
public int targetY = 8;
public int targetZ = 8;
public int targetPosX = 8;
public int targetPosY = 8;
public int targetPosZ = 8;
public ForgeDirection targetFacing = DEFAULT_FACING;
public ForgeDirection facing = DEFAULT_FACING;

public PortalTileEntity() {}
Expand All @@ -47,9 +48,9 @@ public void readFromNBT(NBTTagCompound tag) {
if (tag.hasKey("remotePos")) {
isLegacy = true;
int[] pos_array = tag.getIntArray("remotePos");
this.targetX = pos_array[0];
this.targetY = pos_array[1];
this.targetZ = pos_array[2];
this.targetPosX = pos_array[0];
this.targetPosY = pos_array[1];
this.targetPosZ = pos_array[2];
}
if (tag.hasKey("remoteDir")) {
isLegacy = true;
Expand All @@ -68,8 +69,6 @@ public void readFromNBT(NBTTagCompound tag) {
this.facing = ForgeDirection.SOUTH;
break;
}
this.targetX += 1;
this.targetZ += 1;
}
if (tag.hasKey("localDimensionId")) {
isLegacy = true;
Expand All @@ -84,18 +83,25 @@ public void readFromNBT(NBTTagCompound tag) {
if (tag.hasKey("target")) {
int[] t_array = tag.getIntArray("target");
this.targetDimId = t_array[0];
this.targetX = t_array[1];
this.targetY = t_array[2];
this.targetZ = t_array[3];
this.targetPosX = t_array[1];
this.targetPosY = t_array[2];
this.targetPosZ = t_array[3];
}
if (tag.hasKey("facing")) {
facing = ForgeDirection.getOrientation(tag.getInteger("facing"));
}
if (tag.hasKey("targetFacing")) {
targetFacing = ForgeDirection.getOrientation(tag.getInteger("targetFacing"));
}

if (isLegacy) {
this.active = true;
PersonalSpaceMod.LOG.info(
"Migrated old UW portal to dim {} : target {},{},{}", targetDimId, targetX, targetY, targetZ);
"Migrated old UW portal to dim {} : target {},{},{}",
targetDimId,
targetPosX,
targetPosY,
targetPosZ);
markDirty();
}
if (facing == ForgeDirection.UNKNOWN) {
Expand All @@ -115,8 +121,9 @@ public void markDirty() {
public void writeToNBT(NBTTagCompound tag) {
super.writeToNBT(tag);
tag.setBoolean("active", this.active);
tag.setIntArray("target", new int[] {this.targetDimId, this.targetX, this.targetY, this.targetZ});
tag.setIntArray("target", new int[] {this.targetDimId, this.targetPosX, this.targetPosY, this.targetPosZ});
tag.setInteger("facing", this.facing.ordinal());
tag.setInteger("targetFacing", this.targetFacing.ordinal());
}

@Override
Expand All @@ -139,10 +146,23 @@ public void transport(EntityPlayerMP player) {
return;
}

PersonalTeleporter tp = new PersonalTeleporter((WorldServer) worldObj, targetX, targetY, targetZ);
PersonalTeleporter tp = new PersonalTeleporter(this, (WorldServer) worldObj);

player.mcServer.getConfigurationManager().transferPlayerToDimension(player, this.targetDimId, tp);
}

public int getTargetTeleportX() {
return targetPosX + targetFacing.offsetX;
}

public int getTargetTeleportY() {
return targetPosY + targetFacing.offsetY;
}

public int getTargetTeleportZ() {
return targetPosZ + targetFacing.offsetZ;
}

public void linkOtherPortal(boolean spawnNewPortal, EntityPlayerMP player) {
if (!this.active) {
return;
Expand All @@ -159,12 +179,12 @@ public void linkOtherPortal(boolean spawnNewPortal, EntityPlayerMP player) {
PersonalSpaceMod.LOG.fatal("Couldn't initialize world {}", this.targetDimId);
return;
}
int otherX = targetX, otherY = targetY, otherZ = targetZ;
int otherX = targetPosX, otherY = targetPosY, otherZ = targetPosZ;
searchloop:
for (otherX = targetX - 1; otherX <= targetX + 1; otherX++) {
for (otherY = targetY - 1; otherY <= targetY + 1; otherY++) {
for (otherX = targetPosX - 1; otherX <= targetPosX + 1; otherX++) {
for (otherY = targetPosY - 1; otherY <= targetPosY + 1; otherY++) {
if (otherY < 0 || otherY > otherWorld.getHeight()) continue;
for (otherZ = targetZ - 1; otherZ <= targetZ + 1; otherZ++) {
for (otherZ = targetPosZ - 1; otherZ <= targetPosZ + 1; otherZ++) {
if (!otherWorld.blockExists(otherX, otherY, otherZ)) {
otherWorld.theChunkProviderServer.loadChunk(otherX >> 4, otherZ >> 4);
}
Expand All @@ -182,9 +202,9 @@ public void linkOtherPortal(boolean spawnNewPortal, EntityPlayerMP player) {
otherPortal = (PortalTileEntity) wte;
}
} else if (spawnNewPortal) {
otherX = targetX;
otherY = targetY;
otherZ = targetZ;
otherX = targetPosX;
otherY = targetPosY;
otherZ = targetPosZ;
otherWorld.setBlock(otherX, otherY, otherZ, PersonalSpaceMod.BLOCK_PORTAL, facing.ordinal(), 3);
otherPortal = (PortalTileEntity) otherWorld.getTileEntity(otherX, otherY, otherZ);
}
Expand All @@ -198,9 +218,10 @@ public void linkOtherPortal(boolean spawnNewPortal, EntityPlayerMP player) {
return;
}
otherPortal.targetDimId = worldObj.provider.dimensionId;
otherPortal.targetX = xCoord + facing.offsetX;
otherPortal.targetY = yCoord + facing.offsetY;
otherPortal.targetZ = zCoord + facing.offsetZ;
otherPortal.targetPosX = xCoord;
otherPortal.targetPosY = yCoord;
otherPortal.targetPosZ = zCoord;
otherPortal.targetFacing = facing;
otherPortal.markDirty();
if (player != null) {
player.addChatMessage(new ChatComponentTranslation("chat.personalWorld.relinked", targetDimId));
Expand Down Expand Up @@ -261,9 +282,7 @@ public void updateSettings(EntityPlayerMP player, DimensionConfig unsafeConfig)
PersonalSpaceMod.INSTANCE.onDimSettingsChangeServer(targetDimId);
this.active = true;
this.targetDimId = targetDimId;
this.targetX = 8 + DEFAULT_FACING.offsetX;
this.targetY = sanitized.getGroundLevel() + 1 + DEFAULT_FACING.offsetY;
this.targetZ = 8 + DEFAULT_FACING.offsetZ;
this.targetPosY = sanitized.getGroundLevel() + 1;
markDirty();
createdNewDim = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import net.minecraft.entity.Entity;
import net.minecraft.world.Teleporter;
import net.minecraft.world.WorldServer;
import xyz.kubasz.personalspace.block.PortalTileEntity;

public class PersonalTeleporter extends Teleporter {

int x, y, z;
private PortalTileEntity sourceTeleporter;

public PersonalTeleporter(WorldServer world, int x, int y, int z) {
super(world);
Expand All @@ -15,22 +17,45 @@ public PersonalTeleporter(WorldServer world, int x, int y, int z) {
this.z = z;
}

public PersonalTeleporter(PortalTileEntity sourceTeleporter, WorldServer world) {
super(world);
this.sourceTeleporter = sourceTeleporter;
this.x = sourceTeleporter.getTargetTeleportX();
this.y = sourceTeleporter.getTargetTeleportY();
this.z = sourceTeleporter.getTargetTeleportZ();
}

@Override
public void placeInPortal(Entity entity, double x, double y, double z, float yaw) {
entity.setLocationAndAngles(this.x + 0.2, this.y + 0.1, this.z + 0.2, yaw, 0.0F);
entity.motionX = 0.0F;
entity.motionY = 0.0F;
entity.motionZ = 0.0F;
public void placeInPortal(Entity entity, double entityPosX, double entityPosY, double entityPosZ, float yaw) {
this.placeInExistingPortal(entity, entityPosX, entityPosY, entityPosZ, yaw);
}

@Override
public boolean placeInExistingPortal(
Entity p_77184_1_, double p_77184_2_, double p_77184_4_, double p_77184_6_, float p_77184_8_) {
Entity entity, double entityPosX, double entityPosY, double entityPosZ, float yaw) {
if (sourceTeleporter != null) {

double dX = sourceTeleporter.targetPosX - sourceTeleporter.getTargetTeleportX();
double dZ = sourceTeleporter.targetPosZ - sourceTeleporter.getTargetTeleportZ();
double distanceXZ = Math.sqrt(dX * dX + dZ * dZ);
double newYaw = Math.acos(dX / distanceXZ) * 180 / Math.PI - 90;

if (dZ < 0) {
newYaw = newYaw - 180;
}

yaw = (float) newYaw;
}

entity.setLocationAndAngles(x + 0.5, y + 0.1, z + 0.5, yaw, 0.0F);
entity.motionX = 0.0F;
entity.motionY = 0.0F;
entity.motionZ = 0.0F;
return true;
}

@Override
public boolean makePortal(Entity p_85188_1_) {
public boolean makePortal(Entity player) {
return true;
}

Expand Down

0 comments on commit cf086fe

Please sign in to comment.