Skip to content

Commit

Permalink
Fixed some bugs and stuff
Browse files Browse the repository at this point in the history
Items can now (optionally) be applied to modded inventories
Item loss will now not try and remove the same item twice, and count it as 2 items (more reliable how much is lost)
Running the /clear command after retrieving items from a grave no longer clears the grave backup
Soulbound now works with curios
Graves being moved (like with carryon mod) will now be detected when they reappear
  • Loading branch information
B1n-ry committed Nov 1, 2024
1 parent 59fee5c commit cb04c5f
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 24 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# You're in Grave Danger 2.0.8

### Changes
* Item loss can now optionally be applied to modded inventories

### Fixes
* Item loss will now not try and remove the same item twice, and count it as 2
items (more reliable how much is lost)
* Running the /clear command after retrieving items from a grave no longer clears
the grave backup
* Soulbound now works with curios
* Graves being moved (like with carry-on mod) will now be detected when they reappear

---

# You're in Grave Danger 2.0.7
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ minecraft_version=1.21.1
# as they do not follow standard versioning conventions.
minecraft_version_range=[1.21,1.21.1)
# The Neo version must agree with the Minecraft version to get a valid artifact
neo_version=21.1.1
neo_version=21.1.73
# The Neo version range can use any version of Neo as bounds
neo_version_range=[21.0.0-beta,)
neo_version_range=[21.1.0,)
# The loader version range can only use the major version of FML as bounds
loader_version_range=[4,)

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/b1n_ry/yigd/Yigd.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.minecraft.core.UUIDUtil;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.phys.Vec3;
Expand Down Expand Up @@ -72,7 +73,7 @@ public class Yigd
private static final DeferredRegister<AttachmentType<?>> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.ATTACHMENT_TYPES, Yigd.MOD_ID);
public static final Supplier<AttachmentType<Vec3>> LAST_GROUND_POS = ATTACHMENT_TYPES.register("last_ground_pos", () -> AttachmentType.builder(() -> Vec3.ZERO).serialize(Vec3.CODEC).build());

