Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions paper-api/src/main/java/org/bukkit/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public interface Block extends Metadatable, Translatable, net.kyori.adventure.tr
@NotNull
Material getType();

/**
* Gets the type of this block
*
* @return block type
*/
@NotNull
BlockType getBlockType();

/**
* Gets the light level between 0-15
*
Expand Down Expand Up @@ -330,6 +338,34 @@ public static int getBlockKeyZ(long packed) {
*/
void setType(@NotNull Material type, boolean applyPhysics);

/**
* Sets the type of this block
*
* @param type BlockType to change this block to
*/
void setType(BlockType type);

/**
* Sets the type of this block
*
* <br>
* Note that applyPhysics = false is not in general safe. It should only be
* used when you need to avoid triggering a physics update of neighboring
* blocks, for example when creating a {@link Bisected} block. If you are
* using a custom populator, then this parameter may also be required to
* prevent triggering infinite chunk loads on border blocks. This method
* should NOT be used to "hack" physics by placing blocks in impossible
* locations. Such blocks are liable to be removed on various events such as
* world upgrades. Furthermore setting large amounts of such blocks in close
* proximity may overload the server physics engine if an update is
* triggered at a later point. If this occurs, the resulting behavior is
* undefined.
*
* @param type BlockType to change this block to
* @param applyPhysics False to cancel physics on the changed block.
*/
void setType(BlockType type, boolean applyPhysics);

/**
* Gets the face relation of this block compared to the given block.
* <p>
Expand Down
15 changes: 15 additions & 0 deletions paper-api/src/main/java/org/bukkit/block/BlockState.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public interface BlockState extends Metadatable {
@NotNull
Material getType();

/**
* Gets the type of this block state.
*
* @return block type
*/
@NotNull
BlockType getBlockType();

/**
* Gets the current light level of the block represented by this block state.
*
Expand Down Expand Up @@ -172,6 +180,13 @@ public interface BlockState extends Metadatable {
*/
void setType(@NotNull Material type);

/**
* Sets the type of this block state.
*
* @param type BlockType to change this block state to
*/
void setType(@NotNull BlockType type);

/**
* Attempts to update the block represented by this state, setting it to
* the new values as defined by this state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.BlockSupport;
import org.bukkit.block.BlockType;
import org.bukkit.block.PistonMoveReaction;
import org.bukkit.block.structure.Mirror;
import org.bukkit.block.structure.StructureRotation;
Expand All @@ -27,6 +28,14 @@ public interface BlockData extends Cloneable {
@NotNull
Material getMaterial();

/**
* Get the BlockType represented by this block data.
*
* @return the block type
*/
@NotNull
BlockType getBlockType();

/**
* Gets a string, which when passed into a method such as
* {@link Server#createBlockData(java.lang.String)} will unambiguously
Expand Down
22 changes: 22 additions & 0 deletions paper-api/src/main/java/org/bukkit/inventory/ItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ public Material getType() {
return this.craftDelegate.getType(); // Paper - delegate
}

/**
* Gets the type of this item
*
* @return Type of the items in this stack
*/
@NotNull
public ItemType getItemType() {
return this.craftDelegate.getItemType();
}

