Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regenerate hashes on load from world file #245

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/wraith/fwaystones/block/WaystoneBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public static void ticker(World world, BlockPos blockPos, BlockState blockState,
waystone.tick();
}

public static String createHashString(String dimensionName, BlockPos pos) {
return Utils.getSHA256(
"<POS X:" + pos.getX() +
", Y:" + pos.getY() +
", Z:" + pos.getZ() +
", WORLD: \">" + dimensionName + "\">"
);
}

public void updateActiveState() {
if (world != null && !world.isClient && world.getBlockState(pos).get(WaystoneBlock.ACTIVE) == (owner == null)) {
world.setBlockState(pos, world.getBlockState(pos).with(WaystoneBlock.ACTIVE, this.ownerName != null));
Expand All @@ -72,7 +81,7 @@ public void updateActiveState() {
}

public void createHash(World world, BlockPos pos) {
this.hash = Utils.getSHA256("<POS X:" + pos.getX() + ", Y:" + pos.getY() + ", Z:" + pos.getZ() + ", WORLD: \">" + Utils.getDimensionName(world) + "\">");
this.hash = createHashString(Utils.getDimensionName(world), pos);
markDirty();
}

Expand Down
16 changes: 11 additions & 5 deletions src/main/java/wraith/fwaystones/util/WaystoneStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import net.minecraft.SharedConstants;
import net.minecraft.datafixer.DataFixTypes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.nbt.*;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
Expand Down Expand Up @@ -87,11 +84,20 @@ public void fromTag(NbtCompound tag) {
continue;
}
String name = waystoneTag.getString("name");
String hash = waystoneTag.getString("hash");
String dimension = waystoneTag.getString("dimension");
String nbtHash = waystoneTag.getString("hash");

int[] coordinates = waystoneTag.getIntArray("position");
int color = waystoneTag.contains("color", NbtElement.INT_TYPE) ? waystoneTag.getInt("color") : Utils.getRandomColor();
BlockPos pos = new BlockPos(coordinates[0], coordinates[1], coordinates[2]);
String hash = WaystoneBlockEntity.createHashString(dimension, pos);

// Migrate global hashes from old hash method to new.
if (!hash.equals(nbtHash) && globals.contains(nbtHash)) {
globals.remove(nbtHash);
globals.add(hash);
}

WAYSTONES.put(hash, new Lazy(name, pos, hash, dimension, color, globals.contains(hash)));
}
}
Expand Down
Loading