Skip to content

Commit

Permalink
Modify CustomLeavesBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Mar 30, 2024
1 parent 79f4765 commit 81b65f6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.server.world.ServerWorld;
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.util.random.RandomGenerator;
import net.minecraft.world.WorldAccess;
import org.quiltmc.qsl.item.setting.api.QuiltItemSettings;

Expand All @@ -27,12 +29,6 @@ public class CustomLeavesBlock extends LeavesBlock implements BlockRegistrable,

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 @@ -45,6 +41,12 @@ public CustomLeavesBlock(Settings settings, boolean hasItem, ItemGroup itemGroup

public CustomLeavesBlock(Settings settings, boolean hasItem, Item.Settings itemSettings) {
super(settings);
this.setDefaultState(
this.getDefaultState()
.with(this.getDistanceProperty(), this.getMaxDistance())
.with(CustomLeavesBlock.PERSISTENT, false)
.with(CustomLeavesBlock.WATERLOGGED, false)
);
if (hasItem) this.item = new BlockItem(this, itemSettings);
}

Expand All @@ -60,6 +62,10 @@ protected boolean isLogValid(BlockState state) {
return state.isIn(BlockTags.LOGS);
}

protected boolean hasSeparatedLeaves() {
return false;
}

@Override
public boolean hasRandomTicks(BlockState state) {
return state.get(this.getDistanceProperty()) == this.getMaxDistance() && !state.get(CustomLeavesBlock.PERSISTENT);
Expand All @@ -70,6 +76,11 @@ protected boolean canDecay(BlockState state) {
return !state.get(CustomLeavesBlock.PERSISTENT) && state.get(this.getDistanceProperty()) == this.getMaxDistance();
}

@Override
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, RandomGenerator random) {
world.setBlockState(pos, this.updateDistanceFromLogs(state, world, pos), Block.NOTIFY_ALL);
}

@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
if (state.get(CustomLeavesBlock.WATERLOGGED)) {
Expand Down Expand Up @@ -102,8 +113,12 @@ private BlockState updateDistanceFromLogs(BlockState state, WorldAccess world, B
private int getDistanceFromLog(BlockState state) {
if (this.isLogValid(state)) {
return 0;
} else {
return state.getBlock() instanceof CustomLeavesBlock ? state.get(this.getDistanceProperty()) : this.getMaxDistance();
}
else if (state.getBlock() instanceof CustomLeavesBlock || this.hasSeparatedLeaves()) {
return state.get(this.getDistanceProperty());
}
else {
return this.getMaxDistance();
}
}

Expand All @@ -120,8 +135,8 @@ public BlockState getPlacementState(ItemPlacementContext ctx) {
.getFluidState(ctx.getBlockPos());

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

return this.updateDistanceFromLogs(blockState, ctx.getWorld(), ctx.getBlockPos());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mmodding.mmodding_lib.library.blocks.CustomLeavesBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.LeavesBlock;
import net.minecraft.state.property.Property;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

Expand All @@ -17,4 +18,9 @@ private void removeConditionally(LeavesBlock instance, BlockState blockState, Op
original.call(instance, blockState);
}
}

@WrapOperation(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;with(Lnet/minecraft/state/property/Property;Ljava/lang/Comparable;)Ljava/lang/Object;"))
private Object removeConditionally(BlockState instance, Property<?> property, Comparable<?> comparable, Operation<Object> original) {
return ((Object) this) instanceof CustomLeavesBlock ? instance : original.call(instance, property, comparable);
}
}

0 comments on commit 81b65f6

Please sign in to comment.