NbtAccounter .java

net.minecraft.nbt.NbtAccounter (class): members: UNCOMPRESSED_NBT_QUOTA, DEFAULT_NBT_QUOTA, uncompressedQuota, MAX_STACK_DEPTH, unlimitedHeap, pushDepth.

net.minecraft.nbtclass79 linescommon1.9 KB

API Surface (10)

NbtAccounter create(final long quota)
NbtAccounter defaultQuota()
NbtAccounter uncompressedQuota()
NbtAccounter unlimitedHeap()
void accountBytes(final long bytesPerEntry, final long count)
void accountBytes(final long size)
void pushDepth()
void popDepth()
long getUsage()
int getDepth()

Fields & Constants (7)

DEFAULT_NBT_QUOTA
UNCOMPRESSED_NBT_QUOTA
MAX_STACK_DEPTH
quota
usage
maxDepth
depth

String literals (2)

"Tried to account NBT tag with negative size: "
" bytes where max allowed: "

Full Source

NbtAccounter.java79 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.nbt;
2
3
import com.google.common.annotations.VisibleForTesting;
4
5
public class NbtAccounter {
6
	public static final int DEFAULT_NBT_QUOTA = 2097152;
7
	public static final int UNCOMPRESSED_NBT_QUOTA = 104857600;
8
	private static final int MAX_STACK_DEPTH = 512;
9
	private final long quota;
10
	private long usage;
11
	private final int maxDepth;
12
	private int depth;
13
14
	public NbtAccounter(final long quota, final int maxDepth) {
15
		this.quota = quota;
16
		this.maxDepth = maxDepth;
17
	}
18
19
	public static NbtAccounter create(final long quota) {
20
		return new NbtAccounter(quota, 512);
21
	}
22
23
	public static NbtAccounter defaultQuota() {
24
		return new NbtAccounter(2097152L, 512);
25
	}
26
27
	public static NbtAccounter uncompressedQuota() {
28
		return new NbtAccounter(104857600L, 512);
29
	}
30
31
	public static NbtAccounter unlimitedHeap() {
32
		return new NbtAccounter(Long.MAX_VALUE, 512);
33
	}
34
35
	public void accountBytes(final long bytesPerEntry, final long count) {
36
		this.accountBytes(bytesPerEntry * count);
37
	}
38
39
	public void accountBytes(final long size) {
40
		if (size < 0L) {
41
			throw new IllegalArgumentException("Tried to account NBT tag with negative size: " + size);
42
		}
43
44
		if (this.usage + size > this.quota) {
45
			throw new NbtAccounterException(
46
				"Tried to read NBT tag that was too big; tried to allocate: " + this.usage + " + " + size + " bytes where max allowed: " + this.quota
47
			);
48
		}
49
50
		this.usage += size;
51
	}
52
53
	public void pushDepth() {
54
		if (this.depth >= this.maxDepth) {
55
			throw new NbtAccounterException("Tried to read NBT tag with too high complexity, depth > " + this.maxDepth);
56
		}
57
58
		this.depth++;
59
	}
60
61
	public void popDepth() {
62
		if (this.depth <= 0) {
63
			throw new NbtAccounterException("NBT-Accounter tried to pop stack-depth at top-level");
64
		}
65
66
		this.depth--;
67
	}
68
69
	@VisibleForTesting
70
	public long getUsage() {
71
		return this.usage;
72
	}
73
74
	@VisibleForTesting
75
	public int getDepth() {
76
		return this.depth;
77
	}
78
}
79

Decompiled Minecraft 26.3 source of NbtAccounter (net/minecraft/nbt/NbtAccounter.java) with Mojang official mappings, stored verbatim on the site.