From aa102dfabd8af4e4c5694354fad475b1c32b8394 Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Fri, 12 May 2023 10:55:03 -0700 Subject: [PATCH 1/2] Filtering for filters - Allow filtering for filters when filter is empty - Refine waterwheel model generation code --- .../waterwheel/WaterWheelInstance.java | 1 - .../waterwheel/WaterWheelRenderer.java | 64 +++++++++++-------- .../logistics/item/filter/FilterItem.java | 34 ++++++++-- .../logistics/item/filter/ItemAttribute.java | 2 + 4 files changed, 67 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/waterwheel/WaterWheelInstance.java b/src/main/java/com/simibubi/create/content/contraptions/components/waterwheel/WaterWheelInstance.java index f125d09c2..8fac23bb8 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/waterwheel/WaterWheelInstance.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/waterwheel/WaterWheelInstance.java @@ -42,7 +42,6 @@ public class WaterWheelInstance extends CutoutR return getRotatingMaterial().model(key, () -> { BakedModel model = WaterWheelRenderer.generateModel(key); BlockState state = key.state(); - // TODO waterwheels Direction dir; if (key.large()) { dir = Direction.fromAxisAndDirection(state.getValue(LargeWaterWheelBlock.AXIS), AxisDirection.POSITIVE); diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/waterwheel/WaterWheelRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/waterwheel/WaterWheelRenderer.java index f93835d60..2c8c1405c 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/waterwheel/WaterWheelRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/waterwheel/WaterWheelRenderer.java @@ -5,7 +5,6 @@ import java.util.Map; import java.util.Optional; import java.util.Random; -import com.jozufozu.flywheel.core.PartialModel; import com.jozufozu.flywheel.core.StitchedSprite; import com.mojang.blaze3d.vertex.PoseStack; import com.simibubi.create.AllPartialModels; @@ -31,6 +30,7 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockState; +import net.minecraftforge.client.model.data.EmptyModelData; import net.minecraftforge.registries.ForgeRegistries; public class WaterWheelRenderer extends KineticBlockEntityRenderer { @@ -40,6 +40,8 @@ public class WaterWheelRenderer extends Kinetic public static final StitchedSprite OAK_LOG_TEMPLATE = new StitchedSprite(new ResourceLocation("block/oak_log")); public static final StitchedSprite OAK_LOG_TOP_TEMPLATE = new StitchedSprite(new ResourceLocation("block/oak_log_top")); + private static final String[] LOG_SUFFIXES = new String[] { "_log", "_stem" }; + protected final boolean large; public WaterWheelRenderer(Context context, boolean large) { @@ -59,9 +61,8 @@ public class WaterWheelRenderer extends Kinetic protected SuperByteBuffer getRotatedModel(T be, BlockState state) { WaterWheelModelKey key = new WaterWheelModelKey(large, state, be.material); return CreateClient.BUFFER_CACHE.get(WATER_WHEEL, key, () -> { - BakedModel model = WaterWheelRenderer.generateModel(key); + BakedModel model = generateModel(key); BlockState state1 = key.state(); - // TODO waterwheels Direction dir; if (key.large()) { dir = Direction.fromAxisAndDirection(state1.getValue(LargeWaterWheelBlock.AXIS), AxisDirection.POSITIVE); @@ -73,25 +74,24 @@ public class WaterWheelRenderer extends Kinetic }); } - public static PartialModel getTemplateModel(boolean large, boolean extension) { - if (large) { + public static BakedModel generateModel(WaterWheelModelKey key) { + BakedModel template; + if (key.large()) { + boolean extension = key.state() + .getValue(LargeWaterWheelBlock.EXTENSION); if (extension) { - return AllPartialModels.LARGE_WATER_WHEEL_EXTENSION; + template = AllPartialModels.LARGE_WATER_WHEEL_EXTENSION.get(); } else { - return AllPartialModels.LARGE_WATER_WHEEL; + template = AllPartialModels.LARGE_WATER_WHEEL.get(); } } else { - return AllPartialModels.WATER_WHEEL; + template = AllPartialModels.WATER_WHEEL.get(); } + + return generateModel(template, key.material()); } - public static BakedModel generateModel(WaterWheelModelKey key) { - boolean extension = key.state() - .getOptionalValue(LargeWaterWheelBlock.EXTENSION) - .orElse(false); - BakedModel template = getTemplateModel(key.large(), extension).get(); - - BlockState planksBlockState = key.material(); + public static BakedModel generateModel(BakedModel template, BlockState planksBlockState) { Block planksBlock = planksBlockState.getBlock(); ResourceLocation id = RegisteredObjects.getKeyOrThrow(planksBlock); String path = id.getPath(); @@ -103,7 +103,7 @@ public class WaterWheelRenderer extends Kinetic Map map = new Reference2ReferenceOpenHashMap<>(); map.put(OAK_PLANKS_TEMPLATE.get(), getSpriteOnSide(planksBlockState, Direction.UP)); - map.put(OAK_LOG_TEMPLATE.get(), getSpriteOnSide(logBlockState, Direction.NORTH)); + map.put(OAK_LOG_TEMPLATE.get(), getSpriteOnSide(logBlockState, Direction.SOUTH)); map.put(OAK_LOG_TOP_TEMPLATE.get(), getSpriteOnSide(logBlockState, Direction.UP)); return BakedModelHelper.generateModel(template, map::get); @@ -113,7 +113,7 @@ public class WaterWheelRenderer extends Kinetic } private static BlockState getLogBlockState(String namespace, String wood) { - for (String suffix : new String[] { "_log", "_stem" }) { + for (String suffix : LOG_SUFFIXES) { Optional state = ForgeRegistries.BLOCKS.getHolder(new ResourceLocation(namespace, wood + suffix)) .map(Holder::value) @@ -124,18 +124,28 @@ public class WaterWheelRenderer extends Kinetic return Blocks.OAK_LOG.defaultBlockState(); } - private static TextureAtlasSprite getSpriteOnSide(BlockState blockstate, Direction side) { - BakedModel blockModel = Minecraft.getInstance() + private static TextureAtlasSprite getSpriteOnSide(BlockState state, Direction side) { + BakedModel model = Minecraft.getInstance() .getBlockRenderer() - .getBlockModel(blockstate); - if (blockModel == null) + .getBlockModel(state); + if (model == null) return null; - @SuppressWarnings("deprecation") - List quads = blockModel.getQuads(blockstate, side, new Random()); - if (quads.isEmpty()) - return null; - return quads.get(0) - .getSprite(); + Random random = new Random(42L); + List quads = model.getQuads(state, side, random, EmptyModelData.INSTANCE); + if (!quads.isEmpty()) { + return quads.get(0) + .getSprite(); + } + random.setSeed(42L); + quads = model.getQuads(state, null, random, EmptyModelData.INSTANCE); + if (!quads.isEmpty()) { + for (BakedQuad quad : quads) { + if (quad.getDirection() == side) { + return quad.getSprite(); + } + } + } + return model.getParticleIcon(EmptyModelData.INSTANCE); } } diff --git a/src/main/java/com/simibubi/create/content/logistics/item/filter/FilterItem.java b/src/main/java/com/simibubi/create/content/logistics/item/filter/FilterItem.java index 2c1a56730..b7f134fb4 100644 --- a/src/main/java/com/simibubi/create/content/logistics/item/filter/FilterItem.java +++ b/src/main/java/com/simibubi/create/content/logistics/item/filter/FilterItem.java @@ -127,6 +127,8 @@ public class FilterItem extends Item implements MenuProvider { for (Tag inbt : attributes) { CompoundTag compound = (CompoundTag) inbt; ItemAttribute attribute = ItemAttribute.fromNBT(compound); + if (attribute == null) + continue; boolean inverted = compound.getBoolean("Inverted"); if (count > 3) { list.add(Components.literal("- ...") @@ -194,15 +196,19 @@ public class FilterItem extends Item implements MenuProvider { return test(world, stack, filter, true); } - private static boolean test(Level world, ItemStack stack, ItemStack filter, boolean matchNBT) { + public static boolean test(Level world, ItemStack stack, ItemStack filter, boolean matchNBT) { if (filter.isEmpty()) return true; if (!(filter.getItem() instanceof FilterItem)) - return (matchNBT ? ItemHandlerHelper.canItemStacksStack(filter, stack) : ItemStack.isSame(filter, stack)); + return testDirect(stack, filter, matchNBT); boolean defaults = !filter.hasTag(); + if (defaults) { + return testDirect(filter, stack, matchNBT); + } + if (AllItems.FILTER.get() == filter.getItem()) { ItemStackHandler filterItems = getFilterItems(filter); boolean respectNBT = defaults ? false @@ -211,24 +217,32 @@ public class FilterItem extends Item implements MenuProvider { boolean blacklist = defaults ? false : filter.getTag() .getBoolean("Blacklist"); + boolean isEmpty = true; for (int slot = 0; slot < filterItems.getSlots(); slot++) { ItemStack stackInSlot = filterItems.getStackInSlot(slot); if (stackInSlot.isEmpty()) continue; + isEmpty = false; boolean matches = test(world, stack, stackInSlot, respectNBT); if (matches) return !blacklist; } + if (isEmpty) { + return testDirect(filter, stack, matchNBT); + } return blacklist; } if (AllItems.ATTRIBUTE_FILTER.get() == filter.getItem()) { - WhitelistMode whitelistMode = WhitelistMode.values()[defaults ? 0 - : filter.getTag() - .getInt("WhitelistMode")]; ListTag attributes = defaults ? new ListTag() : filter.getTag() .getList("MatchedAttributes", Tag.TAG_COMPOUND); + if (attributes.isEmpty()) { + return testDirect(filter, stack, matchNBT); + } + WhitelistMode whitelistMode = WhitelistMode.values()[defaults ? 0 + : filter.getTag() + .getInt("WhitelistMode")]; for (Tag inbt : attributes) { CompoundTag compound = (CompoundTag) inbt; ItemAttribute attribute = ItemAttribute.fromNBT(compound); @@ -270,7 +284,7 @@ public class FilterItem extends Item implements MenuProvider { return false; } - private static boolean test(Level world, FluidStack stack, ItemStack filter, boolean matchNBT) { + public static boolean test(Level world, FluidStack stack, ItemStack filter, boolean matchNBT) { if (filter.isEmpty()) return true; if (stack.isEmpty()) @@ -313,4 +327,12 @@ public class FilterItem extends Item implements MenuProvider { return false; } + private static boolean testDirect(ItemStack filter, ItemStack stack, boolean matchNBT) { + if (matchNBT) { + return ItemHandlerHelper.canItemStacksStack(filter, stack); + } else { + return ItemStack.isSame(filter, stack); + } + } + } diff --git a/src/main/java/com/simibubi/create/content/logistics/item/filter/ItemAttribute.java b/src/main/java/com/simibubi/create/content/logistics/item/filter/ItemAttribute.java index be6b26340..7e8939102 100644 --- a/src/main/java/com/simibubi/create/content/logistics/item/filter/ItemAttribute.java +++ b/src/main/java/com/simibubi/create/content/logistics/item/filter/ItemAttribute.java @@ -10,6 +10,7 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.Nullable; import com.simibubi.create.AllRecipeTypes; import com.simibubi.create.content.contraptions.processing.InWorldProcessing; @@ -78,6 +79,7 @@ public interface ItemAttribute { return attributeType; } + @Nullable static ItemAttribute fromNBT(CompoundTag nbt) { for (ItemAttribute itemAttribute : types) if (itemAttribute.canRead(nbt)) From 0fa0bbaf5d1c346cca2723b6a99fc6b3f9ca3d89 Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Fri, 12 May 2023 11:00:54 -0700 Subject: [PATCH 2/2] Unswap parameters to make sense --- .../create/content/logistics/item/filter/FilterItem.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/simibubi/create/content/logistics/item/filter/FilterItem.java b/src/main/java/com/simibubi/create/content/logistics/item/filter/FilterItem.java index b7f134fb4..6b9754b3b 100644 --- a/src/main/java/com/simibubi/create/content/logistics/item/filter/FilterItem.java +++ b/src/main/java/com/simibubi/create/content/logistics/item/filter/FilterItem.java @@ -201,7 +201,7 @@ public class FilterItem extends Item implements MenuProvider { return true; if (!(filter.getItem() instanceof FilterItem)) - return testDirect(stack, filter, matchNBT); + return testDirect(filter, stack, matchNBT); boolean defaults = !filter.hasTag();