-
Notifications
You must be signed in to change notification settings - Fork 811
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API to allow conditional rendering of block entities (#2316)
* Add API to allow conditional rendering of block entities - Extend BlockEntityType to store a predicate, always returning true by default. - Evaluate the predicate in ChunkBuilderMeshingTask before fetching the BlockEntityRenderer. - Expose a method to set the predicate for a given BlockEntityType in the API. * Update to latest dev * Fix build * Merge files * Make the predicate an array * Use custom predicate class --------- Co-authored-by: IMS212 <[email protected]>
- Loading branch information
Showing
7 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
common/src/api/java/net/caffeinemc/mods/sodium/api/blockentity/BlockEntityRenderHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package net.caffeinemc.mods.sodium.api.blockentity; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import net.caffeinemc.mods.sodium.api.internal.DependencyInjection; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.Experimental | ||
@ApiStatus.AvailableSince("0.6.0") | ||
public interface BlockEntityRenderHandler { | ||
BlockEntityRenderHandler INSTANCE = DependencyInjection.load(BlockEntityRenderHandler.class, | ||
"net.caffeinemc.mods.sodium.client.render.chunk.BlockEntityRenderHandlerImpl"); | ||
|
||
static BlockEntityRenderHandler instance() { | ||
return INSTANCE; | ||
} | ||
|
||
/** | ||
* Adds a predicate to determine if a block entity should be rendered. | ||
* | ||
* <p>Upon chunk bake, block entities of the given type will have {@code shouldRender} evaluated. | ||
* <br>If <b>all predicates</b> returns {@code true} (and the block entity has a renderer), the block entity will be | ||
* added to the chunk for future rendering.</p> | ||
* @param type The block entity type to associate the given predicate with. | ||
* @param shouldRender The predicate for the block entity to evaluate. | ||
*/ | ||
<T extends BlockEntity> void addRenderPredicate(BlockEntityType<T> type, BlockEntityRenderPredicate<T> shouldRender); | ||
|
||
/** | ||
* Removes a predicate added by {@code addRenderPredicate}. <b>It must be the same object that was added.</b> | ||
* | ||
* @param type The block entity type to associate the given predicate with. | ||
* @param shouldRender The predicate to remove. | ||
* @return If the predicate existed and was removed. | ||
*/ | ||
<T extends BlockEntity> boolean removeRenderPredicate(BlockEntityType<T> type, BlockEntityRenderPredicate<T> shouldRender); | ||
} |
13 changes: 13 additions & 0 deletions
13
...n/src/api/java/net/caffeinemc/mods/sodium/api/blockentity/BlockEntityRenderPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package net.caffeinemc.mods.sodium.api.blockentity; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.level.BlockGetter; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.Experimental | ||
@ApiStatus.AvailableSince("0.6.0") | ||
@FunctionalInterface | ||
public interface BlockEntityRenderPredicate<T extends BlockEntity> { | ||
boolean shouldRender(BlockGetter blockGetter, BlockPos blockPos, T entity); | ||
} |
20 changes: 20 additions & 0 deletions
20
...ain/java/net/caffeinemc/mods/sodium/client/render/chunk/BlockEntityRenderHandlerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.caffeinemc.mods.sodium.client.render.chunk; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import net.caffeinemc.mods.sodium.api.blockentity.BlockEntityRenderHandler; | ||
import net.caffeinemc.mods.sodium.api.blockentity.BlockEntityRenderPredicate; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
|
||
public class BlockEntityRenderHandlerImpl implements BlockEntityRenderHandler { | ||
@Override | ||
public <T extends BlockEntity> void addRenderPredicate(BlockEntityType<T> type, BlockEntityRenderPredicate<T> predicate) { | ||
ExtendedBlockEntityType.addRenderPredicate(type, predicate); | ||
} | ||
|
||
@Override | ||
public <T extends BlockEntity> boolean removeRenderPredicate(BlockEntityType<T> type, BlockEntityRenderPredicate<T> predicate) { | ||
return ExtendedBlockEntityType.removeRenderPredicate(type, predicate); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/ExtendedBlockEntityType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package net.caffeinemc.mods.sodium.client.render.chunk; | ||
|
||
import net.caffeinemc.mods.sodium.api.blockentity.BlockEntityRenderPredicate; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.level.BlockGetter; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
|
||
import java.util.function.Predicate; | ||
|
||
@SuppressWarnings("unchecked") | ||
public interface ExtendedBlockEntityType<T extends BlockEntity> { | ||
BlockEntityRenderPredicate<T>[] sodium$getRenderPredicates(); | ||
|
||
void sodium$addRenderPredicate(BlockEntityRenderPredicate<T> shouldAddRenderer); | ||
|
||
boolean sodium$removeRenderPredicate(BlockEntityRenderPredicate<T> shouldAddRenderer); | ||
|
||
static <T extends BlockEntity> boolean shouldRender(BlockEntityType<? extends T> type, BlockGetter blockGetter, BlockPos blockPos, T entity) { | ||
BlockEntityRenderPredicate<T>[] predicates = ((ExtendedBlockEntityType<T>) type).sodium$getRenderPredicates(); | ||
|
||
for (int i = 0; i < predicates.length; i++) { | ||
if (!predicates[i].shouldRender(blockGetter, blockPos, entity)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
static <T extends BlockEntity> void addRenderPredicate(BlockEntityType<T> type, BlockEntityRenderPredicate<T> predicate) { | ||
((ExtendedBlockEntityType<T>) type).sodium$addRenderPredicate(predicate); | ||
} | ||
|
||
static <T extends BlockEntity> boolean removeRenderPredicate(BlockEntityType<T> type, BlockEntityRenderPredicate<T> predicate) { | ||
return ((ExtendedBlockEntityType<T>) type).sodium$removeRenderPredicate(predicate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
common/src/main/java/net/caffeinemc/mods/sodium/mixin/core/render/BlockEntityTypeMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package net.caffeinemc.mods.sodium.mixin.core.render; | ||
|
||
import java.util.function.Predicate; | ||
|
||
import net.caffeinemc.mods.sodium.api.blockentity.BlockEntityRenderPredicate; | ||
import net.caffeinemc.mods.sodium.client.render.chunk.ExtendedBlockEntityType; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
|
||
@Mixin(BlockEntityType.class) | ||
public class BlockEntityTypeMixin<T extends BlockEntity> implements ExtendedBlockEntityType<T> { | ||
@Unique | ||
private BlockEntityRenderPredicate<T>[] sodium$renderPredicates = new BlockEntityRenderPredicate[0]; | ||
|
||
@Override | ||
public BlockEntityRenderPredicate<T>[] sodium$getRenderPredicates() { | ||
return sodium$renderPredicates; | ||
} | ||
|
||
@Override | ||
public void sodium$addRenderPredicate(BlockEntityRenderPredicate<T> predicate) { | ||
sodium$renderPredicates = ArrayUtils.add(sodium$renderPredicates, predicate); | ||
} | ||
|
||
@Override | ||
public boolean sodium$removeRenderPredicate(BlockEntityRenderPredicate<T> predicate) { | ||
int index = ArrayUtils.indexOf(sodium$renderPredicates, predicate); | ||
|
||
if (index == ArrayUtils.INDEX_NOT_FOUND) { | ||
return false; | ||
} | ||
|
||
sodium$renderPredicates = ArrayUtils.remove(sodium$renderPredicates, index); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters