Tag Provider, list to array function, surface builder fix
This commit is contained in:
parent
f8eb65d600
commit
7f17e1261c
6 changed files with 52 additions and 8 deletions
|
@ -6,6 +6,8 @@ import net.fabricmc.api.Environment;
|
|||
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
|
||||
import net.minecraft.client.renderer.RenderType;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.tags.Tag;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import ru.bclib.api.biomes.BiomeAPI;
|
||||
import ru.bclib.blocks.BaseBarrelBlock;
|
||||
|
@ -17,13 +19,17 @@ import ru.bclib.client.render.BaseChestBlockEntityRenderer;
|
|||
import ru.bclib.client.render.BaseSignBlockEntityRenderer;
|
||||
import ru.bclib.interfaces.PostInitable;
|
||||
import ru.bclib.interfaces.RenderLayerProvider;
|
||||
import ru.bclib.interfaces.TagProvider;
|
||||
import ru.bclib.registry.BaseBlockEntities;
|
||||
import ru.bclib.util.CollectionsUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class PostInitAPI {
|
||||
private static List<Consumer<Boolean>> postInitFunctions = Lists.newArrayList();
|
||||
private static List<Tag.Named<Block>> blockTags = Lists.newArrayList();
|
||||
private static List<Tag.Named<Item>> itemTags = Lists.newArrayList();
|
||||
|
||||
/**
|
||||
* Register a new function which will be called after all mods are initiated. Will be called on both client and server.
|
||||
|
@ -49,6 +55,8 @@ public class PostInitAPI {
|
|||
}
|
||||
});
|
||||
postInitFunctions = null;
|
||||
blockTags = null;
|
||||
itemTags = null;
|
||||
BiomeAPI.loadFabricAPIBiomes();
|
||||
}
|
||||
|
||||
|
@ -83,5 +91,12 @@ public class PostInitAPI {
|
|||
else if (block instanceof BaseFurnaceBlock) {
|
||||
BaseBlockEntities.FURNACE.registerBlock(block);
|
||||
}
|
||||
if (block instanceof TagProvider) {
|
||||
TagProvider.class.cast(block).addTags(blockTags, itemTags);
|
||||
TagAPI.addTags(block, CollectionsUtil.toArray(blockTags));
|
||||
TagAPI.addTags(block, CollectionsUtil.toArray(itemTags));
|
||||
blockTags.clear();
|
||||
itemTags.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -439,7 +439,7 @@ public class BiomeAPI {
|
|||
SurfaceRuleProvider provider = SurfaceRuleProvider.class.cast(generator);
|
||||
if (rules.size() > 0) {
|
||||
rules.add(provider.getSurfaceRule());
|
||||
provider.setSurfaceRule(SurfaceRules.sequence(rules.toArray(new SurfaceRules.RuleSource[rules.size()])));
|
||||
provider.setSurfaceRule(SurfaceRules.sequence(CollectionsUtil.toArray(rules)));
|
||||
}
|
||||
else {
|
||||
provider.setSurfaceRule(null);
|
||||
|
|
|
@ -8,6 +8,7 @@ import net.minecraft.world.level.block.state.BlockState;
|
|||
import net.minecraft.world.level.levelgen.SurfaceRules;
|
||||
import net.minecraft.world.level.levelgen.SurfaceRules.RuleSource;
|
||||
import net.minecraft.world.level.levelgen.placement.CaveSurface;
|
||||
import ru.bclib.util.CollectionsUtil;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
@ -129,7 +130,8 @@ public class SurfaceRuleBuilder {
|
|||
*/
|
||||
public SurfaceRules.RuleSource build() {
|
||||
Collections.sort(rules);
|
||||
SurfaceRules.RuleSource[] ruleArray = rules.toArray(new SurfaceRules.RuleSource[rules.size()]);
|
||||
List<SurfaceRules.RuleSource> ruleList = rules.stream().map(entry -> entry.getRule()).toList();
|
||||
SurfaceRules.RuleSource[] ruleArray = CollectionsUtil.toArray(ruleList);
|
||||
SurfaceRules.RuleSource rule = SurfaceRules.sequence(ruleArray);
|
||||
if (biomeKey != null) {
|
||||
rule = SurfaceRules.ifTrue(SurfaceRules.isBiome(biomeKey), rule);
|
||||
|
|
12
src/main/java/ru/bclib/interfaces/TagProvider.java
Normal file
12
src/main/java/ru/bclib/interfaces/TagProvider.java
Normal file
|
@ -0,0 +1,12 @@
|
|||
package ru.bclib.interfaces;
|
||||
|
||||
import net.minecraft.tags.Tag;
|
||||
import net.minecraft.tags.Tag.Named;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TagProvider {
|
||||
void addTags(List<Named<Block>> blockTags, List<Tag.Named<Item>> itemTags);
|
||||
}
|
|
@ -2,14 +2,14 @@ package ru.bclib.registry;
|
|||
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.fabricmc.fabric.api.client.rendereregistry.v1.BlockEntityRendererRegistry;
|
||||
import net.fabricmc.fabric.api.client.rendering.v1.BlockEntityRendererRegistry;
|
||||
import ru.bclib.client.render.BaseChestBlockEntityRenderer;
|
||||
import ru.bclib.client.render.BaseSignBlockEntityRenderer;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class BaseBlockEntityRenders {
|
||||
public static void register() {
|
||||
BlockEntityRendererRegistry.INSTANCE.register(BaseBlockEntities.CHEST, BaseChestBlockEntityRenderer::new);
|
||||
BlockEntityRendererRegistry.INSTANCE.register(BaseBlockEntities.SIGN, BaseSignBlockEntityRenderer::new);
|
||||
BlockEntityRendererRegistry.register(BaseBlockEntities.CHEST, BaseChestBlockEntityRenderer::new);
|
||||
BlockEntityRendererRegistry.register(BaseBlockEntities.SIGN, BaseSignBlockEntityRenderer::new);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package ru.bclib.util;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
@ -13,7 +14,7 @@ public class CollectionsUtil {
|
|||
* @param list {@link List} to make mutable.
|
||||
* @return {@link ArrayList} or original {@link List} if it is mutable.
|
||||
*/
|
||||
public static <E extends Object> List<E> getMutable(List<E> list) {
|
||||
public static <E> List<E> getMutable(List<E> list) {
|
||||
if (list instanceof ArrayList) {
|
||||
return list;
|
||||
}
|
||||
|
@ -25,7 +26,7 @@ public class CollectionsUtil {
|
|||
* @param set {@link Set} to make mutable.
|
||||
* @return {@link HashSet} or original {@link Set} if it is mutable.
|
||||
*/
|
||||
public static <E extends Object> Set<E> getMutable(Set<E> set) {
|
||||
public static <E> Set<E> getMutable(Set<E> set) {
|
||||
if (set instanceof HashSet) {
|
||||
return set;
|
||||
}
|
||||
|
@ -37,10 +38,24 @@ public class CollectionsUtil {
|
|||
* @param map {@link Map} to make mutable.
|
||||
* @return {@link HashMap} or original {@link Map} if it is mutable.
|
||||
*/
|
||||
public static <K extends Object, V extends Object> Map<K, V> getMutable(Map<K, V> map) {
|
||||
public static <K, V> Map<K, V> getMutable(Map<K, V> map) {
|
||||
if (map instanceof HashMap) {
|
||||
return map;
|
||||
}
|
||||
return new HashMap<>(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts list into array.
|
||||
* @param list {@link List} to convert.
|
||||
* @return array of list elements. If list is empty will return empty {@link Object} array.
|
||||
*/
|
||||
public static <E> E[] toArray(List<E> list) {
|
||||
if (list.isEmpty()) {
|
||||
return (E[]) new Object[0];
|
||||
}
|
||||
E[] result = (E[]) Array.newInstance(list.get(0).getClass(), list.size());
|
||||
result = list.toArray(result);
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue