Skip to content

Commit

Permalink
Moved LatexCoveredBlocks -> LatexCoveredBlockRenderer, certain code f…
Browse files Browse the repository at this point in the history
…rom AbstractLatexItem -> LatexCoveredBlocks. Removed block state `neighbors` direct accessor, hopefully fixing FerriteCore issue. #1177
  • Loading branch information
LtxProgrammer committed Nov 20, 2024
1 parent e05062d commit ab41897
Show file tree
Hide file tree
Showing 23 changed files with 216 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.ltxprogrammer.changed.entity.variant.TransfurVariantInstance;
import net.ltxprogrammer.changed.init.ChangedGameRules;
import net.ltxprogrammer.changed.init.ChangedItems;
import net.ltxprogrammer.changed.item.AbstractLatexItem;
import net.ltxprogrammer.changed.process.LatexCoveredBlocks;
import net.ltxprogrammer.changed.process.ProcessTransfur;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -52,7 +52,6 @@ public static LatexType getLatexed(BlockState blockState) {
return LatexType.NEUTRAL;
}


public AbstractLatexBlock(Properties p_49795_, LatexType latexType, Supplier<? extends Item> goo) {
super(p_49795_.randomTicks().dynamicShape());
this.latexType = latexType;
Expand Down Expand Up @@ -168,7 +167,7 @@ public static void randomTick(@NotNull BlockState state, @NotNull ServerLevel le
if (Arrays.stream(Direction.values()).noneMatch(direction -> isValidSurface(level, checkPos, checkPos.relative(direction), direction)))
return;

var event = new AbstractLatexItem.CoveringBlockEvent(latexType, checkState, checkPos, level);
var event = new LatexCoveredBlocks.CoveringBlockEvent(latexType, checkState, checkPos, level);
if (Changed.postModEvent(event))
return;
if (event.originalState == event.plannedState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.ltxprogrammer.changed.init.ChangedGameRules;
import net.ltxprogrammer.changed.init.ChangedItems;
import net.ltxprogrammer.changed.init.ChangedTags;
import net.ltxprogrammer.changed.item.AbstractLatexItem;
import net.ltxprogrammer.changed.process.LatexCoveredBlocks;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -70,7 +70,7 @@ public void latexTick(@NotNull BlockState state, @NotNull ServerLevel level, @No
}

@SubscribeEvent
public static void onLatexCover(AbstractLatexItem.CoveringBlockEvent event) {
public static void onLatexCover(LatexCoveredBlocks.CoveringBlockEvent event) {
if (event.latexType != LatexType.DARK_LATEX)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import net.ltxprogrammer.changed.entity.variant.TransfurVariant;
import net.ltxprogrammer.changed.entity.variant.TransfurVariantInstance;
import net.ltxprogrammer.changed.init.*;
import net.ltxprogrammer.changed.item.AbstractLatexItem;
import net.ltxprogrammer.changed.process.LatexCoveredBlocks;
import net.ltxprogrammer.changed.process.ProcessTransfur;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -150,7 +150,7 @@ public void latexTick(@NotNull BlockState state, @NotNull ServerLevel level, @No
);

@SubscribeEvent
public static void onLatexCover(AbstractLatexItem.CoveringBlockEvent event) {
public static void onLatexCover(LatexCoveredBlocks.CoveringBlockEvent event) {
if (event.latexType != LatexType.WHITE_LATEX)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
public class BlockRenderHelper {
public static boolean canBlockRenderAsSolid(BlockState blockState) {
return ItemBlockRenderTypes.canRenderInLayer(blockState, RenderType.solid()) ||
ItemBlockRenderTypes.canRenderInLayer(blockState, LatexCoveredBlocks.latexSolid());
ItemBlockRenderTypes.canRenderInLayer(blockState, LatexCoveredBlockRenderer.latexSolid());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.ltxprogrammer.changed.data.RegistryElementPredicate;
import net.ltxprogrammer.changed.entity.LatexType;
import net.ltxprogrammer.changed.item.AbstractLatexItem;
import net.ltxprogrammer.changed.process.LatexCoveredBlocks;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GameRenderer;
Expand All @@ -33,7 +34,6 @@
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.resources.metadata.animation.AnimationMetadataSection;
import net.minecraft.client.resources.model.*;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
Expand All @@ -45,7 +45,6 @@
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.event.RegisterClientReloadListenersEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
Expand All @@ -65,7 +64,7 @@

import static net.ltxprogrammer.changed.block.AbstractLatexBlock.COVERED;

public abstract class LatexCoveredBlocks {
public abstract class LatexCoveredBlockRenderer {
public static final Logger LOGGER = LogUtils.getLogger();
private static final ImmutableMap<LatexType, MixedTexture.OverlayBlock> TYPE_OVERLAY = ImmutableMap.of(
LatexType.DARK_LATEX, new MixedTexture.OverlayBlock(
Expand Down Expand Up @@ -345,13 +344,13 @@ private static ResourceLocation getDefaultLatexCover(LatexType type) {
return Changed.modResource("builtin/" + type.getSerializedName());
}

private static final Function<LatexType, ResourceLocation> getDefaultLatexCoverCached = Util.memoize(LatexCoveredBlocks::getDefaultLatexCover);
private static final Function<LatexType, ResourceLocation> getDefaultLatexCoverCached = Util.memoize(LatexCoveredBlockRenderer::getDefaultLatexCover);

private static ModelResourceLocation getDefaultLatexModel(LatexType type) {
return new ModelResourceLocation(getDefaultLatexCoverCached.apply(type), "block");
}

private static final Function<LatexType, ModelResourceLocation> getDefaultLatexModelCached = Util.memoize(LatexCoveredBlocks::getDefaultLatexModel);
private static final Function<LatexType, ModelResourceLocation> getDefaultLatexModelCached = Util.memoize(LatexCoveredBlockRenderer::getDefaultLatexModel);

private static boolean namespaceQualifiesForCheap(String sourceNamespace) { // Maybe allow configuring preserved namespaces
if (sourceNamespace.equals("minecraft"))
Expand Down Expand Up @@ -563,7 +562,7 @@ public static void onRegisterReloadListenerEvent(RegisterClientReloadListenersEv
@SubscribeEvent
public static void onInitModelBaking(ModelRegistryEvent event) {
// This step should have already completed by this point, but race conditions will be race conditions
AbstractLatexItem.removeLatexCoveredStates();
LatexCoveredBlocks.removeLatexCoveredStates();
}

@SubscribeEvent
Expand All @@ -575,7 +574,7 @@ public static void onModelBake(ModelBakeEvent event) {
LOGGER.info("Gathering blocks to cover");

HashSet<RegistryElementPredicate<Block>> notCoverable = new HashSet<>();
Changed.postModEvent(new AbstractLatexItem.GatherNonCoverableBlocksEvent(notCoverable));
Changed.postModEvent(new LatexCoveredBlocks.GatherNonCoverableBlocksEvent(notCoverable));

List<Block> toCover = ForgeRegistries.BLOCKS.getValues().stream().filter(block -> {
if (!block.getStateDefinition().getProperties().contains(COVERED))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import mod.chiselsandbits.client.model.baked.chiseled.ChiselRenderType;
import mod.chiselsandbits.client.model.baked.chiseled.VoxelType;
import net.ltxprogrammer.changed.client.LatexCoveredBlocks;
import net.ltxprogrammer.changed.client.LatexCoveredBlockRenderer;
import net.minecraft.client.renderer.RenderType;

import java.lang.reflect.Method;
Expand All @@ -29,8 +29,8 @@ private static ChiselRenderType create(String name, RenderType type, VoxelType v
ctor = tmp;
}

public static final ChiselRenderType LATEX_SOLID = create("LATEX_SOLID", LatexCoveredBlocks.latexSolid(), VoxelType.SOLID);
public static final ChiselRenderType LATEX_SOLID_FLUID = create("LATEX_SOLID_FLUID", LatexCoveredBlocks.latexSolid(), VoxelType.FLUID);
public static final ChiselRenderType LATEX_CUTOUT = create("LATEX_CUTOUT", LatexCoveredBlocks.latexCutout(), VoxelType.UNKNOWN);
public static final ChiselRenderType LATEX_CUTOUT_MIPPED = create("LATEX_CUTOUT_MIPPED", LatexCoveredBlocks.latexCutoutMipped(), VoxelType.UNKNOWN);
public static final ChiselRenderType LATEX_SOLID = create("LATEX_SOLID", LatexCoveredBlockRenderer.latexSolid(), VoxelType.SOLID);
public static final ChiselRenderType LATEX_SOLID_FLUID = create("LATEX_SOLID_FLUID", LatexCoveredBlockRenderer.latexSolid(), VoxelType.FLUID);
public static final ChiselRenderType LATEX_CUTOUT = create("LATEX_CUTOUT", LatexCoveredBlockRenderer.latexCutout(), VoxelType.UNKNOWN);
public static final ChiselRenderType LATEX_CUTOUT_MIPPED = create("LATEX_CUTOUT_MIPPED", LatexCoveredBlockRenderer.latexCutoutMipped(), VoxelType.UNKNOWN);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.ltxprogrammer.changed.extension.rubidium;

import me.jellysquid.mods.sodium.client.render.chunk.passes.BlockRenderPass;
import net.ltxprogrammer.changed.client.LatexCoveredBlocks;
import net.ltxprogrammer.changed.client.LatexCoveredBlockRenderer;
import net.minecraft.client.renderer.RenderType;

import java.lang.reflect.Method;
Expand All @@ -28,7 +28,7 @@ private static BlockRenderPass create(String name, RenderType type, boolean tran
ctor = tmp;
}

public static final BlockRenderPass LATEX_SOLID = create("LATEX_SOLID", LatexCoveredBlocks.latexSolid(), false, 0.0F);
public static final BlockRenderPass LATEX_CUTOUT = create("LATEX_CUTOUT", LatexCoveredBlocks.latexCutout(), false, 0.1F);
public static final BlockRenderPass LATEX_CUTOUT_MIPPED = create("LATEX_CUTOUT_MIPPED", LatexCoveredBlocks.latexCutoutMipped(), false, 0.5F);
public static final BlockRenderPass LATEX_SOLID = create("LATEX_SOLID", LatexCoveredBlockRenderer.latexSolid(), false, 0.0F);
public static final BlockRenderPass LATEX_CUTOUT = create("LATEX_CUTOUT", LatexCoveredBlockRenderer.latexCutout(), false, 0.1F);
public static final BlockRenderPass LATEX_CUTOUT_MIPPED = create("LATEX_CUTOUT_MIPPED", LatexCoveredBlockRenderer.latexCutoutMipped(), false, 0.5F);
}
134 changes: 2 additions & 132 deletions src/main/java/net/ltxprogrammer/changed/item/AbstractLatexItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,19 @@
import net.ltxprogrammer.changed.entity.TransfurContext;
import net.ltxprogrammer.changed.entity.variant.TransfurVariant;
import net.ltxprogrammer.changed.init.ChangedBlocks;
import net.ltxprogrammer.changed.init.ChangedRegistry;
import net.ltxprogrammer.changed.init.ChangedTabs;
import net.ltxprogrammer.changed.process.LatexCoveredBlocks;
import net.ltxprogrammer.changed.process.ProcessTransfur;
import net.ltxprogrammer.changed.util.EntityUtil;
import net.ltxprogrammer.changed.util.StateHolderHelper;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.food.Foods;
import net.minecraft.world.item.ItemNameBlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;

import static net.ltxprogrammer.changed.block.AbstractLatexBlock.COVERED;

public class AbstractLatexItem extends ItemNameBlockItem {
private final LatexType type;
Expand All @@ -61,119 +44,6 @@ public ItemStack finishUsingItem(ItemStack itemStack, Level level, LivingEntity
return super.finishUsingItem(itemStack, level, entity);
}

public static class GatherNonCoverableBlocksEvent extends Event {
private static RegistryElementPredicate<Block> r(String s) { return RegistryElementPredicate.parseString(ForgeRegistries.BLOCKS, s); }
private static final Set<RegistryElementPredicate<Block>> FIXED_BLOCKS = Set.of(
r("bedrock"),
r("integrateddynamics:cable"),
r("dragonsurvival:oak_dragon_door"),
r("dragonsurvival:spruce_dragon_door"),
r("dragonsurvival:acacia_dragon_door"),
r("dragonsurvival:birch_dragon_door"),
r("dragonsurvival:jungle_dragon_door"),
r("dragonsurvival:dark_oak_dragon_door"),
r("dragonsurvival:warped_dragon_door"),
r("dragonsurvival:crimson_dragon_door"),
r("dragonsurvival:cave_dragon_door"),
r("dragonsurvival:forest_dragon_door"),
r("dragonsurvival:sea_dragon_door"),
r("dragonsurvival:iron_dragon_door"),
r("@chiselsandbits")
);

private final HashSet<RegistryElementPredicate<Block>> set;

public GatherNonCoverableBlocksEvent(HashSet<RegistryElementPredicate<Block>> set) {
this.set = set;
this.set.addAll(FIXED_BLOCKS);
}

public void addBlock(Block block) {
set.add(RegistryElementPredicate.forID(ForgeRegistries.BLOCKS, block.getRegistryName()));
}

public void addBlock(Supplier<? extends Block> block) {
set.add(RegistryElementPredicate.forID(ForgeRegistries.BLOCKS, block.get().getRegistryName()));
}

public void addBlock(ResourceLocation registryName) {
set.add(RegistryElementPredicate.forID(ForgeRegistries.BLOCKS, registryName));
}

public void addNamespace(String namespace) {
set.add(RegistryElementPredicate.forNamespace(ForgeRegistries.BLOCKS, namespace));
}
}

private static boolean removalCompleted = false;
public static synchronized void removeLatexCoveredStates() {
if (!removalCompleted) {
HashSet<RegistryElementPredicate<Block>> notCoverable = new HashSet<>();
Changed.postModEvent(new AbstractLatexItem.GatherNonCoverableBlocksEvent(notCoverable));

ForgeRegistries.BLOCKS.forEach(block -> {
if (!block.getStateDefinition().getProperties().contains(AbstractLatexBlock.COVERED))
return;

if (notCoverable.stream().anyMatch(pred -> pred.test(block))) {
var builder = new StateDefinition.Builder<Block, BlockState>(block);
var oldDefault = block.defaultBlockState();
block.getStateDefinition().getProperties().stream().filter(property -> property != AbstractLatexBlock.COVERED).forEach(builder::add);
var newStateDefinition = builder.create(StateHolderHelper.FN_STATE_CREATION_BYPASS, BlockState::new);

// Create new default state
if (!(newStateDefinition.any() instanceof StateHolderHelper<?,?> newDefault))
throw new IllegalStateException("Mixin failed for StateHolderHelper");
for (var property : newStateDefinition.getProperties())
newDefault = newDefault.setValueTypeless(property, oldDefault.getValue(property));

// Update old state stuff
block.stateDefinition = newStateDefinition;
block.defaultBlockState = (BlockState) newDefault.getState();
}
});

removalCompleted = true;
}
}

public static class CoveringBlockEvent extends Event {
public final LatexType latexType;
public final BlockPos blockPos;
public final BlockState originalState;
public final Level level;
public BlockState plannedState;

public CoveringBlockEvent(LatexType latexType, BlockState originalState, BlockPos blockPos, Level level) {
this.latexType = latexType;
this.blockPos = blockPos;
this.originalState = originalState;
this.level = level;
if (originalState.getProperties().contains(COVERED) && originalState.getValue(COVERED) == LatexType.NEUTRAL)
plannedState = originalState.setValue(COVERED, latexType);
else
plannedState = originalState;

if (originalState.is(Blocks.GRASS_BLOCK))
plannedState = Blocks.DIRT.defaultBlockState().setValue(COVERED, latexType);
else if (originalState.is(Blocks.GRASS))
plannedState = Blocks.DEAD_BUSH.defaultBlockState().setValue(COVERED, latexType);
else if (originalState.is(BlockTags.SMALL_FLOWERS))
plannedState = Blocks.DEAD_BUSH.defaultBlockState().setValue(COVERED, latexType);
else if (originalState.is(BlockTags.SAPLINGS))
plannedState = Blocks.DEAD_BUSH.defaultBlockState().setValue(COVERED, latexType);
else if (originalState.is(Blocks.FERN))
plannedState = Blocks.DEAD_BUSH.defaultBlockState().setValue(COVERED, latexType);
}

@Override public boolean isCancelable() {
return true;
}
public void setPlannedState(BlockState blockState) {
this.plannedState = blockState;
}
}

public InteractionResult useOn(UseOnContext context) {
if (context.getPlayer() != null && context.getPlayer().isCrouching())
return super.useOn(context);
Expand All @@ -183,7 +53,7 @@ public InteractionResult useOn(UseOnContext context) {
for (LatexType type : LatexType.values())
if (this.getDefaultInstance().is(type.goo.get()))
thisType = type;
var event = new CoveringBlockEvent(thisType, state, context.getClickedPos(), context.getLevel());
var event = new LatexCoveredBlocks.CoveringBlockEvent(thisType, state, context.getClickedPos(), context.getLevel());
if (Changed.postModEvent(event))
return InteractionResult.FAIL;
if (event.originalState == event.plannedState)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.ltxprogrammer.changed.mixin;

import net.ltxprogrammer.changed.item.AbstractLatexItem;
import net.ltxprogrammer.changed.process.LatexCoveredBlocks;
import net.minecraftforge.registries.GameData;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -11,6 +12,6 @@
public abstract class GameDataMixin {
@Inject(method = "freezeData", at = @At("HEAD"))
private static void removeLatexCoveredStates(CallbackInfo callback) {
AbstractLatexItem.removeLatexCoveredStates();
LatexCoveredBlocks.removeLatexCoveredStates();
}
}
Loading

0 comments on commit ab41897

Please sign in to comment.