Implement BlockStateMeta which allows creating and editting 1.8's blockEntityTag

By: Thinkofdeath <thinkofdeath@spigotmc.org>
This commit is contained in:
CraftBukkit/Spigot
2015-03-24 15:37:15 +00:00
parent 27e257894a
commit 4a37ed5f42
20 changed files with 528 additions and 131 deletions

View File

@@ -6,6 +6,7 @@ import net.minecraft.server.NBTTagCompound;
import net.minecraft.server.NBTTagList;
import net.minecraft.server.TileEntityBanner;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.Banner;
import org.bukkit.block.Block;
import org.bukkit.block.banner.Pattern;
@@ -34,6 +35,20 @@ public class CraftBanner extends CraftBlockState implements Banner {
}
}
public CraftBanner(final Material material, final TileEntityBanner te) {
super(material);
banner = te;
base = DyeColor.getByDyeData((byte) banner.color);
if (banner.patterns != null) {
for (int i = 0; i < banner.patterns.size(); i++) {
NBTTagCompound p = (NBTTagCompound) banner.patterns.get(i);
patterns.add(new Pattern(DyeColor.getByDyeData((byte) p.getInt("Color")), PatternType.getByIdentifier(p.getString("Pattern"))));
}
}
}
@Override
public DyeColor getBaseColor() {
return this.base;

View File

@@ -1,6 +1,7 @@
package org.bukkit.craftbukkit.block;
import net.minecraft.server.TileEntityBeacon;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Beacon;
@@ -19,6 +20,12 @@ public class CraftBeacon extends CraftBlockState implements Beacon {
beacon = (TileEntityBeacon) world.getTileEntityAt(getX(), getY(), getZ());
}
public CraftBeacon(final Material material, final TileEntityBeacon te) {
super(material);
world = null;
beacon = te;
}
public Inventory getInventory() {
return new CraftInventoryBeacon(beacon);
}
@@ -33,5 +40,10 @@ public class CraftBeacon extends CraftBlockState implements Beacon {
return result;
}
@Override
public TileEntityBeacon getTileEntity() {
return beacon;
}
}

View File

