Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working render layers #27

Merged
merged 2 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class DeferredRegistration {
private static Multimap<Identifier, Entry> deferrals = MultimapBuilder.hashKeys().arrayListValues(2).build();

/**
* Called by ArchEx to start resolving deferred registrations.
*/
Expand All @@ -42,7 +42,7 @@ public static void init() {
}
});
}

/**
* Called indirectly by users to register derived blocks.
* @param modId
Expand All @@ -52,16 +52,16 @@ public static void init() {
*/
public static void register(String modId, BlockGroup group, BlockGroup.GroupedBlock groupedBlock, Collection<BlockType> blockTypes, @Nullable BlockCreationCallback callback) {
Entry deferral = new Entry(modId, group, groupedBlock, Set.copyOf(blockTypes), callback);

if (!deferral.register()) {
//ArchitectureExtensions.LOGGER.info("Deferred generation: "+deferral.modId()+" requested "+deferral.getIds()+" and registration was deferred.");
deferrals.put(groupedBlock.id(), deferral);
} else {
//ArchitectureExtensions.LOGGER.info("Deferred generation: "+deferral.modId()+" requested "+deferral.getIds()+" and registration was completed immediately.");
}
}


/**
* Called by ArchEx to warn users that we couldn't create blocks that were requested.
*/
Expand All @@ -71,24 +71,24 @@ public static void assertFinished() {
ArchitectureExtensions.LOGGER.warn(sourceString+" requested architecture extensions blocks derived from base block "+entry.groupedBlock.id()+", but this base block was never registered.");
}
}

private static record Entry(String modId, BlockGroup group, BlockGroup.GroupedBlock groupedBlock, Set<BlockType> blockTypes, BlockCreationCallback callback) {
public boolean register() {
Block baseBlock = groupedBlock.baseBlock().get();
if (baseBlock == Blocks.AIR || baseBlock == null) return false;

for(BlockType blockType : blockTypes) {
BlockType.TypedGroupedBlock created = blockType.register(group, groupedBlock, callback, modId);
BlockType.TypedGroupedBlock created = blockType.register(group, groupedBlock, callback);
DataGeneration.collect(created);
}

return true;
}

public Set<String> getIds() {
String modId = this.modId();
if (modId == "file") modId = ArchitectureExtensions.MOD_CONTAINER.metadata().id(); // If it's a staticdata resource, use our own id

HashSet<String> result = new HashSet<>();
for(BlockType bt : blockTypes) {
Identifier id = new Identifier(modId, groupedBlock.id().getPath() + "_" + bt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,36 @@ public enum BlockType {
private final BiFunction<Block, QuiltBlockSettings, Block> creator;
private final float strength;
private final String[] variants;
private final SafeRenderLayer renderLayer;

BlockType(BiFunction<Block, QuiltBlockSettings, Block> creator, float strength, String[] variants, SafeRenderLayer renderLayer) {
this.creator = creator;
this.strength = strength;
this.variants = variants;
this.renderLayer = renderLayer;
}


public String[] variants() {
return variants;
}

public SafeRenderLayer renderLayer() {
return renderLayer;
}

@Override
public String toString() {
return name().toLowerCase(Locale.ROOT);
}

public TypedGroupedBlock register(BlockGroup group, BlockGroup.GroupedBlock groupedBlock, BlockCreationCallback callback) {
return register(group, groupedBlock, callback, ArchitectureExtensions.MOD_CONTAINER.metadata().id()); // If no id is specified, use our own id
}

public TypedGroupedBlock register(BlockGroup group, BlockGroup.GroupedBlock groupedBlock, BlockCreationCallback callback, String modid) {
Identifier id = new Identifier(ArchitectureExtensions.MOD_CONTAINER.metadata().id(), groupedBlock.id().getPath() + "_" + this);
var baseBlock = groupedBlock.baseBlock().get();
var block = Registry.register(Registries.BLOCK, id, creator.apply(baseBlock, QuiltBlockSettings.copyOf(baseBlock).mapColorProvider(state -> groupedBlock.mapColor()).strength(strength)));

Registry.register(Registries.ITEM, id, new BlockItem(block, new QuiltItemSettings()));

if (callback != null) callback.onBlockCreated(group, this, baseBlock, block);

return new TypedGroupedBlock(this, groupedBlock, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@

import io.github.debuggyteam.architecture_extensions.ArchitectureExtensions;
import io.github.debuggyteam.architecture_extensions.resource.DataGeneration;
import net.minecraft.registry.Registries;
import net.minecraft.resource.ResourceType;
import org.jetbrains.annotations.NotNull;
import org.quiltmc.loader.api.ModContainer;
import org.quiltmc.qsl.base.api.entrypoint.client.ClientModInitializer;
import org.quiltmc.qsl.block.extensions.api.client.BlockRenderLayerMap;
import org.quiltmc.qsl.resource.loader.api.ResourceLoader;
import org.quiltmc.qsl.resource.loader.api.ResourcePackRegistrationContext;

import java.util.ArrayList;
import java.util.List;

public class ArchitectureExtensionsClient implements ClientModInitializer, ResourcePackRegistrationContext.Callback {
@Override
public void onInitializeClient(ModContainer mod) {
ResourceLoader.get(ResourceType.CLIENT_RESOURCES).getRegisterDefaultResourcePackEvent().register(this);

// This will be a pain in the ass
//BlockRenderLayerMap.put(RenderLayer.getCutout(), TransomBlock);
DataGeneration.BLOCKS.forEach(block -> BlockRenderLayerMap.put(block.type().renderLayer().get(), Registries.BLOCK.get(block.id())));

ResourceLoader.get(ResourceType.CLIENT_RESOURCES).getRegisterDefaultResourcePackEvent().register(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import net.minecraft.util.Identifier;

public final class DataGeneration {
static final Set<BlockType.TypedGroupedBlock> BLOCKS = Sets.newHashSet();
public static final Set<BlockType.TypedGroupedBlock> BLOCKS = Sets.newHashSet();

private static final String GROUP_PLACEHOLDER = "group";
private static final String BASE_PLACEHOLDER = "base";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
package io.github.debuggyteam.architecture_extensions.util;

public enum SafeRenderLayer {
SOLID,
TRANSLUCENT,
CUTOUT;

public static SafeRenderLayer combine(SafeRenderLayer first, SafeRenderLayer second) {
if (first == TRANSLUCENT || second == TRANSLUCENT) return TRANSLUCENT;
if (first == CUTOUT || second == CUTOUT) return CUTOUT;
return SOLID;
import java.util.function.Supplier;

import org.quiltmc.loader.api.minecraft.ClientOnly;

import net.minecraft.client.render.RenderLayer;

public enum SafeRenderLayer implements @ClientOnly Supplier<RenderLayer> {
SOLID(0),
TRANSLUCENT(2),
CUTOUT(1);

private final int priority;

SafeRenderLayer(int priority) {
this.priority = priority;
}

@Override
public RenderLayer get() {
return switch (this) {
case SOLID -> RenderLayer.getSolid();
case TRANSLUCENT -> RenderLayer.getTranslucent();
case CUTOUT -> RenderLayer.getCutout();
};
}

public static SafeRenderLayer choose(SafeRenderLayer first, SafeRenderLayer second) {
if (second.priority > first.priority) return second;
return first;
}
}