Multi-version support for 1.13-1.16

This commit is contained in:
dfsek
2021-01-06 10:57:34 -07:00
parent 25ae2b3c9b
commit 512edae9c6
7 changed files with 111 additions and 60 deletions
@@ -55,6 +55,17 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
private WorldHandle handle = new BukkitWorldHandle(this);
private final GenericLoaders genericLoaders = new GenericLoaders(this);
public static final Version BUKKIT_VERSION;
static {
String ver = Bukkit.getServer().getClass().getPackage().getName();
if(ver.contains("1_16")) BUKKIT_VERSION = Version.V1_16;
else if(ver.contains("1_15")) BUKKIT_VERSION = Version.V1_15;
else if(ver.contains("1_14")) BUKKIT_VERSION = Version.V1_14;
else if(ver.contains("1_13")) BUKKIT_VERSION = Version.V1_13;
else BUKKIT_VERSION = Version.UNKNOWN;
}
public void reload() {
Map<World, TerraWorld> newMap = new HashMap<>();
@@ -96,6 +107,11 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
public void onEnable() {
Debug.setLogger(getLogger()); // Set debug logger.
getLogger().info("Running on version " + BUKKIT_VERSION);
if(BUKKIT_VERSION.equals(Version.UNKNOWN)) {
getLogger().warning("Terra is running on an unknown Bukkit version. Proceed with caution.");
}
((BukkitWorldHandle) handle).setTreeTransformer(new Transformer.Builder<String, Tree>()
.addTransform(id -> new BukkitTree(TreeType.valueOf(id), this)) // First try getting directly from enum
.addTransform(new MapTransform<String, Tree>() // Then try map of less stupid names
@@ -196,4 +212,32 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
.registerLoader(EntityType.class, (t, o, l) -> EntityType.valueOf((String) o));
genericLoaders.register(registry);
}
public enum Version {
V1_13(13),
V1_14(14),
V1_15(15),
V1_16(16),
UNKNOWN(Integer.MAX_VALUE); // Assume unknown version is latest.
private final int index;
Version(int index) {
this.index = index;
}
/**
* Gets if this version is above or equal to another.
*
* @param other Other version
* @return Whether this version is equal to or later than other.
*/
public boolean above(Version other) {
return this.index >= other.index;
}
}
}
@@ -14,7 +14,6 @@ import com.dfsek.terra.api.platform.world.Chunk;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.bukkit.BukkitCommandSender;
import org.bukkit.Location;
import org.bukkit.block.data.type.Wall;
import org.bukkit.util.Vector;
/**
@@ -300,32 +299,6 @@ public final class BukkitAdapter {
}
}
public static Wall.Height adapt(com.dfsek.terra.api.platform.block.data.Wall.Height height) {
switch(height) {
case NONE:
return Wall.Height.NONE;
case LOW:
return Wall.Height.LOW;
case TALL:
return Wall.Height.TALL;
default:
throw new IllegalStateException();
}
}
public static com.dfsek.terra.api.platform.block.data.Wall.Height adapt(Wall.Height height) {
switch(height) {
case TALL:
return com.dfsek.terra.api.platform.block.data.Wall.Height.TALL;
case LOW:
return com.dfsek.terra.api.platform.block.data.Wall.Height.LOW;
case NONE:
return com.dfsek.terra.api.platform.block.data.Wall.Height.NONE;
default:
throw new IllegalStateException();
}
}
public static Location adapt(com.dfsek.terra.api.math.vector.Location location) {
return new Location(((BukkitWorld) location.getWorld()).getHandle(), location.getX(), location.getY(), location.getZ());
}
@@ -2,6 +2,7 @@ package com.dfsek.terra.bukkit.world.block.data;
import com.dfsek.terra.api.platform.block.BlockData;
import com.dfsek.terra.api.platform.block.MaterialData;
import com.dfsek.terra.bukkit.TerraBukkitPlugin;
import com.dfsek.terra.bukkit.world.block.BukkitMaterialData;
import org.bukkit.block.data.AnaloguePowerable;
import org.bukkit.block.data.Directional;
@@ -27,7 +28,9 @@ public class BukkitBlockData implements BlockData {
if(bukkitData instanceof Rail) return new BukkitRail((Rail) bukkitData);
if(bukkitData instanceof Stairs) return new BukkitStairs((Stairs) bukkitData);
if(bukkitData instanceof Slab) return new BukkitSlab((Slab) bukkitData);
if(bukkitData instanceof Wall) return new BukkitWall((Wall) bukkitData);
if(TerraBukkitPlugin.BUKKIT_VERSION.above(TerraBukkitPlugin.Version.V1_16) && bukkitData instanceof Wall) { // Wall only exists on 1.16 and up.
return new BukkitWall((Wall) bukkitData);
}
if(bukkitData instanceof RedstoneWire) return new BukkitRedstoneWire((RedstoneWire) bukkitData);
if(bukkitData instanceof AnaloguePowerable) return new BukkitAnaloguePowerable((AnaloguePowerable) bukkitData);
@@ -19,13 +19,39 @@ public class BukkitWall extends BukkitWaterlogged implements Wall {
((org.bukkit.block.data.type.Wall) getHandle()).setUp(up);
}
public static org.bukkit.block.data.type.Wall.Height adapt(com.dfsek.terra.api.platform.block.data.Wall.Height height) {
switch(height) {
case NONE:
return org.bukkit.block.data.type.Wall.Height.NONE;
case LOW:
return org.bukkit.block.data.type.Wall.Height.LOW;
case TALL:
return org.bukkit.block.data.type.Wall.Height.TALL;
default:
throw new IllegalStateException();
}
}
public static com.dfsek.terra.api.platform.block.data.Wall.Height adapt(org.bukkit.block.data.type.Wall.Height height) {
switch(height) {
case TALL:
return com.dfsek.terra.api.platform.block.data.Wall.Height.TALL;
case LOW:
return com.dfsek.terra.api.platform.block.data.Wall.Height.LOW;
case NONE:
return com.dfsek.terra.api.platform.block.data.Wall.Height.NONE;
default:
throw new IllegalStateException();
}
}
@Override
public void setHeight(BlockFace face, Height height) {
((org.bukkit.block.data.type.Wall) getHandle()).setHeight(BukkitAdapter.adapt(face), BukkitAdapter.adapt(height));
((org.bukkit.block.data.type.Wall) getHandle()).setHeight(BukkitAdapter.adapt(face), adapt(height));
}
@Override
public Height getHeight(BlockFace face) {
return BukkitAdapter.adapt(((org.bukkit.block.data.type.Wall) getHandle()).getHeight(BukkitAdapter.adapt(face)));
return adapt(((org.bukkit.block.data.type.Wall) getHandle()).getHeight(BukkitAdapter.adapt(face)));
}
}
@@ -3,7 +3,7 @@ main: "com.dfsek.terra.bukkit.TerraBukkitPlugin"
version: "@VERSION@"
load: "STARTUP"
author: dfsek
api-version: "1.16"
api-version: "1.13"
description: "An insanely powerful free & open-source data-driven world generator."
softdepend: [ "WorldEdit" ]
commands: