Fabric port in progress

This commit is contained in:
Sebastian Hartte
2020-06-28 00:45:33 +02:00
parent b79cd732b2
commit 37ce6b262e
102 changed files with 650 additions and 591 deletions
@@ -152,7 +152,7 @@ class BlockDefinitionBuilder implements IBlockBuilder {
this.bootstrapComponents.forEach(component -> this.factory.addBootstrapComponent(component.apply(block, item)));
if (this.tileEntityDefinition != null) {
// Tell the tile entity definition about the block we've registered
// Tell the block entity definition about the block we've registered
this.tileEntityDefinition.addBlock(block);
}
@@ -11,10 +11,11 @@ import com.google.common.base.Preconditions;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.rendereregistry.v1.BlockEntityRendererRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import appeng.api.features.AEFeature;
import appeng.block.AEBaseTileBlock;
@@ -28,7 +29,7 @@ import appeng.tile.AEBaseBlockEntity;
import appeng.util.Platform;
/**
* Used to define our tile entities and all of their properties that are
* Used to define our block entities and all of their properties that are
* relevant to registering them.
*
* @param <T>
@@ -37,27 +38,26 @@ public class BlockEntityBuilder<T extends AEBaseBlockEntity> {
private final FeatureFactory factory;
private final String registryName;
private final Identifier id;
// The tile entity class
// The block entity class
private final Class<T> tileClass;
private BlockEntityType<T> type;
// The factory for creating tile entity objects
// The factory for creating block entity objects
private final Function<BlockEntityType<T>, T> supplier;
@Environment(EnvType.CLIENT)
private TileEntityRendering<T> tileEntityRendering;
private final List<Block> blocks = new ArrayList<>();
private final EnumSet<AEFeature> features = EnumSet.noneOf(AEFeature.class);
public BlockEntityBuilder(FeatureFactory factory, String registryName, Class<T> tileClass,
public BlockEntityBuilder(FeatureFactory factory, String id, Class<T> tileClass,
Function<BlockEntityType<T>, T> supplier) {
this.factory = factory;
this.registryName = registryName;
this.id = AppEng.makeId(id);
this.tileClass = tileClass;
this.supplier = supplier;
@@ -78,22 +78,22 @@ public class BlockEntityBuilder<T extends AEBaseBlockEntity> {
}
public BlockEntityBuilder<T> rendering(TileEntityRenderingCustomizer<T> customizer) {
DistExecutor.runWhenOn(EnvType.CLIENT, () -> () -> customizer.customize(tileEntityRendering));
customizer.customize(tileEntityRendering);
return this;
}
@SuppressWarnings("unchecked")
public TileEntityDefinition build() {
this.factory.addBootstrapComponent((ITileEntityRegistrationComponent) registry -> {
this.factory.addBootstrapComponent((ITileEntityRegistrationComponent) () -> {
if (blocks.isEmpty()) {
throw new IllegalStateException("No blocks make use of this tile entity: " + tileClass);
throw new IllegalStateException("No blocks make use of this block entity: " + tileClass);
}
Supplier<T> factory = () -> supplier.apply(type);
type = BlockEntityType.Builder.create(factory, blocks.toArray(new Block[0])).build(null);
type.setRegistryName(AppEng.MOD_ID, registryName);
registry.register(type);
Registry.register(Registry.BLOCK_ENTITY_TYPE, id, type);
AEBaseBlockEntity.registerTileItem(tileClass, new BlockStackSrc(blocks.get(0), ActivityState.Enabled));
@@ -103,9 +103,11 @@ public class BlockEntityBuilder<T extends AEBaseBlockEntity> {
baseTileBlock.setTileEntity(tileClass, factory);
}
}
});
DistExecutor.runWhenOn(EnvType.CLIENT, () -> this::buildClient);
if (Platform.hasClientClasses()) {
buildClient();
}
return new TileEntityDefinition(this::addBlock);
@@ -115,7 +117,7 @@ public class BlockEntityBuilder<T extends AEBaseBlockEntity> {
private void buildClient() {
this.factory.addBootstrapComponent((IClientSetupComponent) () -> {
if (tileEntityRendering.tileEntityRenderer != null) {
ClientRegistry.bindTileEntityRenderer(type, tileEntityRendering.tileEntityRenderer);
BlockEntityRendererRegistry.INSTANCE.register(type, tileEntityRendering.tileEntityRenderer);
}
});
}
@@ -76,13 +76,12 @@ class BlockRendering implements IBlockRendering {
if (this.modelCustomizer != null) {
factory.addModelOverride(id.getPath(), this.modelCustomizer);
} else if (block instanceof AEBaseTileBlock) {
// This is a default rotating model if the base-block uses an AE tile entity
// This is a default rotating model if the base-block uses an AE block entity
// which exposes UP/FRONT as
// extended props
factory.addModelOverride(id.getPath(), (l, m) -> new AutoRotatingBakedModel(m));
}
// TODO : 1.12
if (this.blockColor != null) {
factory.addBootstrapComponent(new BlockColorComponent(block, this.blockColor));
}
@@ -0,0 +1,22 @@
package appeng.bootstrap;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.util.Identifier;
import java.util.Map;
@FunctionalInterface
public interface ModelsReloadCallback {
Event<ModelsReloadCallback> EVENT = EventFactory.createArrayBacked(ModelsReloadCallback.class,
(listeners) -> (loadedModels) -> {
for (ModelsReloadCallback listener : listeners) {
listener.onModelsReloaded(loadedModels);
}
});
void onModelsReloaded(Map<Identifier, BakedModel> loadedModels);
}
@@ -18,19 +18,20 @@
package appeng.bootstrap;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import appeng.tile.AEBaseBlockEntity;
/**
* A callback that allows the rendering of a tile entity to be customized. Sadly
* A callback that allows the rendering of a block entity to be customized. Sadly
* this class is required and no lambdas can be used due to them not being able
* to be annotated with @OnlyIn(CLIENT).
*/
public abstract class TileEntityRenderingCustomizer<T extends AEBaseBlockEntity> {
public interface TileEntityRenderingCustomizer<T extends AEBaseBlockEntity> {
@Environment(EnvType.CLIENT)
public abstract void customize(TileEntityRendering<T> rendering);
/**
* Declared as a default method because we will carve out the implementations that
* override this method on the Server side.
*/
default void customize(TileEntityRendering<T> rendering) {
}
}
@@ -1,11 +1,14 @@
package appeng.bootstrap.components;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.util.Identifier;
import appeng.bootstrap.IBootstrapComponent;
import java.util.Map;
public interface IModelBakeComponent extends IBootstrapComponent {
void onModelBakeEvent(ModelBakeEvent event);
void onModelsReloaded(Map<Identifier, BakedModel> loadedModels);
}
@@ -1,11 +1,9 @@
package appeng.bootstrap.components;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraftforge.registries.IForgeRegistry;
import appeng.bootstrap.IBootstrapComponent;
@FunctionalInterface
public interface ITileEntityRegistrationComponent extends IBootstrapComponent {
void register(IForgeRegistry<BlockEntityType<?>> registry);
void register();
}
@@ -28,7 +28,6 @@ import com.google.common.collect.Sets;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.ModelLoader;
import net.minecraft.util.Identifier;
import net.minecraftforge.client.event.ModelBakeEvent;
import appeng.core.AppEng;
@@ -42,17 +41,16 @@ public class ModelOverrideComponent implements IModelBakeComponent {
}
@Override
public void onModelBakeEvent(final ModelBakeEvent event) {
Map<Identifier, BakedModel> modelRegistry = event.getModelRegistry();
Set<Identifier> keys = Sets.newHashSet(modelRegistry.keySet());
BakedModel missingModel = modelRegistry.get(ModelLoader.MISSING);
public void onModelsReloaded(final Map<Identifier, BakedModel> loadedModels) {
Set<Identifier> keys = Sets.newHashSet(loadedModels.keySet());
BakedModel missingModel = loadedModels.get(ModelLoader.MISSING);
for (Identifier location : keys) {
if (!location.getNamespace().equals(AppEng.MOD_ID)) {
continue;
}
BakedModel orgModel = modelRegistry.get(location);
BakedModel orgModel = loadedModels.get(location);
// Don't customize the missing model. This causes Forge to swallow exceptions
if (orgModel == missingModel) {
@@ -64,7 +62,7 @@ public class ModelOverrideComponent implements IModelBakeComponent {
BakedModel newModel = customizer.apply(location, orgModel);
if (newModel != orgModel) {
modelRegistry.put(location, newModel);
loadedModels.put(location, newModel);
}
}
}
@@ -27,7 +27,7 @@ import net.minecraft.block.Block;
*/
public class TileEntityDefinition {
// To be notified when a Block declares that it uses this tile entity
// To be notified when a Block declares that it uses this block entity
private final Consumer<Block> addBlockListener;
public TileEntityDefinition(Consumer<Block> addBlockListener) {