
* CraftBlockState#getWorldHandle() would previously run into a NPE when being invoked for a non-placed BlockState. It now returns null in this case. * CraftChest#getInventory() would previously encounter this NPE when being called for a non-placed BlockState. It will now automatically forward the call to #getBlockInventory() when it is being called for either a non-placed BlockState, or during world generation. * CraftStructureBlock#applyTo was able to run into a NPE when being called for non-placed BlockStates (this is for example called by #getSnapshotNBT()). * Replaced all no-world-generation preconditions with a general 'ensureNoWorldGeneration' method. * Removed a few superfluous #isPlaced() calls. If getWorldHandle() reutrns a World, this already implies that the BlockState is placed.
63 lines
1.9 KiB
Java
63 lines
1.9 KiB
Java
package org.bukkit.craftbukkit.block;
|
|
|
|
import com.google.common.base.Preconditions;
|
|
import net.minecraft.world.level.block.BlockDispenser;
|
|
import net.minecraft.world.level.block.Blocks;
|
|
import net.minecraft.world.level.block.entity.TileEntityDispenser;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.World;
|
|
import org.bukkit.block.Block;
|
|
import org.bukkit.block.Dispenser;
|
|
import org.bukkit.craftbukkit.CraftWorld;
|
|
import org.bukkit.craftbukkit.inventory.CraftInventory;
|
|
import org.bukkit.craftbukkit.projectiles.CraftBlockProjectileSource;
|
|
import org.bukkit.inventory.Inventory;
|
|
import org.bukkit.projectiles.BlockProjectileSource;
|
|
|
|
public class CraftDispenser extends CraftLootable<TileEntityDispenser> implements Dispenser {
|
|
|
|
public CraftDispenser(World world, TileEntityDispenser tileEntity) {
|
|
super(world, tileEntity);
|
|
}
|
|
|
|
@Override
|
|
public Inventory getSnapshotInventory() {
|
|
return new CraftInventory(this.getSnapshot());
|
|
}
|
|
|
|
@Override
|
|
public Inventory getInventory() {
|
|
if (!this.isPlaced()) {
|
|
return this.getSnapshotInventory();
|
|
}
|
|
|
|
return new CraftInventory(this.getTileEntity());
|
|
}
|
|
|
|
@Override
|
|
public BlockProjectileSource getBlockProjectileSource() {
|
|
Block block = getBlock();
|
|
|
|
if (block.getType() != Material.DISPENSER) {
|
|
return null;
|
|
}
|
|
|
|
return new CraftBlockProjectileSource((TileEntityDispenser) this.getTileEntityFromWorld());
|
|
}
|
|
|
|
@Override
|
|
public boolean dispense() {
|
|
ensureNoWorldGeneration();
|
|
Block block = getBlock();
|
|
if (block.getType() == Material.DISPENSER) {
|
|
CraftWorld world = (CraftWorld) this.getWorld();
|
|
BlockDispenser dispense = (BlockDispenser) Blocks.DISPENSER;
|
|
|
|
dispense.dispense(world.getHandle(), this.getPosition());
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|