@@ -14,6 +14,7 @@ import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import java.util.List;
import net.minecraft.server.TileEntity;
public class CraftBlockState implements BlockState {
private final CraftWorld world;
@@ -44,6 +45,14 @@ public class CraftBlockState implements BlockState {
this.flag = flag;
}
public CraftBlockState(Material material) {
world = null;
type = material.getId();
light = 0;
chunk = null;
x = y = z = 0;
}
public static CraftBlockState getBlockState(net.minecraft.server.World world, int x, int y, int z) {
return new CraftBlockState(world.getWorld().getBlockAt(x, y, z));
}
@@ -53,6 +62,7 @@ public class CraftBlockState implements BlockState {
}
public World getWorld() {
requirePlaced();
return world;
}
@@ -69,6 +79,7 @@ public class CraftBlockState implements BlockState {
}
public Chunk getChunk() {
requirePlaced();
return chunk;
}
@@ -125,6 +136,7 @@ public class CraftBlockState implements BlockState {
}
public Block getBlock() {
requirePlaced();
return world.getBlockAt(x, y, z);
}
@@ -137,6 +149,7 @@ public class CraftBlockState implements BlockState {
}
public boolean update(boolean force, boolean applyPhysics) {
requirePlaced();
Block block = getBlock();
if (block.getType() != getType()) {
@@ -227,19 +240,38 @@ public class CraftBlockState implements BlockState {
return hash;
}
public TileEntity getTileEntity() {
return null;
}
public void setMetadata(String metadataKey, MetadataValue newMetadataValue) {
requirePlaced();
chunk.getCraftWorld().getBlockMetadata().setMetadata(getBlock(), metadataKey, newMetadataValue);
}
public List<MetadataValue> getMetadata(String metadataKey) {
requirePlaced();
return chunk.getCraftWorld().getBlockMetadata().getMetadata(getBlock(), metadataKey);
}
public boolean hasMetadata(String metadataKey) {
requirePlaced();
return chunk.getCraftWorld().getBlockMetadata().hasMetadata(getBlock(), metadataKey);
}
public void removeMetadata(String metadataKey, Plugin owningPlugin) {
requirePlaced();
chunk.getCraftWorld().getBlockMetadata().removeMetadata(getBlock(), metadataKey, owningPlugin);
}
@Override
public boolean isPlaced() {
return world != null;
}
protected void requirePlaced() {
if (!isPlaced()) {
throw new IllegalStateException("The blockState must be placed to call this method");
}
}
}

View File

@@ -1,6 +1,7 @@
package org.bukkit.craftbukkit.block;
import net.minecraft.server.TileEntityBrewingStand;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BrewingStand;
import org.bukkit.craftbukkit.CraftWorld;
@@ -16,6 +17,11 @@ public class CraftBrewingStand extends CraftBlockState implements BrewingStand {
brewingStand = (TileEntityBrewingStand) ((CraftWorld) block.getWorld()).getTileEntityAt(getX(), getY(), getZ());
}
public CraftBrewingStand(final Material material, final TileEntityBrewingStand te) {
super(material);
brewingStand = te;
}
public BrewerInventory getInventory() {
return new CraftInventoryBrewer(brewingStand);
}
@@ -38,4 +44,9 @@ public class CraftBrewingStand extends CraftBlockState implements BrewingStand {
public void setBrewingTime(int brewTime) {
brewingStand.brewTime = brewTime;
}
@Override
public TileEntityBrewingStand getTileEntity() {
return brewingStand;
}
}

View File

@@ -22,6 +22,12 @@ public class CraftChest extends CraftBlockState implements Chest {
chest = (TileEntityChest) world.getTileEntityAt(getX(), getY(), getZ());
}
public CraftChest(final Material material, final TileEntityChest te) {
super(material);
chest = te;
world = null;
}
public Inventory getBlockInventory() {
return new CraftInventory(chest);
}
@@ -32,6 +38,9 @@ public class CraftChest extends CraftBlockState implements Chest {
int z = getZ();
// The logic here is basically identical to the logic in BlockChest.interact
CraftInventory inventory = new CraftInventory(chest);
if (!isPlaced()) {
return inventory;
}
int id;
if (world.getBlockTypeIdAt(x, y, z) == Material.CHEST.getId()) {
id = Material.CHEST.getId();
@@ -70,4 +79,9 @@ public class CraftChest extends CraftBlockState implements Chest {
return result;
}
@Override
public TileEntityChest getTileEntity() {
return chest;
}
}

View File

@@ -1,6 +1,7 @@
package org.bukkit.craftbukkit.block;
import net.minecraft.server.TileEntityCommand;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.CommandBlock;
import org.bukkit.craftbukkit.CraftWorld;
@@ -19,6 +20,13 @@ public class CraftCommandBlock extends CraftBlockState implements CommandBlock {
name = commandBlock.getCommandBlock().getName();
}
public CraftCommandBlock(final Material material, final TileEntityCommand te) {
super(material);
commandBlock = te;
command = commandBlock.getCommandBlock().getCommand();
name = commandBlock.getCommandBlock().getName();
}
public String getCommand() {
return command;
}
@@ -45,4 +53,9 @@ public class CraftCommandBlock extends CraftBlockState implements CommandBlock {
return result;
}
@Override
public TileEntityCommand getTileEntity() {
return commandBlock;
}
}

View File

@@ -1,6 +1,7 @@
package org.bukkit.craftbukkit.block;
import net.minecraft.server.TileEntityMobSpawner;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.CreatureSpawner;
@@ -17,6 +18,11 @@ public class CraftCreatureSpawner extends CraftBlockState implements CreatureSpa
spawner = (TileEntityMobSpawner) ((CraftWorld) block.getWorld()).getTileEntityAt(getX(), getY(), getZ());
}
public CraftCreatureSpawner(final Material material, TileEntityMobSpawner te) {
super(material);
spawner = te;
}
@Deprecated
public CreatureType getCreatureType() {
return CreatureType.fromName(spawner.getSpawner().getMobName());
@@ -70,4 +76,8 @@ public class CraftCreatureSpawner extends CraftBlockState implements CreatureSpa
spawner.getSpawner().spawnDelay = delay;
}
@Override
public TileEntityMobSpawner getTileEntity() {
return spawner;
}
}

View File

@@ -25,6 +25,12 @@ public class CraftDispenser extends CraftBlockState implements Dispenser {
dispenser = (TileEntityDispenser) world.getTileEntityAt(getX(), getY(), getZ());
}
public CraftDispenser(final Material material, final TileEntityDispenser te) {
super(material);
world = null;
dispenser = te;
}
public Inventory getInventory() {
return new CraftInventory(dispenser);
}
@@ -62,4 +68,9 @@ public class CraftDispenser extends CraftBlockState implements Dispenser {
return result;
}
@Override
public TileEntityDispenser getTileEntity() {
return dispenser;
}
}

View File

@@ -23,6 +23,12 @@ public class CraftDropper extends CraftBlockState implements Dropper {
dropper = (TileEntityDropper) world.getTileEntityAt(getX(), getY(), getZ());
}
public CraftDropper(final Material material, TileEntityDropper te) {
super(material);
world = null;
dropper = te;
}
public Inventory getInventory() {
return new CraftInventory(dropper);
}
@@ -47,4 +53,9 @@ public class CraftDropper extends CraftBlockState implements Dropper {
return result;
}
@Override
public TileEntityDropper getTileEntity() {
return dropper;
}
}

View File

@@ -1,6 +1,7 @@
package org.bukkit.craftbukkit.block;
import net.minecraft.server.TileEntityFurnace;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Furnace;
import org.bukkit.craftbukkit.CraftWorld;
@@ -16,6 +17,11 @@ public class CraftFurnace extends CraftBlockState implements Furnace {
furnace = (TileEntityFurnace) ((CraftWorld) block.getWorld()).getTileEntityAt(getX(), getY(), getZ());
}
public CraftFurnace(final Material material, final TileEntityFurnace te) {
super(material);
furnace = te;
}
public FurnaceInventory getInventory() {
return new CraftInventoryFurnace(furnace);
}
@@ -46,4 +52,9 @@ public class CraftFurnace extends CraftBlockState implements Furnace {
public void setCookTime(short cookTime) {
furnace.cookTime = cookTime;
}
@Override
public TileEntityFurnace getTileEntity() {
return furnace;
}
}

