Skip to content

Commit fe5f294

Browse files
committed
Update CompoundTagMixin
1 parent 58b19d8 commit fe5f294

File tree

2 files changed

+32
-55
lines changed

2 files changed

+32
-55
lines changed

src/mixins/java/org/spongepowered/common/mixin/core/nbt/CompoundTagMixin.java

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,84 +24,60 @@
2424
*/
2525
package org.spongepowered.common.mixin.core.nbt;
2626

27+
import com.google.common.base.Function;
2728
import net.minecraft.nbt.CompoundTag;
2829
import net.minecraft.nbt.Tag;
2930
import org.apache.logging.log4j.Level;
30-
import org.checkerframework.checker.nullness.qual.Nullable;
3131
import org.spongepowered.asm.mixin.Final;
3232
import org.spongepowered.asm.mixin.Mixin;
3333
import org.spongepowered.asm.mixin.Shadow;
3434
import org.spongepowered.asm.mixin.injection.At;
35-
import org.spongepowered.asm.mixin.injection.Redirect;
35+
import org.spongepowered.asm.mixin.injection.ModifyArg;
3636
import org.spongepowered.common.SpongeCommon;
3737
import org.spongepowered.common.util.PrettyPrinter;
3838

3939
import java.util.Map;
4040

41-
/**
42-
* @author Zidane - Minecraft 1.14.4
43-
*
44-
* Normally this shouldn't be necessary, however, due to unforseen consequences
45-
* of creating block snapshots, there are corner cases where mod authors are
46-
* setting nulls into the compound for their tile entities. This overwrite
47-
* prevents an NPE crashing the game. A pretty warning message will be printed
48-
* out for the client to see and report to both Sponge and the mod author.
49-
*/
5041
@Mixin(CompoundTag.class)
5142
public abstract class CompoundTagMixin {
5243

5344
// @formatter:off
5445
@Shadow @Final private Map<String, Tag> tags;
5546
// @formatter:on
5647

57-
@Redirect(method = "copy()Lnet/minecraft/nbt/CompoundTag;", at = @At(value = "INVOKE", target = "Lnet/minecraft/nbt/Tag;copy()Lnet/minecraft/nbt/Tag;"))
58-
@Nullable
59-
private Tag impl$checkForOverflowOnCopy(Tag inbt) {
60-
try {
61-
return inbt == null ? null : inbt.copy();
62-
} catch (StackOverflowError e) {
63-
final PrettyPrinter printer = new PrettyPrinter(60)
64-
.add("StackOverflow from trying to copy this compound")
65-
.centre()
66-
.hr();
67-
printer.addWrapped(70, "Sponge caught a stack overflow error, printing out some special"
68-
+ " handling and printouts to assist in finding out where this"
69-
+ " recursion is coming from.");
70-
printer.add();
48+
@ModifyArg(method = "copy()Lnet/minecraft/nbt/CompoundTag;", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/Maps;transformValues(Ljava/util/Map;Lcom/google/common/base/Function;)Ljava/util/Map;"))
49+
private Function<Tag, Tag> impl$checkForOverflowOnCopy(final Function<Tag, Tag> copy) {
50+
return (tag) -> {
7151
try {
72-
printer.addWrapped(80, "%s : %s", "This compound", this);
73-
} catch (final Throwable error) {
74-
printer.addWrapped(80, "Unable to get the string of this compound. Printing out some of the entries to better assist");
52+
return copy.apply(tag);
53+
} catch (final StackOverflowError e) {
54+
final PrettyPrinter printer = new PrettyPrinter(60)
55+
.add("StackOverflow from trying to copy this compound")
56+
.centre()
57+
.hr();
58+
printer.addWrapped(70, "Sponge caught a stack overflow error, printing out some special"
59+
+ " handling and printouts to assist in finding out where this"
60+
+ " recursion is coming from.");
61+
printer.add();
62+
try {
63+
printer.addWrapped(80, "%s : %s", "This compound", this);
64+
} catch (final Throwable error) {
65+
printer.addWrapped(80, "Unable to get the string of this compound. Printing out some of the entries to better assist");
7566

76-
for (final Map.Entry<String, Tag> entry : this.tags.entrySet()) {
77-
try {
78-
printer.addWrapped(80, "%s : %s", entry.getKey(), entry.getValue());
79-
} catch (final Throwable throwable) {
80-
printer.add();
81-
printer.addWrapped(80, "The offending key entry is belonging to " + entry.getKey());
82-
break;
67+
for (final Map.Entry<String, Tag> entry : this.tags.entrySet()) {
68+
try {
69+
printer.addWrapped(80, "%s : %s", entry.getKey(), entry.getValue());
70+
} catch (final Throwable throwable) {
71+
printer.add();
72+
printer.addWrapped(80, "The offending key entry is belonging to " + entry.getKey());
73+
break;
74+
}
8375
}
8476
}
77+
printer.add();
78+
printer.log(SpongeCommon.logger(), Level.ERROR);
79+
throw e;
8580
}
86-
printer.add();
87-
printer.log(SpongeCommon.logger(), Level.ERROR);
88-
return null;
89-
}
90-
}
91-
92-
@Redirect(method = "copy()Lnet/minecraft/nbt/CompoundTag;", at = @At(value = "INVOKE", target = "Lnet/minecraft/nbt/CompoundTag;put(Ljava/lang/String;Lnet/minecraft/nbt/Tag;)Lnet/minecraft/nbt/Tag;"))
93-
private Tag impl$checkForNullNBTValuesDuringCopy(CompoundTag compound, String key, Tag value) {
94-
if (value == null) {
95-
final IllegalStateException exception = new IllegalStateException("There is a null NBT component in the compound for key: " + key);
96-
SpongeCommon.logger().error("Printing out a stacktrace to catch an exception in performing an NBTTagCompound.copy!\n"
97-
+ "If you are seeing this, then Sponge is preventing an exception from being thrown due to unforseen\n"
98-
+ "possible bugs in any mods present. Please report this to SpongePowered and/or the relative mod\n"
99-
+ "authors for the offending compound data!", exception);
100-
} else {
101-
compound.put(key, value);
102-
}
103-
104-
return value;
81+
};
10582
}
106-
10783
}

src/mixins/resources/mixins.sponge.core.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"item.EmptyMapItemMixin",
3535
"item.ThrowableInaccurateItemMixin",
3636
"item.YeetableInaccurateItemMixin",
37+
"nbt.CompoundTagMixin",
3738
"network.ConnectionMixin",
3839
"network.PacketEncoderMixin",
3940
"network.chat.Component_SerializerMixin",

0 commit comments

Comments
 (0)