Convert hard-coded villager types to a registry

This commit is contained in:
booky10 committed 2025-02-28 17:55:38 +01:00
1 parent 3d90acad8e
commit d75137fe5f
4 files changed
+95 -32

No files matched your search

@@ -0,0 +1,40 @@
/*
* This file is part of packetevents - https://github.com/retrooper/packetevents
* Copyright (C) 2025 retrooper and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.github.retrooper.packetevents.protocol.entity.villager.type;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.mapper.AbstractMappedEntity;
import com.github.retrooper.packetevents.util.mappings.TypesBuilderData;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
public class StaticVillagerType extends AbstractMappedEntity implements VillagerType {
@ApiStatus.Internal
public StaticVillagerType(@Nullable TypesBuilderData data) {
super(data);
}
@Override
public int getId() {
ServerVersion version = PacketEvents.getAPI().getServerManager().getVersion();
return this.getId(version.toClientVersion());
}
}
@@ -22,4 +22,7 @@ import com.github.retrooper.packetevents.protocol.mapper.StaticMappedEntity;
public interface VillagerType extends StaticMappedEntity {
@Deprecated
@Override
int getId();
}
@@ -18,58 +18,67 @@
package com.github.retrooper.packetevents.protocol.entity.villager.type;
import com.github.retrooper.packetevents.resources.ResourceLocation;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.util.mappings.VersionedRegistry;
import org.jetbrains.annotations.ApiStatus;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class VillagerTypes {
private static final Map<String, VillagerType> VILLAGER_TYPE_MAP = new HashMap<>();
private static final Map<Byte, VillagerType> VILLAGER_TYPE_ID_MAP = new HashMap<>();
public final class VillagerTypes {
private static final VersionedRegistry<VillagerType> REGISTRY = new VersionedRegistry<>("villager_type");
private VillagerTypes() {
}
public static VersionedRegistry<VillagerType> getRegistry() {
return REGISTRY;
}
@ApiStatus.Internal
public static VillagerType define(int id, String name) {
ResourceLocation location = new ResourceLocation(name);
VillagerType type = new VillagerType() {
@Override
public ResourceLocation getName() {
return location;
}
@Override
public int getId() {
return id;
}
};
VILLAGER_TYPE_MAP.put(type.getName().toString(), type);
VILLAGER_TYPE_ID_MAP.put((byte)type.getId(), type);
return type;
return define(name);
}
@ApiStatus.Internal
public static VillagerType define(String name) {
return REGISTRY.define(name, StaticVillagerType::new);
}
@Deprecated
public static VillagerType getById(int id) {
return VILLAGER_TYPE_ID_MAP.get((byte)id);
ServerVersion version = PacketEvents.getAPI().getServerManager().getVersion();
return getById(version.toClientVersion(), id);
}
public static VillagerType getById(ClientVersion version, int id) {
return REGISTRY.getById(version, id);
}
public static VillagerType getByName(String name) {
return VILLAGER_TYPE_MAP.get(name);
return REGISTRY.getByName(name);
}
public static final VillagerType DESERT = define(0, "minecraft:desert");
public static final VillagerType JUNGLE = define(1, "minecraft:jungle");
public static final VillagerType PLAINS = define(2, "minecraft:plains");
public static final VillagerType SAVANNA = define(3, "minecraft:savanna");
public static final VillagerType SNOW = define(4, "minecraft:snow");
public static final VillagerType SWAMP = define(5, "minecraft:swamp");
public static final VillagerType TAIGA = define(6, "minecraft:taiga");
public static final VillagerType DESERT = define("desert");
public static final VillagerType JUNGLE = define("jungle");
public static final VillagerType PLAINS = define("plains");
public static final VillagerType SAVANNA = define("savanna");
public static final VillagerType SNOW = define("snow");
public static final VillagerType SWAMP = define("swamp");
public static final VillagerType TAIGA = define("taiga");
/**
* Returns an immutable view of the villager types.
*
* @return Villager Types
*/
public static Collection<VillagerType> values() {
return Collections.unmodifiableCollection(VILLAGER_TYPE_MAP.values());
return REGISTRY.getEntries();
}
static {
REGISTRY.getEntries();
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"V_1_14": [
"desert",
"jungle",
"plains",
"savanna",
"snow",
"swamp",
"taiga"
]
}