added own WorldGenMinable to search for nearby blocks instead ob replacing them

This commit is contained in:
2016-05-02 12:42:53 +02:00
parent 72b27f87c7
commit bd8db7ee7d
4 changed files with 137 additions and 15 deletions

View File

@@ -1,14 +1,6 @@
package items; package items;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class BasicItem extends Item { public class BasicItem extends Item {
@@ -16,5 +8,4 @@ public class BasicItem extends Item {
setUnlocalizedName(name); setUnlocalizedName(name);
setRegistryName(name); setRegistryName(name);
} }
} }

View File

@@ -5,7 +5,6 @@ import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks; import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenMinable;
import java.util.Random; import java.util.Random;
@@ -35,7 +34,7 @@ public class Generator {
int posX = chunkX + random.nextInt(16); int posX = chunkX + random.nextInt(16);
int posY = random.nextInt(height) + minY; int posY = random.nextInt(height) + minY;
int posZ = chunkZ + random.nextInt(16); int posZ = chunkZ + random.nextInt(16);
new WorldGenMinable(state, size, StateMatcher.forState(replace, property, comparable)).generate(world, random, new BlockPos(posX, posY, posZ)); new ZWorldGenMinable(state, size, StateMatcher.forState(replace, property, comparable)).generate(world, random, new BlockPos(posX, posY, posZ));
} }
} }
} }

View File

@@ -3,6 +3,8 @@ package main.util;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import net.minecraft.block.properties.IProperty; import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class StateMatcher implements Predicate<IBlockState> { public class StateMatcher implements Predicate<IBlockState> {
@@ -20,13 +22,57 @@ public class StateMatcher implements Predicate<IBlockState> {
return new StateMatcher(state, property, value); return new StateMatcher(state, property, value);
} }
public boolean apply(IBlockState state) { public boolean apply(IBlockState state, BlockPos pos, World world) {
if (state != null) { if (state != null) {
if (property != null && value != null) if (property != null && value != null) {
return state.getBlock() == this.state.getBlock() && state.getValue(property) == value; if (checkLayerForBlocks(pos.getX(), pos.getZ(), pos.getY() - 1, world, pos) ||
else checkLayerForBlocks(pos.getX(), pos.getZ(), pos.getY(), world, pos) ||
checkLayerForBlocks(pos.getX(), pos.getZ(), pos.getY() + 1, world, pos))
return true;
} else
return state.getBlock() == this.state.getBlock(); return state.getBlock() == this.state.getBlock();
} }
return false; return false;
} }
@Override
public boolean apply(IBlockState input) {
if (state != null) {
if (property != null && value != null) {
return state.getBlock() == this.state.getBlock() && state.getValue(property) == value;
} else
return state.getBlock() == this.state.getBlock();
}
return false;
}
private boolean checkLayerForBlocks(int X, int Z, int Y, World world, BlockPos origin) {
int x = 0, z = 0, dx = 0, dz = -1;
int t = Math.max(X, Z);
int maxI = t * t;
for (int i = 0; i < maxI; i++) {
if ((-X / 2 <= x) && (x <= X / 2) && (-Z / 2 <= z) && (z <= Z / 2)) {
System.out.println(x + "," + z);
if (new BlockPos(x, Y, z) == origin)
continue;
IBlockState bState = world.getBlockState(new BlockPos(x, Y, z));
if (bState.getBlock() == this.state.getBlock() && bState.getValue(property) == value) {
return true;
}
}
if ((x == z) || ((x < 0) && (x == -z)) || ((x > 0) && (x == 1 - z))) {
t = dx;
dx = -dz;
dz = t;
}
x += dx;
z += dz;
}
return false;
}
} }

View File

@@ -0,0 +1,86 @@
package main.util;
import com.google.common.base.Predicate;
import net.minecraft.block.state.IBlockState;
import net.minecraft.block.state.pattern.BlockMatcher;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenMinable;
import java.util.Random;
public class ZWorldGenMinable extends WorldGenMinable {
private final IBlockState oreBlock;
/**
* The number of blocks to generate.
*/
private final int numberOfBlocks;
private final Predicate<IBlockState> predicate;
public ZWorldGenMinable(IBlockState state, int blockCount) {
this(state, blockCount, BlockMatcher.forBlock(Blocks.STONE));
}
public ZWorldGenMinable(IBlockState state, int blockCount, Predicate<IBlockState> predicate) {
super(state, blockCount, predicate);
this.oreBlock = state;
this.numberOfBlocks = blockCount;
this.predicate = predicate;
}
@Override
public boolean generate(World worldIn, Random rand, BlockPos position) {
float f = rand.nextFloat() * (float) Math.PI;
double d0 = (double) ((float) (position.getX() + 8) + MathHelper.sin(f) * (float) this.numberOfBlocks / 8.0F);
double d1 = (double) ((float) (position.getX() + 8) - MathHelper.sin(f) * (float) this.numberOfBlocks / 8.0F);
double d2 = (double) ((float) (position.getZ() + 8) + MathHelper.cos(f) * (float) this.numberOfBlocks / 8.0F);
double d3 = (double) ((float) (position.getZ() + 8) - MathHelper.cos(f) * (float) this.numberOfBlocks / 8.0F);
double d4 = (double) (position.getY() + rand.nextInt(3) - 2);
double d5 = (double) (position.getY() + rand.nextInt(3) - 2);
for (int i = 0; i < this.numberOfBlocks; ++i) {
float f1 = (float) i / (float) this.numberOfBlocks;
double d6 = d0 + (d1 - d0) * (double) f1;
double d7 = d4 + (d5 - d4) * (double) f1;
double d8 = d2 + (d3 - d2) * (double) f1;
double d9 = rand.nextDouble() * (double) this.numberOfBlocks / 16.0D;
double d10 = (double) (MathHelper.sin((float) Math.PI * f1) + 1.0F) * d9 + 1.0D;
double d11 = (double) (MathHelper.sin((float) Math.PI * f1) + 1.0F) * d9 + 1.0D;
int j = MathHelper.floor_double(d6 - d10 / 2.0D);
int k = MathHelper.floor_double(d7 - d11 / 2.0D);
int l = MathHelper.floor_double(d8 - d10 / 2.0D);
int i1 = MathHelper.floor_double(d6 + d10 / 2.0D);
int j1 = MathHelper.floor_double(d7 + d11 / 2.0D);
int k1 = MathHelper.floor_double(d8 + d10 / 2.0D);
for (int l1 = j; l1 <= i1; ++l1) {
double d12 = ((double) l1 + 0.5D - d6) / (d10 / 2.0D);
if (d12 * d12 < 1.0D) {
for (int i2 = k; i2 <= j1; ++i2) {
double d13 = ((double) i2 + 0.5D - d7) / (d11 / 2.0D);
if (d12 * d12 + d13 * d13 < 1.0D) {
for (int j2 = l; j2 <= k1; ++j2) {
double d14 = ((double) j2 + 0.5D - d8) / (d10 / 2.0D);
if (d12 * d12 + d13 * d13 + d14 * d14 < 1.0D) {
BlockPos blockpos = new BlockPos(l1, i2, j2);
IBlockState state = worldIn.getBlockState(blockpos);
if (((StateMatcher) this.predicate).apply(state, blockpos, worldIn)) {
worldIn.setBlockState(blockpos, this.oreBlock, 2);
}
}
}
}
}
}
}
}
return true;
}
}