Skip to content

Commit

Permalink
Use registry lookups for Bukkit adapters (#2330)
Browse files Browse the repository at this point in the history
* Use registry lookups for Bukkit adapters

* Remove a redundant todo now that this targets master
  • Loading branch information
me4502 authored Jun 18, 2023
1 parent faf4d7c commit 5b88c31
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.block.Biome;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
Expand Down Expand Up @@ -332,10 +334,7 @@ public static Entity adapt(org.bukkit.entity.Entity entity) {
*/
public static Material adapt(ItemType itemType) {
checkNotNull(itemType);
if (!itemType.getId().startsWith("minecraft:")) {
throw new IllegalArgumentException("Bukkit only supports Minecraft items");
}
return Material.getMaterial(itemType.getId().substring(10).toUpperCase(Locale.ROOT));
return Material.matchMaterial(itemType.getId());
}

/**
Expand All @@ -346,10 +345,7 @@ public static Material adapt(ItemType itemType) {
*/
public static Material adapt(BlockType blockType) {
checkNotNull(blockType);
if (!blockType.getId().startsWith("minecraft:")) {
throw new IllegalArgumentException("Bukkit only supports Minecraft blocks");
}
return Material.getMaterial(blockType.getId().substring(10).toUpperCase(Locale.ROOT));
return Material.matchMaterial(blockType.getId());
}

/**
Expand All @@ -375,15 +371,16 @@ public static GameMode adapt(org.bukkit.GameMode gameMode) {
* @return WorldEdit BiomeType
*/
public static BiomeType adapt(Biome biome) {
return biomeBiomeTypeCache.computeIfAbsent(biome, b -> BiomeTypes.get(b.name().toLowerCase(Locale.ROOT)));
return biomeBiomeTypeCache.computeIfAbsent(biome, b -> BiomeTypes.get(b.getKey().toString()));
}

public static Biome adapt(BiomeType biomeType) {
if (!biomeType.getId().startsWith("minecraft:")) {
throw new IllegalArgumentException("Bukkit only supports vanilla biomes");
NamespacedKey biomeKey = NamespacedKey.fromString(biomeType.getId());
if (biomeKey == null) {
throw new IllegalArgumentException("Biome key '" + biomeType.getId() + "' does not map to Bukkit");
}
try {
return biomeTypeBiomeCache.computeIfAbsent(biomeType, type -> Biome.valueOf(type.getId().substring(10).toUpperCase(Locale.ROOT)));
return biomeTypeBiomeCache.computeIfAbsent(biomeType, type -> Registry.BIOME.get(biomeKey));
} catch (IllegalArgumentException e) {
return null;
}
Expand All @@ -396,20 +393,16 @@ public static Biome adapt(BiomeType biomeType) {
* @return WorldEdit EntityType
*/
public static EntityType adapt(org.bukkit.entity.EntityType entityType) {
@SuppressWarnings("deprecation")
final String name = entityType.getName();
if (name == null) {
return null;
}
return EntityTypes.get(name.toLowerCase(Locale.ROOT));
return EntityTypes.get(entityType.getKey().toString());
}

@SuppressWarnings("deprecation")
public static org.bukkit.entity.EntityType adapt(EntityType entityType) {
if (!entityType.getId().startsWith("minecraft:")) {
throw new IllegalArgumentException("Bukkit only supports vanilla entities");
NamespacedKey entityKey = NamespacedKey.fromString(entityType.getId());
if (entityKey == null) {
throw new IllegalArgumentException("Entity key '" + entityType.getId() + "' does not map to Bukkit");
}
return org.bukkit.entity.EntityType.fromName(entityType.getId().substring(10));

return Registry.ENTITY_TYPE.get(entityKey);
}

private static final Map<Material, BlockType> materialBlockTypeCache = Collections.synchronizedMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
import com.sk89q.worldedit.world.DataFixer;
import com.sk89q.worldedit.world.registry.Registries;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.enginehub.piston.CommandManager;

import java.util.ArrayList;
Expand Down Expand Up @@ -104,12 +105,11 @@ public DataFixer getDataFixer() {

@Override
public boolean isValidMobType(String type) {
if (!type.startsWith("minecraft:")) {
NamespacedKey entityKey = NamespacedKey.fromString(type);
if (entityKey == null) {
return false;
}
@SuppressWarnings("deprecation")
final EntityType entityType = EntityType.fromName(type.substring(10));
return entityType != null && entityType.isAlive();
return Registry.ENTITY_TYPE.get(entityKey) != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
Expand Down Expand Up @@ -206,18 +205,18 @@ private void setupWorldData() {
WorldEdit.getInstance().getEventBus().post(new PlatformReadyEvent(platform));
}

@SuppressWarnings({ "deprecation", "unchecked" })
@SuppressWarnings({ "unchecked" })
private void initializeRegistries() {
// Biome
for (Biome biome : Biome.values()) {
if (!biome.name().equals("CUSTOM")) {
String lowerCaseBiomeName = biome.name().toLowerCase(Locale.ROOT);
BiomeType.REGISTRY.register("minecraft:" + lowerCaseBiomeName, new BiomeType("minecraft:" + lowerCaseBiomeName));
String key = biome.getKey().toString();
BiomeType.REGISTRY.register(key, new BiomeType(key));
}
}
// Block & Item
for (Material material : Material.values()) {
if (material.isBlock() && !material.isLegacy()) {
if (material.isBlock()) {
BlockType.REGISTRY.register(material.getKey().toString(), new BlockType(material.getKey().toString(), blockState -> {
// TODO Use something way less hacky than this.
ParserContext context = new ParserContext();
Expand All @@ -240,17 +239,18 @@ private void initializeRegistries() {
}
}));
}
if (material.isItem() && !material.isLegacy()) {
if (material.isItem()) {
ItemType.REGISTRY.register(material.getKey().toString(), new ItemType(material.getKey().toString()));
}
}
// Entity
for (org.bukkit.entity.EntityType entityType : org.bukkit.entity.EntityType.values()) {
String mcid = entityType.getName();
if (mcid != null) {
String lowerCaseMcId = mcid.toLowerCase(Locale.ROOT);
EntityType.REGISTRY.register("minecraft:" + lowerCaseMcId, new EntityType("minecraft:" + lowerCaseMcId));
if (entityType == org.bukkit.entity.EntityType.UNKNOWN) {
// This doesn't have a key - skip it
continue;
}
String key = entityType.getKey().toString();
EntityType.REGISTRY.register(key, new EntityType(key));
}
// ... :|
GameModes.get("");
Expand All @@ -264,15 +264,11 @@ private void initializeRegistries() {

private void setupTags() {
// Tags
try {
for (Tag<Material> blockTag : Bukkit.getTags(Tag.REGISTRY_BLOCKS, Material.class)) {
BlockCategory.REGISTRY.register(blockTag.getKey().toString(), new BlockCategory(blockTag.getKey().toString()));
}
for (Tag<Material> itemTag : Bukkit.getTags(Tag.REGISTRY_ITEMS, Material.class)) {
ItemCategory.REGISTRY.register(itemTag.getKey().toString(), new ItemCategory(itemTag.getKey().toString()));
}
} catch (NoSuchMethodError ignored) {
getLogger().warning("The version of Spigot/Paper you are using doesn't support Tags. The usage of tags with WorldEdit will not work until you update.");
for (Tag<Material> blockTag : Bukkit.getTags(Tag.REGISTRY_BLOCKS, Material.class)) {
BlockCategory.REGISTRY.register(blockTag.getKey().toString(), new BlockCategory(blockTag.getKey().toString()));
}
for (Tag<Material> itemTag : Bukkit.getTags(Tag.REGISTRY_ITEMS, Material.class)) {
ItemCategory.REGISTRY.register(itemTag.getKey().toString(), new ItemCategory(itemTag.getKey().toString()));
}
}

Expand Down

0 comments on commit 5b88c31

Please sign in to comment.