From ae6ee5688cf80627dfda97ed5d1c314e5614afff Mon Sep 17 00:00:00 2001 From: Logan Darklock Date: Sat, 31 Aug 2019 00:51:42 -0700 Subject: [PATCH] add toArray, scanChunkInto usage coming soon --- .../baritone/launch/mixins/MixinBitArray.java | 48 +++++++++++++++++-- .../mixins/MixinBlockStateContainer.java | 11 +++++ .../baritone/utils/accessor/IBitArray.java | 5 ++ .../utils/accessor/IBlockStateContainer.java | 6 +-- 4 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 src/main/java/baritone/utils/accessor/IBitArray.java diff --git a/src/launch/java/baritone/launch/mixins/MixinBitArray.java b/src/launch/java/baritone/launch/mixins/MixinBitArray.java index 54ead39a..614ec351 100644 --- a/src/launch/java/baritone/launch/mixins/MixinBitArray.java +++ b/src/launch/java/baritone/launch/mixins/MixinBitArray.java @@ -17,14 +17,18 @@ package baritone.launch.mixins; +import baritone.utils.accessor.IBitArray; import net.minecraft.util.BitArray; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; + +import java.util.Arrays; @Mixin(BitArray.class) -public abstract class MixinBitArray { +public abstract class MixinBitArray implements IBitArray { @Shadow @Final private long[] longArray; @@ -37,6 +41,10 @@ public abstract class MixinBitArray { @Final private long maxEntryValue; + @Shadow + @Final + private int arraySize; + /** * why did mojang divide by 64 instead of shifting right by 6 (2^6=64)? * why did mojang modulo by 64 instead of ANDing with 63? @@ -47,16 +55,48 @@ public abstract class MixinBitArray { @Overwrite public int getAt(int index) { final int b = bitsPerEntry; + final long mev = maxEntryValue; final int i = index * b; final int j = i >> 6; final int l = i & 63; final int k = ((index + 1) * b - 1) >> 6; if (j == k) { - return (int) (this.longArray[j] >>> l & maxEntryValue); + return (int) (this.longArray[j] >>> l & mev); } else { - int i1 = 64 - l; - return (int) ((this.longArray[j] >>> l | longArray[k] << i1) & maxEntryValue); + return (int) ((this.longArray[j] >>> l | longArray[k] << (64 - l)) & mev); } } + + @Unique + public int[] toArrayBad() { + int[] out = new int[arraySize]; + + for (int i = 0; i < arraySize; i++) { + out[i] = getAt(i); + } + + return out; + } + + @Override + public int[] toArray() { + int[] out = new int[arraySize]; + + for (int idx = 0, kl = bitsPerEntry - 1; idx < arraySize; idx++, kl += bitsPerEntry) { + final int i = idx * bitsPerEntry; + final int j = i >> 6; + final int l = i & 63; + final int k = kl >> 6; + final long jl = longArray[j] >>> l; + + if (j == k) { + out[idx] = (int) (jl & maxEntryValue); + } else { + out[idx] = (int) ((jl | longArray[k] << (64 - l)) & maxEntryValue); + } + } + + return out; + } } diff --git a/src/launch/java/baritone/launch/mixins/MixinBlockStateContainer.java b/src/launch/java/baritone/launch/mixins/MixinBlockStateContainer.java index e3b7cbeb..8a1b000f 100644 --- a/src/launch/java/baritone/launch/mixins/MixinBlockStateContainer.java +++ b/src/launch/java/baritone/launch/mixins/MixinBlockStateContainer.java @@ -17,6 +17,7 @@ package baritone.launch.mixins; +import baritone.utils.accessor.IBitArray; import baritone.utils.accessor.IBlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.util.BitArray; @@ -48,4 +49,14 @@ public abstract class MixinBlockStateContainer implements IBlockStateContainer { public IBlockState getFast(int index) { return palette.getBlockState(storage.getAt(index)); } + + @Override + public IBlockState getAtPalette(int index) { + return palette.getBlockState(index); + } + + @Override + public int[] storageArray() { + return ((IBitArray) storage).toArray(); + } } diff --git a/src/main/java/baritone/utils/accessor/IBitArray.java b/src/main/java/baritone/utils/accessor/IBitArray.java new file mode 100644 index 00000000..fef08044 --- /dev/null +++ b/src/main/java/baritone/utils/accessor/IBitArray.java @@ -0,0 +1,5 @@ +package baritone.utils.accessor; + +public interface IBitArray { + int[] toArray(); +} diff --git a/src/main/java/baritone/utils/accessor/IBlockStateContainer.java b/src/main/java/baritone/utils/accessor/IBlockStateContainer.java index ad602f37..aa2eaab4 100644 --- a/src/main/java/baritone/utils/accessor/IBlockStateContainer.java +++ b/src/main/java/baritone/utils/accessor/IBlockStateContainer.java @@ -11,7 +11,7 @@ public interface IBlockStateContainer { IBlockState getFast(int index); - default IBlockState getFast(int x, int y, int z) { - return getFast(y << 8 | z << 4 | x); - }; + IBlockState getAtPalette(int index); + + int[] storageArray(); }