Prevent unhinged tree generation by doing a sanity check on the block below/block of the tree placement (#1811)

This commit is contained in:
Jordan 2022-06-16 15:23:37 +01:00 committed by GitHub
parent 462bba4f87
commit 9395b3c575
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,6 +26,7 @@ import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.BlockTypes;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -80,10 +81,44 @@ public final class TreeGenerator {
} }
}, },
JUNGLE_BUSH("Jungle bush", "junglebush", "jungleshrub"), JUNGLE_BUSH("Jungle bush", "junglebush", "jungleshrub"),
RED_MUSHROOM("Red mushroom", "redmushroom", "redgiantmushroom"), //FAWE start - perform sanity check on generation location
BROWN_MUSHROOM("Brown mushroom", "brownmushroom", "browngiantmushroom"), RED_MUSHROOM("Red mushroom", "redmushroom", "redgiantmushroom"){
CRIMSON_FUNGUS("Crimson fungus", "crimsonfungus", "rednethermushroom"), @Override
WARPED_FUNGUS("Warped fungus", "warpedfungus", "greennethermushroom"), public boolean canGenerateOn(final BlockType type) {
return type == BlockTypes.MYCELIUM
|| type == BlockTypes.WARPED_NYLIUM
|| type == BlockTypes.CRIMSON_NYLIUM
|| super.canGenerateOn(type);
}
},
BROWN_MUSHROOM("Brown mushroom", "brownmushroom", "browngiantmushroom"){
@Override
public boolean canGenerateOn(final BlockType type) {
return type == BlockTypes.MYCELIUM
|| type == BlockTypes.WARPED_NYLIUM
|| type == BlockTypes.CRIMSON_NYLIUM
|| super.canGenerateOn(type);
}
},
CRIMSON_FUNGUS("Crimson fungus", "crimsonfungus", "rednethermushroom"){
@Override
public boolean canGenerateOn(final BlockType type) {
return type == BlockTypes.MYCELIUM
|| type == BlockTypes.WARPED_NYLIUM
|| type == BlockTypes.CRIMSON_NYLIUM
|| super.canGenerateOn(type);
}
},
WARPED_FUNGUS("Warped fungus", "warpedfungus", "greennethermushroom") {
@Override
public boolean canGenerateOn(final BlockType type) {
return type == BlockTypes.MYCELIUM
|| type == BlockTypes.WARPED_NYLIUM
|| type == BlockTypes.CRIMSON_NYLIUM
|| super.canGenerateOn(type);
}
},
//FAWE end
RANDOM_MUSHROOM("Random mushroom", "randmushroom", "randommushroom") { RANDOM_MUSHROOM("Random mushroom", "randmushroom", "randommushroom") {
@Override @Override
public boolean generate(EditSession editSession, BlockVector3 pos) throws MaxChangedBlocksException { public boolean generate(EditSession editSession, BlockVector3 pos) throws MaxChangedBlocksException {
@ -98,16 +133,33 @@ public final class TreeGenerator {
PINE("Pine tree", "pine") { PINE("Pine tree", "pine") {
@Override @Override
public boolean generate(EditSession editSession, BlockVector3 pos) throws MaxChangedBlocksException { public boolean generate(EditSession editSession, BlockVector3 pos) throws MaxChangedBlocksException {
makePineTree(editSession, pos); //FAWE start - perform sanity check on generation location
return true; if (editSession
.getBlockType(pos.getX(), pos.getY(), pos.getZ())
.getMaterial()
.isAir() && canGenerateOn(editSession.getBlockType(pos.getX(), pos.getY() - 1, pos.getZ()))) {
makePineTree(editSession, pos);
return true;
}
return false;
//FAWE end
} }
}, },
CHORUS_PLANT("Chorus plant", "chorusplant") { CHORUS_PLANT("Chorus plant", "chorusplant") {
@Override @Override
public boolean generate(EditSession editSession, BlockVector3 pos) throws MaxChangedBlocksException { public boolean generate(EditSession editSession, BlockVector3 pos) throws MaxChangedBlocksException {
//FAWE start - ensure canGenerateOn is called.
// chorus plants have to generate starting in the end stone itself, not the air above the ground // chorus plants have to generate starting in the end stone itself, not the air above the ground
return editSession.getWorld().generateTree(this, editSession, pos.subtract(0, 1, 0)); return super.generate(editSession, pos.subtract(0, 1, 0));
//FAWE end
} }
//FAWE start - perform sanity check on generation location
@Override
public boolean canGenerateOn(BlockType type) {
return type == BlockTypes.END_STONE;
}
//FAWE end
}, },
RANDOM("Random tree", "rand", "random") { RANDOM("Random tree", "rand", "random") {
@Override @Override
@ -151,9 +203,29 @@ public final class TreeGenerator {
} }
public boolean generate(EditSession editSession, BlockVector3 pos) throws MaxChangedBlocksException { public boolean generate(EditSession editSession, BlockVector3 pos) throws MaxChangedBlocksException {
return editSession.getWorld().generateTree(this, editSession, pos); //FAWE start - check for ability for tree to generate on block
if (editSession
.getBlockType(pos.getX(), pos.getY(), pos.getZ())
.getMaterial()
.isAir() && canGenerateOn(editSession.getBlockType(pos.getX(), pos.getY() - 1, pos.getZ()))) {
return editSession.getWorld().generateTree(this, editSession, pos);
}
return false;
//FAWE end
} }
//FAWE start - check for ability for tree to generate on block
public boolean canGenerateOn(BlockType type) {
return type == BlockTypes.DIRT
|| type == BlockTypes.GRASS_BLOCK
|| type == BlockTypes.PODZOL
|| type == BlockTypes.COARSE_DIRT
|| type == BlockTypes.MOSS_BLOCK
|| type == BlockTypes.ROOTED_DIRT
|| type == BlockTypes.MUD;
}
//FAWE end
/** /**
* Get user-friendly tree type name. * Get user-friendly tree type name.
* *