mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-03 08:25:31 +00:00
clean up WorldHandle, add generic utility classes
This commit is contained in:
parent
06956a7a2d
commit
bf5be91868
@ -1,22 +1,26 @@
|
||||
package com.dfsek.terra.api.platform.handle;
|
||||
|
||||
import com.dfsek.terra.api.platform.block.Block;
|
||||
import com.dfsek.terra.api.math.vector.Location;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import com.dfsek.terra.api.platform.entity.EntityType;
|
||||
import com.dfsek.terra.api.platform.entity.Player;
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
|
||||
/**
|
||||
* Interface to be implemented for world manipulation.
|
||||
*/
|
||||
public interface WorldHandle {
|
||||
default void setBlockData(Block block, BlockData data, boolean physics) {
|
||||
block.setBlockData(data, physics);
|
||||
}
|
||||
|
||||
default BlockData getBlockData(Block block) {
|
||||
return block.getBlockData();
|
||||
}
|
||||
|
||||
BlockData createBlockData(String data);
|
||||
|
||||
EntityType getEntity(String id);
|
||||
|
||||
/**
|
||||
* Get the locations selected by a player. (Usually via WorldEdit)
|
||||
*
|
||||
* @param player Player to get locations for
|
||||
* @return Pair of locations.
|
||||
*/
|
||||
default Pair<Location, Location> getSelectedLocation(Player player) {
|
||||
throw new UnsupportedOperationException("Cannot get selection on this platform.");
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,52 @@
|
||||
package com.dfsek.terra.api.util.generic.either;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class Either<L, R> {
|
||||
private final L left;
|
||||
private final R right;
|
||||
private final boolean leftPresent;
|
||||
|
||||
private Either(L left, R right, boolean leftPresent) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.leftPresent = leftPresent;
|
||||
}
|
||||
|
||||
public static <L1, R1> Either<L1, R1> left(L1 left) {
|
||||
return new Either<>(left, null, true);
|
||||
}
|
||||
|
||||
public static <L1, R1> Either<L1, R1> right(R1 right) {
|
||||
return new Either<>(null, right, false);
|
||||
}
|
||||
|
||||
public Optional<L> getLeft() {
|
||||
if(leftPresent) return Optional.of(left);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public Optional<R> getRight() {
|
||||
if(!leftPresent) return Optional.of(right);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public Either<L, R> ifLeft(Consumer<L> action) {
|
||||
if(leftPresent) action.accept(left);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Either<L, R> ifRight(Consumer<R> action) {
|
||||
if(!leftPresent) action.accept(right);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean hasLeft() {
|
||||
return leftPresent;
|
||||
}
|
||||
|
||||
public boolean hasRight() {
|
||||
return !leftPresent;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.dfsek.terra.api.util.generic.pair;
|
||||
|
||||
public class ImmutablePair<L, R> {
|
||||
private final L left;
|
||||
private final R right;
|
||||
|
||||
public ImmutablePair(L left, R right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public static <L1, R1> ImmutablePair<L1, R1> of(L1 left, R1 right) {
|
||||
return new ImmutablePair<>(left, right);
|
||||
}
|
||||
|
||||
public R getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public L getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public Pair<L, R> mutable() {
|
||||
return new Pair<>(left, right);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.dfsek.terra.api.util.generic.pair;
|
||||
|
||||
public class Pair<L, R> {
|
||||
private L left;
|
||||
private R right;
|
||||
|
||||
public Pair(L left, R right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public static <L1, R1> Pair<L1, R1> of(L1 left, R1 right) {
|
||||
return new Pair<>(left, right);
|
||||
}
|
||||
|
||||
public L getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(L left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public R getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(R right) {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public ImmutablePair<L, R> immutable() {
|
||||
return new ImmutablePair<>(left, right);
|
||||
}
|
||||
}
|
@ -11,7 +11,6 @@ import com.dfsek.terra.api.platform.world.World;
|
||||
import com.dfsek.terra.api.util.world.PopulationUtil;
|
||||
import com.dfsek.terra.api.world.generation.TerraBlockPopulator;
|
||||
import com.dfsek.terra.carving.UserDefinedCarver;
|
||||
import com.dfsek.terra.config.pack.ConfigPack;
|
||||
import com.dfsek.terra.config.pack.WorldConfig;
|
||||
import com.dfsek.terra.config.templates.CarverTemplate;
|
||||
import com.dfsek.terra.profiler.ProfileFuture;
|
||||
@ -49,7 +48,7 @@ public class CavePopulator implements TerraBlockPopulator {
|
||||
Set<Block> updateNeeded = new HashSet<>();
|
||||
c.carve(chunk.getX(), chunk.getZ(), world, (v, type) -> {
|
||||
Block b = chunk.getBlock(v.getBlockX(), v.getBlockY(), v.getBlockZ());
|
||||
BlockData m = handle.getBlockData(b);
|
||||
BlockData m = b.getBlockData();
|
||||
BlockType re = m.getBlockType();
|
||||
switch(type) {
|
||||
case CENTER:
|
||||
@ -85,18 +84,18 @@ public class CavePopulator implements TerraBlockPopulator {
|
||||
for(Map.Entry<Location, BlockData> entry : shiftCandidate.entrySet()) {
|
||||
Location l = entry.getKey();
|
||||
Location mut = l.clone();
|
||||
BlockData orig = handle.getBlockData(l.getBlock());
|
||||
BlockData orig = l.getBlock().getBlockData();
|
||||
do mut.subtract(0, 1, 0);
|
||||
while(mut.getY() > 0 && handle.getBlockData(mut.getBlock()).matches(orig));
|
||||
while(mut.getY() > 0 && mut.getBlock().getBlockData().matches(orig));
|
||||
try {
|
||||
if(template.getShift().get(entry.getValue().getBlockType()).contains(mut.getBlock().getBlockData().getBlockType())) {
|
||||
handle.setBlockData(mut.getBlock(), shiftStorage.computeIfAbsent(entry.getValue().getBlockType(), BlockType::getDefaultData), false);
|
||||
mut.getBlock().setBlockData(shiftStorage.computeIfAbsent(entry.getValue().getBlockType(), BlockType::getDefaultData), false);
|
||||
}
|
||||
} catch(NullPointerException ignore) {
|
||||
}
|
||||
}
|
||||
for(Block b : updateNeeded) {
|
||||
BlockData orig = handle.getBlockData(b);
|
||||
BlockData orig = b.getBlockData();
|
||||
b.setBlockData(AIR, false);
|
||||
b.setBlockData(orig, true);
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ public class TerraFlora implements Flora {
|
||||
((Rotatable) data).setRotation(oneFace);
|
||||
}
|
||||
}
|
||||
handle.setBlockData(location.clone().add(0, i + c, 0).getBlock(), data, physics);
|
||||
location.clone().add(0, i + c, 0).getBlock().setBlockData(data, physics);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ public class DeformedSphereOre extends Ore {
|
||||
if(oreLoc.distance(origin) < (rad + 0.5) * ((ore.getNoise(x, y, z) + 1) * deform)) {
|
||||
Block b = c.getBlock(oreLoc.getBlockX(), oreLoc.getBlockY(), oreLoc.getBlockZ());
|
||||
if(getReplaceable().contains(b.getType()) && b.getLocation().getY() >= 0)
|
||||
handle.setBlockData(b, getMaterial(), isApplyGravity());
|
||||
b.setBlockData(getMaterial(), isApplyGravity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -68,7 +68,7 @@ public class VanillaOre extends Ore {
|
||||
if(x > 15 || z > 15 || y > 255 || x < 0 || z < 0 || y < 0) continue;
|
||||
Block block = chunk.getBlock(x, y, z);
|
||||
if((d13 * d13 + d14 * d14 + d15 * d15 < 1.0D) && getReplaceable().contains(block.getType())) {
|
||||
handle.setBlockData(block, getMaterial(), isApplyGravity());
|
||||
block.setBlockData(getMaterial(), isApplyGravity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +1,18 @@
|
||||
package com.dfsek.terra.bukkit.handles;
|
||||
|
||||
import com.dfsek.terra.api.platform.block.Block;
|
||||
import com.dfsek.terra.api.math.vector.Location;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import com.dfsek.terra.api.platform.entity.EntityType;
|
||||
import com.dfsek.terra.api.platform.entity.Player;
|
||||
import com.dfsek.terra.api.platform.handle.WorldHandle;
|
||||
import com.dfsek.terra.api.util.generic.pair.Pair;
|
||||
import com.dfsek.terra.bukkit.structure.WorldEditUtil;
|
||||
import com.dfsek.terra.bukkit.world.BukkitAdapter;
|
||||
import com.dfsek.terra.bukkit.world.block.data.BukkitBlockData;
|
||||
import com.dfsek.terra.bukkit.world.entity.BukkitEntityType;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class BukkitWorldHandle implements WorldHandle {
|
||||
@Override
|
||||
public void setBlockData(Block block, BlockData data, boolean physics) {
|
||||
block.setBlockData(data, physics);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockData getBlockData(Block block) {
|
||||
return block.getBlockData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockData createBlockData(String data) {
|
||||
@ -29,4 +24,10 @@ public class BukkitWorldHandle implements WorldHandle {
|
||||
public EntityType getEntity(String id) {
|
||||
return new BukkitEntityType(org.bukkit.entity.EntityType.valueOf(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Location, Location> getSelectedLocation(Player player) {
|
||||
org.bukkit.Location[] locations = WorldEditUtil.getSelectionLocations(BukkitAdapter.adapt(player));
|
||||
return new Pair<>(BukkitAdapter.adapt(locations[0]), BukkitAdapter.adapt(locations[1]));
|
||||
}
|
||||
}
|
||||
|
@ -25,12 +25,10 @@ public final class WorldEditUtil {
|
||||
try {
|
||||
selection = we.getSession(sender).getSelection(BukkitAdapter.adapt(sender.getWorld()));
|
||||
} catch(IncompleteRegionException | ClassCastException e) {
|
||||
sender.sendMessage("Invalid/incomplete selection!");
|
||||
return null;
|
||||
throw new IllegalStateException("Invalid/incomplete selection!");
|
||||
}
|
||||
if(selection == null) {
|
||||
sender.sendMessage("Please make a selection before attempting to export!");
|
||||
return null;
|
||||
throw new IllegalStateException("Please make a selection before attempting to export!");
|
||||
}
|
||||
BlockVector3 min = selection.getMinimumPoint();
|
||||
BlockVector3 max = selection.getMaximumPoint();
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.dfsek.terra.fabric.world;
|
||||
|
||||
import com.dfsek.terra.api.platform.block.Block;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import com.dfsek.terra.api.platform.entity.EntityType;
|
||||
import com.dfsek.terra.api.platform.handle.WorldHandle;
|
||||
import com.dfsek.terra.fabric.world.block.FabricBlockData;
|
||||
@ -15,15 +13,6 @@ import net.minecraft.util.registry.Registry;
|
||||
import java.util.Locale;
|
||||
|
||||
public class FabricWorldHandle implements WorldHandle {
|
||||
@Override
|
||||
public void setBlockData(Block block, BlockData data, boolean physics) {
|
||||
block.setBlockData(data, physics);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockData getBlockData(Block block) {
|
||||
return block.getBlockData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FabricBlockData createBlockData(String data) {
|
||||
|
@ -1,20 +1,10 @@
|
||||
package com.dfsek.terra.platform;
|
||||
|
||||
import com.dfsek.terra.api.platform.block.Block;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import com.dfsek.terra.api.platform.entity.EntityType;
|
||||
import com.dfsek.terra.api.platform.handle.WorldHandle;
|
||||
|
||||
public class RawWorldHandle implements WorldHandle {
|
||||
@Override
|
||||
public void setBlockData(Block block, BlockData data, boolean physics) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockData getBlockData(Block block) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlockData createBlockData(String data) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user