Skip to content
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
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ mod_version=1.0.14

# Base info
mc_version=1.18.2
forge_version=40.1.0
forge_version=40.2.0
mcp_channel=official
mcp_mappings=1.18.2

# optional dependencies
jei_version=jei-1.18.2:9.5.5.174
curios_version=1.18.2-5.0.7.0
jei_version=jei-1.18.2:9.7.1.255
curios_version=1.18.2-5.0.7.1
8 changes: 8 additions & 0 deletions src/main/java/com/lothrazar/PartEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.lothrazar;

public enum PartEnum {
NONE,
HOTBAR,
ARMOR,
HOTBAR_AND_ARMOR
}
5 changes: 5 additions & 0 deletions src/main/java/com/lothrazar/simpletomb/ConfigTomb.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.electronwill.nightconfig.core.io.WritingMode;
import com.lothrazar.PartEnum;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.ForgeConfigSpec.BooleanValue;
import net.minecraftforge.common.ForgeConfigSpec.EnumValue;
import net.minecraftforge.common.ForgeConfigSpec.IntValue;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;

Expand All @@ -25,6 +27,7 @@ public class ConfigTomb {
public static IntValue VSEARCHRANGE;
public static IntValue HSEARCHRANGE;
public static BooleanValue KEYOPENONUSE;
public static EnumValue<PartEnum> KEEPPARTS;
static final String WALL = "####################################################################################";

public static void setup(Path path) {
Expand Down Expand Up @@ -57,6 +60,8 @@ private static void initConfig() {
.defineInRange("search_height", 16, 2, 128);
HSEARCHRANGE = CFG.comment("\r\nWhen searching for a grave location, this is the maximum range to check")
.defineInRange("search_range", 8, 2, 128);
KEEPPARTS = CFG.comment("\r\nKeep parts of the inventory")
.defineEnum("keep_parts", PartEnum.NONE);
CFG.pop();
CFG.comment(WALL).push("key");
KEYGIVEN = CFG.comment("\r\nWhether to give a Grave Key item to the player on death. Tomb can be opened without they key, but the key will help the player locate the grave")
Expand Down
57 changes: 53 additions & 4 deletions src/main/java/com/lothrazar/simpletomb/event/PlayerTombEvents.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lothrazar.simpletomb.event;

import com.lothrazar.PartEnum;
import com.lothrazar.simpletomb.ConfigTomb;
import com.lothrazar.simpletomb.ModTomb;
import com.lothrazar.simpletomb.TombRegistry;
Expand Down Expand Up @@ -27,6 +28,7 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerEvent.PlayerLoggedInEvent;
Expand Down Expand Up @@ -105,11 +107,13 @@ public void onPlayerRespawn(PlayerRespawnEvent event) {

private void storeSoulboundsOnBody(Player player, List<ItemStack> keys) {
CompoundTag persistentTag = EntityHelper.getPersistentTag(player);
ListTag stackList = new ListTag();
persistentTag.put(TB_SOULBOUND_STACKS, stackList);
ListTag stackList = persistentTag.contains(TB_SOULBOUND_STACKS) ?
persistentTag.getList(TB_SOULBOUND_STACKS, CompoundTag.TAG_COMPOUND) :
new ListTag();
for (ItemStack key : keys) {
stackList.add(key.serializeNBT());
}
persistentTag.put(TB_SOULBOUND_STACKS, stackList);
keys.clear();
}
// private void storeIntegerStorageMap(PlayerEntity player) {
Expand All @@ -132,6 +136,42 @@ private void storeSoulboundsOnBody(Player player, List<ItemStack> keys) {
// storeIntegerStorageMap(player);
// }
// }
@SubscribeEvent(receiveCanceled = true)
public void onLivingDeath(LivingDeathEvent event) {
if (!EntityHelper.isValidPlayer(event.getEntityLiving()) ||
WorldHelper.isRuleKeepInventory((Player) event.getEntityLiving())) {
return;
}

if(!event.isCanceled()) {
Player player = (Player) event.getEntityLiving();
PartEnum part = ConfigTomb.KEEPPARTS.get();
switch (part) {
case HOTBAR -> {
List<ItemStack> hotbarStacks = new ArrayList<>();
for (int i = 0; i < 9; i++) {
hotbarStacks.add(i, player.getInventory().items.get(i));
}
keepingMap.put(player.getUUID(), hotbarStacks);
}
case ARMOR -> {
List<ItemStack> armorStacks = new ArrayList<>();
player.getInventory().armor.forEach(armorStacks::add);
keepingMap.put(player.getUUID(), armorStacks);
}
case HOTBAR_AND_ARMOR -> {
List<ItemStack> stacks = new ArrayList<>();
for (int i = 0; i < 9; i++) {
stacks.add(i, player.getInventory().items.get(i));
}
player.getInventory().armor.forEach(stacks::add);
keepingMap.put(player.getUUID(), stacks);
}
}
}
}

private final static Map<UUID, List<ItemStack>> keepingMap = new HashMap<>();

@SubscribeEvent(priority = EventPriority.LOWEST, receiveCanceled = true)
public void onPlayerDrops(LivingDropsEvent event) {
Expand All @@ -142,6 +182,15 @@ public void onPlayerDrops(LivingDropsEvent event) {
WorldHelper.isRuleKeepInventory((Player) event.getEntityLiving())) {
return;
}

Player player = (Player) event.getEntityLiving();
List<ItemStack> keepStacks = keepingMap.getOrDefault(player.getUUID(), new ArrayList<>());
if(!keepStacks.isEmpty()) {
event.getDrops().removeIf(entity -> keepStacks.contains(entity.getItem()));
this.storeSoulboundsOnBody(player, keepStacks);
keepingMap.remove(player.getUUID());
}

saveBackup(event);
placeTombstone(event);
}
Expand All @@ -151,7 +200,7 @@ public void onSaveFile(PlayerEvent.SaveToFile event) {
Player player = event.getPlayer();
File mctomb = new File(event.getPlayerDirectory(), player.getUUID() + TOMB_FILE_EXT);
//
//save player data to the file
//save player data to the file
if (grv.containsKey(player.getUUID())) {
//yes i have data to save
PlayerTombRecords dataToSave = grv.get(player.getUUID());
Expand Down Expand Up @@ -214,7 +263,7 @@ private void saveBackup(LivingDropsEvent event) {
}
if (!isEmpty) {
//NEW data model. write to string
//timestamp
//timestamp
tombstoneTag.putLong("timestamp", System.currentTimeMillis());
tombstoneTag.put("drops", drops);
tombstoneTag.put("pos", NbtUtils.writeBlockPos(player.blockPosition()));
Expand Down