|
24 | 24 | */ |
25 | 25 | package org.spongepowered.common.mixin.core.nbt; |
26 | 26 |
|
| 27 | +import com.google.common.base.Function; |
27 | 28 | import net.minecraft.nbt.CompoundTag; |
28 | 29 | import net.minecraft.nbt.Tag; |
29 | 30 | import org.apache.logging.log4j.Level; |
30 | | -import org.checkerframework.checker.nullness.qual.Nullable; |
31 | 31 | import org.spongepowered.asm.mixin.Final; |
32 | 32 | import org.spongepowered.asm.mixin.Mixin; |
33 | 33 | import org.spongepowered.asm.mixin.Shadow; |
34 | 34 | import org.spongepowered.asm.mixin.injection.At; |
35 | | -import org.spongepowered.asm.mixin.injection.Redirect; |
| 35 | +import org.spongepowered.asm.mixin.injection.ModifyArg; |
36 | 36 | import org.spongepowered.common.SpongeCommon; |
37 | 37 | import org.spongepowered.common.util.PrettyPrinter; |
38 | 38 |
|
39 | 39 | import java.util.Map; |
40 | 40 |
|
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 | | - */ |
50 | 41 | @Mixin(CompoundTag.class) |
51 | 42 | public abstract class CompoundTagMixin { |
52 | 43 |
|
53 | 44 | // @formatter:off |
54 | 45 | @Shadow @Final private Map<String, Tag> tags; |
55 | 46 | // @formatter:on |
56 | 47 |
|
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) -> { |
71 | 51 | 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"); |
75 | 66 |
|
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 | + } |
83 | 75 | } |
84 | 76 | } |
| 77 | + printer.add(); |
| 78 | + printer.log(SpongeCommon.logger(), Level.ERROR); |
| 79 | + throw e; |
85 | 80 | } |
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 | + }; |
105 | 82 | } |
106 | | - |
107 | 83 | } |
0 commit comments