Skip to content

Commit 84cba16

Browse files
fix: spawner type in pattern (#3042)
* fix: try parsing special block data before blindly applying NBT * Fix legacy block wrappers with new NBT API (#2361) (cherry picked from commit 5b67474) --------- Co-authored-by: Maddy Miller <[email protected]>
1 parent b95bcde commit 84cba16

File tree

5 files changed

+81
-26
lines changed

5 files changed

+81
-26
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* WorldEdit, a Minecraft world manipulation toolkit
3+
* Copyright (C) sk89q <http://www.sk89q.com>
4+
* Copyright (C) WorldEdit team and contributors
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package com.sk89q.worldedit.blocks;
21+
22+
import com.sk89q.jnbt.CompoundTag;
23+
import com.sk89q.worldedit.util.concurrency.LazyReference;
24+
import com.sk89q.worldedit.world.block.BaseBlock;
25+
import com.sk89q.worldedit.world.block.BlockState;
26+
import org.enginehub.linbus.tree.LinCompoundTag;
27+
28+
import javax.annotation.Nullable;
29+
30+
@Deprecated
31+
public class LegacyBaseBlockWrapper extends BaseBlock {
32+
protected LegacyBaseBlockWrapper(BlockState blockState) {
33+
super(blockState);
34+
}
35+
36+
// These two methods force the legacy blocks to use the old NBT methods.
37+
@Nullable
38+
@Override
39+
public LazyReference<LinCompoundTag> getNbtReference() {
40+
CompoundTag nbtData = getNbtData();
41+
return nbtData == null ? null : LazyReference.from(nbtData::toLinTag);
42+
}
43+
44+
@Override
45+
public void setNbtReference(@Nullable LazyReference<LinCompoundTag> nbtData) {
46+
setNbtData(nbtData == null ? null : new CompoundTag(nbtData.getValue()));
47+
}
48+
}

worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/MobSpawnerBlock.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.sk89q.jnbt.ShortTag;
2929
import com.sk89q.jnbt.StringTag;
3030
import com.sk89q.jnbt.Tag;
31-
import com.sk89q.worldedit.world.block.BaseBlock;
3231
import com.sk89q.worldedit.world.block.BlockState;
3332
import com.sk89q.worldedit.world.storage.InvalidFormatException;
3433

@@ -42,7 +41,7 @@
4241
* deprecated for removal without replacement
4342
*/
4443
@Deprecated(forRemoval = true)
45-
public class MobSpawnerBlock extends BaseBlock {
44+
public class MobSpawnerBlock extends LegacyBaseBlockWrapper {
4645

4746
private String mobType;
4847
private short delay = -1;
@@ -114,6 +113,7 @@ public void setDelay(short delay) {
114113
}
115114

116115
@Override
116+
@Deprecated
117117
public boolean hasNbtData() {
118118
return true;
119119
}
@@ -124,6 +124,7 @@ public String getNbtId() {
124124
}
125125

126126
@Override
127+
@Deprecated
127128
public CompoundTag getNbtData() {
128129
Map<String, Tag<?, ?>> values = new HashMap<>();
129130
values.put("Delay", new ShortTag(delay));
@@ -165,6 +166,7 @@ public CompoundTag getNbtData() {
165166
}
166167

167168
@Override
169+
@Deprecated
168170
public void setNbtData(CompoundTag rootTag) {
169171
if (rootTag == null) {
170172
return;
@@ -184,7 +186,7 @@ public void setNbtData(CompoundTag rootTag) {
184186
try {
185187
spawnDataTag = NBTUtils.getChildTag(values, "SpawnData", CompoundTag.class);
186188
mobType = spawnDataTag.getString("id");
187-
if (mobType.equals("")) {
189+
if (mobType.isEmpty()) {
188190
throw new InvalidFormatException("No spawn id.");
189191
}
190192
this.mobType = mobType;

worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/SignBlock.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@
3737

3838
/**
3939
* Represents a sign block.
40+
*
41+
* @deprecated WorldEdit does not handle interpreting NBT,
42+
* deprecated for removal without replacement
4043
*/
41-
public class SignBlock extends BaseBlock {
44+
@Deprecated
45+
public class SignBlock extends LegacyBaseBlockWrapper {
4246

4347
private String[] text;
4448

@@ -93,6 +97,7 @@ public void setText(String[] text) {
9397
}
9498

9599
@Override
100+
@Deprecated
96101
public boolean hasNbtData() {
97102
return true;
98103
}
@@ -103,6 +108,7 @@ public String getNbtId() {
103108
}
104109

105110
@Override
111+
@Deprecated
106112
public CompoundTag getNbtData() {
107113
Map<String, Tag<?, ?>> values = new HashMap<>();
108114
if (isLegacy()) {
@@ -111,7 +117,7 @@ public CompoundTag getNbtData() {
111117
values.put("Text3", new StringTag(text[2]));
112118
values.put("Text4", new StringTag(text[3]));
113119
} else {
114-
ListTag messages = new ListTag(StringTag.class, Arrays.stream(text).map(StringTag::new).collect(Collectors.toList()));
120+
ListTag<?, ?> messages = new ListTag<>(StringTag.class, Arrays.stream(text).map(StringTag::new).collect(Collectors.toList()));
115121
Map<String, Tag<?, ?>> frontTextTag = new HashMap<>();
116122
frontTextTag.put("messages", messages);
117123
values.put("front_text", new CompoundTag(frontTextTag));
@@ -120,6 +126,7 @@ public CompoundTag getNbtData() {
120126
}
121127

122128
@Override
129+
@Deprecated
123130
public void setNbtData(CompoundTag rootTag) {
124131
if (rootTag == null) {
125132
return;
@@ -158,7 +165,7 @@ public void setNbtData(CompoundTag rootTag) {
158165
}
159166
} else {
160167
CompoundTag frontTextTag = (CompoundTag) values.get("front_text");
161-
ListTag messagesTag = frontTextTag.getListTag("messages");
168+
ListTag<?, ?> messagesTag = frontTextTag.getListTag("messages");
162169
for (int i = 0; i < messagesTag.getValue().size(); i++) {
163170
StringTag tag = (StringTag) messagesTag.getValue().get(i);
164171
text[i] = tag.getValue();

worldedit-core/src/legacy/java/com/sk89q/worldedit/blocks/SkullBlock.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.sk89q.jnbt.StringTag;
2424
import com.sk89q.jnbt.Tag;
2525
import com.sk89q.worldedit.internal.util.DeprecationUtil;
26-
import com.sk89q.worldedit.world.block.BaseBlock;
2726
import com.sk89q.worldedit.world.block.BlockState;
2827

2928
import java.util.HashMap;
@@ -36,7 +35,7 @@
3635
* deprecated for removal without replacement
3736
*/
3837
@Deprecated(forRemoval = true)
39-
public class SkullBlock extends BaseBlock {
38+
public class SkullBlock extends LegacyBaseBlockWrapper {
4039

4140
private String owner = ""; // notchian
4241

@@ -89,6 +88,7 @@ public String getOwner() {
8988
}
9089

9190
@Override
91+
@Deprecated
9292
public boolean hasNbtData() {
9393
return true;
9494
}
@@ -99,6 +99,7 @@ public String getNbtId() {
9999
}
100100

101101
@Override
102+
@Deprecated
102103
public CompoundTag getNbtData() {
103104
Map<String, Tag<?, ?>> values = new HashMap<>();
104105
Map<String, Tag<?, ?>> inner = new HashMap<>();
@@ -108,6 +109,7 @@ public CompoundTag getNbtData() {
108109
}
109110

110111
@Override
112+
@Deprecated
111113
public void setNbtData(CompoundTag rootTag) {
112114
if (rootTag == null) {
113115
return;

worldedit-core/src/main/java/com/sk89q/worldedit/extension/factory/parser/DefaultBlockParser.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@
4848
import com.sk89q.worldedit.extension.platform.Capability;
4949
import com.sk89q.worldedit.extent.inventory.BlockBag;
5050
import com.sk89q.worldedit.internal.registry.InputParser;
51+
import com.sk89q.worldedit.internal.util.DeprecationUtil;
5152
import com.sk89q.worldedit.math.BlockVector3;
5253
import com.sk89q.worldedit.registry.state.Property;
5354
import com.sk89q.worldedit.util.HandSide;
5455
import com.sk89q.worldedit.util.formatting.text.TextComponent;
5556
import com.sk89q.worldedit.world.World;
5657
import com.sk89q.worldedit.world.block.BaseBlock;
57-
import com.sk89q.worldedit.world.block.BlockCategories;
5858
import com.sk89q.worldedit.world.block.BlockState;
5959
import com.sk89q.worldedit.world.block.BlockStateHolder;
6060
import com.sk89q.worldedit.world.block.BlockType;
@@ -538,24 +538,19 @@ private BaseBlock parseLogic(String input, ParserContext context) throws InputPa
538538
//FAWE end
539539
}
540540

541-
if (nbt != null) {
542-
BaseBlock result = blockStates.size() > 0 ? state.toBaseBlock(nbt) : new BlanketBaseBlock(state, nbt);
543-
return validate(context, result);
544-
}
545-
546-
if (blockType == BlockTypes.SIGN || blockType == BlockTypes.WALL_SIGN
547-
|| BlockCategories.SIGNS.contains(blockType)) {
541+
if (DeprecationUtil.isSign(blockType)) {
548542
// Allow special sign text syntax
549543
String[] text = new String[4];
550544
text[0] = blockAndExtraData.length > 1 ? blockAndExtraData[1] : "";
551545
text[1] = blockAndExtraData.length > 2 ? blockAndExtraData[2] : "";
552546
text[2] = blockAndExtraData.length > 3 ? blockAndExtraData[3] : "";
553547
text[3] = blockAndExtraData.length > 4 ? blockAndExtraData[4] : "";
554548
return validate(context, new SignBlock(state, text));
555-
} else if (blockType == BlockTypes.SPAWNER) {
549+
} else if (blockType == BlockTypes.SPAWNER && (blockAndExtraData.length > 1 || nbt != null)) {
556550
// Allow setting mob spawn type
551+
String mobName;
557552
if (blockAndExtraData.length > 1) {
558-
String mobName = blockAndExtraData[1];
553+
mobName = blockAndExtraData[1];
559554
EntityType ent = EntityTypes.get(mobName.toLowerCase(Locale.ROOT));
560555
if (ent == null) {
561556
throw new NoMatchException(Caption.of("worldedit.error.unknown-entity", TextComponent.of(mobName)));
@@ -564,14 +559,13 @@ private BaseBlock parseLogic(String input, ParserContext context) throws InputPa
564559
if (!worldEdit.getPlatformManager().queryCapability(Capability.USER_COMMANDS).isValidMobType(mobName)) {
565560
throw new NoMatchException(Caption.of("worldedit.error.unknown-mob", TextComponent.of(mobName)));
566561
}
567-
return validate(context, new MobSpawnerBlock(state, mobName));
568562
} else {
569-
//noinspection ConstantConditions
570-
return validate(context, new MobSpawnerBlock(state, EntityTypes.PIG.id()));
563+
mobName = EntityTypes.PIG.id();
571564
}
572-
} else if (blockType == BlockTypes.PLAYER_HEAD || blockType == BlockTypes.PLAYER_WALL_HEAD) {
565+
return validate(context, new MobSpawnerBlock(state, mobName));
566+
} else if ((blockType == BlockTypes.PLAYER_HEAD || blockType == BlockTypes.PLAYER_WALL_HEAD) && (blockAndExtraData.length > 1 || nbt != null)) {
573567
// allow setting type/player/rotation
574-
if (blockAndExtraData.length <= 1) {
568+
if (blockAndExtraData.length == 1) {
575569
return validate(context, new SkullBlock(state));
576570
}
577571

@@ -580,12 +574,14 @@ private BaseBlock parseLogic(String input, ParserContext context) throws InputPa
580574
return validate(context, new SkullBlock(state, type.replace(" ", "_"))); // valid MC usernames
581575
} else {
582576
//FAWE start
583-
nbt = state.getNbtData();
577+
if (nbt == null) {
578+
nbt = state.getNbtData();
579+
}
584580
BaseBlock result;
585581
if (nbt != null) {
586-
result = blockStates.size() > 0 ? state.toBaseBlock(nbt) : new BlanketBaseBlock(state, nbt);
582+
result = !blockStates.isEmpty() ? state.toBaseBlock(nbt) : new BlanketBaseBlock(state, nbt);
587583
} else {
588-
result = blockStates.size() > 0 ? new BaseBlock(state) : state.toBaseBlock();
584+
result = !blockStates.isEmpty() ? new BaseBlock(state) : state.toBaseBlock();
589585
}
590586
return validate(context, result);
591587
//FAWE end

0 commit comments

Comments
 (0)