Skip to content

Commit

Permalink
Make every Vec3i use BetterBlockPos hash
Browse files Browse the repository at this point in the history
  • Loading branch information
babbaj committed Sep 16, 2024
1 parent ef72c56 commit b825986
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
5 changes: 0 additions & 5 deletions src/api/java/baritone/api/utils/BetterBlockPos.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ public static BetterBlockPos from(BlockPos pos) {
return new BetterBlockPos(pos);
}

@Override
public int hashCode() {
return (int) longHash(x, y, z);
}

public static long longHash(BetterBlockPos pos) {
return longHash(pos.x, pos.y, pos.z);
}
Expand Down
39 changes: 39 additions & 0 deletions src/launch/java/baritone/launch/mixins/MixinVec3i.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/


package baritone.launch.mixins;

import baritone.api.utils.BetterBlockPos;
import net.minecraft.core.Vec3i;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(Vec3i.class)
public class MixinVec3i {

/**
* @author Babbaj
* @reason overriding hashCode with different behavior violates the general contract of hashCode.
* 2 BlockPos objects that are equal but give different hashCodes trolls hashmaps and causes duplicate entries.
*/
@Overwrite
public int hashCode() {
Vec3i vec = (Vec3i) (Object) this;
return (int) BetterBlockPos.longHash(vec.getX(), vec.getY(), vec.getZ());
}
}
3 changes: 2 additions & 1 deletion src/launch/resources/mixins.baritone.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"MixinPalettedContainer$Data",
"MixinPlayerController",
"MixinScreen",
"MixinWorldRenderer"
"MixinWorldRenderer",
"MixinVec3i"
]
}

0 comments on commit b825986

Please sign in to comment.