2010-10-14 08:31:05 +00:00
|
|
|
/*
|
2014-04-04 22:03:18 +00:00
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
2010-10-14 08:31:05 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
2010-10-14 08:31:05 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
2010-10-14 08:31:05 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2010-10-14 08:31:05 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2012-08-23 23:52:37 +00:00
|
|
|
*/
|
2010-10-14 08:31:05 +00:00
|
|
|
|
|
|
|
package com.sk89q.worldedit.blocks;
|
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
import com.sk89q.jnbt.CompoundTag;
|
|
|
|
import com.sk89q.jnbt.StringTag;
|
|
|
|
import com.sk89q.jnbt.Tag;
|
2018-08-10 10:29:06 +00:00
|
|
|
import com.sk89q.worldedit.util.gson.GsonUtil;
|
|
|
|
import com.sk89q.worldedit.world.block.BaseBlock;
|
2018-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockState;
|
2019-04-02 22:21:02 +00:00
|
|
|
|
2014-06-30 00:47:08 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2010-10-14 08:31:05 +00:00
|
|
|
|
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* Represents a sign block.
|
2010-10-14 08:31:05 +00:00
|
|
|
*/
|
2018-08-13 12:18:12 +00:00
|
|
|
public class SignBlock extends BaseBlock {
|
2012-08-23 23:52:37 +00:00
|
|
|
|
2010-10-14 08:31:05 +00:00
|
|
|
private String[] text;
|
|
|
|
|
2016-04-14 22:53:51 +00:00
|
|
|
private static String EMPTY = "{\"text\":\"\"}";
|
|
|
|
|
2010-10-14 08:31:05 +00:00
|
|
|
/**
|
|
|
|
* Construct the sign with text.
|
2019-07-23 23:17:38 +00:00
|
|
|
*
|
2018-06-17 13:13:22 +00:00
|
|
|
* @param blockState The block state
|
2012-08-23 23:52:37 +00:00
|
|
|
* @param text lines of text
|
2010-10-14 08:31:05 +00:00
|
|
|
*/
|
2018-06-17 13:13:22 +00:00
|
|
|
public SignBlock(BlockState blockState, String[] text) {
|
|
|
|
super(blockState);
|
2012-08-23 23:52:37 +00:00
|
|
|
if (text == null) {
|
2016-04-14 22:53:51 +00:00
|
|
|
this.text = new String[] { EMPTY, EMPTY, EMPTY, EMPTY };
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < text.length; i++) {
|
|
|
|
if (text[i].isEmpty()) {
|
|
|
|
text[i] = EMPTY;
|
|
|
|
} else {
|
2016-06-18 16:49:46 +00:00
|
|
|
text[i] = "{\"text\":" + GsonUtil.stringValue(text[i]) + "}";
|
2016-04-14 22:53:51 +00:00
|
|
|
}
|
2012-08-23 23:52:37 +00:00
|
|
|
}
|
2010-10-14 08:31:05 +00:00
|
|
|
this.text = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* Get the text.
|
2019-07-23 23:17:38 +00:00
|
|
|
*
|
2010-10-14 08:31:05 +00:00
|
|
|
* @return the text
|
|
|
|
*/
|
|
|
|
public String[] getText() {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-08-23 23:52:37 +00:00
|
|
|
* Set the text.
|
2019-07-23 23:17:38 +00:00
|
|
|
*
|
2010-10-14 08:31:05 +00:00
|
|
|
* @param text the text to set
|
|
|
|
*/
|
|
|
|
public void setText(String[] text) {
|
2012-08-23 23:52:37 +00:00
|
|
|
if (text == null) {
|
|
|
|
throw new IllegalArgumentException("Can't set null text for a sign");
|
|
|
|
}
|
2010-10-14 08:31:05 +00:00
|
|
|
this.text = text;
|
|
|
|
}
|
2019-07-23 23:17:38 +00:00
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
@Override
|
|
|
|
public boolean hasNbtData() {
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-14 08:31:05 +00:00
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
@Override
|
|
|
|
public String getNbtId() {
|
2019-07-23 23:17:38 +00:00
|
|
|
return "minecraft:sign";
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
@Override
|
|
|
|
public CompoundTag getNbtData() {
|
2019-02-16 02:46:10 +00:00
|
|
|
Map<String, Tag> values = new HashMap<>();
|
2014-11-14 19:38:54 +00:00
|
|
|
values.put("Text1", new StringTag(text[0]));
|
|
|
|
values.put("Text2", new StringTag(text[1]));
|
|
|
|
values.put("Text3", new StringTag(text[2]));
|
|
|
|
values.put("Text4", new StringTag(text[3]));
|
|
|
|
return new CompoundTag(values);
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
@Override
|
2014-06-30 00:47:08 +00:00
|
|
|
public void setNbtData(CompoundTag rootTag) {
|
2012-08-23 23:52:37 +00:00
|
|
|
if (rootTag == null) {
|
2010-10-14 08:31:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-09-24 19:32:03 +00:00
|
|
|
|
2012-08-23 23:52:37 +00:00
|
|
|
Map<String, Tag> values = rootTag.getValue();
|
|
|
|
|
2010-10-14 08:31:05 +00:00
|
|
|
Tag t;
|
|
|
|
|
2016-04-14 22:53:51 +00:00
|
|
|
text = new String[] { EMPTY, EMPTY, EMPTY, EMPTY };
|
2010-10-14 08:31:05 +00:00
|
|
|
|
|
|
|
t = values.get("id");
|
2019-07-23 23:17:38 +00:00
|
|
|
if (!(t instanceof StringTag) || !((StringTag) t).getValue().equals(getNbtId())) {
|
|
|
|
throw new RuntimeException(String.format("'%s' tile entity expected", getNbtId()));
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t = values.get("Text1");
|
|
|
|
if (t instanceof StringTag) {
|
2011-11-23 01:29:48 +00:00
|
|
|
text[0] = ((StringTag) t).getValue();
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t = values.get("Text2");
|
|
|
|
if (t instanceof StringTag) {
|
2011-11-23 01:29:48 +00:00
|
|
|
text[1] = ((StringTag) t).getValue();
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t = values.get("Text3");
|
|
|
|
if (t instanceof StringTag) {
|
2011-11-23 01:29:48 +00:00
|
|
|
text[2] = ((StringTag) t).getValue();
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t = values.get("Text4");
|
|
|
|
if (t instanceof StringTag) {
|
2011-11-23 01:29:48 +00:00
|
|
|
text[3] = ((StringTag) t).getValue();
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-29 18:04:04 +00:00
|
|
|
|
2010-10-14 08:31:05 +00:00
|
|
|
}
|