blablubbabc 960f31098d
#937: Fixes related to unplaced BlockStates and the recent world generation changes.
* 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.
2021-10-10 07:55:53 +11:00

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;
}
}
}