Create/src/main/java/com/simibubi/create/CreateClient.java

194 lines
7.3 KiB
Java
Raw Normal View History

package com.simibubi.create;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import com.simibubi.create.foundation.behaviour.filtering.FilteringRenderer;
import com.simibubi.create.foundation.behaviour.linked.LinkRenderer;
import com.simibubi.create.foundation.behaviour.scrollvalue.ScrollValueRenderer;
2020-05-22 20:08:02 +02:00
import com.simibubi.create.foundation.block.IHaveColorHandler;
import com.simibubi.create.foundation.block.render.CustomBlockModels;
import com.simibubi.create.foundation.block.render.SpriteShifter;
import com.simibubi.create.foundation.item.IHaveCustomItemModel;
import com.simibubi.create.foundation.utility.SuperByteBufferCache;
import com.simibubi.create.foundation.utility.outliner.Outliner;
import com.simibubi.create.modules.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.modules.contraptions.components.contraptions.ChassisRangeDisplay;
import com.simibubi.create.modules.contraptions.components.contraptions.ContraptionRenderer;
import com.simibubi.create.modules.schematics.ClientSchematicLoader;
import com.simibubi.create.modules.schematics.client.SchematicAndQuillHandler;
import com.simibubi.create.modules.schematics.client.SchematicHandler;
2020-05-22 20:08:02 +02:00
import com.tterrag.registrate.util.entry.RegistryEntry;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockModelShapes;
2020-05-22 20:08:02 +02:00
import net.minecraft.client.renderer.color.BlockColors;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.model.ModelResourceLocation;
import net.minecraft.inventory.container.PlayerContainer;
import net.minecraft.resources.IReloadableResourceManager;
import net.minecraft.resources.IResourceManager;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
public class CreateClient {
public static ClientSchematicLoader schematicSender;
public static SchematicHandler schematicHandler;
public static SchematicAndQuillHandler schematicAndQuillHandler;
public static SuperByteBufferCache bufferCache;
public static Outliner outliner;
private static CustomBlockModels customBlockModels;
public static void addListeners(IEventBus modEventBus) {
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> {
modEventBus.addListener(CreateClient::clientInit);
modEventBus.addListener(CreateClient::onModelBake);
modEventBus.addListener(CreateClient::onModelRegistry);
modEventBus.addListener(CreateClient::onTextureStitch);
modEventBus.addListener(AllParticles::registerFactories);
});
}
public static void clientInit(FMLClientSetupEvent event) {
schematicSender = new ClientSchematicLoader();
schematicHandler = new SchematicHandler();
schematicAndQuillHandler = new SchematicAndQuillHandler();
outliner = new Outliner();
bufferCache = new SuperByteBufferCache();
bufferCache.registerCompartment(KineticTileEntityRenderer.KINETIC_TILE);
bufferCache.registerCompartment(ContraptionRenderer.CONTRAPTION, 20);
AllKeys.register();
AllContainers.registerScreenFactories();
AllTileEntities.registerRenderers();
AllItems.registerColorHandlers();
AllEntities.registerRenderers();
2020-05-22 20:08:02 +02:00
registerColorHandlers();
IResourceManager resourceManager = Minecraft.getInstance()
.getResourceManager();
if (resourceManager instanceof IReloadableResourceManager)
((IReloadableResourceManager) resourceManager).addReloadListener(new ResourceReloadHandler());
}
public static void gameTick() {
schematicSender.tick();
schematicAndQuillHandler.tick();
schematicHandler.tick();
FilteringRenderer.tick();
LinkRenderer.tick();
ScrollValueRenderer.tick();
ChassisRangeDisplay.tick();
outliner.tickOutlines();
}
@OnlyIn(Dist.CLIENT)
public static void onTextureStitch(TextureStitchEvent.Pre event) {
if (!event.getMap()
.getId()
.equals(PlayerContainer.BLOCK_ATLAS_TEXTURE))
return;
SpriteShifter.getAllTargetSprites()
.forEach(event::addSprite);
}
@OnlyIn(Dist.CLIENT)
public static void onModelBake(ModelBakeEvent event) {
Map<ResourceLocation, IBakedModel> modelRegistry = event.getModelRegistry();
AllBlockPartials.onModelBake(event);
getCustomBlockModels()
.foreach((block, modelFunc) -> swapModels(modelRegistry, getAllBlockStateModelLocations(block), modelFunc));
// todo modelswap for item registrate
for (AllItems item : AllItems.values()) {
if (item.get() instanceof IHaveCustomItemModel)
swapModels(modelRegistry, getItemModelLocation(item),
m -> ((IHaveCustomItemModel) item.get()).createModel(m)
.loadPartials(event));
}
}
@OnlyIn(Dist.CLIENT)
public static void onModelRegistry(ModelRegistryEvent event) {
AllBlockPartials.onModelRegistry(event);
// Register submodels for custom rendered item models
for (AllItems item : AllItems.values()) {
if (item.get() instanceof IHaveCustomItemModel)
((IHaveCustomItemModel) item.get()).createModel(null)
.getModelLocations()
.forEach(ModelLoader::addSpecialModel);
}
}
@OnlyIn(Dist.CLIENT)
protected static ModelResourceLocation getItemModelLocation(AllItems item) {
return new ModelResourceLocation(item.get()
.getRegistryName(), "inventory");
}
@OnlyIn(Dist.CLIENT)
protected static List<ModelResourceLocation> getAllBlockStateModelLocations(Block block) {
List<ModelResourceLocation> models = new ArrayList<>();
block.getStateContainer()
.getValidStates()
.forEach(state -> {
models.add(getBlockModelLocation(block, BlockModelShapes.getPropertyMapString(state.getValues())));
});
return models;
}
@OnlyIn(Dist.CLIENT)
protected static ModelResourceLocation getBlockModelLocation(Block block, String suffix) {
return new ModelResourceLocation(block.getRegistryName(), suffix);
}
@OnlyIn(Dist.CLIENT)
protected static <T extends IBakedModel> void swapModels(Map<ResourceLocation, IBakedModel> modelRegistry,
ModelResourceLocation location, Function<IBakedModel, T> factory) {
modelRegistry.put(location, factory.apply(modelRegistry.get(location)));
}
@OnlyIn(Dist.CLIENT)
protected static <T extends IBakedModel> void swapModels(Map<ResourceLocation, IBakedModel> modelRegistry,
List<ModelResourceLocation> locations, Function<IBakedModel, T> factory) {
locations.forEach(location -> {
swapModels(modelRegistry, location, factory);
});
}
2020-05-22 20:08:02 +02:00
@OnlyIn(Dist.CLIENT)
protected static void registerColorHandlers() {
BlockColors blockColors = Minecraft.getInstance()
.getBlockColors();
for (RegistryEntry<Block> registryEntry : Create.registrate()
.getAll(Block.class)) {
Block blockEntry = registryEntry.get();
if (blockEntry instanceof IHaveColorHandler) {
blockColors.register(((IHaveColorHandler) blockEntry).getColorHandler(), blockEntry);
}
}
}
public static CustomBlockModels getCustomBlockModels() {
if (customBlockModels == null)
customBlockModels = new CustomBlockModels();
return customBlockModels;
}
}