View File

@@ -1,6 +1,7 @@
package org.bukkit.craftbukkit.block;
import net.minecraft.server.TileEntityHopper;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Hopper;
import org.bukkit.craftbukkit.CraftWorld;
@@ -16,6 +17,12 @@ public class CraftHopper extends CraftBlockState implements Hopper {
hopper = (TileEntityHopper) ((CraftWorld) block.getWorld()).getTileEntityAt(getX(), getY(), getZ());
}
public CraftHopper(final Material material, final TileEntityHopper te) {
super(material);
hopper = te;
}
public Inventory getInventory() {
return new CraftInventory(hopper);
}
@@ -30,4 +37,9 @@ public class CraftHopper extends CraftBlockState implements Hopper {
return result;
}
@Override
public TileEntityHopper getTileEntity() {
return hopper;
}
}

View File

@@ -20,6 +20,12 @@ public class CraftJukebox extends CraftBlockState implements Jukebox {
jukebox = (TileEntityRecordPlayer) world.getTileEntityAt(getX(), getY(), getZ());
}
public CraftJukebox(final Material material, TileEntityRecordPlayer te) {
super(material);
world = null;
jukebox = te;
}
@Override
public Material getPlaying() {
ItemStack record = jukebox.getRecord();
@@ -37,6 +43,9 @@ public class CraftJukebox extends CraftBlockState implements Jukebox {
} else {
jukebox.setRecord(new ItemStack(CraftMagicNumbers.getItem(record), 1));
}
if (!isPlaced()) {
return;
}
jukebox.update();
if (record == Material.AIR) {
world.getHandle().setTypeAndData(new BlockPosition(getX(), getY(), getZ()),
@@ -55,8 +64,14 @@ public class CraftJukebox extends CraftBlockState implements Jukebox {
}
public boolean eject() {
requirePlaced();
boolean result = isPlaying();
((BlockJukeBox) Blocks.JUKEBOX).dropRecord(world.getHandle(), new BlockPosition(getX(), getY(), getZ()), null);
return result;
}
@Override
public TileEntityRecordPlayer getTileEntity() {
return jukebox;
}
}

View File

@@ -22,6 +22,12 @@ public class CraftNoteBlock extends CraftBlockState implements NoteBlock {
note = (TileEntityNote) world.getTileEntityAt(getX(), getY(), getZ());
}
public CraftNoteBlock(final Material material, final TileEntityNote te) {
super(material);
world = null;
note = te;
}
public Note getNote() {
return new Note(note.note);
}
@@ -72,4 +78,9 @@ public class CraftNoteBlock extends CraftBlockState implements NoteBlock {
return false;
}
}
@Override
public TileEntityNote getTileEntity() {
return note;
}
}

View File

@@ -3,6 +3,7 @@ package org.bukkit.craftbukkit.block;
import net.minecraft.server.ChatComponentText;
import net.minecraft.server.IChatBaseComponent;
import net.minecraft.server.TileEntitySign;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.craftbukkit.CraftWorld;
@@ -21,6 +22,13 @@ public class CraftSign extends CraftBlockState implements Sign {
System.arraycopy(revertComponents(sign.lines), 0, lines, 0, lines.length);
}
public CraftSign(final Material material, final TileEntitySign te) {
super(material);
sign = te;
lines = new String[sign.lines.length];
System.arraycopy(revertComponents(sign.lines), 0, lines, 0, lines.length);
}
public String[] getLines() {
return lines;
}
@@ -71,4 +79,9 @@ public class CraftSign extends CraftBlockState implements Sign {
private static String revertComponent(IChatBaseComponent component) {
return CraftChatMessage.fromComponent(component);
}
@Override
public TileEntitySign getTileEntity() {
return sign;
}
}

View File

@@ -3,6 +3,7 @@ package org.bukkit.craftbukkit.block;
import com.mojang.authlib.GameProfile;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.TileEntitySkull;
import org.bukkit.Material;
import org.bukkit.SkullType;
import org.bukkit.block.Block;
@@ -27,6 +28,14 @@ public class CraftSkull extends CraftBlockState implements Skull {
rotation = (byte) skull.getRotation();
}
public CraftSkull(final Material material, final TileEntitySkull te) {
super(material);
skull = te;
profile = skull.getGameProfile();
skullType = getSkullType(skull.getSkullType());
rotation = (byte) skull.getRotation();
}
static SkullType getSkullType(int id) {
switch (id) {
case 0:
@@ -202,4 +211,9 @@ public class CraftSkull extends CraftBlockState implements Skull {
return result;
}
@Override
public TileEntitySkull getTileEntity() {
return skull;
}
}