Skip to content

Commit a4e3ed6

Browse files
committed
Revert Chunk NBT Pre-processing
1 parent c8880ed commit a4e3ed6

File tree

1 file changed

+56
-96
lines changed

1 file changed

+56
-96
lines changed

src/main/java/com/gtnewhorizons/neid/mixins/early/minecraft/MixinAnvilChunkLoader.java

+56-96
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,31 @@
11
package com.gtnewhorizons.neid.mixins.early.minecraft;
22

3-
import java.nio.ByteBuffer;
4-
53
import net.minecraft.nbt.NBTTagCompound;
6-
import net.minecraft.nbt.NBTTagList;
7-
import net.minecraft.world.World;
84
import net.minecraft.world.chunk.NibbleArray;
95
import net.minecraft.world.chunk.storage.AnvilChunkLoader;
106
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
117

128
import org.spongepowered.asm.mixin.Mixin;
139
import org.spongepowered.asm.mixin.injection.At;
14-
import org.spongepowered.asm.mixin.injection.Inject;
1510
import org.spongepowered.asm.mixin.injection.Redirect;
16-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
17-
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
1811

1912
import com.gtnewhorizons.neid.Constants;
2013
import com.gtnewhorizons.neid.NEIDConfig;
2114
import com.gtnewhorizons.neid.mixins.interfaces.IExtendedBlockStorageMixin;
2215
import com.llamalad7.mixinextras.sugar.Local;
23-
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
2416

25-
@Mixin(value = AnvilChunkLoader.class, priority = 1)
17+
@Mixin(AnvilChunkLoader.class)
2618
public class MixinAnvilChunkLoader {
2719

28-
@Inject(method = "writeChunkToNBT", at = @At("HEAD"))
29-
private void neid$injectLevelTag(CallbackInfo ci, @Local NBTTagCompound tag) {
30-
tag.setBoolean("NEID", true);
31-
}
32-
3320
@Redirect(
34-
method = "writeChunkToNBT",
35-
at = @At(
36-
value = "INVOKE",
37-
target = "Lnet/minecraft/nbt/NBTTagCompound;setByteArray(Ljava/lang/String;[B)V",
38-
ordinal = 0),
39-
require = 1)
21+
method = "writeChunkToNBT",
22+
at = @At(
23+
value = "INVOKE",
24+
target = "Lnet/minecraft/nbt/NBTTagCompound;setByteArray(Ljava/lang/String;[B)V",
25+
ordinal = 0),
26+
require = 1)
4027
private void neid$overrideWriteLSBArray(NBTTagCompound nbt, String s, byte[] oldbrokenbytes,
41-
@Local(ordinal = 0) ExtendedBlockStorage ebs) {
28+
@Local(ordinal = 0) ExtendedBlockStorage ebs) {
4229
IExtendedBlockStorageMixin ebsMixin = (IExtendedBlockStorageMixin) ebs;
4330
nbt.setByteArray("Blocks16", ebsMixin.getBlockData());
4431
if (NEIDConfig.PostNeidWorldsSupport) {
@@ -73,14 +60,14 @@ public class MixinAnvilChunkLoader {
7360
}
7461

7562
@Redirect(
76-
method = "writeChunkToNBT",
77-
at = @At(
78-
value = "INVOKE",
79-
target = "Lnet/minecraft/nbt/NBTTagCompound;setByteArray(Ljava/lang/String;[B)V",
80-
ordinal = 2),
81-
require = 1)
63+
method = "writeChunkToNBT",
64+
at = @At(
65+
value = "INVOKE",
66+
target = "Lnet/minecraft/nbt/NBTTagCompound;setByteArray(Ljava/lang/String;[B)V",
67+
ordinal = 2),
68+
require = 1)
8269
private void neid$overrideWriteMetadataArray(NBTTagCompound nbt, String s, byte[] oldbrokenbytes,
83-
@Local(ordinal = 0) ExtendedBlockStorage ebs) {
70+
@Local(ordinal = 0) ExtendedBlockStorage ebs) {
8471
IExtendedBlockStorageMixin ebsMixin = (IExtendedBlockStorageMixin) ebs;
8572
nbt.setByteArray("Data16", ebsMixin.getBlockMeta());
8673
if (NEIDConfig.PostNeidWorldsSupport) {
@@ -104,94 +91,67 @@ public class MixinAnvilChunkLoader {
10491
}
10592
}
10693

107-
@Inject(
108-
method = "checkedReadChunkFromNBT__Async",
109-
at = @At(
110-
value = "INVOKE",
111-
target = "Lnet/minecraft/world/chunk/storage/AnvilChunkLoader;readChunkFromNBT(Lnet/minecraft/world/World;Lnet/minecraft/nbt/NBTTagCompound;)Lnet/minecraft/world/chunk/Chunk;"),
112-
remap = false)
113-
private void neid$preprocessOldChunk(CallbackInfoReturnable<Object[]> cir, @Local World world,
114-
@Local LocalRef<NBTTagCompound> tag) {
115-
NBTTagCompound level = tag.get().getCompoundTag("Level");
116-
117-
if (!level.hasKey("NEID")) {
118-
NBTTagList nbttaglist = level.getTagList("Sections", 10);
119-
for (int i = 0; i < nbttaglist.tagCount(); i++) {
120-
NBTTagCompound tag1 = nbttaglist.getCompoundTagAt(i);
121-
if (tag1.hasKey("Blocks") && !tag1.hasKey("Blocks16")) {
122-
final byte[] lsbData = tag1.getByteArray("Blocks");
123-
final short[] out = new short[Constants.BLOCKS_PER_EBS];
124-
if (tag1.hasKey("Add")) {
125-
final byte[] msbData = tag1.getByteArray("Add");
126-
for (int j = 0; j < out.length; j += 2) {
127-
final byte msPart = msbData[j / 2];
128-
out[j] = (short) ((lsbData[j] & 0xFF) | (msPart & 0xF) << 8);
129-
out[j + 1] = (short) ((lsbData[j + 1] & 0xFF) | (msPart & 0xF0) << 4);
130-
}
131-
} else {
132-
for (int j = 0; j < out.length; j++) {
133-
out[j] = (short) (lsbData[j] & 0xFF);
134-
}
135-
}
136-
final byte[] ret = new byte[out.length * 2];
137-
ByteBuffer.wrap(ret).asShortBuffer().put(out);
138-
tag1.setByteArray("Blocks16", ret);
139-
}
140-
if (tag1.hasKey("Data") && !tag1.hasKey("Data16")) {
141-
final byte[] metaData = tag1.getByteArray("Data");
142-
final short[] out = new short[Constants.BLOCKS_PER_EBS];
143-
for (int j = 0; j < out.length; j += 2) {
144-
final byte meta = metaData[j / 2];
145-
out[j] = (short) (meta & 0xF);
146-
out[j + 1] = (short) ((meta >> 4) & 0xF);
147-
}
148-
final byte[] ret = new byte[out.length * 2];
149-
ByteBuffer.wrap(ret).asShortBuffer().put(out);
150-
tag1.setByteArray("Data16", ret);
151-
}
152-
}
153-
level.setBoolean("NEID", true);
154-
}
155-
}
156-
15794
@Redirect(
158-
method = "readChunkFromNBT",
159-
at = @At(
160-
value = "INVOKE",
161-
target = "Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;setBlockLSBArray([B)V"),
162-
require = 1)
95+
method = "readChunkFromNBT",
96+
at = @At(
97+
value = "INVOKE",
98+
target = "Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;setBlockLSBArray([B)V"),
99+
require = 1)
163100
private void neid$overrideReadLSBArray(ExtendedBlockStorage ebs, byte[] oldbrokenbytes,
164-
@Local(ordinal = 1) NBTTagCompound nbt) {
101+
@Local(ordinal = 1) NBTTagCompound nbt) {
165102
IExtendedBlockStorageMixin ebsMixin = (IExtendedBlockStorageMixin) ebs;
166103
if (nbt.hasKey("Blocks16")) {
167104
ebsMixin.setBlockData(nbt.getByteArray("Blocks16"), 0);
105+
} else if (nbt.hasKey("Blocks")) {
106+
final short[] out = ebsMixin.getBlock16BArray();
107+
final byte[] lsbData = nbt.getByteArray("Blocks");
108+
if (nbt.hasKey("Add")) {
109+
final byte[] msbData = nbt.getByteArray("Add");
110+
for (int i = 0; i < out.length; i += 2) {
111+
final byte msPart = msbData[i / 2];
112+
out[i] = (short) ((lsbData[i] & 0xFF) | (msPart & 0xF) << 8);
113+
out[i + 1] = (short) ((lsbData[i + 1] & 0xFF) | (msPart & 0xF0) << 4);
114+
}
115+
} else {
116+
for (int j = 0; j < out.length; ++j) {
117+
out[j] = (short) (lsbData[j] & 0xFF);
118+
}
119+
}
168120
} else {
169121
assert false;
170122
}
171123
}
172124

173125
@Redirect(
174-
method = "readChunkFromNBT",
175-
at = @At(
176-
value = "INVOKE",
177-
target = "Lnet/minecraft/nbt/NBTTagCompound;hasKey(Ljava/lang/String;I)Z",
178-
ordinal = 0),
179-
require = 1)
126+
method = "readChunkFromNBT",
127+
at = @At(
128+
value = "INVOKE",
129+
target = "Lnet/minecraft/nbt/NBTTagCompound;hasKey(Ljava/lang/String;I)Z",
130+
ordinal = 0),
131+
require = 1)
180132
private boolean neid$overrideReadMSBArray(NBTTagCompound nbttagcompound1, String s, int i) {
181133
return false;
182134
}
183135

