fix world coordinate issues on Fabric

This commit is contained in:
dfsek
2021-06-25 00:45:24 -07:00
parent ff4cbda294
commit da0fb7dd15
25 changed files with 89 additions and 79 deletions

View File

@@ -165,10 +165,10 @@ public class StructureScript implements Structure {
@Override
@SuppressWarnings("try")
public boolean generateDirect(Location location, Random random, Rotation rotation) {
public boolean generateDirect(Vector3 location, World world, Random random, Rotation rotation) {
try(ProfileFrame ignore = main.getProfiler().profile("terrascript_direct:" + id)) {
DirectBuffer buffer = new DirectBuffer(location.toVector(), location.getWorld());
return applyBlock(new TerraImplementationArguments(buffer, rotation, random, location.getWorld(), 0));
DirectBuffer buffer = new DirectBuffer(location, world);
return applyBlock(new TerraImplementationArguments(buffer, rotation, random, world, 0));
}
}

View File

@@ -42,7 +42,7 @@ public class BiomeFunction implements Function<String> {
BiomeProvider grid = main.getWorld(arguments.getWorld()).getBiomeProvider();
return ((UserDefinedBiome) grid.getBiome(arguments.getBuffer().getOrigin().clone().add(new Vector3Impl(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(), FastMath.roundToInt(xz.getZ()))))).getID();
return grid.getBiome(arguments.getBuffer().getOrigin().clone().add(new Vector3Impl(FastMath.roundToInt(xz.getX()), y.apply(implementationArguments, variableMap).intValue(), FastMath.roundToInt(xz.getZ())))).getID();
}

View File

@@ -28,7 +28,7 @@ public class IntermediateBuffer implements Buffer {
@Override
public Buffer addItem(BufferedItem item, Vector3 location) {
return original.addItem(item, location.add(offset));
return original.addItem(item, location.clone().add(offset));
}
@Override
@@ -38,12 +38,12 @@ public class IntermediateBuffer implements Buffer {
@Override
public String getMark(Vector3 location) {
return original.getMark(location.add(offset));
return original.getMark(location.clone().add(offset));
}
@Override
public Buffer setMark(String mark, Vector3 location) {
original.setMark(mark, location.add(offset));
original.setMark(mark, location.clone().add(offset));
return this;
}
}

View File

@@ -110,20 +110,20 @@ public class DefaultChunkGenerator3D implements TerraChunkGenerator {
data = PaletteUtil.getPalette(x, y, z, c, sampler).get(paletteLevel, cx, y, cz);
chunk.setBlock(x, y, z, data);
if(paletteLevel == 0 && c.doSlabs() && y < 255) {
prepareBlockPartFloor(data, chunk.getBlockData(x, y + 1, z), chunk, new Vector3Impl(x, y + 1, z), c.getSlabPalettes(),
prepareBlockPartFloor(data, chunk.getBlock(x, y + 1, z), chunk, new Vector3Impl(x, y + 1, z), c.getSlabPalettes(),
c.getStairPalettes(), c.getSlabThreshold(), sampler);
}
paletteLevel++;
} else if(y <= sea) {
chunk.setBlock(x, y, z, seaPalette.get(sea - y, x + xOrig, y, z + zOrig));
if(justSet && c.doSlabs()) {
prepareBlockPartCeiling(data, chunk.getBlockData(x, y, z), chunk, new Vector3Impl(x, y, z), c.getSlabPalettes(), c.getStairPalettes(), c.getSlabThreshold(), sampler);
prepareBlockPartCeiling(data, chunk.getBlock(x, y, z), chunk, new Vector3Impl(x, y, z), c.getSlabPalettes(), c.getStairPalettes(), c.getSlabThreshold(), sampler);
}
justSet = false;
paletteLevel = 0;
} else {
if(justSet && c.doSlabs()) {
prepareBlockPartCeiling(data, chunk.getBlockData(x, y, z), chunk, new Vector3Impl(x, y, z), c.getSlabPalettes(), c.getStairPalettes(), c.getSlabThreshold(), sampler);
prepareBlockPartCeiling(data, chunk.getBlock(x, y, z), chunk, new Vector3Impl(x, y, z), c.getSlabPalettes(), c.getStairPalettes(), c.getSlabThreshold(), sampler);
}
justSet = false;
paletteLevel = 0;

View File

@@ -7,7 +7,6 @@ import com.dfsek.terra.api.config.WorldConfig;
import com.dfsek.terra.api.handle.WorldHandle;
import com.dfsek.terra.api.profiler.ProfileFrame;
import com.dfsek.terra.api.util.PopulationUtil;
import com.dfsek.terra.api.vector.Location;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.api.world.Chunk;
import com.dfsek.terra.api.world.TerraWorld;
@@ -19,10 +18,8 @@ import com.dfsek.terra.config.templates.CarverTemplate;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
public class CavePopulator implements TerraBlockPopulator, Chunkified {
private static final Map<BlockType, BlockData> shiftStorage = new HashMap<>(); // Persist BlockData created for shifts, to avoid re-calculating each time.
@@ -49,30 +46,30 @@ public class CavePopulator implements TerraBlockPopulator, Chunkified {
Map<Vector3, BlockData> shiftCandidate = new HashMap<>();
c.carve(chunk.getX(), chunk.getZ(), world, (v, type) -> {
try(ProfileFrame ignored = main.getProfiler().profile("carving:" + c.getConfig().getID())) {
BlockData m = chunk.getBlockData(v.getBlockX(), v.getBlockY(), v.getBlockZ());
BlockData m = chunk.getBlock(v.getBlockX(), v.getBlockY(), v.getBlockZ());
BlockType re = m.getBlockType();
switch(type) {
case CENTER:
if(template.getInner().canReplace(re)) {
chunk.setBlockData(v.getBlockX(), v.getBlockY(), v.getBlockZ(), template.getInner().get(v.getBlockY()).get(random), template.getUpdate().contains(re));
chunk.setBlock(v.getBlockX(), v.getBlockY(), v.getBlockZ(), template.getInner().get(v.getBlockY()).get(random), template.getUpdate().contains(re));
if(template.getShift().containsKey(re)) shiftCandidate.put(v, m);
}
break;
case WALL:
if(template.getOuter().canReplace(re)) {
chunk.setBlockData(v.getBlockX(), v.getBlockY(), v.getBlockZ(), template.getOuter().get(v.getBlockY()).get(random), template.getUpdate().contains(re));
chunk.setBlock(v.getBlockX(), v.getBlockY(), v.getBlockZ(), template.getOuter().get(v.getBlockY()).get(random), template.getUpdate().contains(re));
if(template.getShift().containsKey(re)) shiftCandidate.put(v, m);
}
break;
case TOP:
if(template.getTop().canReplace(re)) {
chunk.setBlockData(v.getBlockX(), v.getBlockY(), v.getBlockZ(), template.getTop().get(v.getBlockY()).get(random), template.getUpdate().contains(re));
chunk.setBlock(v.getBlockX(), v.getBlockY(), v.getBlockZ(), template.getTop().get(v.getBlockY()).get(random), template.getUpdate().contains(re));
if(template.getShift().containsKey(re)) shiftCandidate.put(v, m);
}
break;
case BOTTOM:
if(template.getBottom().canReplace(re)) {
chunk.setBlockData(v.getBlockX(), v.getBlockY(), v.getBlockZ(), template.getBottom().get(v.getBlockY()).get(random), template.getUpdate().contains(re));
chunk.setBlock(v.getBlockX(), v.getBlockY(), v.getBlockZ(), template.getBottom().get(v.getBlockY()).get(random), template.getUpdate().contains(re));
if(template.getShift().containsKey(re)) shiftCandidate.put(v, m);
}
break;
@@ -82,12 +79,12 @@ public class CavePopulator implements TerraBlockPopulator, Chunkified {
for(Map.Entry<Vector3, BlockData> entry : shiftCandidate.entrySet()) {
Vector3 l = entry.getKey();
Vector3 mut = l.clone();
BlockData orig = chunk.getBlockData(l.getBlockX(), l.getBlockY(), l.getBlockZ());
BlockData orig = chunk.getBlock(l.getBlockX(), l.getBlockY(), l.getBlockZ());
do mut.subtract(0, 1, 0);
while(mut.getY() > world.getMinHeight() && chunk.getBlockData(mut.getBlockX(), mut.getBlockY(), mut.getBlockZ()).matches(orig));
while(mut.getY() > world.getMinHeight() && chunk.getBlock(mut.getBlockX(), mut.getBlockY(), mut.getBlockZ()).matches(orig));
try {
if(template.getShift().get(entry.getValue().getBlockType()).contains(chunk.getBlockData(mut.getBlockX(), mut.getBlockY(), mut.getBlockZ()).getBlockType())) {
chunk.setBlockData(mut.getBlockX(), mut.getBlockY(), mut.getBlockZ(), shiftStorage.computeIfAbsent(entry.getValue().getBlockType(), BlockType::getDefaultData), false);
if(template.getShift().get(entry.getValue().getBlockType()).contains(chunk.getBlock(mut.getBlockX(), mut.getBlockY(), mut.getBlockZ()).getBlockType())) {
chunk.setBlock(mut.getBlockX(), mut.getBlockY(), mut.getBlockZ(), shiftStorage.computeIfAbsent(entry.getValue().getBlockType(), BlockType::getDefaultData), false);
}
} catch(NullPointerException ignored) {
}

View File

@@ -20,7 +20,9 @@ public class FloraLayer extends PlaceableLayer<Flora> {
@Override
public void place(Chunk chunk, Vector2 coords) {
Flora item = layer.get(noise, (chunk.getX() << 4) + coords.getX(), (chunk.getZ() << 4) + coords.getZ());
item.getValidSpawnsAt(chunk, (int) coords.getX(), (int) coords.getZ(), level).forEach(block -> item.plant(block.toLocation(chunk.getWorld())));
int cx = chunk.getX() << 4;
int cz = chunk.getZ() << 4;
Flora item = layer.get(noise, coords.getX() + cx, coords.getZ() + cz);
item.getValidSpawnsAt(chunk, (int) coords.getX(), (int) coords.getZ(), level).forEach(block -> item.plant(block.toLocation(chunk.getWorld()).add(cx, 0, cz)));
}
}

View File

@@ -67,7 +67,7 @@ public class TerraFlora implements Flora {
for(int y : range) {
if(y > 255 || y < 0) continue;
current = current.add(0, search.equals(Search.UP) ? 1 : -1, 0);
if((spawnBlacklist != spawnable.contains(chunk.getBlockData(current.getBlockX(), current.getBlockY(), current.getBlockZ()).getBlockType())) && isIrrigated(current.add(0, irrigableOffset, 0), chunk) && valid(size, current.clone(), chunk)) {
if((spawnBlacklist != spawnable.contains(chunk.getBlock(current.getBlockX(), current.getBlockY(), current.getBlockZ()).getBlockType())) && isIrrigated(current.add(0, irrigableOffset, 0), chunk) && valid(size, current.clone(), chunk)) {
blocks.add(current.clone());
if(maxPlacements > 0 && blocks.size() >= maxPlacements) break;
}
@@ -79,17 +79,17 @@ public class TerraFlora implements Flora {
for(int i = 0; i < size; i++) { // Down if ceiling, up if floor
if(block.getY() + 1 > 255 || block.getY() < 0) return false;
block.add(0, ceiling ? -1 : 1, 0);
if(!replaceable.contains(chunk.getBlockData(block.getBlockX(), block.getBlockY(), block.getBlockZ()).getBlockType())) return false;
if(!replaceable.contains(chunk.getBlock(block.getBlockX(), block.getBlockY(), block.getBlockZ()).getBlockType())) return false;
}
return true;
}
private boolean isIrrigated(Vector3 b, Chunk chunk) {
if(irrigable == null) return true;
return irrigable.contains(chunk.getBlockData(b.getBlockX()+1, b.getBlockY(), b.getBlockZ()).getBlockType())
|| irrigable.contains(chunk.getBlockData(b.getBlockX()-1, b.getBlockY(), b.getBlockZ()).getBlockType())
|| irrigable.contains(chunk.getBlockData(b.getBlockX(), b.getBlockY(), b.getBlockZ()+1).getBlockType())
|| irrigable.contains(chunk.getBlockData(b.getBlockX(), b.getBlockY(), b.getBlockZ()-1).getBlockType());
return irrigable.contains(chunk.getBlock(b.getBlockX()+1, b.getBlockY(), b.getBlockZ()).getBlockType())
|| irrigable.contains(chunk.getBlock(b.getBlockX()-1, b.getBlockY(), b.getBlockZ()).getBlockType())
|| irrigable.contains(chunk.getBlock(b.getBlockX(), b.getBlockY(), b.getBlockZ()+1).getBlockType())
|| irrigable.contains(chunk.getBlock(b.getBlockX(), b.getBlockY(), b.getBlockZ()-1).getBlockType());
}

View File

@@ -68,9 +68,9 @@ public class VanillaOre extends Ore {
double d15 = (z + 0.5D - (d3 + (d4 - d3) * iFactor)) / (d11 / 2.0D);
if(x > 15 || z > 15 || y > 255 || x < 0 || z < 0 || y < 0) continue;
BlockType type = chunk.getBlockData(x, y, z).getBlockType();
BlockType type = chunk.getBlock(x, y, z).getBlockType();
if((d13 * d13 + d14 * d14 + d15 * d15 < 1.0D) && getReplaceable().contains(type)) {
chunk.setBlockData(x, y, z, getMaterial(type), isApplyGravity());
chunk.setBlock(x, y, z, getMaterial(type), isApplyGravity());
}
}
}

View File

@@ -4,8 +4,9 @@ import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.structure.rotation.Rotation;
import com.dfsek.terra.api.util.ProbabilityCollection;
import com.dfsek.terra.api.util.collections.MaterialSet;
import com.dfsek.terra.api.vector.Location;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.api.world.Tree;
import com.dfsek.terra.api.world.World;
import java.util.Random;
@@ -21,8 +22,8 @@ public class TerraTree implements Tree {
}
@Override
public synchronized boolean plant(Location location, Random random) {
return structure.get(random).generateDirect(location.clone().add(0, yOffset, 0), random, Rotation.fromDegrees(90 * random.nextInt(4)));
public synchronized boolean plant(Vector3 location, World world, Random random) {
return structure.get(random).generateDirect(location.clone().add(0, yOffset, 0), world, random, Rotation.fromDegrees(90 * random.nextInt(4)));
}
@Override

View File

@@ -24,8 +24,8 @@ public class TreeLayer extends PlaceableLayer<Tree> {
Vector3 running = coords.extrude(level.getMax());
for(int ignored : level) {
running.subtract(0,1,0);
if(item.getSpawnable().contains(chunk.getBlockData(running.getBlockX(), running.getBlockY(), running.getBlockZ()).getBlockType())) {
item.plant(running.toLocation(chunk.getWorld()).add(cx, 1, cz), PopulationUtil.getRandom(chunk, coords.hashCode()));
if(item.getSpawnable().contains(chunk.getBlock(running.getBlockX(), running.getBlockY(), running.getBlockZ()).getBlockType())) {
item.plant(running.clone().add(cx, 1, cz), chunk.getWorld(), PopulationUtil.getRandom(chunk, coords.hashCode()));
}
}
}