Add skull handler.

This commit is contained in:
wizjany 2019-04-25 11:13:04 -04:00 committed by Matthew Miller
parent 526b3366b5
commit fcb42f05cf
3 changed files with 87 additions and 4 deletions

View File

@ -39,6 +39,7 @@ import com.sk89q.worldedit.extent.clipboard.io.legacycompat.NBTCompatibilityHand
import com.sk89q.worldedit.extent.clipboard.io.legacycompat.NoteBlockCompatibilityHandler;
import com.sk89q.worldedit.extent.clipboard.io.legacycompat.Pre13HangingCompatibilityHandler;
import com.sk89q.worldedit.extent.clipboard.io.legacycompat.SignCompatibilityHandler;
import com.sk89q.worldedit.extent.clipboard.io.legacycompat.SkullBlockCompatibilityHandler;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
@ -67,8 +68,9 @@ public class MCEditSchematicReader extends NBTSchematicReader {
= ImmutableList.of(
new SignCompatibilityHandler(),
new FlowerPotCompatibilityHandler(),
new NoteBlockCompatibilityHandler()
// TODO - skulls, item tags for inventories...? DFUs :>
new NoteBlockCompatibilityHandler(),
new SkullBlockCompatibilityHandler()
// TODO - item tags for inventories...? DFUs :>
);
private static final ImmutableList<EntityNBTCompatibilityHandler> ENTITY_COMPATIBILITY_HANDLERS
= ImmutableList.of(

View File

@ -29,13 +29,13 @@ public class NoteBlockCompatibilityHandler implements NBTCompatibilityHandler {
@Override
public <B extends BlockStateHolder<B>> B updateNBT(B block, Map<String, Tag> values) {
// note that instrument was note stored (in state or nbt) previously.
// note that instrument was not stored (in state or nbt) previously.
// it will be updated to the block below when it gets set into the world for the first time
Tag noteTag = values.get("note");
if (noteTag instanceof ByteTag) {
Byte note = ((ByteTag) noteTag).getValue();
if (note != null) {
return (B) block.with(NoteProperty, (int) note);
return block.with(NoteProperty, (int) note);
}
}
return block;

View File

@ -0,0 +1,81 @@
package com.sk89q.worldedit.extent.clipboard.io.legacycompat;
import com.sk89q.jnbt.ByteTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.registry.state.DirectionalProperty;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import java.util.Map;
public class SkullBlockCompatibilityHandler implements NBTCompatibilityHandler {
private static final DirectionalProperty FacingProperty;
static {
DirectionalProperty tempFacing;
try {
tempFacing = (DirectionalProperty) (Property<?>) BlockTypes.SKELETON_WALL_SKULL.getProperty("facing");
} catch (NullPointerException | IllegalArgumentException | ClassCastException e) {
tempFacing = null;
}
FacingProperty = tempFacing;
}
@Override
public <B extends BlockStateHolder<B>> boolean isAffectedBlock(B block) {
return block.getBlockType() == BlockTypes.SKELETON_SKULL
|| block.getBlockType() == BlockTypes.SKELETON_WALL_SKULL;
}
@Override
public <B extends BlockStateHolder<B>> B updateNBT(B block, Map<String, Tag> values) {
boolean isWall = block.getBlockType() == BlockTypes.SKELETON_WALL_SKULL;
Tag typeTag = values.get("SkullType");
if (typeTag instanceof ByteTag) {
String skullType = convertSkullType(((ByteTag) typeTag).getValue(), isWall);
if (skullType != null) {
BlockType type = BlockTypes.get("minecraft:" + skullType);
if (type != null) {
BlockState state = type.getDefaultState();
if (isWall) {
Property newProp = type.getProperty("facing");
state = state.with(newProp, block.getState(FacingProperty));
} else {
Tag rotTag = values.get("Rot");
if (rotTag instanceof ByteTag) {
Property newProp = type.getProperty("rotation");
state = state.with(newProp, (int) ((ByteTag) rotTag).getValue());
}
}
values.remove("SkullType");
values.remove("Rot");
return (B) state;
}
}
}
return block;
}
private String convertSkullType(Byte oldType, boolean isWall) {
switch (oldType) {
case 0:
return isWall ? "skeleton_wall_skull" : "skeleton_skull";
case 1:
return isWall ? "wither_skeleton_wall_skull" : "wither_skeleton_skull";
case 2:
return isWall ? "zombie_wall_head" : "zombie_head";
case 3:
return isWall ? "player_wall_head" : "player_head";
case 4:
return isWall ? "creeper_wall_head" : "creeper_head";
case 5:
return isWall ? "dragon_wall_head" : "dragon_head";
default:
return null;
}
}
}