Skip to content

Commit

Permalink
add biome change per player
Browse files Browse the repository at this point in the history
  • Loading branch information
yannicklamprecht committed Dec 26, 2024
1 parent a07cec1 commit 081d848
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 129 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.papermc.paper.event.packet;

import org.bukkit.BiomesSnapshot;
import org.bukkit.Chunk;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.world.ChunkEvent;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.NullMarked;

/**
* Is called when a {@link Player} receives {@link org.bukkit.block.Biome}s
* <p>
* Can for example be used for replacing Biomes when the player receives the Biome list.
* <p>
* Should only be used for packet/clientside related stuff.
* Not intended for modifying server side state.
*/
@NullMarked
public class PlayerBiomesLoadEvent extends ChunkEvent {

private static final HandlerList HANDLER_LIST = new HandlerList();

private final Player player;
private @Nullable BiomesSnapshot biomesSnapshot;

@ApiStatus.Internal
public PlayerBiomesLoadEvent(final Player player, Chunk chunk) {
super(chunk);
this.player = player;
}

@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}

public static HandlerList getHandlerList() {
return HANDLER_LIST;
}

/**
* Returns the player that is receiving the biomes
*
* @return the player
*/
public Player getPlayer() {
return player;
}

/**
* Returns a biomes snapshot for the given chunk, by default it won't be set.
*
* @return biome snapshot if one is set
*/
public @Nullable BiomesSnapshot getBiomeSnapshot() {
return biomesSnapshot;
}

/**
* Sets the biome snapshot for the given chunk that will be sent as an override to the player
*
* @param biomesSnapshot the biome override
*/
public void setBiomeSnapshot(@Nullable final BiomesSnapshot biomesSnapshot) {
this.biomesSnapshot = biomesSnapshot;
}

