Files
src
build.gradle
settings.gradle
Code
4 170
src/main/java/com/kodari/oakgrove/OakGrovePlugin.java
package com.kodari.oakgrove;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Leaves;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.StructureGrowEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.TreeType;
import java.util.concurrent.ThreadLocalRandom;
public final class OakGrovePlugin extends JavaPlugin implements Listener {
private Material oakSapling;
private Material oakLog;
private Material oakLeaves;
@Override
public void onEnable() {
oakSapling = resolveMaterial("OAK_SAPLING");
oakLog = resolveMaterial("OAK_LOG");
oakLeaves = resolveMaterial("OAK_LEAVES");
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler(ignoreCancelled = true)
public void onOakGrow(StructureGrowEvent event) {
if (event.getSpecies() != TreeType.TREE && event.getSpecies() != TreeType.BIG_TREE) {
return;
}
Block base = findSaplingOrigin(event.getLocation().getBlock());
int height = ThreadLocalRandom.current().nextInt(5, 8);
if (!canPlaceTree(base, height)) {
return;
}
event.setCancelled(true);
placeTree(base, height);
}
private Block findSaplingOrigin(Block trigger) {
for (int xOffset = -1; xOffset <= 0; xOffset++) {
for (int zOffset = -1; zOffset <= 0; zOffset++) {
Block candidate = trigger.getRelative(xOffset, 0, zOffset);
if (isSapling(candidate)
&& isSapling(candidate.getRelative(1, 0, 0))
&& isSapling(candidate.getRelative(0, 0, 1))
&& isSapling(candidate.getRelative(1, 0, 1))) {
return candidate;
}
}
}
return trigger;
}
private boolean canPlaceTree(Block base, int height) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < 2; x++) {
for (int z = 0; z < 2; z++) {
if (!canReplace(base.getRelative(x, y, z))) {
return false;
}
}
}
}
for (int layer = 0; layer < 4; layer++) {
int radius = layer < 2 ? 2 : 1;
int y = height - 2 + layer;
for (int x = -radius; x <= 1 + radius; x++) {
for (int z = -radius; z <= 1 + radius; z++) {
if (isCanopyCorner(x, z, radius) || isTrunkPosition(x, z)) {
continue;
}
if (!canReplace(base.getRelative(x, y, z))) {
return false;
}
}
}
}
return true;
}
private void placeTree(Block base, int height) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < 2; x++) {
for (int z = 0; z < 2; z++) {
base.getRelative(x, y, z).setType(oakLog, false);
}
}
}
for (int layer = 0; layer < 4; layer++) {
int radius = layer < 2 ? 2 : 1;
int y = height - 2 + layer;
for (int x = -radius; x <= 1 + radius; x++) {
for (int z = -radius; z <= 1 + radius; z++) {
if (isCanopyCorner(x, z, radius) || isTrunkPosition(x, z)) {
continue;
}
Block leafBlock = base.getRelative(x, y, z);
leafBlock.setType(oakLeaves, false);
BlockData data = leafBlock.getBlockData();
if (data instanceof Leaves leaves) {
leaves.setPersistent(true);
leafBlock.setBlockData(leaves, false);
}
}
}
}
}
private boolean isSapling(Block block) {
return block.getType().equals(oakSapling);
}
private boolean canReplace(Block block) {
Material type = block.getType();
return type.isAir() || type.equals(oakSapling) || type.equals(oakLeaves);
}
private boolean isTrunkPosition(int x, int z) {
return x >= 0 && x <= 1 && z >= 0 && z <= 1;
}
private boolean isCanopyCorner(int x, int z, int radius) {
int min = -radius;
int max = 1 + radius;
return (x == min || x == max) && (z == min || z == max);
}
private Material resolveMaterial(String name) {
return XMaterial.matchXMaterial(name)
.map(XMaterial::parseMaterial)
.orElseThrow(() -> new IllegalStateException("Unsupported material: " + name));
}
}