SPIGOT-7195, SPIGOT-7197: Add DataPack API

By: Doc <nachito94@msn.com>
This commit is contained in:
CraftBukkit/Spigot
2023-05-30 19:05:48 +10:00
parent d8a3b1e7f0
commit a345ec2e35
9 changed files with 334 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
package org.bukkit.craftbukkit.packs;
import java.util.Set;
import java.util.stream.Collectors;
import net.minecraft.server.packs.repository.PackSource;
import net.minecraft.server.packs.repository.ResourcePackLoader;
import org.bukkit.Bukkit;
import org.bukkit.FeatureFlag;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.CraftFeatureFlag;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftChatMessage;
import org.bukkit.packs.DataPack;
public class CraftDataPack implements DataPack {
private final ResourcePackLoader handle;
public CraftDataPack(ResourcePackLoader handler) {
this.handle = handler;
}
public ResourcePackLoader getHandle() {
return this.handle;
}
public String getRawId() {
return getHandle().getId();
}
@Override
public String getTitle() {
return CraftChatMessage.fromComponent(this.getHandle().getTitle());
}
@Override
public String getDescription() {
return CraftChatMessage.fromComponent(this.getHandle().getDescription());
}
@Override
public int getPackFormat() {
ResourcePackLoader.a info = ResourcePackLoader.readPackInfo(this.getRawId(), this.getHandle().resources);
return (info == null) ? 0 : info.format();
}
@Override
public boolean isRequired() {
return getHandle().isRequired();
}
@Override
public Compatibility getCompatibility() {
return switch (this.getHandle().getCompatibility()) {
case COMPATIBLE -> Compatibility.COMPATIBLE;
case TOO_NEW -> Compatibility.NEW;
case TOO_OLD -> Compatibility.OLD;
};
}
@Override
public boolean isEnabled() {
return ((CraftServer) Bukkit.getServer()).getServer().getPackRepository().getSelectedIds().contains(getRawId());
}
@Override
public DataPack.Source getSource() {
if (this.getHandle().getPackSource() == PackSource.BUILT_IN) {
return Source.BUILT_IN;
} else if (this.getHandle().getPackSource() == PackSource.FEATURE) {
return Source.FEATURE;
} else if (this.getHandle().getPackSource() == PackSource.WORLD) {
return Source.WORLD;
} else if (this.getHandle().getPackSource() == PackSource.SERVER) {
return Source.SERVER;
}
return Source.DEFAULT;
}
@Override
public Set<FeatureFlag> getRequestedFeatures() {
return CraftFeatureFlag.getFromNMS(this.getHandle().getRequestedFeatures()).stream().map(FeatureFlag.class::cast).collect(Collectors.toUnmodifiableSet());
}
@Override
public NamespacedKey getKey() {
return NamespacedKey.fromString(getRawId());
}
@Override
public String toString() {
String requestedFeatures = getRequestedFeatures().stream().map(featureFlag -> featureFlag.getKey().toString()).collect(Collectors.joining(","));
return "CraftDataPack{rawId=" + this.getRawId() + ",id=" + this.getKey() + ",title=" + this.getTitle() + ",description=" + this.getDescription() + ",packformat=" + this.getPackFormat() + ",compatibility=" + this.getCompatibility() + ",source=" + this.getSource() + ",enabled=" + this.isEnabled() + ",requestedFeatures=[" + requestedFeatures + "]}";
}
}

View File

@@ -0,0 +1,102 @@
package org.bukkit.craftbukkit.packs;
import com.google.common.base.Preconditions;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.MinecraftKey;
import net.minecraft.server.packs.repository.ResourcePackLoader;
import net.minecraft.server.packs.repository.ResourcePackRepository;
import net.minecraft.world.entity.EntityTypes;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.entity.EntityType;
import org.bukkit.packs.DataPack;
import org.bukkit.packs.DataPackManager;
public class CraftDataPackManager implements DataPackManager {
private final ResourcePackRepository handle;
public CraftDataPackManager(ResourcePackRepository resourcePackRepository) {
this.handle = resourcePackRepository;
}
public ResourcePackRepository getHandle() {
return this.handle;
}
@Override
public Collection<DataPack> getDataPacks() {
// Based in the command for datapacks need reload for get the updated list of datapacks
this.getHandle().reload();
Collection<ResourcePackLoader> availablePacks = this.getHandle().getAvailablePacks();
return availablePacks.stream().map(CraftDataPack::new).collect(Collectors.toUnmodifiableList());
}
@Override
public DataPack getDataPack(NamespacedKey namespacedKey) {
Preconditions.checkArgument(namespacedKey != null, "namespacedKey cannot be null");
return new CraftDataPack(this.getHandle().getPack(namespacedKey.getKey()));
}
@Override
public Collection<DataPack> getEnabledDataPacks(World world) {
Preconditions.checkArgument(world != null, "world cannot be null");
CraftWorld craftWorld = ((CraftWorld) world);
return craftWorld.getHandle().serverLevelData.getDataConfiguration().dataPacks().getEnabled().stream().map(packName -> {
ResourcePackLoader resourcePackLoader = this.getHandle().getPack(packName);
if (resourcePackLoader != null) {
return new CraftDataPack(resourcePackLoader);
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toUnmodifiableList());
}
@Override
public Collection<DataPack> getDisabledDataPacks(World world) {
Preconditions.checkArgument(world != null, "world cannot be null");
CraftWorld craftWorld = ((CraftWorld) world);
return craftWorld.getHandle().serverLevelData.getDataConfiguration().dataPacks().getDisabled().stream().map(packName -> {
ResourcePackLoader resourcePackLoader = this.getHandle().getPack(packName);
if (resourcePackLoader != null) {
return new CraftDataPack(resourcePackLoader);
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toUnmodifiableList());
}
@Override
public boolean isEnabledByFeature(Material material, World world) {
Preconditions.checkArgument(material != null, "material cannot be null");
Preconditions.checkArgument(material.isItem() || material.isBlock(), "material need to be a item or block");
Preconditions.checkArgument(world != null, "world cannot be null");
CraftWorld craftWorld = ((CraftWorld) world);
if (material.isItem()) {
return CraftMagicNumbers.getItem(material).isEnabled(craftWorld.getHandle().enabledFeatures());
} else if (material.isBlock()) {
return CraftMagicNumbers.getBlock(material).isEnabled(craftWorld.getHandle().enabledFeatures());
}
return false;
}
@Override
public boolean isEnabledByFeature(EntityType entityType, World world) {
Preconditions.checkArgument(entityType != null, "entityType cannot be null");
Preconditions.checkArgument(world != null, "world cannot be null");
Preconditions.checkArgument(entityType != EntityType.UNKNOWN, "EntityType.UNKNOWN its not allowed here");
CraftWorld craftWorld = ((CraftWorld) world);
EntityTypes<?> nmsEntity = BuiltInRegistries.ENTITY_TYPE.get(new MinecraftKey(entityType.getKey().getKey()));
return nmsEntity.isEnabled(craftWorld.getHandle().enabledFeatures());
}
}