#1060: Cache Material to BlockType and ItemType conversion

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
Bukkit/Spigot 2024-09-24 21:07:47 +10:00
parent f73b5cbdb3
commit 3ea55de712

View file

@ -1,12 +1,14 @@
package org.bukkit;
import com.google.common.base.Preconditions;
import com.google.common.base.Suppliers;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import java.lang.reflect.Constructor;
import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.block.Block;
@ -4620,6 +4622,8 @@ public enum Material implements Keyed, Translatable {
public final Class<?> data;
private final boolean legacy;
private final NamespacedKey key;
private final Supplier<ItemType> itemType;
private final Supplier<BlockType> blockType;
private Material(final int id) {
this(id, 64);
@ -4660,6 +4664,21 @@ public enum Material implements Keyed, Translatable {
} catch (SecurityException ex) {
throw new AssertionError(ex);
}
this.itemType = Suppliers.memoize(() -> {
Material material = this;
if (isLegacy()) {
material = Bukkit.getUnsafe().fromLegacy(new MaterialData(this), true);
}
return Registry.ITEM.get(material.key);
});
this.blockType = Suppliers.memoize(() -> {
Material material = this;
if (isLegacy()) {
material = Bukkit.getUnsafe().fromLegacy(new MaterialData(this), false);
}
return Registry.BLOCK.get(material.key);
});
}
/**
@ -5496,11 +5515,7 @@ public enum Material implements Keyed, Translatable {
@ApiStatus.Internal
@Nullable
public ItemType asItemType() {
Material material = this;
if (isLegacy()) {
material = Bukkit.getUnsafe().fromLegacy(this);
}
return Registry.ITEM.get(material.key);
return itemType.get();
}
/**
@ -5512,10 +5527,6 @@ public enum Material implements Keyed, Translatable {
@ApiStatus.Internal
@Nullable
public BlockType asBlockType() {
Material material = this;
if (isLegacy()) {
material = Bukkit.getUnsafe().fromLegacy(this);
}
return Registry.BLOCK.get(material.key);
return blockType.get();
}
}