Skip to content

Commit

Permalink
Fix cherry leaf decay range, start cherry trees
Browse files Browse the repository at this point in the history
Ugh, the modern tree code is an insurmountably large wall of unreadable spaghetti, I got the branches for now but the leaves for some reason are a whole separate spaghetti class I'll read later.
  • Loading branch information
Roadhog360 committed Nov 16, 2023
1 parent fec27be commit e82b61a
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 11 deletions.
99 changes: 99 additions & 0 deletions src/main/java/ganymedes01/etfuturum/blocks/BaseLeaves.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLeaves;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
Expand All @@ -10,13 +11,15 @@
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

import java.util.List;
import java.util.Random;

public abstract class BaseLeaves extends BlockLeaves implements ISubBlocksBlock {

private int[] field_150128_a;
private final String[] types;

public BaseLeaves(String... types) {
Expand All @@ -26,6 +29,10 @@ public BaseLeaves(String... types) {
}
}

public int getRange(int meta) {
return 4;
}

@Override
@SideOnly(Side.CLIENT)
@SuppressWarnings({"unchecked", "rawtypes"})
Expand Down Expand Up @@ -91,4 +98,96 @@ public String[] getTypes() {
public String getNameFor(ItemStack stack) {
return types[stack.getItemDamage() % types.length];
}

public void updateTick(World p_149674_1_, int p_149674_2_, int p_149674_3_, int p_149674_4_, Random p_149674_5_) {
if (!p_149674_1_.isRemote) {
int l = p_149674_1_.getBlockMetadata(p_149674_2_, p_149674_3_, p_149674_4_);

if ((l & 8) != 0 && (l & 4) == 0) {
int b0 = getRange(l % 4);
int i1 = b0 + 1;
byte b1 = 32;
int j1 = b1 * b1;
int k1 = b1 / 2;

if (this.field_150128_a == null) {
this.field_150128_a = new int[b1 * b1 * b1];
}

int l1;

if (p_149674_1_.checkChunksExist(p_149674_2_ - i1, p_149674_3_ - i1, p_149674_4_ - i1, p_149674_2_ + i1, p_149674_3_ + i1, p_149674_4_ + i1)) {
int i2;
int j2;

for (l1 = -b0; l1 <= b0; ++l1) {
for (i2 = -b0; i2 <= b0; ++i2) {
for (j2 = -b0; j2 <= b0; ++j2) {
Block block = p_149674_1_.getBlock(p_149674_2_ + l1, p_149674_3_ + i2, p_149674_4_ + j2);

int i = (l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1;
if (!block.canSustainLeaves(p_149674_1_, p_149674_2_ + l1, p_149674_3_ + i2, p_149674_4_ + j2)) {
if (block.isLeaves(p_149674_1_, p_149674_2_ + l1, p_149674_3_ + i2, p_149674_4_ + j2)) {
this.field_150128_a[i] = -2;
} else {
this.field_150128_a[i] = -1;
}
} else {
this.field_150128_a[i] = 0;
}
}
}
}

for (l1 = 1; l1 <= 4; ++l1) {
for (i2 = -b0; i2 <= b0; ++i2) {
for (j2 = -b0; j2 <= b0; ++j2) {
for (int k2 = -b0; k2 <= b0; ++k2) {
if (this.field_150128_a[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1] == l1 - 1) {
int i = (i2 + k1 - 1) * j1 + (j2 + k1) * b1 + k2 + k1;
if (this.field_150128_a[i] == -2) {
this.field_150128_a[i] = l1;
}

int i3 = (i2 + k1 + 1) * j1 + (j2 + k1) * b1 + k2 + k1;
if (this.field_150128_a[i3] == -2) {
this.field_150128_a[i3] = l1;
}

int i4 = (i2 + k1) * j1 + (j2 + k1 - 1) * b1 + k2 + k1;
if (this.field_150128_a[i4] == -2) {
this.field_150128_a[i4] = l1;
}

int i5 = (i2 + k1) * j1 + (j2 + k1 + 1) * b1 + k2 + k1;
if (this.field_150128_a[i5] == -2) {
this.field_150128_a[i5] = l1;
}

int i6 = (i2 + k1) * j1 + (j2 + k1) * b1 + (k2 + k1 - 1);
if (this.field_150128_a[i6] == -2) {
this.field_150128_a[i6] = l1;
}

int i7 = (i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1 + 1;
if (this.field_150128_a[i7] == -2) {
this.field_150128_a[i7] = l1;
}
}
}
}
}
}
}

l1 = this.field_150128_a[k1 * j1 + k1 * b1 + k1];

if (l1 >= 0) {
p_149674_1_.setBlockMetadataWithNotify(p_149674_2_, p_149674_3_, p_149674_4_, l & -9, 4);
} else {
this.removeLeaves(p_149674_1_, p_149674_2_, p_149674_3_, p_149674_4_);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public void getSubBlocks(Item p_149666_1_, CreativeTabs p_149666_2_, List p_1496
}
}

public int getRange(int meta) {
return meta == 1 ? 7 : 4;
}

@Override
public int quantityDropped(int meta, int fortune, Random random) {
if (meta % 4 == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,135 @@
package ganymedes01.etfuturum.world.generate.decorate;

import ganymedes01.etfuturum.ModBlocks;
import ganymedes01.etfuturum.core.utils.Utils;
import ganymedes01.etfuturum.core.utils.helpers.BlockPos;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLog;
import net.minecraft.init.Blocks;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenBigTree;
import net.minecraft.world.gen.feature.WorldGenAbstractTree;
import net.minecraftforge.common.util.ForgeDirection;

import java.util.Random;

public class WorldGenCherryTrees extends WorldGenAbstractTree {

private final int branchCountMin;
private final int branchCountMax;
private final int branchHorizontalLengthMin;
private final int branchHorizontalLengthMax;
// private final int secondBranchStartOffsetFromTop;
private final int branchStartOffsetFromTopMin;
private final int branchStartOffsetFromTopMax;
private final int branchEndOffsetFromTopMin;
private final int branchEndOffsetFromTopMax;

public class WorldGenCherryTrees extends WorldGenBigTree {
public WorldGenCherryTrees(boolean p_i2008_1_) {
super(p_i2008_1_);
this.branchCountMin = 1;
this.branchCountMax = 3;
this.branchHorizontalLengthMin = 2;
this.branchHorizontalLengthMax = 4;
this.branchStartOffsetFromTopMin = -4;
this.branchStartOffsetFromTopMax = -3;
// this.secondBranchStartOffsetFromTop = p_272917_;
this.branchEndOffsetFromTopMin = -1;
this.branchEndOffsetFromTopMax = 0;
}

@Override
public boolean generate(World world, Random rand, int x, int y, int z) {
int height = 7;

world.setBlock(x, y - 1, z, Blocks.dirt);

int secondBranchOffsetFromTop = Math.max(0, height - 1 + MathHelper.getRandomIntegerInRange(rand, branchEndOffsetFromTopMin, branchEndOffsetFromTopMax)); //second branch
int firstBranchOffsetFromTop = Math.max(0, height - 1 + MathHelper.getRandomIntegerInRange(rand, branchEndOffsetFromTopMin, branchEndOffsetFromTopMax));
if (firstBranchOffsetFromTop >= secondBranchOffsetFromTop) {
++firstBranchOffsetFromTop;
}

int branchCount = MathHelper.getRandomIntegerInRange(rand, branchCountMin, branchCountMax);
boolean flag = branchCount == 3;
boolean flag1 = branchCount >= 2;
int treeHeight;
if (flag) {
treeHeight = height;
} else if (flag1) {
treeHeight = Math.max(secondBranchOffsetFromTop, firstBranchOffsetFromTop) + 1;
} else {
treeHeight = secondBranchOffsetFromTop + 1;
}

for (int i1 = 0; i1 < treeHeight; ++i1) {
world.setBlock(x, y + i1, z, ModBlocks.CHERRY_LOG.get());
}

if (flag) {
placeFoliage(world, x, y + treeHeight, z, rand);
}

BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos();
ForgeDirection direction = ForgeDirection.VALID_DIRECTIONS[rand.nextInt(4) + 2];
this.generateBranch(world, x, y, z, rand, height, direction, secondBranchOffsetFromTop, secondBranchOffsetFromTop < treeHeight - 1);
if (flag1) {
this.generateBranch(world, x, y, z, rand, height, direction.getOpposite(), firstBranchOffsetFromTop, firstBranchOffsetFromTop < treeHeight - 1);
}

return true;
}

private void generateBranch(World world, int x, int y, int z, Random rand, int height, ForgeDirection dir, int offsetFromTop, boolean lower) {
int mutableX = x;
int mutableY = y + offsetFromTop;
int mutableZ = z;
int i = height - 1 + MathHelper.getRandomIntegerInRange(rand, branchEndOffsetFromTopMin, branchEndOffsetFromTopMax);
boolean flag = lower || i < offsetFromTop;
int j = MathHelper.getRandomIntegerInRange(rand, branchHorizontalLengthMin, branchHorizontalLengthMax) + (flag ? 1 : 0);
int k = flag ? 2 : 1;
int branchEndX = mutableX + (dir.offsetX * j);
int branchEndY = mutableY + i;
int branchEndZ = mutableZ + (dir.offsetZ * j);
for (int l = 0; l < k; ++l) {
mutableX += dir.offsetX;
mutableZ += dir.offsetZ;
setBlockAndNotifyAdequately(world, mutableX, mutableY, mutableZ, ModBlocks.CHERRY_LOG.get(), dir.offsetX != 0 ? 4 : dir.offsetZ != 0 ? 8 : 0);
}

ForgeDirection branchDir = branchEndY > mutableY ? ForgeDirection.UP : ForgeDirection.DOWN;

while (true) {
int i1 = Utils.distManhattan(branchEndX, branchEndY, branchEndZ, mutableX, mutableY, mutableZ);
if (i1 == 0) {
placeFoliage(world, branchEndX, branchEndY + 1, branchEndZ, rand);
return;
}

float f = (float) Math.abs(branchEndY - mutableY) / (float) i1;
boolean flag1 = rand.nextFloat() < f;
ForgeDirection logDir = flag1 ? branchDir : dir;
mutableX += logDir.offsetX;
mutableY += logDir.offsetY;
mutableZ += logDir.offsetZ;
setBlockAndNotifyAdequately(world, mutableX, mutableY, mutableZ, ModBlocks.CHERRY_LOG.get(), logDir.offsetY != 0 ? 0 : logDir.offsetX != 0 ? 4 : logDir.offsetZ != 0 ? 8 : 0);
}
}

private void placeFoliage(World world, int x, int y, int z, Random rand) {
//Empty for now; the cherry leaf placer code is impossible to read
}

@Override
protected void setBlockAndNotifyAdequately(World p_150516_1_, int p_150516_2_, int p_150516_3_, int p_150516_4_, Block p_150516_5_, int p_150516_6_) {
int meta = p_150516_6_;
Block block = p_150516_5_;
if (block == Blocks.log) {
block = ModBlocks.CHERRY_LOG.get();
} else if (block == Blocks.leaves) {
block = ModBlocks.LEAVES.get();
meta = 1;
}
super.setBlockAndNotifyAdequately(p_150516_1_, p_150516_2_, p_150516_3_, p_150516_4_, block, meta);
if (isReplaceable(p_150516_1_, p_150516_2_, p_150516_3_, p_150516_4_)) {
super.setBlockAndNotifyAdequately(p_150516_1_, p_150516_2_, p_150516_3_, p_150516_4_, p_150516_5_, p_150516_6_);
}
}

private void placeLeaves(World world, int x, int y, int z) {
if (!(world.getBlock(x, y, z) instanceof BlockLog)) {
setBlockAndNotifyAdequately(world, x, y, z, ModBlocks.LEAVES.get(), 1);
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/etfuturum_at.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public net.minecraft.block.Block field_149781_w #blockResistance
public net.minecraft.block.Block field_149768_d #textureName
public-f net.minecraft.block.Block field_149764_J #blockMaterial

protected net.minecraft.block.BlockLeaves func_150126_e(Lnet/minecraft/world/World;III)V #removeLeaves

protected net.minecraft.block.BlockDoor field_150017_a #upper door icons
protected net.minecraft.block.BlockDoor field_150016_b #lower door icons

Expand Down

0 comments on commit e82b61a

Please sign in to comment.