feat: 26.3 and entity snbt

This commit is contained in:
ArikSquad committed 2026-09-28 19:45:31 +03:00
1 parent 47d4307761
commit e5411f732a
44 files changed
+706 -207

No files matched your search

@@ -6,10 +6,12 @@ import org.slf4j.LoggerFactory;
import java.util.List;
import com.dfsek.terra.bukkit.util.VersionUtil;
import com.dfsek.terra.bukkit.util.BukkitNMSAccess;
import com.dfsek.terra.bukkit.util.BukkitUtils;
public interface NMSInitializer {
List<String> SUPPORTED_VERSIONS = List.of("v26.1.2", "v26.2");
List<String> SUPPORTED_VERSIONS = List.of("v26.1.2", "v26.2", "v26.3");
String MINECRAFT_VERSION = VersionUtil.getMinecraftVersionInfo().toString();
String TERRA_PACKAGE = NMSInitializer.class.getPackageName();
@@ -44,10 +46,19 @@ public interface NMSInitializer {
private static PlatformImpl constructPlatform(TerraBukkitPlugin plugin) {
try {
Class<?> platformClass = Class.forName(TERRA_PACKAGE + ".nms.NMSPlatform");
return (PlatformImpl) platformClass
String bindingsVersion = switch(MINECRAFT_VERSION) {
case "v26.3" -> "v26_3";
case "v26.1.2", "v26.2" -> "v26_1";
default -> "v26_1";
};
Class<?> platformClass = Class.forName(TERRA_PACKAGE + ".nms." + bindingsVersion + ".NMSPlatform");
PlatformImpl platform = (PlatformImpl) platformClass
.getConstructor(TerraBukkitPlugin.class)
.newInstance(plugin);
if(platform instanceof BukkitNMSAccess nmsAccess) {
BukkitUtils.setNMSAccess(nmsAccess);
}
return platform;
} catch(ReflectiveOperationException e) {
throw new RuntimeException("Error initializing NMS bindings. Report this to Terra.", e);
}
@@ -0,0 +1,16 @@
package com.dfsek.terra.bukkit.util;
import org.bukkit.Location;
import org.bukkit.block.CreatureSpawner;
import com.dfsek.terra.api.entity.EntityType;
import com.dfsek.terra.api.entity.EntityTypeExtended;
public interface BukkitNMSAccess {
EntityType getEntityType(String data);
org.bukkit.entity.Entity spawnEntity(Object target, Location location, EntityType type);
void setSpawnerEntity(CreatureSpawner spawner, EntityTypeExtended type);
}
@@ -1,40 +1,62 @@
package com.dfsek.terra.bukkit.util;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.data.BlockData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Locale;
import com.dfsek.terra.api.entity.EntityType;
import com.dfsek.terra.api.entity.EntityTypeExtended;
import com.dfsek.terra.bukkit.world.entity.BukkitEntityType;
public class BukkitUtils {
private static final Logger logger = LoggerFactory.getLogger(BukkitUtils.class);
private static volatile BukkitNMSAccess nmsAccess;
public static boolean isLiquid(BlockData blockState) {
Material material = blockState.getMaterial();
return material == Material.WATER || material == Material.LAVA;
}
public static EntityType getEntityType(String id) {
if(!id.startsWith("minecraft:")) throw new IllegalArgumentException("Invalid entity identifier " + id);
String entityID = id.toUpperCase(Locale.ROOT).substring(10);
public static void setNMSAccess(BukkitNMSAccess access) {
nmsAccess = access;
}
// TODO: remove this
// the default end seems to use this, this should be removed otherwise
// or replaced by a proper entity & NBT loader
if (entityID.contains("END_CRYSTAL")) {
entityID = "END_CRYSTAL";
public static EntityType getEntityType(String id) {
BukkitNMSAccess access = nmsAccess;
return access == null ? getBukkitEntityType(id) : access.getEntityType(id);
}
public static org.bukkit.entity.Entity spawnEntity(Object region, org.bukkit.Location location, EntityType entityType) {
if(entityType instanceof EntityTypeExtended) {
BukkitNMSAccess access = nmsAccess;
if(access == null) throw new IllegalStateException("Entity NBT loading requires Terra's NMS bindings");
return access.spawnEntity(region, location, entityType);
}
return new BukkitEntityType(switch(entityID) {
case "END_CRYSTAL" -> org.bukkit.entity.EntityType.END_CRYSTAL;
case "ENDER_CRYSTAL" -> throw new IllegalArgumentException(
"Invalid entity identifier " + id); // make sure this issue can't happen the other way around.
default -> org.bukkit.entity.EntityType.valueOf(entityID);
});
org.bukkit.entity.EntityType bukkitType = ((BukkitEntityType) entityType).getHandle();
if(region instanceof org.bukkit.generator.LimitedRegion limitedRegion) {
return limitedRegion.spawnEntity(location, bukkitType);
}
return ((org.bukkit.World) region).spawnEntity(location, bukkitType);
}
public static void setSpawnerEntity(CreatureSpawner spawner, EntityTypeExtended type) {
BukkitNMSAccess access = nmsAccess;
if(access == null) throw new IllegalStateException("Entity NBT loading requires Terra's NMS bindings");
access.setSpawnerEntity(spawner, type);
}
private static EntityType getBukkitEntityType(String data) {
int nbtStart = data.indexOf('{');
if(nbtStart >= 0) {
throw new IllegalArgumentException("Entity NBT loading requires Terra's NMS bindings: " + data);
}
NamespacedKey key = NamespacedKey.fromString(data);
org.bukkit.entity.EntityType type = key == null ? null : Registry.ENTITY_TYPE.get(key);
if(type == null) throw new IllegalArgumentException("Invalid entity identifier " + data);
return new BukkitEntityType(type);
}
}
@@ -24,7 +24,6 @@ import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
import com.dfsek.terra.bukkit.util.BukkitUtils;
import com.dfsek.terra.bukkit.world.block.data.BukkitBlockState;
import com.dfsek.terra.bukkit.world.block.state.BukkitBlockEntity;
import com.dfsek.terra.bukkit.world.entity.BukkitEntityType;
public class BukkitProtoWorld implements ProtoWorld {
@@ -89,7 +88,7 @@ public class BukkitProtoWorld implements ProtoWorld {
@Override
public Entity spawnEntity(double x, double y, double z, EntityType entityType) {
return access((int) x, (int) y, (int) z, () -> new BukkitEntity(
delegate.spawnEntity(new Location(delegate.getWorld(), x, y, z), ((BukkitEntityType) entityType).getHandle()))).orElse(
BukkitUtils.spawnEntity(delegate, new Location(delegate.getWorld(), x, y, z), entityType))).orElse(
null);
}
@@ -30,8 +30,8 @@ import com.dfsek.terra.api.world.chunk.Chunk;
import com.dfsek.terra.api.world.chunk.generation.ChunkGenerator;
import com.dfsek.terra.bukkit.BukkitEntity;
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
import com.dfsek.terra.bukkit.util.BukkitUtils;
import com.dfsek.terra.bukkit.world.block.state.BukkitBlockEntity;
import com.dfsek.terra.bukkit.world.entity.BukkitEntityType;
public class BukkitServerWorld implements ServerWorld {
@@ -43,8 +43,7 @@ public class BukkitServerWorld implements ServerWorld {
@Override
public Entity spawnEntity(double x, double y, double z, EntityType entityType) {
return new BukkitEntity(
delegate.spawnEntity(new Location(delegate, x, y, z), ((BukkitEntityType) entityType).getHandle()));
return new BukkitEntity(BukkitUtils.spawnEntity(delegate, new Location(delegate, x, y, z), entityType));
}
@Override
@@ -23,6 +23,7 @@ import org.jetbrains.annotations.NotNull;
import com.dfsek.terra.api.block.entity.MobSpawner;
import com.dfsek.terra.api.block.entity.SerialState;
import com.dfsek.terra.api.entity.EntityType;
import com.dfsek.terra.api.entity.EntityTypeExtended;
import com.dfsek.terra.bukkit.util.BukkitUtils;
import com.dfsek.terra.bukkit.world.entity.BukkitEntityType;
@@ -39,7 +40,12 @@ public class BukkitMobSpawner extends BukkitBlockEntity implements MobSpawner {
@Override
public void setSpawnedType(@NotNull EntityType creatureType) {
((CreatureSpawner) getHandle()).setSpawnedType(((BukkitEntityType) creatureType).getHandle());
CreatureSpawner spawner = (CreatureSpawner) getHandle();
if(creatureType instanceof EntityTypeExtended extended) {
BukkitUtils.setSpawnerEntity(spawner, extended);
} else {
spawner.setSpawnedType(((BukkitEntityType) creatureType).getHandle());
}
}
@Override
@@ -0,0 +1,16 @@
package com.dfsek.terra.bukkit.world.entity;
import com.dfsek.terra.api.data.ExtendedData;
public record BukkitEntityData(String snbt) implements ExtendedData {
@Override
public Object getHandle() {
return snbt;
}
@Override
public String toString() {
return snbt;
}
}
@@ -0,0 +1,34 @@
package com.dfsek.terra.bukkit.world.entity;
import com.dfsek.terra.api.data.ExtendedData;
import com.dfsek.terra.api.entity.EntityType;
import com.dfsek.terra.api.entity.EntityTypeExtended;
public final class BukkitEntityTypeExtended extends BukkitEntityType implements EntityTypeExtended {
private final BukkitEntityData data;
public BukkitEntityTypeExtended(org.bukkit.entity.EntityType delegate, String snbt) {
super(delegate);
this.data = new BukkitEntityData(snbt);
}
@Override
public ExtendedData getData() {
return data;
}
@Override
public EntityTypeExtended setData(ExtendedData data) {
Object handle = data.getHandle();
if(!(handle instanceof String snbt)) {
throw new IllegalArgumentException("Bukkit entity data must contain SNBT text");
}
return new BukkitEntityTypeExtended(getHandle(), snbt);
}
@Override
public EntityType getType() {
return new BukkitEntityType(getHandle());
}
}