Refactored position registry

This commit is contained in:
LemADEC 2016-05-23 16:57:59 -04:00
parent 6d5faa6863
commit 78e971116e
3 changed files with 98 additions and 74 deletions

View file

@ -72,8 +72,8 @@ public abstract class TileEntityAbstractBase extends TileEntity implements IBloc
int transfer;
while (qtyLeft > 0) {
transfer = Math.min(qtyLeft, stack.getMaxStackSize());
ItemStack dropItemStack = copyWithSize(stack, transfer);
EntityItem entityItem = new EntityItem(worldObj, xCoord + 0.5D, yCoord + 1.0D, zCoord + 0.5D, dropItemStack);
ItemStack itemStackDrop = copyWithSize(stack, transfer);
EntityItem entityItem = new EntityItem(worldObj, xCoord + 0.5D, yCoord + 1.0D, zCoord + 0.5D, itemStackDrop);
worldObj.spawnEntityInWorld(entityItem);
qtyLeft -= transfer;
}

View file

@ -0,0 +1,84 @@
package cr0s.warpdrive.data;
import cr0s.warpdrive.config.WarpDriveConfig;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.WorldServer;
import net.minecraft.world.gen.ChunkProviderServer;
import net.minecraftforge.common.DimensionManager;
public class GlobalPosition {
public final int dimensionId;
public final int x, y, z;
public GlobalPosition(final int dimensionId, final int x, final int y, final int z) {
this.dimensionId = dimensionId;
this.x = x;
this.y = y;
this.z = z;
}
public GlobalPosition(TileEntity tileEntity) {
this(tileEntity.getWorldObj().provider.dimensionId, tileEntity.xCoord, tileEntity.yCoord, tileEntity.zCoord);
}
public WorldServer getWorldServerIfLoaded() {
WorldServer world = DimensionManager.getWorld(dimensionId);
// skip unloaded worlds
if (world == null) {
return null;
}
boolean isLoaded;
if (world.getChunkProvider() instanceof ChunkProviderServer) {
ChunkProviderServer chunkProviderServer = (ChunkProviderServer) world.getChunkProvider();
try {
isLoaded = chunkProviderServer.loadedChunkHashMap.containsItem(ChunkCoordIntPair.chunkXZ2Int(x >> 4, z >> 4));
} catch (NoSuchFieldError exception) {
isLoaded = chunkProviderServer.chunkExists(x >> 4, z >> 4);
}
} else {
isLoaded = world.getChunkProvider().chunkExists(x >> 4, z >> 4);
}
// skip unloaded chunks
if (!isLoaded) {
return null;
}
return world;
}
public boolean isLoaded() {
return getWorldServerIfLoaded() != null;
}
public VectorI getSpaceCoordinates() {
if (dimensionId == WarpDriveConfig.G_SPACE_DIMENSION_ID) {
return new VectorI(x, y + 256, z);
}
if (dimensionId == WarpDriveConfig.G_HYPERSPACE_DIMENSION_ID) {
return new VectorI(x, y + 512, z);
}
for (Planet planet : WarpDriveConfig.PLANETS) {
if (planet.dimensionId == dimensionId) {
if ( (Math.abs(x - planet.dimensionCenterX) <= planet.borderSizeX)
&& (Math.abs(z - planet.dimensionCenterZ) <= planet.borderSizeZ)) {
return new VectorI(
x - planet.dimensionCenterX + planet.spaceCenterX,
y,
z - planet.dimensionCenterZ + planet.spaceCenterZ);
}
}
}
return null;
}
public boolean equals(final TileEntity tileEntity) {
return dimensionId == tileEntity.getWorldObj().provider.dimensionId
&& x == tileEntity.xCoord && y == tileEntity.yCoord && z == tileEntity.zCoord;
}
@Override
public int hashCode() {
return dimensionId << 24 + (x >> 10) << 12 + y << 10 + (z >> 10);
}
}

View file

@ -2,18 +2,11 @@ package cr0s.warpdrive.data;
import java.util.UUID;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.WorldServer;
import net.minecraft.world.gen.ChunkProviderServer;
import net.minecraftforge.common.DimensionManager;
import cr0s.warpdrive.block.movement.TileEntityShipCore;
import cr0s.warpdrive.config.WarpDriveConfig;
public class StarMapEntry {
public StarMapEntryType type = StarMapEntryType.UNDEFINED;
public UUID uuid = null;
public int dimensionId = -666;
public int x, y, z = 0;
public class StarMapEntry extends GlobalPosition {
public StarMapEntryType type;
public UUID uuid;
public int maxX, maxY, maxZ;
public int minX, minY, minZ;
public int volume;
@ -41,18 +34,15 @@ public class StarMapEntry {
}
public StarMapEntry(
final int type, final UUID uuid,
final StarMapEntryType type, final UUID uuid,
final int dimensionId, final int x, final int y, final int z,
final int maxX, final int maxY, final int maxZ,
final int minX, final int minY, final int minZ,
final int volume, final double isolationRate,
final String name) {
this.type = StarMapEntryType.SHIP;
super(dimensionId, x, y, z);
this.type = type;
this.uuid = uuid;
this.dimensionId = dimensionId;
this.x = x;
this.y = y;
this.z = z;
this.maxX = maxX;
this.maxY = maxY;
this.maxZ = maxZ;
@ -66,62 +56,12 @@ public class StarMapEntry {
public StarMapEntry(TileEntityShipCore core) {
this(
0, core.uuid,
core.getWorldObj().provider.dimensionId, core.xCoord, core.yCoord, core.zCoord,
core.maxX, core.maxY, core.maxZ,
core.minX, core.minY, core.minZ,
core.shipMass, core.isolationRate,
core.shipName);
}
public WorldServer getWorldServerIfLoaded() {
WorldServer world = DimensionManager.getWorld(dimensionId);
// skip unloaded worlds
if (world == null) {
return null;
}
boolean isLoaded;
if (world.getChunkProvider() instanceof ChunkProviderServer) {
ChunkProviderServer chunkProviderServer = (ChunkProviderServer) world.getChunkProvider();
try {
isLoaded = chunkProviderServer.loadedChunkHashMap.containsItem(ChunkCoordIntPair.chunkXZ2Int(x >> 4, z >> 4));
} catch (NoSuchFieldError exception) {
isLoaded = chunkProviderServer.chunkExists(x >> 4, z >> 4);
}
} else {
isLoaded = world.getChunkProvider().chunkExists(x >> 4, z >> 4);
}
// skip unloaded chunks
if (!isLoaded) {
return null;
}
return world;
}
public boolean isLoaded() {
return getWorldServerIfLoaded() != null;
}
public VectorI getSpaceCoordinates() {
if (dimensionId == WarpDriveConfig.G_SPACE_DIMENSION_ID) {
return new VectorI(x, y + 256, z);
}
if (dimensionId == WarpDriveConfig.G_HYPERSPACE_DIMENSION_ID) {
return new VectorI(x, y + 512, z);
}
for (Planet planet : WarpDriveConfig.PLANETS) {
if (planet.dimensionId == dimensionId) {
if ( (Math.abs(x - planet.dimensionCenterX) <= planet.borderSizeX)
&& (Math.abs(z - planet.dimensionCenterZ) <= planet.borderSizeZ)) {
return new VectorI(
x - planet.dimensionCenterX + planet.spaceCenterX,
y,
z - planet.dimensionCenterZ + planet.spaceCenterZ);
}
}
}
return null;
StarMapEntryType.SHIP, core.uuid,
core.getWorldObj().provider.dimensionId, core.xCoord, core.yCoord, core.zCoord,
core.maxX, core.maxY, core.maxZ,
core.minX, core.minY, core.minZ,
core.shipMass, core.isolationRate,
core.shipName);
}
@Override