Skip to content

Commit b77337a

Browse files
authored
Merge pull request #9 from GTNewHorizons/multiplayer-fixes
Fixes for Multiplayer
2 parents b6221e4 + f5c4bd1 commit b77337a

File tree

6 files changed

+73
-38
lines changed

6 files changed

+73
-38
lines changed

src/main/java/com/gtnewhorizons/neid/Constants.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ public class Constants {
5959
/**
6060
* This number is the total bytes stored in an EBS, minute the lighting data. It is: (LSB + MSB + Metadata) *
6161
* BLOCKS_PER_EBS(4096) In vanilla: 8 + 4 + 4 = 16 bits per block = 2 bytes per block * 4096 = 8192 Our equation: 16
62-
* + 0 + 16 = 20 bits per block = 4 bytes per block * 4096 = 16384.
62+
* + 0 + 16 = 32 bits per block = 4 bytes per block * 4096 = 16384. However, even though we're not using MSB, it
63+
* still needs to get padded into the byte[] where this calculation is used. So we have to pad in 2048 bits for
64+
* that. So our calcuation is actually: 16 + 0.5 + 16 = 32.5 bits per block = 4.5 bytes per block * 4096 = 18432.
6365
*
6466
* If you look at vanilla source code, this value will be 2048 because seemingly Vanilla handles less EBS per chunk?
6567
* Unsure, but Forge ASM's this value to 8192, so that is what we are looking for to modify.
6668
*/
67-
public static final int BYTES_PER_EBS_MINUS_LIGHTING = 16384;
68-
public static final int VANILLA_BYTES_PER_EBS_MINUS_LIGHTING = 8192;
69+
public static final int BYTES_PER_EBS_MINUS_LIGHTING_BUT_INCLUDE_MSB = 18432;
70+
public static final int VANILLA_BYTES_PER_EBS_MINUS_LIGHTING_BUT_INCLUDE_MSB = 8192;
6971

7072
public static final int MAX_DATA_WATCHER_ID = 127;
7173
}

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

-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import net.minecraft.block.Block;
77
import net.minecraft.init.Blocks;
8-
import net.minecraft.world.chunk.NibbleArray;
98
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
109

1110
import org.spongepowered.asm.mixin.Mixin;
@@ -53,11 +52,6 @@ public byte[] getBlockMeta() {
5352
return ret;
5453
}
5554

