Skip to content

Commit

Permalink
Copycat trapdoors can now accept trapdoors
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed Jul 29, 2024
1 parent ede622b commit 1e00124
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,10 @@

public class CopycatCogWheelModelCore extends CopycatModelCore {

private static BlockState prepareMaterial(BlockState state, BlockState material) {
if (material.getBlock() instanceof CogWheelBlock) {
return state.getOptionalValue(CogWheelBlock.AXIS)
.map(val -> material.trySetValue(CogWheelBlock.AXIS, val))
.orElse(material);
}
return material;
}

@Override
public void registerModels(List<ModelEntry> entries) {
entries.add(new ModelEntry(MATERIAL_KEY, (state, mat) -> getModelOf(prepareMaterial(state, mat)), this, EntryType.KINETIC_COPYCAT));
entries.add(new ModelEntry("cogwheel", (state, mat) -> getModelOf(prepareMaterial(state, mat)), this, EntryType.KINETIC_COPYCAT));
entries.add(new ModelEntry(MATERIAL_KEY, updatePropertiesIfMatch(CogWheelBlock.class), this, EntryType.KINETIC_COPYCAT));
entries.add(new ModelEntry("cogwheel", updatePropertiesIfMatch(CogWheelBlock.class), this, EntryType.KINETIC_COPYCAT));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,10 @@

public class CopycatLargeCogWheelModelCore extends CopycatModelCore {

private static BlockState prepareMaterial(BlockState state, BlockState material) {
if (material.getBlock() instanceof CogWheelBlock) {
return state.getOptionalValue(CogWheelBlock.AXIS)
.map(val -> material.trySetValue(CogWheelBlock.AXIS, val))
.orElse(material);
}
return material;
}

@Override
public void registerModels(List<ModelEntry> entries) {
entries.add(new ModelEntry(MATERIAL_KEY, (state, mat) -> getModelOf(prepareMaterial(state, mat)), this, EntryType.KINETIC_COPYCAT));
entries.add(new ModelEntry("cogwheel", (state, mat) -> getModelOf(prepareMaterial(state, mat)), this, EntryType.KINETIC_COPYCAT));
entries.add(new ModelEntry(MATERIAL_KEY, updatePropertiesIfMatch(CogWheelBlock.class), this, EntryType.KINETIC_COPYCAT));
entries.add(new ModelEntry("cogwheel", updatePropertiesIfMatch(CogWheelBlock.class), this, EntryType.KINETIC_COPYCAT));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,10 @@

public class CopycatShaftModelCore extends CopycatModelCore {

private static BlockState prepareMaterial(BlockState state, BlockState material) {
if (material.getBlock() instanceof ShaftBlock) {
return state.getOptionalValue(ShaftBlock.AXIS)
.map(val -> material.trySetValue(ShaftBlock.AXIS, val))
.orElse(material);
}
return material;
}

@Override
public void registerModels(List<ModelEntry> entries) {
entries.add(new ModelEntry(MATERIAL_KEY, (state, mat) -> getModelOf(prepareMaterial(state, mat)), this, EntryType.KINETIC_COPYCAT));
entries.add(new ModelEntry("shaft", (state, mat) -> getModelOf(prepareMaterial(state, mat)), this, EntryType.KINETIC_COPYCAT));
entries.add(new ModelEntry(MATERIAL_KEY, updatePropertiesIfMatch(ShaftBlock.class), this, EntryType.KINETIC_COPYCAT));
entries.add(new ModelEntry("shaft", updatePropertiesIfMatch(ShaftBlock.class), this, EntryType.KINETIC_COPYCAT));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public void playerWillDestroy(Level pLevel, BlockPos pPos, BlockState pState, Pl
super.playerWillDestroy(pLevel, pPos, pState, pPlayer);
}

@Override
public boolean isAcceptedRegardless(BlockState material) {
return material.getBlock() instanceof TrapDoorBlock;
}

@Override
public Class<CCCopycatBlockEntity> getBlockEntityClass() {
return CCCopycatBlockEntity.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@
import com.copycatsplus.copycats.foundation.copycat.model.CopycatModelCore;
import com.copycatsplus.copycats.foundation.copycat.model.assembly.CopycatRenderContext;
import com.copycatsplus.copycats.foundation.copycat.model.assembly.AssemblyTransform;
import net.minecraft.world.level.block.TrapDoorBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Half;

import java.util.List;

import static com.copycatsplus.copycats.foundation.copycat.model.assembly.CopycatRenderContext.*;
import static com.copycatsplus.copycats.foundation.copycat.model.assembly.MutableCullFace.*;
import static net.minecraft.world.level.block.TrapDoorBlock.*;

public class CopycatTrapdoorModelCore extends CopycatModelCore {

@Override
public void registerModels(List<ModelEntry> entries) {
entries.add(new ModelEntry(MATERIAL_KEY, updatePropertiesIfMatch(TrapDoorBlock.class), this, EntryType.COPYCAT));
}

@Override
public void emitCopycatQuads(String key, BlockState state, CopycatRenderContext context, BlockState material) {
if (material.getBlock() instanceof TrapDoorBlock) {
context.assembleAll();
return;
}

int rot = (int) state.getValue(FACING).toYRot();
boolean flipY = state.getValue(HALF) == Half.TOP;
boolean open = state.getValue(OPEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ default void init() {
}

default ICopycatBlock getBlock() {
return (ICopycatBlock) getBlockState().getBlock();
Block block = getBlockState().getBlock();
if (block instanceof ICopycatBlock copycatBlock)
return copycatBlock;
// the block state might not be a copycat block in some virtual worlds
// return sensible defaults in those cases
return new ICopycatBlock() {
};
}

default boolean hasCustomMaterial() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.copycatsplus.copycats.foundation.copycat.model.assembly.CopycatModelPart;
import com.copycatsplus.copycats.foundation.copycat.model.assembly.CopycatRenderContext;
import com.copycatsplus.copycats.foundation.copycat.multistate.IMultiStateCopycatBlock;
import com.copycatsplus.copycats.utility.BlockUtils;
import dev.architectury.injectables.annotations.ExpectPlatform;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.BakedModel;
Expand All @@ -12,6 +13,7 @@

import javax.annotation.Nullable;
import java.util.List;
import java.util.function.BiFunction;

/**
* Block-specific but platform-independent model generation logic for copycats.
Expand Down Expand Up @@ -117,6 +119,19 @@ public static BakedModel getModelOf(BlockState state) {
.getBlockModel(state);
}

/**
* Helper method to copy block state properties from the copycat to the material before retrieving block model.
* This is useful for copycats that allow blocks similar to themselves to be used as materials.
*/
public static ModelGetter updatePropertiesIfMatch(Class<?> clazz) {
return (state, mat) -> {
if (clazz.isInstance(mat.getBlock())) {
return getModelOf(BlockUtils.tryCopyProperties(state, mat));
}
return getModelOf(mat);
};
}

/**
* Create a platform-specific {@link BakedModel} implementation for a copycat which wraps the original model and
* renders with the provided core.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.copycatsplus.copycats.utility;

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;

public class BlockUtils {
public static BlockState tryCopyProperties(BlockState from, BlockState to) {
for (Property<?> property : from.getProperties()) {
to = tryCopyProperty(from, to, property);
}
return to;
}

public static <T extends Comparable<T>> BlockState tryCopyProperty(BlockState from, BlockState to, Property<T> property) {
if (from.hasProperty(property) && to.hasProperty(property)) {
return to.setValue(property, from.getValue(property));
}
return to;
}
}

0 comments on commit 1e00124

Please sign in to comment.