autogen dimensional door blocks for standard doors and trapdoors

This commit is contained in:
CreepyCre 2021-04-09 19:17:11 +02:00
parent 630ec3df62
commit f82c4dd78e
5 changed files with 126 additions and 5 deletions

View file

@ -1,30 +1,29 @@
package org.dimdev.dimdoors;
import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import org.dimdev.dimdoors.block.ModBlocks;
import org.dimdev.dimdoors.block.entity.ModBlockEntityTypes;
import org.dimdev.dimdoors.client.DetachedRiftBlockEntityRenderer;
import org.dimdev.dimdoors.client.DimensionalDoorModelVariantProvider;
import org.dimdev.dimdoors.client.EntranceRiftBlockEntityRenderer;
import org.dimdev.dimdoors.client.ModSkyRendering;
import org.dimdev.dimdoors.entity.ModEntityTypes;
import org.dimdev.dimdoors.entity.MonolithEntity;
import org.dimdev.dimdoors.entity.ai.MonolithAggroGoal;
import org.dimdev.dimdoors.fluid.ModFluids;
import org.dimdev.dimdoors.network.client.ExtendedClientPlayNetworkHandler;
import org.dimdev.dimdoors.particle.ModParticleTypes;
import org.dimdev.dimdoors.particle.client.MonolithParticle;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendereregistry.v1.BlockEntityRendererRegistry;
import net.fabricmc.loader.api.FabricLoader;
@Environment(EnvType.CLIENT)
public class DimensionalDoorsClientInitializer implements ClientModInitializer {
@Override
public void onInitializeClient() {
ModelLoadingRegistry.INSTANCE.registerVariantProvider((manager) -> new DimensionalDoorModelVariantProvider());
ModEntityTypes.initClient();
ModSkyRendering.initClient();
ModFluids.initClient();

View file

@ -7,8 +7,11 @@ import java.util.function.Supplier;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import me.sargunvohra.mcmods.autoconfig1u.ConfigHolder;
import net.fabricmc.fabric.api.event.registry.RegistryEntryAddedCallback;
import net.minecraft.util.registry.Registry;
import org.dimdev.dimdoors.api.DimensionalDoorsApi;
import org.dimdev.dimdoors.block.ModBlocks;
import org.dimdev.dimdoors.block.door.DimensionalDoorBlockRegistrar;
import org.dimdev.dimdoors.block.door.data.condition.Condition;
import org.dimdev.dimdoors.block.entity.ModBlockEntityTypes;
import org.dimdev.dimdoors.command.ModCommands;
@ -20,6 +23,7 @@ import org.dimdev.dimdoors.fluid.ModFluids;
import org.dimdev.dimdoors.item.ModItems;
import org.dimdev.dimdoors.listener.AttackBlockCallbackListener;
import org.dimdev.dimdoors.listener.ChunkLoadListener;
import org.dimdev.dimdoors.listener.ItemRegistryEntryAddedListener;
import org.dimdev.dimdoors.listener.pocket.PlayerBlockBreakEventBeforeListener;
import org.dimdev.dimdoors.listener.pocket.PocketAttackBlockCallbackListener;
import org.dimdev.dimdoors.listener.pocket.UseBlockCallbackListener;
@ -115,6 +119,10 @@ public class DimensionalDoorsInitializer implements ModInitializer {
ModParticleTypes.init();
ModCriteria.init();
DimensionalDoorBlockRegistrar dimensionalDoorBlockRegistrar = new DimensionalDoorBlockRegistrar(Registry.BLOCK);
dimensionalDoorBlockRegistrar.init();
RegistryEntryAddedCallback.event(Registry.BLOCK).register(new ItemRegistryEntryAddedListener(dimensionalDoorBlockRegistrar));
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(PocketLoader.getInstance());
ResourceManagerHelper.registerBuiltinResourcePack(new Identifier("dimdoors", "default"), dimDoorsMod, CONFIG_MANAGER.get().getPocketsConfig().defaultPocketsResourcePackActivationType.asResourcePackActivationType());
ResourceManagerHelper.registerBuiltinResourcePack(new Identifier("dimdoors", "classic"), dimDoorsMod, CONFIG_MANAGER.get().getPocketsConfig().classicPocketsResourcePackActivationType.asResourcePackActivationType());

View file

@ -0,0 +1,73 @@
package org.dimdev.dimdoors.block.door;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.DoorBlock;
import net.minecraft.block.TrapdoorBlock;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class DimensionalDoorBlockRegistrar {
private static final String PREFIX = "autogen_";
private final Registry<Block> registry;
public DimensionalDoorBlockRegistrar(Registry<Block> registry) {
this.registry = registry;
}
public void init() {
new ArrayList<>(registry.getEntries())
.forEach(entry -> handleEntry(entry.getKey().getValue(), entry.getValue()));
}
public void handleEntry(Identifier identifier, Block block) {
switch (DoorClassCase.getCase(block)) {
case DOOR_BLOCK:
register(identifier, block, DimensionalDoorBlock::new);
break;
case TRAPDOOR_BLOCK:
register(identifier, block, DimensionalTrapdoorBlock::new);
break;
default:
// do nothing
break;
}
}
private void register(Identifier identifier, Block block, Function<AbstractBlock.Settings, ? extends Block> constructor) {
Registry.register(registry, new Identifier("dimdoors", PREFIX + identifier.getNamespace() + "_dimdoors_" + identifier.getPath()), constructor.apply(AbstractBlock.Settings.copy(block)));
}
// Just in case we want to support more Door classes in the future.
// Need to move away from an enum for better mod support though.
private enum DoorClassCase {
NONE(null),
DOOR_BLOCK(DoorBlock.class),
TRAPDOOR_BLOCK(TrapdoorBlock.class);
private static final Map<Class<? extends Block>, DoorClassCase> CASE_MAP = new HashMap<>();
static {
for (DoorClassCase doorClassCase : DoorClassCase.values()) {
CASE_MAP.put(doorClassCase.doorClazz, doorClassCase);
}
}
public static DoorClassCase getCase(Block block) {
DoorClassCase doorClassCase = CASE_MAP.get(block.getClass());
return doorClassCase == null ? NONE : doorClassCase;
}
private final Class<? extends Block> doorClazz;
DoorClassCase(Class<? extends Block> doorClazz) {
this.doorClazz = doorClazz;
}
}
}

View file

@ -0,0 +1,22 @@
package org.dimdev.dimdoors.client;
import net.fabricmc.fabric.api.client.model.ModelProviderContext;
import net.fabricmc.fabric.api.client.model.ModelProviderException;
import net.fabricmc.fabric.api.client.model.ModelVariantProvider;
import net.minecraft.client.render.model.UnbakedModel;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
public class DimensionalDoorModelVariantProvider implements ModelVariantProvider {
private static final String PREFIX = "autogen_";
@Override
public @Nullable UnbakedModel loadModelVariant(ModelIdentifier modelId, ModelProviderContext context) throws ModelProviderException {
String path = modelId.getPath();
if (!path.startsWith(PREFIX)) return null;
String[] separatedId = path.substring(PREFIX.length()).split("_dimdoors_");
ModelIdentifier newId = new ModelIdentifier(new Identifier(separatedId[0], separatedId[1]), modelId.getVariant());
return context.loadModel(newId);
}
}

View file

@ -0,0 +1,19 @@
package org.dimdev.dimdoors.listener;
import net.fabricmc.fabric.api.event.registry.RegistryEntryAddedCallback;
import net.minecraft.block.Block;
import net.minecraft.util.Identifier;
import org.dimdev.dimdoors.block.door.DimensionalDoorBlockRegistrar;
public class ItemRegistryEntryAddedListener implements RegistryEntryAddedCallback<Block> {
private final DimensionalDoorBlockRegistrar registrar;
public ItemRegistryEntryAddedListener(DimensionalDoorBlockRegistrar registrar) {
this.registrar = registrar;
}
@Override
public void onEntryAdded(int rawId, Identifier id, Block object) {
registrar.handleEntry(id, object);
}
}