Commits
DerFrZocker authored and md_5 committed 3fab4384ef2
1 1 | package org.bukkit; |
2 2 | |
3 3 | import com.google.common.base.Preconditions; |
4 + | import com.google.common.base.Suppliers; |
4 5 | import com.google.common.collect.Maps; |
5 6 | import com.google.common.collect.Multimap; |
6 7 | import java.lang.reflect.Constructor; |
7 8 | import java.util.Locale; |
8 9 | import java.util.Map; |
9 10 | import java.util.function.Consumer; |
11 + | import java.util.function.Supplier; |
10 12 | import org.bukkit.attribute.Attribute; |
11 13 | import org.bukkit.attribute.AttributeModifier; |
12 14 | import org.bukkit.block.Block; |
13 15 | import org.bukkit.block.BlockType; |
14 16 | import org.bukkit.block.data.Ageable; |
15 17 | import org.bukkit.block.data.AnaloguePowerable; |
16 18 | import org.bukkit.block.data.Bisected; |
17 19 | import org.bukkit.block.data.BlockData; |
18 20 | import org.bukkit.block.data.Brushable; |
19 21 | import org.bukkit.block.data.Directional; |
4613 4615 | public static final String LEGACY_PREFIX = "LEGACY_"; |
4614 4616 | |
4615 4617 | private final int id; |
4616 4618 | private final Constructor<? extends MaterialData> ctor; |
4617 4619 | private static final Map<String, Material> BY_NAME = Maps.newHashMap(); |
4618 4620 | private final int maxStack; |
4619 4621 | private final short durability; |
4620 4622 | public final Class<?> data; |
4621 4623 | private final boolean legacy; |
4622 4624 | private final NamespacedKey key; |
4625 + | private final Supplier<ItemType> itemType; |
4626 + | private final Supplier<BlockType> blockType; |
4623 4627 | |
4624 4628 | private Material(final int id) { |
4625 4629 | this(id, 64); |
4626 4630 | } |
4627 4631 | |
4628 4632 | private Material(final int id, final int stack) { |
4629 4633 | this(id, stack, MaterialData.class); |
4630 4634 | } |
4631 4635 | |
4632 4636 | private Material(final int id, final int stack, final int durability) { |
4653 4657 | if (MaterialData.class.isAssignableFrom(data)) { |
4654 4658 | this.ctor = (Constructor<? extends MaterialData>) data.getConstructor(Material.class, byte.class); |
4655 4659 | } else { |
4656 4660 | this.ctor = null; |
4657 4661 | } |
4658 4662 | } catch (NoSuchMethodException ex) { |
4659 4663 | throw new AssertionError(ex); |
4660 4664 | } catch (SecurityException ex) { |
4661 4665 | throw new AssertionError(ex); |
4662 4666 | } |
4667 + | |
4668 + | this.itemType = Suppliers.memoize(() -> { |
4669 + | Material material = this; |
4670 + | if (isLegacy()) { |
4671 + | material = Bukkit.getUnsafe().fromLegacy(new MaterialData(this), true); |
4672 + | } |
4673 + | return Registry.ITEM.get(material.key); |
4674 + | }); |
4675 + | this.blockType = Suppliers.memoize(() -> { |
4676 + | Material material = this; |
4677 + | if (isLegacy()) { |
4678 + | material = Bukkit.getUnsafe().fromLegacy(new MaterialData(this), false); |
4679 + | } |
4680 + | return Registry.BLOCK.get(material.key); |
4681 + | }); |
4663 4682 | } |
4664 4683 | |
4665 4684 | /** |
4666 4685 | * Do not use for any reason. |
4667 4686 | * |
4668 4687 | * @return ID of this material |
4669 4688 | * @deprecated Magic value |
4670 4689 | */ |
4671 4690 | |
4672 4691 | public int getId() { |
5489 5508 | |
5490 5509 | /** |
5491 5510 | * Tries to convert this Material to an item type |
5492 5511 | * |
5493 5512 | * @return the converted item type or null |
5494 5513 | * @apiNote only for internal use |
5495 5514 | */ |
5496 5515 | Internal | .
5497 5516 | |
5498 5517 | public ItemType asItemType() { |
5499 - | Material material = this; |
5500 - | if (isLegacy()) { |
5501 - | material = Bukkit.getUnsafe().fromLegacy(this); |
5502 - | } |
5503 - | return Registry.ITEM.get(material.key); |
5518 + | return itemType.get(); |
5504 5519 | } |
5505 5520 | |
5506 5521 | /** |
5507 5522 | * Tries to convert this Material to a block type |
5508 5523 | * |
5509 5524 | * @return the converted block type or null |
5510 5525 | * @apiNote only for internal use |
5511 5526 | */ |
5512 5527 | Internal | .
5513 5528 | |
5514 5529 | public BlockType asBlockType() { |
5515 - | Material material = this; |
5516 - | if (isLegacy()) { |
5517 - | material = Bukkit.getUnsafe().fromLegacy(this); |
5518 - | } |
5519 - | return Registry.BLOCK.get(material.key); |
5530 + | return blockType.get(); |
5520 5531 | } |
5521 5532 | } |