Remove legacy registry methods

This commit is contained in:
TheMode 2021-07-27 09:55:01 +02:00
parent d685a7b136
commit c549ec1663
9 changed files with 14 additions and 81 deletions

View File

@ -4,9 +4,7 @@ package net.minestom.server.registry;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.minestom.server.entity.EntityType; import net.minestom.server.entity.EntityType;
import net.minestom.server.fluid.Fluid; import net.minestom.server.fluid.Fluid;
import net.minestom.server.instance.block.Block;
import net.minestom.server.item.Enchantment; import net.minestom.server.item.Enchantment;
import net.minestom.server.item.Material;
import net.minestom.server.particle.Particle; import net.minestom.server.particle.Particle;
import net.minestom.server.potion.PotionEffect; import net.minestom.server.potion.PotionEffect;
import net.minestom.server.potion.PotionType; import net.minestom.server.potion.PotionType;
@ -17,23 +15,11 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Objects;
/** /**
* AUTOGENERATED * AUTOGENERATED
*/ */
public final class Registries { public final class Registries {
/**
* Should only be used for internal code, please use the get* methods.
*/
@Deprecated
public static final HashMap<NamespaceID, Block> blocks = new HashMap<>();
/**
* Should only be used for internal code, please use the get* methods.
*/
@Deprecated
public static final HashMap<NamespaceID, Material> materials = new HashMap<>();
/** /**
* Should only be used for internal code, please use the get* methods. * Should only be used for internal code, please use the get* methods.
@ -83,54 +69,6 @@ public final class Registries {
@Deprecated @Deprecated
public static final HashMap<NamespaceID, Fluid> fluids = new HashMap<>(); public static final HashMap<NamespaceID, Fluid> fluids = new HashMap<>();
/**
* Returns the corresponding Block matching the given id. Returns 'AIR' if none match.
*/
@Deprecated
public static @NotNull Block getBlock(String id) {
return Objects.requireNonNullElse(Block.fromNamespaceId(id), Block.AIR);
}
/**
* Returns the corresponding Block matching the given id. Returns 'AIR' if none match.
*/
@Deprecated
public static @NotNull Block getBlock(NamespaceID id) {
return Objects.requireNonNullElse(Block.fromNamespaceId(id), Block.AIR);
}
/**
* Returns the corresponding Block matching the given key. Returns 'AIR' if none match.
*/
@NotNull
public static Block getBlock(Key key) {
return getBlock(NamespaceID.from(key));
}
/**
* Returns the corresponding Material matching the given id. Returns 'AIR' if none match.
*/
@NotNull
public static Material getMaterial(String id) {
return getMaterial(NamespaceID.from(id));
}
/**
* Returns the corresponding Material matching the given id. Returns 'AIR' if none match.
*/
@NotNull
public static Material getMaterial(NamespaceID id) {
return materials.getOrDefault(id, Material.AIR);
}
/**
* Returns the corresponding Material matching the given key. Returns 'AIR' if none match.
*/
@NotNull
public static Material getMaterial(Key key) {
return getMaterial(NamespaceID.from(key));
}
/** /**
* Returns the corresponding Enchantment matching the given id. Returns null if none match. * Returns the corresponding Enchantment matching the given id. Returns null if none match.
*/ */

View File

@ -6,7 +6,6 @@ import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material; import net.minestom.server.item.Material;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import net.minestom.server.registry.Registries;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTException; import org.jglrxavpok.hephaistos.nbt.NBTException;
@ -56,12 +55,12 @@ public class ArgumentItemStack extends Argument<ItemStack> {
if (nbtIndex == -1) { if (nbtIndex == -1) {
// Only material name // Only material name
final Material material = Registries.getMaterial(input); final Material material = Material.fromNamespaceId(input);
return ItemStack.of(material); return ItemStack.of(material);
} else { } else {
// Material plus additional NBT // Material plus additional NBT
final String materialName = input.substring(0, nbtIndex); final String materialName = input.substring(0, nbtIndex);
final Material material = Registries.getMaterial(materialName); final Material material = Material.fromNamespaceId(materialName);
final String sNBT = input.substring(nbtIndex).replace("\\\"", "\""); final String sNBT = input.substring(nbtIndex).replace("\\\"", "\"");

View File

@ -5,7 +5,7 @@ import net.minestom.server.gamedata.loottables.LootTable;
import net.minestom.server.gamedata.loottables.LootTableEntryType; import net.minestom.server.gamedata.loottables.LootTableEntryType;
import net.minestom.server.gamedata.loottables.LootTableFunction; import net.minestom.server.gamedata.loottables.LootTableFunction;
import net.minestom.server.gamedata.loottables.LootTableManager; import net.minestom.server.gamedata.loottables.LootTableManager;
import net.minestom.server.registry.Registries; import net.minestom.server.item.Material;
import net.minestom.server.utils.NamespaceID; import net.minestom.server.utils.NamespaceID;
import java.util.List; import java.util.List;
@ -17,6 +17,6 @@ public class ItemType implements LootTableEntryType {
@Override @Override
public LootTable.Entry create(LootTableManager lootTableManager, String name, List<Condition> conditions, List<LootTable.Entry> children, boolean expand, List<LootTableFunction> functions, int weight, int quality) { public LootTable.Entry create(LootTableManager lootTableManager, String name, List<Condition> conditions, List<LootTable.Entry> children, boolean expand, List<LootTableFunction> functions, int weight, int quality) {
NamespaceID itemID = NamespaceID.from(name); NamespaceID itemID = NamespaceID.from(name);
return new ItemEntry(this, Registries.getMaterial(itemID), weight, quality, functions, conditions); return new ItemEntry(this, Material.fromNamespaceId(itemID), weight, quality, functions, conditions);
} }
} }

View File

@ -6,7 +6,6 @@ import net.minestom.server.gamedata.loottables.LootTable;
import net.minestom.server.gamedata.tags.Tag; import net.minestom.server.gamedata.tags.Tag;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material; import net.minestom.server.item.Material;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.NamespaceID; import net.minestom.server.utils.NamespaceID;
import java.util.List; import java.util.List;
@ -32,7 +31,7 @@ public class TagEntry extends LootTable.Entry {
Material[] asArrayOfItems = new Material[values.size()]; Material[] asArrayOfItems = new Material[values.size()];
int ptr = 0; int ptr = 0;
for (NamespaceID id : values) { for (NamespaceID id : values) {
asArrayOfItems[ptr++] = Registries.getMaterial(id); asArrayOfItems[ptr++] = Material.fromNamespaceId(id);
} }
if (expand) { if (expand) {
Material selectedMaterial = asArrayOfItems[rng.nextInt(asArrayOfItems.length)]; Material selectedMaterial = asArrayOfItems[rng.nextInt(asArrayOfItems.length)];

View File

@ -4,7 +4,6 @@ import net.minestom.server.item.ItemMeta;
import net.minestom.server.item.ItemMetaBuilder; import net.minestom.server.item.ItemMetaBuilder;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material; import net.minestom.server.item.Material;
import net.minestom.server.registry.Registries;
import net.minestom.server.utils.validate.Check; import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBTCompound; import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -152,7 +151,7 @@ public class CrossbowMeta extends ItemMeta implements ItemMetaBuilder.Provider<S
for (NBTCompound projectileCompound : projectilesList) { for (NBTCompound projectileCompound : projectilesList) {
final byte count = projectileCompound.getByte("Count"); final byte count = projectileCompound.getByte("Count");
final String id = projectileCompound.getString("id"); final String id = projectileCompound.getString("id");
final Material material = Registries.getMaterial(id); final Material material = Material.fromNamespaceId(id);
final NBTCompound tagsCompound = projectileCompound.getCompound("tag"); final NBTCompound tagsCompound = projectileCompound.getCompound("tag");
ItemStack itemStack = ItemStack.fromNBT(material, tagsCompound, count); ItemStack itemStack = ItemStack.fromNBT(material, tagsCompound, count);

View File

@ -4,6 +4,7 @@ import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.EntityType; import net.minestom.server.entity.EntityType;
import net.minestom.server.gamedata.tags.Tag; import net.minestom.server.gamedata.tags.Tag;
import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.Block;
import net.minestom.server.item.Material;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.registry.Registries; import net.minestom.server.registry.Registries;
@ -12,10 +13,7 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class TagsPacket implements ServerPacket { public class TagsPacket implements ServerPacket {
@ -132,7 +130,9 @@ public class TagsPacket implements ServerPacket {
writer.writeVarInt(values.size()); writer.writeVarInt(values.size());
// entries // entries
for (NamespaceID name : values) { for (NamespaceID name : values) {
writer.writeVarInt(Registries.getMaterial(name).id()); // FIXME: invalid namespace
final var material = Objects.requireNonNullElse(Material.fromNamespaceId(name), Material.AIR);
writer.writeVarInt(material.id());
} }
} }
break; break;

View File

@ -75,7 +75,7 @@ public class Registry {
this.blockEntity = getString("blockEntity", null); this.blockEntity = getString("blockEntity", null);
{ {
final String materialNamespace = getString("correspondingItem", null); final String materialNamespace = getString("correspondingItem", null);
this.materialSupplier = materialNamespace != null ? () -> Registries.getMaterial(materialNamespace) : () -> null; this.materialSupplier = materialNamespace != null ? () -> Material.fromNamespaceId(materialNamespace) : () -> null;
} }
} }

View File

@ -26,7 +26,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader;
import java.util.*; import java.util.*;
// for lack of a better name // for lack of a better name
@ -69,7 +68,7 @@ public final class NBTUtils {
public static void loadAllItems(@NotNull NBTList<NBTCompound> items, @NotNull Inventory destination) { public static void loadAllItems(@NotNull NBTList<NBTCompound> items, @NotNull Inventory destination) {
destination.clear(); destination.clear();
for (NBTCompound tag : items) { for (NBTCompound tag : items) {
Material material = Registries.getMaterial(tag.getString("id")); Material material = Material.fromNamespaceId(tag.getString("id"));
if (material == Material.AIR) { if (material == Material.AIR) {
material = Material.STONE; material = Material.STONE;
} }

View File

@ -3,7 +3,6 @@ package demo.block;
import net.minestom.server.instance.block.BlockHandler; import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.item.ItemStack; import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material; import net.minestom.server.item.Material;
import net.minestom.server.registry.Registries;
import net.minestom.server.tag.Tag; import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagReadable; import net.minestom.server.tag.TagReadable;
import net.minestom.server.tag.TagSerializer; import net.minestom.server.tag.TagSerializer;
@ -33,7 +32,7 @@ public class CampfireHandler implements BlockHandler {
item.forEach(nbtCompound -> { item.forEach(nbtCompound -> {
int amount = nbtCompound.getAsByte("Count"); int amount = nbtCompound.getAsByte("Count");
String id = nbtCompound.getString("id"); String id = nbtCompound.getString("id");
Material material = Registries.getMaterial(id); Material material = Material.fromNamespaceId(id);
result.add(ItemStack.of(material, amount)); result.add(ItemStack.of(material, amount));
}); });
return result; return result;