Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
Add getHighestBlockYAt and spawnEntity to DecorationArea
Browse files Browse the repository at this point in the history
  • Loading branch information
rutgerkok committed Jun 21, 2021
1 parent 20e67e3 commit 895ebde
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.util.Objects;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_17_R1.block.CraftBanner;
import org.bukkit.craftbukkit.v1_17_R1.block.CraftBarrel;
import org.bukkit.craftbukkit.v1_17_R1.block.CraftBeacon;
Expand Down Expand Up @@ -38,12 +40,17 @@
import org.bukkit.craftbukkit.v1_17_R1.block.CraftSmoker;
import org.bukkit.craftbukkit.v1_17_R1.block.CraftStructureBlock;
import org.bukkit.craftbukkit.v1_17_R1.block.data.CraftBlockData;
import org.bukkit.entity.Entity;
import org.bukkit.event.entity.CreatureSpawnEvent;

import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.core.Registry;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.WorldGenRegion;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.SpawnGroupData;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.entity.BannerBlockEntity;
Expand Down Expand Up @@ -77,6 +84,7 @@
import net.minecraft.world.level.block.entity.StructureBlockEntity;
import net.minecraft.world.level.block.entity.TheEndGatewayBlockEntity;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.levelgen.Heightmap;
import nl.rutgerkok.worldgeneratorapi.decoration.DecorationArea;

class DecorationAreaImpl implements DecorationArea {
Expand Down Expand Up @@ -275,6 +283,11 @@ public int getCenterZ() {
return chunkPos.getMinBlockZ();
}

@Override
public int getHighestBlockYAt(int x, int z) {
return this.region.getHeight(Heightmap.Types.WORLD_SURFACE_WG, x, z);
}

@Override
public void setBlock(int x, int y, int z, Material material) {
setBlockData(x, y, z, material.createBlockData());
Expand Down Expand Up @@ -304,4 +317,30 @@ public void setBlockState(int x, int y, int z, BlockState blockState) {
}
}

}
@Override
public <T extends Entity> T spawnEntity(Class<T> entityClass, double x, double y, double z)
throws IllegalArgumentException {
// Inspired on
// https://github.com/PaperMC/Paper/blob/c4c6e26c00665989d3c1c82fb115a8f7f8de659b/patches/server/0714-Add-Feature-Generation-API.patch
Objects.requireNonNull(entityClass, "entityClass");

CraftWorld world = this.region.getMinecraftWorld().getWorld();
net.minecraft.world.entity.Entity entity = world.createEntity(new Location(world, x, y, z), entityClass);
if (entity == null) {
throw new IllegalArgumentException("No entity for " + entityClass);
}
if (entity instanceof Mob) {
((Mob) entity).finalizeSpawn(this.region, this.region
.getCurrentDifficultyAt(entity.blockPosition()), MobSpawnType.COMMAND, (SpawnGroupData) null, null);
}

// SpawnReason is unused by WorldGenRegion
this.region.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM);

// Should be safe, see world.createEntity
@SuppressWarnings("unchecked")
T bukkitEntity = (T) entity.getBukkitEntity();
return bukkitEntity;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.bukkit.block.Chest;
import org.bukkit.block.Furnace;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.Entity;

/**
* Represents a square area of 32 x 32 blocks which should be populated. (So 2x2
Expand Down Expand Up @@ -131,6 +133,18 @@ public interface DecorationArea {
*/
int getCenterZ();

/**
* Gets the highest non-empty coordinate at the given coordinates.
*
* @param x
* The block x in the world.
* @param z
* The block z in the world.
* @return The block y.
* @since 1.2
*/
int getHighestBlockYAt(int x, int z);

/**
* Sets the material at the given position.
*
Expand Down Expand Up @@ -182,4 +196,25 @@ public interface DecorationArea {
* @since 0.3
*/
void setBlockState(int x, int y, int z, BlockState blockState);

/**
* Spawns an entity in the world.
*
* @param <T>
* Type of the entity.
* @param entityClass
* Class of the entity, for example {@link Creeper}.class.
* @param x
* Block x in the world.
* @param y
* Block y in the world.
* @param z
* Block z in the world.
* @return The entity, so that you can modify it further.
* @throws IllegalArgumentException
* If spawning entities of this entity type is not possible.
* @since 1.2
*/
<T extends Entity> T spawnEntity(Class<T> entityClass, double x, double y, double z)
throws IllegalArgumentException;
}

0 comments on commit 895ebde

Please sign in to comment.