/**
* Sets the type of this item
* <p>
Expand Down Expand Up @@ -230,6 +240,18 @@ public void setType(@NotNull Material type) {
public ItemStack withType(@NotNull Material type) {
return this.craftDelegate.withType(type); // Paper - delegate
}

/**
* Creates a new ItemStack with the specified ItemType, where the item count and item meta is preserved.
*
* @param type The ItemType of the new ItemStack.
* @return A new ItemStack instance with the specified ItemType.
*/
@NotNull
@org.jetbrains.annotations.Contract(value = "_ -> new", pure = true)
public ItemStack withType(@NotNull ItemType type) {
return this.craftDelegate.withType(type);
}
// Paper end

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- a/net/minecraft/world/level/block/state/BlockState.java
+++ b/net/minecraft/world/level/block/state/BlockState.java
@@ -10,6 +_,17 @@
@@ -10,6 +_,28 @@
public class BlockState extends BlockBehaviour.BlockStateBase {
public static final Codec<BlockState> CODEC = codec(BuiltInRegistries.BLOCK.byNameCodec(), Block::defaultBlockState).stable();

Expand All @@ -14,6 +14,17 @@
+ return this.cachedMaterial;
+ }
+ // Paper end - optimise getType calls
+
+ // Paper start - add BlockType getter
+ org.bukkit.block.@org.jspecify.annotations.Nullable BlockType cachedBlockType;
+
+ public final org.bukkit.block.BlockType getBukkitBlockType() {
+ if (this.cachedBlockType == null) {
+ this.cachedBlockType = org.bukkit.craftbukkit.block.CraftBlockType.minecraftToBukkitNew(this.getBlock());
+ }
+ return this.cachedBlockType;
+ }
+ // Paper end - add BlockType getter
+
public BlockState(Block owner, Reference2ObjectArrayMap<Property<?>, Comparable<?>> values, MapCodec<BlockState> propertiesCodec) {
super(owner, values, propertiesCodec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.BlockType;
import org.bukkit.block.PistonMoveReaction;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.CraftFluidCollisionMode;
Expand Down Expand Up @@ -180,6 +181,17 @@ public void setType(Material type, boolean applyPhysics) {
this.setBlockData(type.createBlockData(), applyPhysics);
}

@Override
public void setType(final BlockType type) {
this.setType(type, true);
}

@Override
public void setType(BlockType type, boolean applyPhysics) {
Preconditions.checkArgument(type != null, "BlockType cannot be null");
this.setBlockData(type.createBlockData(), applyPhysics);
}

@Override
public void setBlockData(BlockData data) {
this.setBlockData(data, true);
Expand Down Expand Up @@ -230,6 +242,11 @@ public Material getType() {
return this.world.getBlockState(this.position).getBukkitMaterial(); // Paper - optimise getType calls
}

@Override
public BlockType getBlockType() {
return this.world.getBlockState(this.position).getBukkitBlockType();
}

@Override
public byte getLightLevel() {
return (byte) this.world.getMinecraftWorld().getMaxLocalRawBrightness(this.position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.BlockType;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.block.data.CraftBlockData;
Expand Down Expand Up @@ -175,11 +176,25 @@ public void setType(final Material type) {
}
}

@Override
public void setType(final BlockType type) {
Preconditions.checkArgument(type != null, "BlockType cannot be null");

if (this.getBlockType().equals(type)) {
this.data = CraftBlockType.bukkitToMinecraftNew(type).defaultBlockState();
}
}

@Override
public Material getType() {
return this.data.getBukkitMaterial(); // Paper - optimise getType calls
}

@Override
public BlockType getBlockType() {
return this.data.getBukkitBlockType();
}

public void setFlags(int flags) {
this.capturedFlags = flags;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public Material getMaterial() {
return this.state.getBukkitMaterial(); // Paper - optimise getType calls
}

@Override
public BlockType getBlockType() {
return this.state.getBukkitBlockType();
}

public net.minecraft.world.level.block.state.BlockState getState() {
return this.state;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ItemType;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.MaterialData;
import org.bukkit.persistence.PersistentDataContainer;
Expand Down Expand Up @@ -194,6 +195,11 @@ public Material getType() {
return this.handle != null ? CraftItemType.minecraftToBukkit(this.handle.getItem()) : Material.AIR;
}

@Override
public ItemType getItemType() {
return this.handle != null ? CraftItemType.minecraftToBukkitNew(this.handle.getItem()) : ItemType.AIR;
}

@Override
public void setType(Material type) {
if (this.getType() == type) {
Expand Down Expand Up @@ -484,6 +490,25 @@ public ItemStack withType(final Material type) {
}
// Paper end

@Override
public ItemStack withType(final ItemType type) {
if (type == ItemType.AIR) {
return CraftItemStack.asCraftMirror(null);
}

final net.minecraft.world.item.ItemStack copy = new net.minecraft.world.item.ItemStack(
CraftItemType.bukkitToMinecraftNew(type), this.getAmount()
);

if (this.handle != null) {
copy.applyComponents(this.handle.getComponentsPatch());
}

final CraftItemStack mirrored = CraftItemStack.asCraftMirror(copy);
mirrored.setItemMeta(mirrored.getItemMeta());
return mirrored;
}

public static final String PDC_CUSTOM_DATA_KEY = "PublicBukkitValues";
private net.minecraft.nbt.CompoundTag getPdcTag() {
if (this.handle == null) {
Expand Down