Skip to content

Commit

Permalink
Change CustomLeavesBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Mar 30, 2024
1 parent 8971d81 commit 806dace
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
package com.mmodding.mmodding_lib.library.blocks;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.LeavesBlock;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.IntProperty;
import net.minecraft.tag.BlockTags;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.WorldAccess;
import org.quiltmc.qsl.item.setting.api.QuiltItemSettings;

import java.util.concurrent.atomic.AtomicBoolean;

public class CustomLeavesBlock extends LeavesBlock implements BlockRegistrable, BlockWithItem {
public abstract class CustomLeavesBlock extends LeavesBlock implements BlockRegistrable, BlockWithItem {

private final AtomicBoolean registered = new AtomicBoolean(false);

private BlockItem item = null;

public CustomLeavesBlock(Settings settings) {
this(settings, false);
this.setDefaultState(
this.getDefaultState()
.with(this.getDistanceProperty(), this.getMaxDistance())
.with(CustomLeavesBlock.PERSISTENT, false)
.with(CustomLeavesBlock.WATERLOGGED, false)
);
}

public CustomLeavesBlock(Settings settings, boolean hasItem) {
Expand All @@ -31,6 +48,84 @@ public CustomLeavesBlock(Settings settings, boolean hasItem, Item.Settings itemS
if (hasItem) this.item = new BlockItem(this, itemSettings);
}

protected IntProperty getDistanceProperty() {
return CustomLeavesBlock.DISTANCE;
}

protected int getMaxDistance() {
return 7;
}

protected boolean isLogValid(BlockState state) {
return state.isIn(BlockTags.LOGS);
}

@Override
public boolean hasRandomTicks(BlockState state) {
return state.get(this.getDistanceProperty()) == this.getMaxDistance() && !state.get(CustomLeavesBlock.PERSISTENT);
}

@Override
protected boolean canDecay(BlockState state) {
return !state.get(CustomLeavesBlock.PERSISTENT) && state.get(this.getDistanceProperty()) == this.getMaxDistance();
}

@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
if (state.get(CustomLeavesBlock.WATERLOGGED)) {
world.scheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));
}

int i = this.getDistanceFromLog(neighborState) + 1;
if (i != 1 || state.get(this.getDistanceProperty()) != i) {
world.scheduleBlockTick(pos, this, 1);
}

return state;
}

private BlockState updateDistanceFromLogs(BlockState state, WorldAccess world, BlockPos pos) {
int i = this.getMaxDistance();
BlockPos.Mutable mutable = new BlockPos.Mutable();

for (Direction direction : Direction.values()) {
mutable.set(pos, direction);
i = Math.min(i, this.getDistanceFromLog(world.getBlockState(mutable)) + 1);
if (i == 1) {
break;
}
}

return state.with(this.getDistanceProperty(), i);
}

private int getDistanceFromLog(BlockState state) {
if (this.isLogValid(state)) {
return 0;
} else {
return state.getBlock() instanceof CustomLeavesBlock ? state.get(this.getDistanceProperty()) : this.getMaxDistance();
}
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(this.getDistanceProperty());
builder.add(CustomLeavesBlock.PERSISTENT);
builder.add(CustomLeavesBlock.WATERLOGGED);
}

@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
FluidState fluidState = ctx.getWorld()
.getFluidState(ctx.getBlockPos());

BlockState blockState = this.getDefaultState()
.with(PERSISTENT, false)
.with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER);

return this.updateDistanceFromLogs(blockState, ctx.getWorld(), ctx.getBlockPos());
}

@Override
public BlockItem getItem() {
return this.item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
@ClientOnly
public class AnimationUtils {

public static boolean isMoving(LivingEntity livingEntity, float limbDistance) {
return AnimationUtils.isMoving(livingEntity, limbDistance, 0.015f);
}

public static boolean isMoving(LivingEntity livingEntity, float limbDistance, float motionThreshold) {
Vec3d velocity = livingEntity.getVelocity();
float averageVelocity = (MathHelper.abs((float) velocity.x) + MathHelper.abs((float) velocity.z)) / 2f;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mmodding.mmodding_lib.mixin.injectors;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mmodding.mmodding_lib.library.blocks.CustomLeavesBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.LeavesBlock;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(LeavesBlock.class)
public class LeavesBlockMixin {

@WrapOperation(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/LeavesBlock;setDefaultState(Lnet/minecraft/block/BlockState;)V"))
private void removeConditionally(LeavesBlock instance, BlockState blockState, Operation<Void> original) {
if (!(instance instanceof CustomLeavesBlock)) {
original.call(instance, blockState);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/mmodding_lib.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"injectors.ItemMixin",
"injectors.ItemStackMixin",
"injectors.LavaFluidMixin",
"injectors.LeavesBlockMixin",
"injectors.LivingEntityMixin",
"injectors.MiningToolItemMixin",
"injectors.MobEntityMixin",
Expand Down

0 comments on commit 806dace

Please sign in to comment.