Skip to content

Commit

Permalink
Claimed graves should now be ignored by 'pointing to closest', and co…
Browse files Browse the repository at this point in the history
…mpasses should change straight away when claiming a grave
  • Loading branch information
B1n-ry committed Feb 7, 2024
1 parent 98a6b96 commit 21e827a
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 27 deletions.
3 changes: 0 additions & 3 deletions src/main/java/com/b1n_ry/yigd/compat/LevelzCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public DefaultedList<ItemStack> storeToPlayer(ServerPlayerEntity player) {
public void handleDropRules(DeathContext context) {
// Only default drop rule should matter, which is tested in #filterInv()
// There are no specific drop rules for this component (except drop). Only a set standard from the config
// if (YigdConfig.getConfig().compatConfig.defaultLevelzDropRule == DropRule.DROP) {
// this.dropItems(context.world(), context.deathPos());
// }
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.b1n_ry.yigd.compat;

import com.b1n_ry.yigd.components.InventoryComponent;
import com.b1n_ry.yigd.config.YigdConfig;
import com.b1n_ry.yigd.data.DeathContext;
import com.b1n_ry.yigd.events.DropRuleEvent;
Expand All @@ -16,7 +15,6 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Pair;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.Vec3d;

import java.util.function.Predicate;

Expand Down Expand Up @@ -155,10 +153,6 @@ public DefaultedList<Pair<ItemStack, DropRule>> getAsStackDropList() {

@Override
public CompatComponent<Long> filterInv(Predicate<DropRule> predicate) {
// if (predicate.test(YigdConfig.getConfig().compatConfig.defaultNumismaticDropRule)) {
// return new NumismaticCompatComponent(this.inventory, this.dropValue, this.keepValue, this.destroyValue, this.graveValue);
// }

long totalValue = 0;
if (predicate.test(DropRule.DROP))
totalValue += this.dropValue;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/b1n_ry/yigd/compat/OriginsCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Pair;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.Vec3d;

import java.util.*;
import java.util.function.Predicate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ public DefaultedList<ItemStack> merge(CompatComponent<?> mergingComponent) {
public DefaultedList<ItemStack> storeToPlayer(ServerPlayerEntity player) {
if (this.inventory.getLeft().isEmpty()) return DefaultedList.of();

ITravelersBackpackComponent component = ComponentUtils.getComponent(player);

component.setWearable(this.inventory.getLeft());
component.setContents(this.inventory.getLeft());
component.sync();
ComponentUtils.equipBackpack(player, this.inventory.getLeft());

return DefaultedList.of();
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/b1n_ry/yigd/components/GraveComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.b1n_ry.yigd.events.GraveGenerationEvent;
import com.b1n_ry.yigd.packets.LightGraveData;
import com.b1n_ry.yigd.util.DropRule;
import com.b1n_ry.yigd.util.GraveCompassHelper;
import com.b1n_ry.yigd.util.GraveOverrideAreas;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand Down Expand Up @@ -154,6 +155,10 @@ public void setLocked(boolean locked) {
DeathInfoManager.INSTANCE.markDirty();
}
public void setStatus(GraveStatus status) {
if (this.status == GraveStatus.UNCLAIMED
&& YigdConfig.getConfig().extraFeatures.graveCompass.pointToClosest != YigdConfig.ExtraFeatures.GraveCompassConfig.CompassGraveTarget.DISABLED) {
GraveCompassHelper.setClaimed(this.worldRegistryKey, this.pos);
}
this.status = status;
DeathInfoManager.INSTANCE.markDirty();
}
Expand Down Expand Up @@ -381,8 +386,7 @@ public ActionResult claim(ServerPlayerEntity player, ServerWorld world, BlockSta
}
}

this.status = GraveStatus.CLAIMED;
DeathInfoManager.INSTANCE.markDirty();
this.setStatus(GraveStatus.CLAIMED);

if (thisIsARobbery && config.graveConfig.graveRobbing.notifyWhenRobbed) {
MinecraftServer server = world.getServer();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/b1n_ry/yigd/config/YigdConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ public static class GraveCompassConfig {
public boolean deleteWhenUnlinked = true;
public boolean cloneRecoveryCompassWithGUI = false;
@ConfigEntry.Gui.RequiresRestart
@ConfigEntry.Gui.EnumHandler(option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON)
public CompassGraveTarget pointToClosest = CompassGraveTarget.DISABLED;
public enum CompassGraveTarget {
DISABLED, PLAYER, ALL
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/b1n_ry/yigd/data/DeathInfoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public void addBackup(GameProfile profile, GraveComponent component) {
}
}

if (config.extraFeatures.graveCompass.pointToClosest != YigdConfig.ExtraFeatures.GraveCompassConfig.CompassGraveTarget.DISABLED) {
if (config.extraFeatures.graveCompass.pointToClosest != YigdConfig.ExtraFeatures.GraveCompassConfig.CompassGraveTarget.DISABLED
&& component.getStatus() == GraveStatus.UNCLAIMED) {
GraveCompassHelper.addGravePosition(component.getWorldRegistryKey(), component.getPos(), profile.getId());
}
}
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/com/b1n_ry/yigd/events/YigdServerEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.b1n_ry.yigd.data.DeathInfoManager;
import com.b1n_ry.yigd.util.DropRule;
import com.b1n_ry.yigd.data.ListMode;
import com.b1n_ry.yigd.util.GraveCompassHelper;
import com.b1n_ry.yigd.util.GraveOverrideAreas;
import com.b1n_ry.yigd.util.YigdTags;
import me.lucko.fabric.api.permissions.v0.PermissionCheckEvent;
Expand Down Expand Up @@ -97,17 +98,23 @@ public static void registerEventCallbacks() {

YigdConfig config = YigdConfig.getConfig();

if (config.extraFeatures.graveCompass.consumeOnUse) {
if (config.extraFeatures.graveCompass.consumeOnUse || config.extraFeatures.graveCompass.pointToClosest != YigdConfig.ExtraFeatures.GraveCompassConfig.CompassGraveTarget.DISABLED) {
PlayerInventory inventory = player.getInventory();
for (int i = 0; i < inventory.size(); i++) {
ItemStack stack = inventory.getStack(i);
NbtCompound stackNbt = stack.getNbt();
if (stack.isOf(Items.COMPASS) && stackNbt != null && stackNbt.contains("linked_grave")) {
UUID graveId = stackNbt.getUuid("linked_grave");
if (graveId.equals(grave.getGraveId())) {
stack.setCount(0);
break;
if (!stack.isOf(Items.COMPASS)) continue;

if (config.extraFeatures.graveCompass.consumeOnUse) {
NbtCompound stackNbt = stack.getNbt();
if (stack.isOf(Items.COMPASS) && stackNbt != null && stackNbt.contains("linked_grave")) {
UUID graveId = stackNbt.getUuid("linked_grave");
if (graveId.equals(grave.getGraveId())) {
stack.setCount(0);
break;
}
}
} else { // Redirect closest grave pointer
GraveCompassHelper.updateClosestNbt(world.getRegistryKey(), player.getBlockPos(), player.getUuid(), stack);
}
}
}
Expand Down
35 changes: 33 additions & 2 deletions src/main/java/com/b1n_ry/yigd/util/GraveCompassHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public static void updateClosestNbt(RegistryKey<World> worldKey, BlockPos pos, U
NbtCompound compassNbt = compass.getNbt();
if (compassNbt == null) return;

if (!compassNbt.contains("grave_pos")) return; // Not a grave compass

BlockPos closestPos = findClosest(holderId, worldKey, pos);
if (closestPos != null) {
compassNbt.put("grave_pos", NbtHelper.fromBlockPos(closestPos));
Expand Down Expand Up @@ -88,6 +90,34 @@ private static void addGravePosition(KDNode parent, UUID ownerId, int[] pos, int
}
}

public static void setClaimed(RegistryKey<World> worldKey, BlockPos gravePos) {
KDNode root = GRAVE_POSITIONS.get(worldKey);
if (root == null) {
return;
}

int[] pos = new int[] { gravePos.getX(), gravePos.getY(), gravePos.getZ() };

setClaimed(root, pos, 0);
}
private static void setClaimed(KDNode node, int[] pos, int depth) {
if (node == null) {
return;
}

if (node.pos[0] == pos[0] && node.pos[1] == pos[1] && node.pos[2] == pos[2]) {
node.isUnclaimed = false;
return;
}

int cmp = depth % 3;
if (node.pos[cmp] < pos[cmp]) {
setClaimed(node.right, pos, depth + 1);
} else {
setClaimed(node.left, pos, depth + 1);
}
}

public static @Nullable BlockPos findClosest(UUID ownerId, RegistryKey<World> worldKey, BlockPos pos) {
GraveCompassConfig config = YigdConfig.getConfig().extraFeatures.graveCompass;
if (config.pointToClosest == GraveCompassConfig.CompassGraveTarget.DISABLED) return null; // What how are we here?
Expand All @@ -104,14 +134,14 @@ private static void addGravePosition(KDNode parent, UUID ownerId, int[] pos, int
return closest == null ? null : new BlockPos(closest[0], closest[1], closest[2]);
}
private static int[] findClosest(KDNode node, UUID ownerId, int[] pos, int depth, GraveCompassConfig config) {
if (node == null) {
if (node == null || depth > 100) {
return null;
}

int cmp = depth % 3;
int[] closest = null;
// If not all, it's player specific. If not that, it's disabled, and won't reach this
if (config.pointToClosest == GraveCompassConfig.CompassGraveTarget.ALL || node.ownerId.equals(ownerId)) {
if ((config.pointToClosest == GraveCompassConfig.CompassGraveTarget.ALL || node.ownerId.equals(ownerId)) && node.isUnclaimed) {
closest = node.pos;
}

Expand Down Expand Up @@ -144,6 +174,7 @@ private static double distanceSquared(int[] pos1, int[] pos2) {
private static class KDNode {
@NotNull
private final UUID ownerId;
private boolean isUnclaimed = true;
private final int @NotNull [] pos;
@Nullable
private KDNode left;
Expand Down

0 comments on commit 21e827a

Please sign in to comment.