private static final DeferredRegister.DataComponents DATA_COMPONENTS = DeferredRegister.createDataComponents(Yigd.MOD_ID);
private static final DeferredRegister.DataComponents DATA_COMPONENTS = DeferredRegister.createDataComponents(Registries.DATA_COMPONENT_TYPE, Yigd.MOD_ID);
public static final DeferredHolder<DataComponentType<?>, DataComponentType<UUID>> GRAVE_ID = DATA_COMPONENTS.registerComponentType("grave_id", builder -> builder.persistent(UUIDUtil.CODEC).networkSynchronized(UUIDUtil.STREAM_CODEC));
public static final DeferredHolder<DataComponentType<?>, DataComponentType<GlobalPos>> GRAVE_LOCATION = DATA_COMPONENTS.registerComponentType("grave_location", builder -> builder.persistent(GlobalPos.CODEC).networkSynchronized(GlobalPos.STREAM_CODEC));
public static final DeathHandler DEATH_HANDLER = new DeathHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ public boolean hasCustomOutlineRendering(@NotNull Player player) {
public static void tick(Level world, BlockPos pos, BlockState ignoredState, GraveBlockEntity be) {
if (world.isClientSide) return;

if (be.component == null) return;
if (be.component == null) {
if (be.graveId == null) return;
DeathInfoManager.INSTANCE.getGrave(be.graveId).ifPresent(be::setComponent);
if (be.component == null) return;
}
if (world.getGameTime() % 2400 == 0) cachedConfig = YigdConfig.getConfig(); // Reloads the config every 60 seconds

YigdConfig.GraveConfig.GraveTimeout timeoutConfig = cachedConfig.graveConfig.graveTimeout;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/b1n_ry/yigd/compat/AccessoriesCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,20 @@ public NonNullList<ItemStack> storeToPlayer(ServerPlayer player) {
for (int i = 0; i < inventorySlot.normal.size(); i++) {
Tuple<ItemStack, DropRule> pair = inventorySlot.normal.get(i);
if (i >= normalAccessories.getContainerSize()) {
extraItems.add(pair.getA());
extraItems.add(pair.getA().copy());
continue;
}

normalAccessories.setItem(i, pair.getA());
normalAccessories.setItem(i, pair.getA().copy());
}
for (int i = 0; i < inventorySlot.cosmetic.size(); i++) {
Tuple<ItemStack, DropRule> pair = inventorySlot.cosmetic.get(i);
if (i >= cosmeticAccessories.getContainerSize()) {
extraItems.add(pair.getA());
extraItems.add(pair.getA().copy());
continue;
}

cosmeticAccessories.setItem(i, pair.getA());
cosmeticAccessories.setItem(i, pair.getA().copy());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public NonNullList<ItemStack> storeToPlayer(ServerPlayer player) {

for (int i = 0; i < cosArmor.getContainerSize(); i++) {
if (i >= this.inventory.size()) break;
ItemStack stack = this.inventory.get(i).getA();
ItemStack stack = this.inventory.get(i).getA().copy();
if (cosArmor.getItem(i).isEmpty()) {
cosArmor.setItem(i, stack);
} else {
Expand Down
26 changes: 19 additions & 7 deletions src/main/java/com/b1n_ry/yigd/compat/CuriosCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.neoforged.neoforge.common.NeoForge;
import top.theillusivec4.curios.api.CuriosApi;
import top.theillusivec4.curios.api.SlotContext;
import top.theillusivec4.curios.api.event.DropRulesEvent;
import top.theillusivec4.curios.api.type.capability.ICurio;
import top.theillusivec4.curios.api.type.capability.ICuriosItemHandler;
import top.theillusivec4.curios.api.type.inventory.ICurioStacksHandler;
Expand Down Expand Up @@ -251,39 +252,50 @@ public NonNullList<ItemStack> storeToPlayer(ServerPlayer player) {
for (int i = 0; i < slotEntry.normal.size(); i++) {
Tuple<ItemStack, DropRule> tuple = slotEntry.normal.get(i);
if (i >= normalEquipped.getSlots()) {
extraItems.add(tuple.getA());
extraItems.add(tuple.getA().copy());
continue;
}

normalEquipped.setStackInSlot(i, tuple.getA());
normalEquipped.setStackInSlot(i, tuple.getA().copy());
}
for (int i = 0; i < slotEntry.cosmetic.size(); i++) {
Tuple<ItemStack, DropRule> tuple = slotEntry.cosmetic.get(i);
if (i >= cosmeticEquipped.getSlots()) {
extraItems.add(tuple.getA());
extraItems.add(tuple.getA().copy());
continue;
}

cosmeticEquipped.setStackInSlot(i, tuple.getA());
cosmeticEquipped.setStackInSlot(i, tuple.getA().copy());
}
}
return extraItems;
}

private ICurio.DropRule getDropRule(ItemStack stack, String key, int index, DeathContext context, boolean cosmetic) {
private ICurio.DropRule getDropRule(ItemStack stack, String key, int index, DeathContext context, boolean cosmetic, List<Tuple<Predicate<ItemStack>, ICurio.DropRule>> overrides) {
for (Tuple<Predicate<ItemStack>, ICurio.DropRule> t : overrides) {
if (t.getA().test(stack)) {
return t.getB();
}
}
Optional<ICurio> iCurio = CuriosApi.getCurio(stack);
return iCurio.map(curio -> curio.getDropRule(new SlotContext(key, context.player(), index, cosmetic, false), context.deathSource(), 0, true)).orElse(ICurio.DropRule.DEFAULT);
}

@Override
public void handleDropRules(DeathContext context) {
ServerPlayer player = context.player();
List<Tuple<Predicate<ItemStack>, ICurio.DropRule>> overrides = new ArrayList<>();
CuriosApi.getCuriosInventory(player).ifPresent(handler -> {
DropRulesEvent event = NeoForge.EVENT_BUS.post(new DropRulesEvent(player, handler, context.deathSource(), 0, false));
overrides.addAll(event.getOverrides());
});
for (Map.Entry<String, CuriosSlotEntry> entry : this.inventory.entrySet()) {
String key = entry.getKey();
CuriosSlotEntry inventorySlot = entry.getValue();
for (int i = 0; i < inventorySlot.normal.size(); i++) {
Tuple<ItemStack, DropRule> pair = inventorySlot.normal.get(i);
ItemStack stack = pair.getA();
DropRule dropRule = switch(this.getDropRule(stack, key, i, context, false)) {
DropRule dropRule = switch(this.getDropRule(stack, key, i, context, false, overrides)) {
case DESTROY -> DropRule.DESTROY;
case ALWAYS_KEEP -> DropRule.KEEP;
default -> {
Expand All @@ -300,7 +312,7 @@ public void handleDropRules(DeathContext context) {
for (int i = 0; i < inventorySlot.cosmetic.size(); i++) {
Tuple<ItemStack, DropRule> pair = inventorySlot.cosmetic.get(i);
ItemStack stack = pair.getA();
DropRule dropRule = switch(this.getDropRule(stack, key, i, context, true)) {
DropRule dropRule = switch(this.getDropRule(stack, key, i, context, true, overrides)) {
case DESTROY -> DropRule.DESTROY;
case ALWAYS_KEEP -> DropRule.KEEP;
default -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public NonNullList<ItemStack> merge(CompatComponent<?> mergingComponent, ServerP
public NonNullList<ItemStack> storeToPlayer(ServerPlayer player) {
if (this.inventory.getA().isEmpty()) return NonNullList.create();

AttachmentUtils.equipBackpack(player, this.inventory.getA());
AttachmentUtils.equipBackpack(player, this.inventory.getA().copy());

return NonNullList.create();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ public void onDestroyed() {

YigdConfig config = YigdConfig.getConfig();

Yigd.LOGGER.info("Grave belonging to {} was detected destroyed at X: {}, Y: {}, Z: {} / {}", owner.getGameProfile().getName(), this.pos.getX(), this.pos.getY(), this.pos.getZ(), this.worldRegistryKey.location());
if (config.graveConfig.notifyOwnerIfDestroyed) {
owner.sendSystemMessage(Component.translatable("text.yigd.message.grave_destroyed"));
}
Expand Down
32 changes: 28 additions & 4 deletions src/main/java/com/b1n_ry/yigd/components/InventoryComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ private void loseRandomItem() {
YigdConfig.InventoryConfig.ItemLossConfig itemLoss = config.inventoryConfig.itemLoss;

List<Integer> itemSlots = new ArrayList<>();
for (int i = 0; i < this.items.size(); i++) {
int vanillaLimit = this.items.size();
for (int i = 0; i < vanillaLimit; i++) {
Tuple<ItemStack, DropRule> pair = this.items.get(i);
ItemStack stack = pair.getA();
if (stack.isEmpty()) continue;
Expand All @@ -220,19 +221,42 @@ private void loseRandomItem() {

itemSlots.add(i);
}
NonNullList<ItemStack> extraItems = NonNullList.create();
if (itemLoss.includeModdedInventories) {
for (CompatComponent<?> compatComponent : this.modInventoryItems.values()) {
for (Tuple<ItemStack, DropRule> tuple : compatComponent.getAsStackDropList()) {
if (tuple.getA().isEmpty()) continue;
extraItems.add(tuple.getA());
}
}
for (int i = 0; i < extraItems.size(); i++) {
itemSlots.add(vanillaLimit + i);
}
}

if (itemSlots.isEmpty()) return;

int random = RANDOM.nextInt(itemSlots.size());

int slot = itemSlots.get(random);
if (itemLoss.affectStacks) {
this.items.get(slot).setB(DropRule.DESTROY);
if (slot >= vanillaLimit) {
ItemStack toBeRemoved = extraItems.get(slot - vanillaLimit);
this.handleItemPairs(s -> !s.equals("vanilla"), (stack, s, pair) -> {
if (stack.equals(toBeRemoved)) pair.setB(DropRule.DESTROY);
});
} else {
this.items.get(slot).setB(DropRule.DESTROY);
}
} else {
ItemStack stack = this.items.get(slot).getA();
ItemStack stack = slot >= vanillaLimit ? extraItems.get(slot - vanillaLimit) : this.items.get(slot).getA();

stack.shrink(1);
}
ItemStack decreased = this.items.get(slot).getA();
if (decreased.isEmpty() || decreased.getCount() == 0) {
itemSlots.remove(Integer.valueOf(slot)); // Make sure we can't lose this item again
}
}

public void dropAll(ServerLevel world, Vec3 pos) {
Expand Down Expand Up @@ -539,7 +563,7 @@ public NonNullList<ItemStack> applyToPlayer(ServerPlayer player) {
playerInvIndex = groupIndex + invMainSize + invArmorSize + invOffHandSize;
}

ItemStack stack = this.items.get(i).getA();
ItemStack stack = this.items.get(i).getA().copy();

if (playerInvIndex >= inventory.getContainerSize() || playerInvIndex == -1) {
extraItems.add(stack);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/b1n_ry/yigd/config/YigdConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static class ItemLossConfig {
public int percentChanceOfLoss = 50;
@Comment("If true, you can lose soulbound items from the item loss feature")
public boolean canLoseSoulbound = false;
public boolean includeModdedInventories = true;
}
}

Expand Down Expand Up @@ -134,9 +135,10 @@ public static class GraveConfig {
public boolean storeXp = true;
@Comment("Inform player where the grave generated when respawning")
public boolean informGraveLocation = true;
@Comment("If true, you HAVE to have one of `requiredItem` for a grave to generate. One of that item will then be consumed")
@Comment("If true, you HAVE to have `requiredItemCount` number of `requiredItem` for a grave to generate. That many of that item will then be consumed")
public boolean requireItem = false;
public String requiredItem = "yigd:grave";
public int requiredItemCount = 1;
// require shovel to open
public boolean requireShovelToLoot = false;
// retrieve method (list with enums)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/b1n_ry/yigd/events/ServerEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.component.ResolvableProfile;
import net.minecraft.world.level.GameRules;
import net.neoforged.bus.api.EventPriority;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.living.LivingDeathEvent;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void serverStarted(ServerStartedEvent event) {
DeathInfoManager.INSTANCE.setDirty();
}

@SubscribeEvent
@SubscribeEvent(priority = EventPriority.LOWEST)
public void afterRespawn(PlayerEvent.Clone event) {
if (!event.isWasDeath()) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void allowGraveGenerationEvent(AllowGraveGenerationEvent event) {

if (graveConfig.requireItem) {
Item item = BuiltInRegistries.ITEM.get(ResourceLocation.parse(graveConfig.requiredItem));
if (!grave.getInventoryComponent().removeItem(stack -> stack.is(item), 1)) {
if (!grave.getInventoryComponent().removeItem(stack -> stack.is(item), graveConfig.requiredItemCount)) {
event.setAllowGeneration(false);
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/yigd/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"text.autoconfig.yigd.option.inventoryConfig.itemLoss.lossRangeTo": "Range To",
"text.autoconfig.yigd.option.inventoryConfig.itemLoss.percentChanceOfLoss": "% Chance of Loss Per Item",
"text.autoconfig.yigd.option.inventoryConfig.itemLoss.canLoseSoulbound": "Can Affect Soulbound",
"text.autoconfig.yigd.option.inventoryConfig.itemLoss.includeModdedInventories": "Enable Loss from Modded Inventories",
"text.autoconfig.yigd.option.inventoryConfig.loseSoulboundLevelOnDeath": "Lose Soulbound Level on Death",
"text.autoconfig.yigd.option.inventoryConfig.vanishingSlots": "\"Trash Slots\"",
"text.autoconfig.yigd.option.inventoryConfig.soulboundSlots": "Soulbound Slots",
Expand All @@ -119,6 +120,7 @@
"text.autoconfig.yigd.option.graveConfig.informGraveLocation": "Inform Grave Location",
"text.autoconfig.yigd.option.graveConfig.requireItem": "Require Item",
"text.autoconfig.yigd.option.graveConfig.requiredItem": "Required Item",
"text.autoconfig.yigd.option.graveConfig.requiredItemCount": "Required Item Count",
"text.autoconfig.yigd.option.graveConfig.requireShovelToLoot": "Require Shovel to Loot",
"text.autoconfig.yigd.option.graveConfig.retrieveMethods": "Retrieve-Methods",
"text.autoconfig.yigd.option.graveConfig.retrieveMethods.onClick": "On Click",
Expand Down

0 comments on commit cb04c5f

Please sign in to comment.