Skip to content

Commit

Permalink
MixinBlockGrass : Fix ArrayIndexOutOfBoundsException crash
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin452 committed Jan 19, 2024
1 parent f7f95b1 commit 785031f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static fr.iamacat.optimizationsandtweaks.utilsformods.vanilla.BlockGrass2.optimizationsAndTweaks$spreadGrass;

@Mixin(BlockGrass.class)
public class MixinBlockGrass {

Expand Down Expand Up @@ -44,32 +46,4 @@ public void updateTick(World worldIn, int x, int y, int z, Random random, Callba
private boolean optimizationsAndTweaks$canSpreadGrass(World worldIn, int x, int y, int z) {
return worldIn.getBlockLightValue(x, y + 1, z) >= 9;
}

private void optimizationsAndTweaks$spreadGrass(World worldIn, int x, int y, int z, Random random) {
int chunkX = x >> 4;
int chunkZ = z >> 4;

Chunk chunk = worldIn.getChunkFromChunkCoords(chunkX, chunkZ);

if (!chunk.isChunkLoaded) {
return;
}

if (worldIn.getTotalWorldTime() % 10 == 0) {
for (int l = 0; l < 4; ++l) {
int i1 = x + random.nextInt(3) - 1;
int j1 = y + random.nextInt(5) - 3;
int k1 = z + random.nextInt(3) - 1;
Block block = chunk.getBlock(i1 & 15, j1, k1 & 15);
int metadata = chunk.getBlockMetadata(i1 & 15, j1, k1 & 15);

int lightValue = worldIn.getBlockLightValue(i1, j1 + 1, k1);
int lightOpacity = worldIn.getBlockLightOpacity(i1, j1 + 1, k1);

if (block == Blocks.dirt && metadata == 0 && lightValue >= 4 && lightOpacity <= 2) {
worldIn.setBlock(i1, j1, k1, Blocks.grass, 0, 2);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fr.iamacat.optimizationsandtweaks.utilsformods.vanilla;

import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;

import java.util.Random;

public class BlockGrass2 {
public static void optimizationsAndTweaks$spreadGrass(World worldIn, int x, int y, int z, Random random) {
int chunkX = x >> 4;
int chunkZ = z >> 4;
Chunk chunk = worldIn.getChunkFromChunkCoords(chunkX, chunkZ);
if(chunk == null) {
return;
}
if(!chunk.isChunkLoaded) {
return;
}
if (worldIn.getTotalWorldTime() % 10 == 0) {
for (int l = 0; l < 4; ++l) {
int i1 = x + random.nextInt(3) - 1;
int j1 = y + random.nextInt(5) - 3;
int k1 = z + random.nextInt(3) - 1;
Block block = chunk.getBlock(i1 & 15, j1, k1 & 15);
int metadata = chunk.getBlockMetadata(i1 & 15, j1, k1 & 15);

int lightValue = worldIn.getBlockLightValue(i1, j1 + 1, k1);
int lightOpacity = worldIn.getBlockLightOpacity(i1, j1 + 1, k1);

if (block == Blocks.dirt && metadata == 0 && lightValue >= 4 && lightOpacity <= 2) {
worldIn.setBlock(i1, j1, k1, Blocks.grass, 0, 2);
}
}
}
}
}

0 comments on commit 785031f

Please sign in to comment.