Skip to content

Commit

Permalink
Rewrote block parsing, and further switch to BlockState
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Aug 4, 2018
1 parent 5f5a179 commit 811f1d4
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 403 deletions.
Expand Up @@ -28,7 +28,6 @@
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.BlockType;
import com.sk89q.worldedit.blocks.ItemID;
import com.sk89q.worldedit.blocks.SkullBlock;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.util.Location;
import org.bukkit.DyeColor;
Expand Down Expand Up @@ -136,9 +135,6 @@ public static BaseBlock toBlock(com.sk89q.worldedit.world.World world, ItemStack
}
break;

case ItemID.HEAD:
return new SkullBlock(0, (byte) itemStack.getDurability());

default:
final BaseBlock baseBlock = BlockType.getBlockForItem(typeId, itemStack.getDurability());
if (baseBlock != null) {
Expand Down
Expand Up @@ -25,7 +25,7 @@
import com.sk89q.jnbt.ShortTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.blocks.metadata.MobType;
import com.sk89q.worldedit.blocks.type.BlockState;
import com.sk89q.worldedit.world.storage.InvalidFormatException;

import java.util.HashMap;
Expand All @@ -49,41 +49,23 @@ public class MobSpawnerBlock extends BaseBlock implements TileEntityBlock {
private short maxNearbyEntities;
private short requiredPlayerRange;

/**
* Construct the mob spawner block with a pig as the mob type.
*/
public MobSpawnerBlock() {
super(BlockID.MOB_SPAWNER);
this.mobType = MobType.PIG.getName();
}

/**
* Construct the mob spawner block with a given mob type.
*
* @param mobType mob type
*/
public MobSpawnerBlock(String mobType) {
super(BlockID.MOB_SPAWNER);
this.mobType = mobType;
}

/**
* Construct the mob spawner block with a specified data value.
*
* @param data data value
* @param blockState The block state
*/
public MobSpawnerBlock(int data) {
super(BlockID.MOB_SPAWNER, data);
public MobSpawnerBlock(BlockState blockState) {
super(blockState);
}

/**
* Construct the mob spawner block.
*
* @param data data value
* @param blockState The block state
* @param mobType mob type
*/
public MobSpawnerBlock(int data, String mobType) {
super(BlockID.MOB_SPAWNER, data);
public MobSpawnerBlock(BlockState blockState, String mobType) {
super(blockState);
this.mobType = mobType;
}

Expand Down
Expand Up @@ -22,6 +22,7 @@
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.blocks.type.BlockState;
import com.sk89q.worldedit.util.gson.GsonUtil;

import java.util.HashMap;
Expand All @@ -36,26 +37,14 @@ public class SignBlock extends BaseBlock implements TileEntityBlock {

private static String EMPTY = "{\"text\":\"\"}";

/**
* Construct the sign without text.
*
* @param type type ID
* @param data data value (orientation)
*/
public SignBlock(int type, int data) {
super(type, data);
this.text = new String[] { EMPTY, EMPTY, EMPTY, EMPTY };
}

/**
* Construct the sign with text.
*
* @param type type ID
* @param data data value (orientation)
* @param blockState The block state
* @param text lines of text
*/
public SignBlock(int type, int data, String[] text) {
super(type, data);
public SignBlock(BlockState blockState, String[] text) {
super(blockState);
if (text == null) {
this.text = new String[] { EMPTY, EMPTY, EMPTY, EMPTY };
return;
Expand Down
Expand Up @@ -19,10 +19,10 @@

package com.sk89q.worldedit.blocks;

import com.sk89q.jnbt.ByteTag;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.blocks.type.BlockState;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -33,60 +33,25 @@
public class SkullBlock extends BaseBlock implements TileEntityBlock {

private String owner = ""; // notchian
private byte skullType; // stored here for block, in damage value for item
private byte rot; // only matters if block data == 0x1 (on floor)

/**
* Construct the skull block with a default type of skelton.
* @param data data value to set, controls placement
* @param state BlockState to set
*/
public SkullBlock(int data) {
this(data, (byte) 0);
}

/**
* Construct the skull block with a given type.
* 0 - skeleton
* 1 - wither skelly
* 2 - zombie
* 3 - human
* 4 - creeper
* @param data data value to set, controls placement
* @param type type of skull
*/
public SkullBlock(int data, byte type) {
this(data, type, (byte) 0);
}

/**
* Construct the skull block with a given type and rotation.
* @param data data value to set, controls placement
* @param type type of skull
* @param rot rotation (if on floor)
*/
public SkullBlock(int data, byte type, byte rot) {
super(BlockID.HEAD, data);
if (type < (byte) 0 || type > (byte) 4) {
this.skullType = (byte) 0;
} else {
this.skullType = type;
}
this.rot = rot;
public SkullBlock(BlockState state) {
super(state);
this.owner = "";
}

/**
* Construct the skull block with a given rotation and owner.
* The type is assumed to be player unless owner is null or empty.
* @param data data value to set, controls placement
* @param rot rotation of skull
* @param blockState BlockState to set
* @param owner name of player
*/
public SkullBlock(int data, byte rot, String owner) {
super(BlockID.HEAD, data);
this.rot = rot;
public SkullBlock(BlockState blockState, String owner) {
super(blockState);
this.setOwner(owner);
if (owner == null || owner.isEmpty()) this.skullType = (byte) 0;
}

/**
Expand All @@ -100,7 +65,6 @@ public void setOwner(String owner) {
if (owner.length() > 16 || owner.isEmpty()) this.owner = "";
else this.owner = owner;
}
if (!this.owner.isEmpty()) this.skullType = (byte) 3;
}

/**
Expand All @@ -111,38 +75,6 @@ public String getOwner() {
return owner;
}

/**
* Get the type of skull.
* @return the skullType
*/
public byte getSkullType() {
return skullType;
}

/**
* Set the type of skull;
* @param skullType the skullType to set
*/
public void setSkullType(byte skullType) {
this.skullType = skullType;
}

/**
* Get rotation of skull. This only means anything if the block data is 1.
* @return the rotation
*/
public byte getRot() {
return rot;
}

/**
* Set the rotation of skull.
* @param rot the rotation to set
*/
public void setRot(byte rot) {
this.rot = rot;
}

@Override
public boolean hasNbtData() {
return true;
Expand All @@ -155,11 +87,9 @@ public String getNbtId() {

@Override
public CompoundTag getNbtData() {
Map<String, Tag> values = new HashMap<String, Tag>();
values.put("SkullType", new ByteTag(skullType));
Map<String, Tag> values = new HashMap<>();
if (owner == null) owner = "";
values.put("ExtraType", new StringTag( owner));
values.put("Rot", new ByteTag(rot));
values.put("ExtraType", new StringTag(owner));
return new CompoundTag(values);
}

Expand All @@ -178,17 +108,9 @@ public void setNbtData(CompoundTag rootTag) {
throw new RuntimeException("'Skull' tile entity expected");
}

t = values.get("SkullType");
if (t instanceof ByteTag) {
skullType = ((ByteTag) t).getValue();
}
t = values.get("ExtraType");
if (t != null && t instanceof StringTag) {
if (t instanceof StringTag) {
owner = ((StringTag) t).getValue();
}
t = values.get("Rot");
if (t instanceof ByteTag) {
rot = ((ByteTag) t).getValue();
}
}
}
Expand Up @@ -23,6 +23,7 @@
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.CuboidClipboard.FlipDirection;
import com.sk89q.worldedit.blocks.type.BlockState;
import com.sk89q.worldedit.blocks.type.BlockType;
import com.sk89q.worldedit.blocks.type.BlockTypes;
import com.sk89q.worldedit.function.mask.Mask;
Expand Down Expand Up @@ -76,6 +77,11 @@ public BaseBlock(int id) {
this.states = new HashMap<>();
}

public BaseBlock(BlockState blockState) {
this.blockType = blockState.getBlockType();
this.states = blockState.getStates();
}

/**
* Construct a block with the given type and default data.
*
Expand Down

0 comments on commit 811f1d4

Please sign in to comment.