mirror of
https://github.com/FabricMC/fabric.git
synced 2024-11-22 23:58:02 -05:00
Add helper methods to create custom villager types. (#1076)
* Add helper methods to create custom villager types. * Rename biome method * Make it register and register the instance of the villager type. * Javadoc 2 * Add texture javadoc
This commit is contained in:
parent
c36cc5ad54
commit
1304192f58
3 changed files with 118 additions and 1 deletions
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.fabricmc.fabric.api.object.builder.v1.villager;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.registry.RegistryKey;
|
||||
import net.minecraft.village.VillagerProfession;
|
||||
import net.minecraft.village.VillagerType;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
|
||||
import net.fabricmc.fabric.mixin.object.builder.VillagerTypeAccessor;
|
||||
|
||||
/**
|
||||
* Utilities related to the creation of {@link VillagerType}s.
|
||||
* Not to be confused with a {@link VillagerProfession}, a villager type defines the appearance of a villager.
|
||||
*
|
||||
* <p>Creation and registration of custom villager types may be done by using {@link VillagerTypeHelper#register(Identifier)}.
|
||||
*
|
||||
* <p>Creation and registration of a villager type does not guarantee villagers of a specific type will be created in a world.
|
||||
* Typically the villager type is bound to a specific group of biomes.
|
||||
* To allow a villager type to be spawned in a specific biome, use {@link VillagerTypeHelper#addVillagerTypeToBiome(RegistryKey, VillagerType)}.
|
||||
*
|
||||
* <p>The texture used for the appearance of the villager is located at {@code assets/IDENTIFIER_NAMESPACE/textures/entity/villager/type/IDENTIFIER_PATH.png}.
|
||||
*/
|
||||
public final class VillagerTypeHelper {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
/**
|
||||
* Creates and registers a new villager type.
|
||||
*
|
||||
* @param id the id of the villager type
|
||||
* @return a new villager type
|
||||
*/
|
||||
public static VillagerType register(Identifier id) {
|
||||
Objects.requireNonNull(id, "Id of villager type cannot be null");
|
||||
return VillagerTypeAccessor.callRegister(id.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the biome a villager type can spawn in.
|
||||
*
|
||||
* @param biomeKey the registry key of the biome
|
||||
* @param villagerType the villager type
|
||||
*/
|
||||
public static void addVillagerTypeToBiome(RegistryKey<Biome> biomeKey, VillagerType villagerType) {
|
||||
Objects.requireNonNull(biomeKey, "Biome registry key cannot be null");
|
||||
Objects.requireNonNull(villagerType, "Villager type cannot be null");
|
||||
|
||||
if (VillagerTypeAccessor.getBiomeTypeToIdMap().put(biomeKey, villagerType) != null) {
|
||||
LOGGER.debug("Overriding existing Biome -> VillagerType registration for Biome {}", biomeKey.getValue().toString());
|
||||
}
|
||||
}
|
||||
|
||||
private VillagerTypeHelper() {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.fabricmc.fabric.mixin.object.builder;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
||||
import org.spongepowered.asm.mixin.gen.Invoker;
|
||||
|
||||
import net.minecraft.util.registry.RegistryKey;
|
||||
import net.minecraft.village.VillagerType;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
|
||||
@Mixin(VillagerType.class)
|
||||
public interface VillagerTypeAccessor {
|
||||
@Accessor("BIOME_TO_TYPE")
|
||||
static Map<RegistryKey<Biome>, VillagerType> getBiomeTypeToIdMap() {
|
||||
throw new AssertionError("Untransformed Accessor!");
|
||||
}
|
||||
|
||||
// FIXME: This should be called `register` in yarn
|
||||
@Invoker("create")
|
||||
static VillagerType callRegister(String id) {
|
||||
throw new AssertionError("Untransformed Accessor!");
|
||||
}
|
||||
}
|
|
@ -16,7 +16,8 @@
|
|||
"TradeOffersAccessor",
|
||||
"TradeOffersMixin",
|
||||
"TypeAwareTradeMixin",
|
||||
"VillagerProfessionAccessor"
|
||||
"VillagerProfessionAccessor",
|
||||
"VillagerTypeAccessor"
|
||||
],
|
||||
"client": [
|
||||
"ModelPredicateProviderRegistryAccessor",
|
||||
|
|
Loading…
Reference in a new issue