Skip to content

Commit

Permalink
refactor Grinder gear rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
luxtracon committed Sep 1, 2024
1 parent 4cef301 commit fbfb6e5
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
Expand All @@ -27,7 +28,7 @@
@ParametersAreNonnullByDefault

public class GrinderBlockRenderer implements BlockEntityRenderer<GrinderBlockEntity> {
public int rotationModifier = -16;
public long prevTime;

public GrinderBlockRenderer(BlockEntityRendererProvider.Context pContext) {

Expand All @@ -41,151 +42,164 @@ public int getViewDistance() {
@Override
public void render(GrinderBlockEntity pBlockEntity, float pPartialTick, PoseStack pPoseStack, MultiBufferSource pBufferSource, int pPackedLight, int pPackedOverlay) {
if (IcariaConfig.RENDER_GRINDER_GEARS.get()) {
boolean grinding = pBlockEntity.getBlockState().getValue(IcariaBlockStateProperties.GRINDER_GRINDING);
var state = pBlockEntity.getBlockState();

int rotation = pBlockEntity.getBlockState().getValue(IcariaBlockStateProperties.GRINDER_ROTATION) * rotationModifier;
var gear = pBlockEntity.getGear();

var direction = pBlockEntity.getBlockState().getValue(BlockStateProperties.HORIZONTAL_FACING);
var itemRenderer = Minecraft.getInstance().getItemRenderer();
var level = pBlockEntity.getLevel();
var mainGear = pBlockEntity.getGear();

// STONE
var facing = state.getValue(BlockStateProperties.HORIZONTAL_FACING);

pPoseStack.pushPose();
pPoseStack.translate(0.5F, 0.5F, 0.5F);
pPoseStack.scale(2.0F, 2.0F, 2.0F);
if (grinding) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(rotation));
}
var grinding = state.getValue(IcariaBlockStateProperties.GRINDER_GRINDING);

itemRenderer.renderStatic(new ItemStack(IcariaItems.GRINDER_STONE.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
pPoseStack.popPose();
var minecraft = Minecraft.getInstance();

// SHAFT
var renderer = Minecraft.getInstance().getItemRenderer();

pPoseStack.pushPose();
if (direction == Direction.NORTH) {
pPoseStack.translate(-0.5F, 0.5F, 0.5F);
pPoseStack.scale(2.0F, 2.0F, 2.0F);
} else if (direction == Direction.EAST) {
pPoseStack.translate(0.5, 0.5F, -0.5F);
pPoseStack.scale(2.0F, 2.0F, 2.0F);
} else if (direction == Direction.SOUTH) {
pPoseStack.translate(1.5F, 0.5F, 0.5F);
pPoseStack.scale(2.0F, 2.0F, 2.0F);
} else if (direction == Direction.WEST) {
pPoseStack.translate(0.5F, 0.5F, 1.5F);
pPoseStack.scale(2.0F, 2.0F, 2.0F);
}
if (level != null) {
long gameTime = level.getGameTime();

if (grinding) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(rotation));
}
float time = Mth.lerp(minecraft.getTimer().getGameTimeDeltaTicks(), this.prevTime, gameTime) * -16.0F;

itemRenderer.renderStatic(new ItemStack(IcariaItems.GRINDER_SHAFT.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
pPoseStack.popPose();

// LEFT CHAIN

pPoseStack.pushPose();
if (direction == Direction.NORTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(0.5F, 0.4050F, 0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (direction == Direction.EAST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(270.0F));
pPoseStack.translate(0.5F, 0.4050F, -0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (direction == Direction.SOUTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(180.0F));
pPoseStack.translate(-0.5F, 0.4050F, -0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (direction == Direction.WEST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(90.0F));
pPoseStack.translate(-0.5F, 0.4050F, 0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
}
this.prevTime = gameTime;

if (grinding) {
itemRenderer.renderStatic(new ItemStack(IcariaItems.LEFT_GRINDER_CHAIN.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
} else {
itemRenderer.renderStatic(new ItemStack(IcariaItems.STATIC_LEFT_GRINDER_CHAIN.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
}
// STONE

pPoseStack.popPose();

// RIGHT CHAIN

pPoseStack.pushPose();
if (direction == Direction.NORTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(-0.5F, 0.4050F, 0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (direction == Direction.EAST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(270.0F));
pPoseStack.translate(-0.5F, 0.4050F, -0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (direction == Direction.SOUTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(180.0F));
pPoseStack.translate(-1.5F, 0.4050F, -0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (direction == Direction.WEST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(90.0F));
pPoseStack.translate(-1.5F, 0.4050F, 0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
}

if (grinding) {
itemRenderer.renderStatic(new ItemStack(IcariaItems.RIGHT_GRINDER_CHAIN.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
} else {
itemRenderer.renderStatic(new ItemStack(IcariaItems.STATIC_RIGHT_GRINDER_CHAIN.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
}

pPoseStack.popPose();

// GEAR

pPoseStack.pushPose();
if (direction == Direction.NORTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(-0.5F, 0.3425F, 0.5F);
pPoseStack.scale(0.625F, 1.0F, 0.625F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
} else if (direction == Direction.EAST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(0.5F, 0.3425F, -0.5F);
pPoseStack.scale(0.625F, 1.0F, 0.625F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
} else if (direction == Direction.SOUTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(1.5F, 0.3425F, 0.5F);
pPoseStack.scale(0.625F, 1.0F, 0.625F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
} else if (direction == Direction.WEST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(0.5F, 0.3425F, 1.5F);
pPoseStack.scale(0.625F, 1.0F, 0.625F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
}

if (grinding) {
pPoseStack.mulPose(Axis.ZN.rotationDegrees(rotation));
}

if (!mainGear.isEmpty()) {
itemRenderer.renderStatic(mainGear, ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
pPoseStack.pushPose();
pPoseStack.translate(0.5F, 0.5F, 0.5F);
pPoseStack.scale(2.0F, 2.0F, 2.0F);
if (grinding) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(time));
}

renderer.renderStatic(new ItemStack(IcariaItems.GRINDER_STONE.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
pPoseStack.popPose();

// SHAFT

pPoseStack.pushPose();
if (facing == Direction.NORTH) {
pPoseStack.translate(-0.5F, 0.5F, 0.5F);
pPoseStack.scale(2.0F, 2.0F, 2.0F);
} else if (facing == Direction.EAST) {
pPoseStack.translate(0.5, 0.5F, -0.5F);
pPoseStack.scale(2.0F, 2.0F, 2.0F);
} else if (facing == Direction.SOUTH) {
pPoseStack.translate(1.5F, 0.5F, 0.5F);
pPoseStack.scale(2.0F, 2.0F, 2.0F);
} else if (facing == Direction.WEST) {
pPoseStack.translate(0.5F, 0.5F, 1.5F);
pPoseStack.scale(2.0F, 2.0F, 2.0F);
}

if (grinding) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(time));
}

renderer.renderStatic(new ItemStack(IcariaItems.GRINDER_SHAFT.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
pPoseStack.popPose();

// LEFT CHAIN

pPoseStack.pushPose();
if (facing == Direction.NORTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(0.5F, 0.4050F, 0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (facing == Direction.EAST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(270.0F));
pPoseStack.translate(0.5F, 0.4050F, -0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (facing == Direction.SOUTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(180.0F));
pPoseStack.translate(-0.5F, 0.4050F, -0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (facing == Direction.WEST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(90.0F));
pPoseStack.translate(-0.5F, 0.4050F, 0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
}

if (grinding) {
renderer.renderStatic(new ItemStack(IcariaItems.LEFT_GRINDER_CHAIN.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
} else {
renderer.renderStatic(new ItemStack(IcariaItems.STATIC_LEFT_GRINDER_CHAIN.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
}

pPoseStack.popPose();

// RIGHT CHAIN

pPoseStack.pushPose();
if (facing == Direction.NORTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(-0.5F, 0.4050F, 0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (facing == Direction.EAST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(270.0F));
pPoseStack.translate(-0.5F, 0.4050F, -0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (facing == Direction.SOUTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(180.0F));
pPoseStack.translate(-1.5F, 0.4050F, -0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
} else if (facing == Direction.WEST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(90.0F));
pPoseStack.translate(-1.5F, 0.4050F, 0.5F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(1.0F, 1.0F, 1.0F);
}

if (grinding) {
renderer.renderStatic(new ItemStack(IcariaItems.RIGHT_GRINDER_CHAIN.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
} else {
renderer.renderStatic(new ItemStack(IcariaItems.STATIC_RIGHT_GRINDER_CHAIN.get()), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
}

pPoseStack.popPose();

// GEAR

pPoseStack.pushPose();
if (facing == Direction.NORTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(-0.5F, 0.3425F, 0.5F);
pPoseStack.scale(0.625F, 1.0F, 0.625F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
} else if (facing == Direction.EAST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(0.5F, 0.3425F, -0.5F);
pPoseStack.scale(0.625F, 1.0F, 0.625F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
} else if (facing == Direction.SOUTH) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(1.5F, 0.3425F, 0.5F);
pPoseStack.scale(0.625F, 1.0F, 0.625F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
} else if (facing == Direction.WEST) {
pPoseStack.mulPose(Axis.YP.rotationDegrees(0.0F));
pPoseStack.translate(0.5F, 0.3425F, 1.5F);
pPoseStack.scale(0.625F, 1.0F, 0.625F);
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
}

if (grinding) {
pPoseStack.mulPose(Axis.ZN.rotationDegrees(time));
}

if (!gear.isEmpty()) {
renderer.renderStatic(gear, ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, level, 1);
}

pPoseStack.popPose();
}

pPoseStack.popPose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class GrinderBlock extends BaseEntityBlock {

public GrinderBlock(Properties pProperties) {
super(pProperties);
this.registerDefaultState(this.stateDefinition.any().setValue(IcariaBlockStateProperties.GRINDER_GRINDING, false).setValue(IcariaBlockStateProperties.GRINDER_ROTATION, 0).setValue(BlockStateProperties.HORIZONTAL_FACING, Direction.NORTH).setValue(IcariaBlockStateProperties.SIDE, Side.LEFT));
this.registerDefaultState(this.stateDefinition.any().setValue(IcariaBlockStateProperties.GRINDER_GRINDING, false).setValue(BlockStateProperties.HORIZONTAL_FACING, Direction.NORTH).setValue(IcariaBlockStateProperties.SIDE, Side.LEFT));
}

@Override
Expand Down Expand Up @@ -115,7 +115,7 @@ public void animateTick(BlockState pState, Level pLevel, BlockPos pPos, RandomSo

@Override
public void createBlockStateDefinition(Builder<Block, BlockState> pBuilder) {
pBuilder.add(IcariaBlockStateProperties.GRINDER_GRINDING, IcariaBlockStateProperties.GRINDER_ROTATION, BlockStateProperties.HORIZONTAL_FACING, IcariaBlockStateProperties.SIDE);
pBuilder.add(IcariaBlockStateProperties.GRINDER_GRINDING, BlockStateProperties.HORIZONTAL_FACING, IcariaBlockStateProperties.SIDE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public class GrinderBlockEntity extends BlockEntity {
public int maxFuel = 0;
public int progress = 0;
public int maxProgress = 0;
public int rotation = 0;
public int size = 6;

public ItemStackHandler fuelHandler = new GrinderFuelItemStackHandler(1, this);
Expand Down Expand Up @@ -241,12 +240,6 @@ public static void tick(Level pLevel, BlockPos pPos, BlockState pState, GrinderB
}

if (pBlockEntity.hasFuel() && pBlockEntity.hasRecipe()) {
if (pBlockEntity.rotation >= 90) {
pBlockEntity.rotation = 0;
} else {
pBlockEntity.rotation++;
}

if (IcariaConfig.GRINDER_SOUNDS.get()) {
if (pBlockEntity.lastSound >= 6) {
pBlockEntity.lastSound = 0;
Expand All @@ -269,8 +262,8 @@ public static void tick(Level pLevel, BlockPos pPos, BlockState pState, GrinderB

pBlockEntity.progress++;
pBlockEntity.fuel--;
pLevel.setBlockAndUpdate(pPos, pState.setValue(IcariaBlockStateProperties.SIDE, Side.LEFT).setValue(IcariaBlockStateProperties.GRINDER_GRINDING, true).setValue(IcariaBlockStateProperties.GRINDER_ROTATION, pBlockEntity.rotation));
pLevel.setBlockAndUpdate(pPos.offset(facing.getCounterClockWise().getNormal()), pState.setValue(IcariaBlockStateProperties.SIDE, Side.RIGHT).setValue(IcariaBlockStateProperties.GRINDER_GRINDING, true).setValue(IcariaBlockStateProperties.GRINDER_ROTATION, pBlockEntity.rotation));
pLevel.setBlockAndUpdate(pPos, pState.setValue(IcariaBlockStateProperties.SIDE, Side.LEFT).setValue(IcariaBlockStateProperties.GRINDER_GRINDING, true));
pLevel.setBlockAndUpdate(pPos.offset(facing.getCounterClockWise().getNormal()), pState.setValue(IcariaBlockStateProperties.SIDE, Side.RIGHT).setValue(IcariaBlockStateProperties.GRINDER_GRINDING, true));
} else {
pBlockEntity.resetProgress();
pLevel.setBlockAndUpdate(pPos, pState.setValue(IcariaBlockStateProperties.SIDE, Side.LEFT).setValue(IcariaBlockStateProperties.GRINDER_GRINDING, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,5 @@ public class IcariaBlockStateProperties {
public static final EnumProperty<Vine> VINE = EnumProperty.create("vine", Vine.class);

public static final IntegerProperty CAKE_BITE = IntegerProperty.create("cake_bite", 0, 3);
public static final IntegerProperty GRINDER_ROTATION = IntegerProperty.create("grinder_rotation", 0, 90);
public static final IntegerProperty TROUGH_FILL = IntegerProperty.create("trough_fill", 0, 9);
}

0 comments on commit fbfb6e5

Please sign in to comment.