/**
* Returns if chunk biomes were overridden
*
* @return true if override was made, else false
*/
public boolean hasOverrides() {
return biomesSnapshot != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
* Not intended for modifying server side state.
*/
@NullMarked
public class PlayerChunkLoadEvent extends ChunkEvent {
public class PlayerChunkLoadEvent extends PlayerBiomesLoadEvent {

private static final HandlerList HANDLER_LIST = new HandlerList();

private final Player player;

@ApiStatus.Internal
public PlayerChunkLoadEvent(final Chunk chunk, final Player player) {
super(chunk);
super(player, chunk);
this.player = player;
}

Expand Down
73 changes: 73 additions & 0 deletions paper-api/src/main/java/org/bukkit/BiomesSnapshot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.bukkit;

import org.bukkit.block.Biome;
import org.jspecify.annotations.NullMarked;

/**
* Represents a static, thread-safe snapshot of chunk of biomes.
* <p>
* Purpose is to allow clean, efficient copy of a biome data to be made, and
* then handed off for processing in another thread
*/
@NullMarked
public interface BiomesSnapshot {

/**
* Gets the X-coordinate of this chunk
*
* @return X-coordinate
*/
int getX();

/**
* Gets the Z-coordinate of this chunk
*
* @return Z-coordinate
*/
int getZ();

/**
* Gets name of the world containing this chunk
*
* @return Parent World Name
*/
String getWorldName();

/**
* Get biome at given coordinates
*
* @param x X-coordinate (0-15)
* @param y Y-coordinate (world minHeight (inclusive) - world maxHeight (exclusive))
* @param z Z-coordinate (0-15)
* @return Biome at given coordinate
*/
Biome getBiome(int x, int y, int z);

/**
* Get biome at given coordinates
*
* @param x X-coordinate (0-15)
* @param y Y-coordinate (world minHeight (inclusive) - world maxHeight (exclusive))
* @param z Z-coordinate (0-15)
* @param biome the biome to set at the give coordinate
*/
void setBiome(int x, int y, int z, Biome biome);

/**
* Get raw biome temperature at given coordinates
*
* @param x X-coordinate (0-15)
* @param y Y-coordinate (world minHeight (inclusive) - world maxHeight (exclusive))
* @param z Z-coordinate (0-15)
* @return temperature at given coordinate
*/
double getRawBiomeTemperature(int x, int y, int z);

/**
* Tests if this chunk contains the specified biome.
*
* @param biome biome to test
* @return if the biome is contained within
*/
boolean contains(Biome biome);
}
53 changes: 1 addition & 52 deletions paper-api/src/main/java/org/bukkit/ChunkSnapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,7 @@
* Purpose is to allow clean, efficient copy of a chunk data to be made, and
* then handed off for processing in another thread (e.g. map rendering)
*/
public interface ChunkSnapshot {

/**
* Gets the X-coordinate of this chunk
*
* @return X-coordinate
*/
int getX();

/**
* Gets the Z-coordinate of this chunk
*
* @return Z-coordinate
*/
int getZ();

/**
* Gets name of the world containing this chunk
*
* @return Parent World Name
*/
@NotNull
String getWorldName();
public interface ChunkSnapshot extends BiomesSnapshot {

/**
* Get block type for block at corresponding coordinate in the chunk
Expand Down Expand Up @@ -110,17 +88,6 @@ public interface ChunkSnapshot {
@Deprecated(since = "1.15")
Biome getBiome(int x, int z);

/**
* Get biome at given coordinates
*
* @param x X-coordinate (0-15)
* @param y Y-coordinate (world minHeight (inclusive) - world maxHeight (exclusive))
* @param z Z-coordinate (0-15)
* @return Biome at given coordinate
*/
@NotNull
Biome getBiome(int x, int y, int z);

/**
* Get raw biome temperature at given coordinates
*
Expand All @@ -132,16 +99,6 @@ public interface ChunkSnapshot {
@Deprecated(since = "1.15")
double getRawBiomeTemperature(int x, int z);

/**
* Get raw biome temperature at given coordinates
*
* @param x X-coordinate (0-15)
* @param y Y-coordinate (world minHeight (inclusive) - world maxHeight (exclusive))
* @param z Z-coordinate (0-15)
* @return temperature at given coordinate
*/
double getRawBiomeTemperature(int x, int y, int z);

/**
* Get world full time when chunk snapshot was captured
*
Expand All @@ -164,12 +121,4 @@ public interface ChunkSnapshot {
* @return if the block is contained within
*/
boolean contains(@NotNull BlockData block);

/**
* Tests if this chunk contains the specified biome.
*
* @param biome biome to test
* @return if the biome is contained within
*/
boolean contains(@NotNull Biome biome);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.bukkit.craftbukkit;

import com.google.common.base.Preconditions;
import com.google.common.base.Predicates;
import java.util.Objects;
import java.util.function.Predicate;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.world.level.chunk.PalettedContainer;
import net.minecraft.world.level.chunk.PalettedContainerRO;
import org.bukkit.BiomesSnapshot;
import org.bukkit.block.Biome;
import org.bukkit.craftbukkit.block.CraftBiome;
import org.jspecify.annotations.NullMarked;

@NullMarked
public class CraftBiomesSnapshot implements BiomesSnapshot {

private final int x, z;
private final String worldname;
private final int minHeight, maxHeight, seaLevel;
private final Registry<net.minecraft.world.level.biome.Biome> biomeRegistry;
private final PalettedContainer<Holder<net.minecraft.world.level.biome.Biome>>[] biome;

public CraftBiomesSnapshot(final int x, final int z, final String worldname, final int minHeight, final int maxHeight, int seaLevel, final Registry<net.minecraft.world.level.biome.Biome> registry, final PalettedContainer<Holder<net.minecraft.world.level.biome.Biome>>[] biome) {
this.x = x;
this.z = z;
this.worldname = worldname;
this.minHeight = minHeight;
this.maxHeight = maxHeight;
this.biomeRegistry = registry;
this.biome = biome;
this.seaLevel = seaLevel;
}

@Override
public int getX() {
return this.x;
}

@Override
public int getZ() {
return this.z;
}

@Override
public String getWorldName() {
return this.worldname;
}

@Override
public void setBiome(final int x, final int y, final int z, final Biome biome) {
Preconditions.checkState(this.biome != null, "ChunkSnapshot created without biome. Please call getSnapshot with includeBiome=true");
Objects.requireNonNull(biome, "biome cannot be null");
this.validateChunkCoordinates(x, y, z);
PalettedContainer<Holder<net.minecraft.world.level.biome.Biome>> biomeLocal = this.biome[this.getSectionIndex(y)];
biomeLocal.set(x >> 2, (y & 0xF) >> 2, z >> 2, CraftBiome.bukkitToMinecraftHolder(biome));
}

@Override
public final double getRawBiomeTemperature(int x, int y, int z) {
Preconditions.checkState(this.biome != null, "ChunkSnapshot created without biome. Please call getSnapshot with includeBiome=true");
this.validateChunkCoordinates(x, y, z);

PalettedContainerRO<Holder<net.minecraft.world.level.biome.Biome>> biome = this.biome[this.getSectionIndex(y)]; // SPIGOT-7188: Don't need to convert y to biome coordinate scale since it is bound to the block chunk section
return biome.get(x >> 2, (y & 0xF) >> 2, z >> 2).value().getTemperature(new BlockPos((this.x << 4) | x, y, (this.z << 4) | z), seaLevel);
}

@Override
public final Biome getBiome(int x, int y, int z) {
Preconditions.checkState(this.biome != null, "ChunkSnapshot created without biome. Please call getSnapshot with includeBiome=true");
this.validateChunkCoordinates(x, y, z);

PalettedContainerRO<Holder<net.minecraft.world.level.biome.Biome>> biome = this.biome[this.getSectionIndex(y)]; // SPIGOT-7188: Don't need to convert y to biome coordinate scale since it is bound to the block chunk section
return CraftBiome.minecraftHolderToBukkit(biome.get(x >> 2, (y & 0xF) >> 2, z >> 2));
}


@Override
public boolean contains(Biome biome) {
Preconditions.checkArgument(biome != null, "Biome cannot be null");

Predicate<Holder<net.minecraft.world.level.biome.Biome>> nms = Predicates.equalTo(CraftBiome.bukkitToMinecraftHolder(biome));
for (PalettedContainerRO<Holder<net.minecraft.world.level.biome.Biome>> palette : this.biome) {
if (palette.maybeHas(nms)) {
return true;
}
}

return false;
}

public PalettedContainer<Holder<net.minecraft.world.level.biome.Biome>>[] getBiome(){
return biome;
}

protected void validateChunkCoordinates(int x, int y, int z) {
CraftChunk.validateChunkCoordinates(this.minHeight, this.maxHeight, x, y, z);
}

protected int getSectionIndex(int y) {
return (y - this.minHeight) >> 4;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public ChunkSnapshot getChunkSnapshot(boolean includeMaxBlockY, boolean includeB
byte[][] sectionEmitLights = includeLightData ? new byte[cs.length][] : null;
// Paper end - Add getChunkSnapshot includeLightData parameter
boolean[] sectionEmpty = new boolean[cs.length];
PalettedContainerRO<Holder<net.minecraft.world.level.biome.Biome>>[] biome = (includeBiome || includeBiomeTempRain) ? new PalettedContainer[cs.length] : null;
PalettedContainer<Holder<net.minecraft.world.level.biome.Biome>>[] biome = (includeBiome || includeBiomeTempRain) ? new PalettedContainer[cs.length] : null;

Registry<net.minecraft.world.level.biome.Biome> iregistry = this.worldServer.registryAccess().lookupOrThrow(Registries.BIOME);

Expand Down
Loading

0 comments on commit 081d848

Please sign in to comment.