184136
@Redirect(
185-
method = "readChunkFromNBT",
186-
at = @At(
187-
value = "INVOKE",
188-
target = "Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;setBlockMetadataArray(Lnet/minecraft/world/chunk/NibbleArray;)V"),
189-
require = 1)
137+
method = "readChunkFromNBT",
138+
at = @At(
139+
value = "INVOKE",
140+
target = "Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;setBlockMetadataArray(Lnet/minecraft/world/chunk/NibbleArray;)V"),
141+
require = 1)
190142
private void neid$overrideReadMetadataArray(ExtendedBlockStorage ebs, NibbleArray oldBrokenNibbleArray,
191-
@Local(ordinal = 1) NBTTagCompound nbt) {
143+
@Local(ordinal = 1) NBTTagCompound nbt) {
192144
IExtendedBlockStorageMixin ebsMixin = (IExtendedBlockStorageMixin) ebs;
193145
if (nbt.hasKey("Data16")) {
194146
ebsMixin.setBlockMeta(nbt.getByteArray("Data16"), 0);
147+
} else if (nbt.hasKey("Data")) {
148+
final short[] out = ebsMixin.getBlock16BMetaArray();
149+
final byte[] metaData = nbt.getByteArray("Data");
150+
for (int i = 0; i < out.length; i += 2) {
151+
final byte meta = metaData[i / 2];
152+
out[i] = (short) (meta & 0xF);
153+
out[i + 1] = (short) ((meta >> 4) & 0xF);
154+
}
195155
} else {
196156
assert false;
197157
}

0 commit comments

Comments
 (0)