56-
@Override
57-
public NibbleArray getBlockMetaNibble() {
58-
return new NibbleArray(this.getBlockMeta(), 4);
59-
}
60-
6155
@Override
6256
public void setBlockData(byte[] data, int offset) {
6357
ShortBuffer.wrap(this.block16BArray)

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

+54-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@
1212

1313
import com.gtnewhorizons.neid.Constants;
1414
import com.gtnewhorizons.neid.mixins.interfaces.IExtendedBlockStorageMixin;
15+
import com.llamalad7.mixinextras.injector.WrapWithCondition;
16+
import com.llamalad7.mixinextras.sugar.Local;
17+
import com.llamalad7.mixinextras.sugar.ref.LocalIntRef;
1518

1619
@Mixin(S21PacketChunkData.class)
1720
public class MixinS21PacketChunkData {
1821

22+
private static final byte[] fakeByteArray = new byte[0];
23+
private static final NibbleArray fakeNibbleArray = new NibbleArray(fakeByteArray, 0);
24+
1925
@ModifyConstant(
2026
method = "<clinit>",
2127
constant = @Constant(intValue = Constants.VANILLA_BYTES_PER_CHUNK),
@@ -46,8 +52,24 @@ public class MixinS21PacketChunkData {
4652
value = "INVOKE",
4753
target = "Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;getBlockLSBArray()[B"),
4854
require = 1)
49-
private static byte[] neid$RedirectGetLSB(ExtendedBlockStorage ebs) {
50-
return ((IExtendedBlockStorageMixin) ebs).getBlockData();
55+
private static byte[] neid$injectNewDataCopy(ExtendedBlockStorage ebs, @Local(ordinal = 0) byte[] thebytes,
56+
@Local(ordinal = 1) LocalIntRef offset) {
57+
IExtendedBlockStorageMixin ebsMixin = (IExtendedBlockStorageMixin) ebs;
58+
byte[] data = ebsMixin.getBlockData();
59+
System.arraycopy(data, 0, thebytes, offset.get(), Constants.BLOCKS_PER_EBS * 2);
60+
offset.set(offset.get() + (Constants.BLOCKS_PER_EBS * 2));
61+
return fakeByteArray;
62+
}
63+
64+
@WrapWithCondition(
65+
method = "func_149269_a",
66+
at = @At(
67+
value = "INVOKE",
68+
target = "Ljava/lang/System;arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V",
69+
ordinal = 0),
70+
require = 1)
71+
private static boolean neid$cancelLSBArrayCopy(Object a, int i, Object b, int j, int k) {
72+
return false;
5173
}
5274

5375
@Redirect(
@@ -56,7 +78,35 @@ public class MixinS21PacketChunkData {
5678
value = "INVOKE",
5779
target = "Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;getMetadataArray()Lnet/minecraft/world/chunk/NibbleArray;"),
5880
require = 1)
59-
private static NibbleArray neid$RedirectGetMetadata(ExtendedBlockStorage ebs) {
60-
return ((IExtendedBlockStorageMixin) ebs).getBlockMetaNibble();
81+
private static NibbleArray neid$injectNewMetadataCopy(ExtendedBlockStorage ebs, @Local(ordinal = 0) byte[] thebytes,
82+
@Local(ordinal = 1) LocalIntRef offset) {
83+
IExtendedBlockStorageMixin ebsMixin = (IExtendedBlockStorageMixin) ebs;
84+
byte[] meta = ebsMixin.getBlockMeta();
85+
System.arraycopy(meta, 0, thebytes, offset.get(), Constants.BLOCKS_PER_EBS * 2);
86+
offset.set(offset.get() + (Constants.BLOCKS_PER_EBS * 2));
87+
return fakeNibbleArray;
6188
}
89+
90+
@WrapWithCondition(
91+
method = "func_149269_a",
92+
at = @At(
93+
value = "INVOKE",
94+
target = "Ljava/lang/System;arraycopy(Ljava/lang/Object;ILjava/lang/Object;II)V",
95+
ordinal = 1),
96+
require = 1)
97+
private static boolean neid$cancelMetadataArrayCopy(Object a, int i, Object b, int j, int k) {
98+
return false;
99+
}
100+
101+
@Redirect(
102+
method = "func_149269_a",
103+
at = @At(
104+
value = "INVOKE",
105+
target = "Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;getBlockMSBArray()Lnet/minecraft/world/chunk/NibbleArray;",
106+
ordinal = 0),
107+
require = 1)
108+
private static NibbleArray neid$nukeMSBLoop(ExtendedBlockStorage ebs) {
109+
return null;
110+
}
111+
62112
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public class MixinS26PacketMapChunkBulk {
1313

1414
@ModifyConstant(
1515
method = "readPacketData",
16-
constant = @Constant(intValue = Constants.VANILLA_BYTES_PER_EBS_MINUS_LIGHTING),
16+
constant = @Constant(intValue = Constants.VANILLA_BYTES_PER_EBS_MINUS_LIGHTING_BUT_INCLUDE_MSB),
1717
require = 1)
1818
private static int neid$readPacketConstantUpdate(int i) {
19-
return Constants.BYTES_PER_EBS_MINUS_LIGHTING;
19+
return Constants.BYTES_PER_EBS_MINUS_LIGHTING_BUT_INCLUDE_MSB;
2020
}
2121
}

src/main/java/com/gtnewhorizons/neid/mixins/early/minecraft/client/MixinChunk.java

+12-19
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,51 @@
1212
import org.spongepowered.asm.mixin.injection.At;
1313
import org.spongepowered.asm.mixin.injection.Constant;
1414
import org.spongepowered.asm.mixin.injection.ModifyConstant;
15-
import org.spongepowered.asm.mixin.injection.ModifyVariable;
1615
import org.spongepowered.asm.mixin.injection.Redirect;
1716

1817
import com.gtnewhorizons.neid.Constants;
1918
import com.gtnewhorizons.neid.mixins.interfaces.IExtendedBlockStorageMixin;
2019
import com.llamalad7.mixinextras.injector.WrapWithCondition;
2120
import com.llamalad7.mixinextras.sugar.Local;
21+
import com.llamalad7.mixinextras.sugar.ref.LocalIntRef;
2222

2323
@Mixin(Chunk.class)
2424
public class MixinChunk {
2525

26+
private static final byte[] fakeByteArray = new byte[0];
27+
private static final NibbleArray fakeNibbleArray = new NibbleArray(fakeByteArray, 0);
28+
2629
@Shadow
2730
private ExtendedBlockStorage[] storageArrays;
2831

29-
// TODO: Can we make this not need to return a fake byte array?
3032
@Redirect(
3133
method = "fillChunk",
3234
at = @At(
3335
value = "INVOKE",
3436
target = "Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;getBlockLSBArray()[B"),
3537
require = 1)
3638
private byte[] neid$injectNewDataCopy(ExtendedBlockStorage ebs, @Local(ordinal = 0) byte[] thebytes,
37-
@Local(ordinal = 3) int forIndex, @Local(ordinal = 2) int offset) {
39+
@Local(ordinal = 2) LocalIntRef offset) {
3840
IExtendedBlockStorageMixin ebsMixin = (IExtendedBlockStorageMixin) ebs;
3941
ShortBuffer.wrap(ebsMixin.getBlock16BArray())
40-
.put(ByteBuffer.wrap(thebytes, offset, Constants.BLOCKS_PER_EBS * 2).asShortBuffer());
41-
return new byte[0];
42+
.put(ByteBuffer.wrap(thebytes, offset.get(), Constants.BLOCKS_PER_EBS * 2).asShortBuffer());
43+
offset.set(offset.get() + (Constants.BLOCKS_PER_EBS * 2));
44+
return fakeByteArray;
4245
}
4346

44-
// TODO: Can we make this not need to return a fake NibbleArray?
4547
@Redirect(
4648
method = "fillChunk",
4749
at = @At(
4850
value = "INVOKE",
4951
target = "Lnet/minecraft/world/chunk/storage/ExtendedBlockStorage;getMetadataArray()Lnet/minecraft/world/chunk/NibbleArray;"),
5052
require = 1)
5153
private NibbleArray neid$injectNewMetadataCopy(ExtendedBlockStorage ebs, @Local(ordinal = 0) byte[] thebytes,
52-
@Local(ordinal = 3) int forIndex, @Local(ordinal = 2) int offset) {
54+
@Local(ordinal = 2) LocalIntRef offset) {
5355
IExtendedBlockStorageMixin ebsMixin = (IExtendedBlockStorageMixin) ebs;
5456
ShortBuffer.wrap(ebsMixin.getBlock16BMetaArray())
55-
.put(ByteBuffer.wrap(thebytes, offset, Constants.BLOCKS_PER_EBS * 2).asShortBuffer());
56-
return new NibbleArray(0, 0);
57+
.put(ByteBuffer.wrap(thebytes, offset.get(), Constants.BLOCKS_PER_EBS * 2).asShortBuffer());
58+
offset.set(offset.get() + (Constants.BLOCKS_PER_EBS * 2));
59+
return fakeNibbleArray;
5760
}
5861

5962
@WrapWithCondition(
@@ -78,16 +81,6 @@ public class MixinChunk {
7881
return false;
7982
}
8083

81-
@ModifyVariable(method = "fillChunk", at = @At(value = "STORE", ordinal = 1), ordinal = 2, require = 1)
82-
private int neid$cancelLSBOffsetIncrement(int i, @Local(ordinal = 2) int old) {
83-
return old + (Constants.BLOCKS_PER_EBS * 2);
84-
}
85-
86-
@ModifyVariable(method = "fillChunk", at = @At(value = "STORE", ordinal = 2), ordinal = 2, require = 1)
87-
private int neid$cancelMetaOffsetIncrement(int i, @Local(ordinal = 2) int old) {
88-
return old + (Constants.BLOCKS_PER_EBS * 2);
89-
}
90-
9184
@ModifyConstant(method = "fillChunk", constant = @Constant(intValue = 0, ordinal = 10), require = 1)
9285
private int neid$NukeMSBForLoop(int i) {
9386
return this.storageArrays.length + 1;

src/main/java/com/gtnewhorizons/neid/mixins/interfaces/IExtendedBlockStorageMixin.java

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.gtnewhorizons.neid.mixins.interfaces;
22

3-
import net.minecraft.world.chunk.NibbleArray;
4-
53
public interface IExtendedBlockStorageMixin {
64

75
short[] getBlock16BArray();
@@ -12,8 +10,6 @@ public interface IExtendedBlockStorageMixin {
1210

1311
byte[] getBlockMeta();
1412

15-
NibbleArray getBlockMetaNibble();
16-
1713
void setBlockData(byte[] data, int offset);
1814

1915
void setBlockMeta(byte[] data, int offset);

0 commit comments

Comments
 (0)