Skip to content

Commit

Permalink
Add support for predicates in BlockResourceCreator, make empty models…
Browse files Browse the repository at this point in the history
… prefer "best" blockstate
  • Loading branch information
Patbox committed Dec 13, 2024
1 parent 16ee7fb commit 77121f0
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
import eu.pb4.polymer.core.impl.PolymerImpl;
import eu.pb4.polymer.resourcepack.api.ResourcePackCreator;
import eu.pb4.polymer.resourcepack.impl.generation.DefaultRPBuilder;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.LeavesBlock;
import net.minecraft.block.Waterloggable;
import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
import net.minecraft.block.*;
import net.minecraft.registry.Registries;
import net.minecraft.state.property.Properties;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Predicate;

public final class BlockResourceCreator {
private static final PolymerBlockModel EMPTY = PolymerBlockModel.of(Identifier.of("polymer", "block/empty"));
Expand All @@ -46,7 +45,8 @@ public static BlockResourceCreator of(ResourcePackCreator creator) {
}

BlockResourceCreator(ResourcePackCreator creator, BlockExtBlockMapper blockMapper, Runnable onRegister) {
this.states = new EnumMap<>(DefaultModelData.USABLE_STATES);
this.states = new EnumMap<>(BlockModelType.class);
DefaultModelData.USABLE_STATES.forEach((key, value) -> this.states.put(key, new ReferenceArrayList<>(value)));
this.models = new IdentityHashMap<>(DefaultModelData.MODELS);
this.creator = creator;
this.blockMapper = blockMapper;
Expand Down Expand Up @@ -75,7 +75,24 @@ public BlockState requestEmpty(BlockModelType type) {
if (x != null) {
return x;
}
x = requestBlock(type, EMPTY);
Predicate<BlockState> predicate = null;
if (type.name().contains("TRAPDOOR")) {
predicate = b -> b.isOf(Blocks.IRON_TRAPDOOR);
} else if (type.name().contains("DOOR")) {
predicate = b -> b.isOf(Blocks.IRON_TRAPDOOR);
} else if (type == BlockModelType.VINES_BLOCK) {
predicate = b -> b.isOf(Blocks.TWISTING_VINES);
}

if (predicate != null) {
x = requestBlock(type, predicate, EMPTY);
}
if (x == null) {
x = requestBlock(type, EMPTY);
}
if (x == null) {
return null;
}
this.emptyBlocks.put(type, x);
if (!this.registeredEmpty) {
this.registeredEmpty = true;
Expand All @@ -86,12 +103,25 @@ public BlockState requestEmpty(BlockModelType type) {

@Nullable
public BlockState requestBlock(BlockModelType type, PolymerBlockModel... model) {
return requestBlock(type, x -> true, model);
}
public BlockState requestBlock(BlockModelType type, Predicate<BlockState> predicate, PolymerBlockModel... model) {
var states = this.states.get(type);
if (!states.isEmpty()) {
this.registerEvent();
var state = states.removeFirst();
BlockState state = null;
for (var s : states) {
if (predicate.test(s)) {
state = s;
break;
}
}
if (state == null) {
return null;
}
states.remove(state);
models.put(state, model);
this.hasRequested.add(state.getBlock());
this.registerEvent();

if (state.getBlock() instanceof Waterloggable) {
this.blockMapper.stateMap.put(state, DefaultModelData.SPECIAL_REMAPS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import eu.pb4.polymer.blocks.api.PolymerBlockModel;
import eu.pb4.polymer.core.impl.PolymerImpl;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
import net.minecraft.block.*;
import net.minecraft.block.enums.*;
import net.minecraft.registry.Registries;
Expand Down Expand Up @@ -37,7 +38,7 @@ public class DefaultModelData {
MODELS.put(Blocks.FARMLAND.getDefaultState().with(FarmlandBlock.MOISTURE, 7), new PolymerBlockModel[]{PolymerBlockModel.of(Identifier.of("minecraft:block/farmland_moist"))});


var list = new ArrayList<BlockState>();
var list = new ReferenceArrayList<BlockState>();
for (int i = 2; i < 7; i++) {
var state = Blocks.FARMLAND.getDefaultState().with(FarmlandBlock.MOISTURE, i);
list.add(state);
Expand All @@ -48,7 +49,7 @@ public class DefaultModelData {
}

{
var vines = new ArrayList<BlockState>();
var vines = new ReferenceArrayList<BlockState>();

for (var block : new Block[]{Blocks.TWISTING_VINES, Blocks.WEEPING_VINES}) {
var id = Registries.BLOCK.getId(block);
Expand Down Expand Up @@ -81,7 +82,7 @@ public class DefaultModelData {


{
var plant = new ArrayList<BlockState>();
var plant = new ReferenceArrayList<BlockState>();

{
var id = Registries.BLOCK.getId(Blocks.SUGAR_CANE);
Expand All @@ -98,7 +99,7 @@ public class DefaultModelData {
}

{
var plant = new ArrayList<BlockState>();
var plant = new ReferenceArrayList<BlockState>();

for (var block : new Block[]{Blocks.OAK_SAPLING, Blocks.BIRCH_SAPLING, Blocks.SPRUCE_SAPLING, Blocks.JUNGLE_SAPLING, Blocks.ACACIA_SAPLING, Blocks.DARK_OAK_SAPLING, Blocks.CHERRY_SAPLING}) {
var id = Registries.BLOCK.getId(block);
Expand Down Expand Up @@ -169,7 +170,7 @@ public class DefaultModelData {

{
{
List<BlockState> list = new ObjectArrayList<>();
List<BlockState> list = new ReferenceArrayList<>();
addDoor(Direction.NORTH, DoorHinge.LEFT, DoubleBlockHalf.UPPER, false, list);
addDoor(Direction.NORTH, DoorHinge.LEFT, DoubleBlockHalf.LOWER, false, list);
addDoor(Direction.NORTH, DoorHinge.RIGHT, DoubleBlockHalf.UPPER, false, list);
Expand All @@ -181,7 +182,7 @@ public class DefaultModelData {
DefaultModelData.USABLE_STATES.put(BlockModelType.NORTH_DOOR, list);
}
{
List<BlockState> list = new ObjectArrayList<>();
List<BlockState> list = new ReferenceArrayList<>();
addDoor(Direction.EAST, DoorHinge.LEFT, DoubleBlockHalf.UPPER, false, list);
addDoor(Direction.EAST, DoorHinge.LEFT, DoubleBlockHalf.LOWER, false, list);
addDoor(Direction.EAST, DoorHinge.RIGHT, DoubleBlockHalf.UPPER, false, list);
Expand All @@ -193,7 +194,7 @@ public class DefaultModelData {
DefaultModelData.USABLE_STATES.put(BlockModelType.EAST_DOOR, list);
}
{
List<BlockState> list = new ObjectArrayList<>();
List<BlockState> list = new ReferenceArrayList<>();
addDoor(Direction.SOUTH, DoorHinge.LEFT, DoubleBlockHalf.UPPER, false, list);
addDoor(Direction.SOUTH, DoorHinge.LEFT, DoubleBlockHalf.LOWER, false, list);
addDoor(Direction.SOUTH, DoorHinge.RIGHT, DoubleBlockHalf.UPPER, false, list);
Expand All @@ -205,7 +206,7 @@ public class DefaultModelData {
DefaultModelData.USABLE_STATES.put(BlockModelType.SOUTH_DOOR, list);
}
{
List<BlockState> list = new ObjectArrayList<>();
List<BlockState> list = new ReferenceArrayList<>();
addDoor(Direction.WEST, DoorHinge.LEFT, DoubleBlockHalf.UPPER, false, list);
addDoor(Direction.WEST, DoorHinge.LEFT, DoubleBlockHalf.LOWER, false, list);
addDoor(Direction.WEST, DoorHinge.RIGHT, DoubleBlockHalf.UPPER, false, list);
Expand All @@ -220,12 +221,12 @@ public class DefaultModelData {

{
{
List<BlockState> list = new ObjectArrayList<>();
List<BlockState> list = new ReferenceArrayList<>();
addSculkBlocks(false, list);
DefaultModelData.USABLE_STATES.put(BlockModelType.SCULK_SENSOR_BLOCK, list);
}
{
List<BlockState> list = new ObjectArrayList<>();
List<BlockState> list = new ReferenceArrayList<>();
addSculkBlocks(true, list);
DefaultModelData.USABLE_STATES.put(BlockModelType.SCULK_SENSOR_BLOCK_WATERLOGGED, list);
}
Expand Down Expand Up @@ -312,36 +313,36 @@ private static BlockState addSingleDoor(Block block, Block replacement, Directio
}

private static void addTrapdoorHalf(Direction facing, BlockHalf half, boolean waterlogged, BlockModelType modelType) {
ObjectArrayList<BlockState> list = new ObjectArrayList<>();
ReferenceArrayList<BlockState> list = new ReferenceArrayList<>();
list.add(addSingleClosedTrapdoor(Blocks.COPPER_TRAPDOOR, Blocks.WAXED_COPPER_TRAPDOOR, facing, half, waterlogged));
list.add(addSingleClosedTrapdoor(Blocks.EXPOSED_COPPER_TRAPDOOR, Blocks.WAXED_EXPOSED_COPPER_TRAPDOOR, facing, half, waterlogged));
list.add(addSingleClosedTrapdoor(Blocks.WEATHERED_COPPER_TRAPDOOR, Blocks.WAXED_WEATHERED_COPPER_TRAPDOOR, facing, half, waterlogged));
list.add(addSingleClosedTrapdoor(Blocks.OXIDIZED_COPPER_TRAPDOOR, Blocks.WAXED_OXIDIZED_COPPER_TRAPDOOR, facing, half, waterlogged));

list.add(addSinglePoweredOpenTrapdoor(Blocks.ACACIA_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.BAMBOO_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.BIRCH_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.CHERRY_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.CRIMSON_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.DARK_OAK_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.JUNGLE_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.MANGROVE_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.OAK_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.SPRUCE_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.WARPED_TRAPDOOR, facing, half, waterlogged));

list.add(addSinglePoweredOpenTrapdoor(Blocks.IRON_TRAPDOOR, facing, half, waterlogged));

list.add(addSinglePoweredOpenTrapdoor(Blocks.WAXED_COPPER_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.WAXED_EXPOSED_COPPER_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.WAXED_WEATHERED_COPPER_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredOpenTrapdoor(Blocks.WAXED_OXIDIZED_COPPER_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.ACACIA_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.BAMBOO_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.BIRCH_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.CHERRY_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.CRIMSON_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.DARK_OAK_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.JUNGLE_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.MANGROVE_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.OAK_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.SPRUCE_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.WARPED_TRAPDOOR, facing, half, waterlogged));

list.add(addSinglePoweredClosedTrapdoor(Blocks.IRON_TRAPDOOR, facing, half, waterlogged));

list.add(addSinglePoweredClosedTrapdoor(Blocks.WAXED_COPPER_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.WAXED_EXPOSED_COPPER_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.WAXED_WEATHERED_COPPER_TRAPDOOR, facing, half, waterlogged));
list.add(addSinglePoweredClosedTrapdoor(Blocks.WAXED_OXIDIZED_COPPER_TRAPDOOR, facing, half, waterlogged));

DefaultModelData.USABLE_STATES.put(modelType, list);
}

private static void addTrapdoorDirection(Direction facing, BlockHalf half, boolean waterlogged, BlockModelType modelType) {
ObjectArrayList<BlockState> list = new ObjectArrayList<>();
ReferenceArrayList<BlockState> list = new ReferenceArrayList<>();

list.add(addSingleOpenTrapdoor(Blocks.COPPER_TRAPDOOR, Blocks.WAXED_COPPER_TRAPDOOR, facing, half, waterlogged));
list.add(addSingleOpenTrapdoor(Blocks.EXPOSED_COPPER_TRAPDOOR, Blocks.WAXED_EXPOSED_COPPER_TRAPDOOR, facing, half, waterlogged));
Expand Down Expand Up @@ -399,7 +400,7 @@ private static BlockState addSinglePoweredClosedTrapdoor(Block block, Direction
}

private static void addSlabs(SlabType slabType, boolean waterlogged, BlockModelType modelType) {
ObjectArrayList<BlockState> list = new ObjectArrayList<>();
ReferenceArrayList<BlockState> list = new ReferenceArrayList<>();

addSlab(slabType, waterlogged, Blocks.OAK_SLAB, Blocks.PETRIFIED_OAK_SLAB, list);
addSlab(slabType, waterlogged, Blocks.CUT_COPPER_SLAB, Blocks.WAXED_CUT_COPPER_SLAB, list);
Expand All @@ -410,14 +411,14 @@ private static void addSlabs(SlabType slabType, boolean waterlogged, BlockModelT
DefaultModelData.USABLE_STATES.put(modelType, list);
}

private static void addSlab(SlabType slabType, boolean waterlogged, Block to, Block from, ObjectArrayList<BlockState> list) {
private static void addSlab(SlabType slabType, boolean waterlogged, Block to, Block from, ReferenceArrayList<BlockState> list) {
BlockState state = from.getDefaultState().with(SlabBlock.WATERLOGGED, waterlogged).with(SlabBlock.TYPE, slabType);
list.add(state);
DefaultModelData.SPECIAL_REMAPS.put(state, to.getStateWithProperties(state));
}

private static void addDisarmedTripwire(boolean attached, BlockModelType modelType) {
ObjectArrayList<BlockState> list = new ObjectArrayList<>();
ReferenceArrayList<BlockState> list = new ReferenceArrayList<>();
// generate all permutations of north, south, east, west, powered
{
var base = Blocks.TRIPWIRE.getDefaultState().with(TripwireBlock.DISARMED, true);
Expand Down Expand Up @@ -451,7 +452,7 @@ private static void generateDefault(BlockModelType type, Block... blocks) {
}

private static void generateDefault(BlockModelType type, Predicate<BlockState> shouldInclude, Block... blocks) {
var list = new ArrayList<BlockState>();
var list = new ReferenceArrayList<BlockState>();

for (var block : blocks) {
var id = Registries.BLOCK.getId(block);
Expand Down
47 changes: 47 additions & 0 deletions polymer-core/src/testmod/java/eu/pb4/polymertest/DynamicItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package eu.pb4.polymertest;

import eu.pb4.polymer.core.api.item.PolymerItem;
import eu.pb4.polymer.core.api.item.SimplePolymerItem;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.ConsumableComponent;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.Items;
import net.minecraft.item.consume.UseAction;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.registry.Registries;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.Rarity;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.List;

public class DynamicItem extends Item implements PolymerItem {
public DynamicItem(Settings settings) {
super(settings);
}

@Override
public Identifier getPolymerItemModel(ItemStack stack, PacketContext context) {
return null;
}

@Override
public ActionResult use(World world, PlayerEntity user, Hand hand) {
user.sendMessage(Text.literal("Used!" + hand), false);
return super.use(world, user, hand);
}

@Override
public Item getPolymerItem(ItemStack itemStack, PacketContext context) {
return itemStack.getOrDefault(TestMod.CLIENT_ITEM, Items.STICK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void accept(ItemGroup.DisplayContext arg, ItemGroup.Entries entries) {
public static RegistryEntry<EntityAttribute> ATTRIBUTE = Registry.registerReference(Registries.ATTRIBUTE, Identifier.of("test:attribute"),
new ClampedEntityAttribute("test.attribute", 0, -5, 5)
.setCategory(EntityAttribute.Category.POSITIVE).setTracked(true));
public static DynamicItem DYNAMIC_ITEM = registerItem(Identifier.of("test", "dynamic"), DynamicItem::new);
public static SimplePolymerItem ITEM = registerItem(Identifier.of("test", "item"), (s) -> new TestItem(s.fireproof().maxCount(5), Items.IRON_HOE));
public static SimplePolymerItem ITEM_2 = registerItem(Identifier.of("test", "item_2"), (s) -> new SimplePolymerItem(s.fireproof().maxCount(99)
.attributeModifiers(AttributeModifiersComponent.builder().add(ATTRIBUTE,
Expand Down Expand Up @@ -200,6 +201,10 @@ public ActionResult useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity
public static final ComponentType<String> TEST = register(Registries.DATA_COMPONENT_TYPE, Identifier.of("test", "test"),
ComponentType.<String>builder().codec(Codec.STRING).build());

public static final ComponentType<Item> CLIENT_ITEM = register(Registries.DATA_COMPONENT_TYPE, Identifier.of("test", "item"),
ComponentType.<Item>builder().codec(Registries.ITEM.getCodec()).build());


//public static final SoundEvent GHOST_HURT = new PolymerSoundEvent(PolymerResourcePackUtils.getMainUuid(), Identifier.of("polymertest", "ghosthurt"), 16, true, SoundEvents.ENTITY_GHAST_HURT);


Expand Down Expand Up @@ -511,7 +516,7 @@ public void onInitialize() {
PolymerItemUtils.syncDefaultComponent(Items.DIAMOND, DataComponentTypes.MAX_STACK_SIZE);
PolymerItemUtils.syncDefaultComponent(Items.CHAINMAIL_HELMET, DataComponentTypes.EQUIPPABLE);

PolymerComponent.registerDataComponent(TEST);
PolymerComponent.registerDataComponent(TEST, CLIENT_ITEM);

if (PolymerImpl.IS_CLIENT) {
InternalClientRegistry.decodeState(-1);
Expand Down

0 comments on commit 77121f0

Please sign in to comment.