Jump to content

Recommended Posts

Posted

I'm trying to store the players inventory so that they will keep it after dying if they are holding a specific item, I made a capability extending IItemHandler and ItemStackHandler to hold it which I attached to the players and I've been testing it's functions and it seems to store everything correctly, but no matter what I try, after respawing it's back to being empty and no items were copied

Here's my most resent try:

@Mod.EventBusSubscriber(modid = ExtraTotems.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class CommonEvents {

    @SubscribeEvent
    public static void onEntityDeath(LivingDeathEvent event){
        LivingEntity entity = event.getEntityLiving();
        if(entity instanceof Player player && !event.getSource().isBypassInvul() && checkTotemInventoryProtection(event)){ //last function checks if the totem was in one of the players hands
            player.getCapability(Capabilities.EXTRA_ITEM_HANDLER_CAPABILITY).resolve().ifPresent(deathInventory ->
                    deathInventory.copyInventory(player.getInventory())); //copies the inventory before death to the capability
        }
        //... some extra unrelated stuff
    }

    //...

    @SubscribeEvent
    public static void onPlayerClone(PlayerEvent.Clone event){
        if(event.isWasDeath()) {
            Player originalPlayer = event.getOriginal();
          	Player newPlayer = event.getPlayer();
            int MAIN_HAND = originalPlayer.getInventory().selected;
            int OFF_HAND = 40;

            originalPlayer.getCapability(Capabilities.EXTRA_ITEM_HANDLER_CAPABILITY).resolve().ifPresent(deathInventory ->
                    newPlayer.getCapability(Capabilities.EXTRA_ITEM_HANDLER_CAPABILITY).resolve().ifPresent(newInventory -> {
                        int totemSlot = (deathInventory.getStackInSlot(MAIN_HAND).is(ModItems.TOTEM_OF_KEEPING) ? MAIN_HAND : OFF_HAND); //saves the slot number where the item was held
                        Inventory inv = new Inventory(newPlayer);
                        for (int i = 0; i < 41; ++i) {
                            if (i == totemSlot) continue; //avoids copying the item to have it destroyed
                            inv.setItem(i, deathInventory.getStackInSlot(i));
                        }
                        newInventory.copyInventory(inv); //before I was doing a method where I was trying to directly copy what's in deathInventory to newInventory (not creating an actual inventory in the middle) but it wasn't working
                    })
            );
        } 
    }
  
  	@SubscribeEvent
    public static void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event){
        if(!event.isEndConquered()){
            Player player = event.getPlayer();
            player.getCapability(Capabilities.EXTRA_ITEM_HANDLER_CAPABILITY).resolve().ifPresent(deathInventory -> {
                for (int i = 0; i < 41; i++){
                    player.getInventory().setItem(i, deathInventory.getStackInSlot(i));
                }
            });
        } //also tried just doing this directly on onPlayerClone instead of copying the capability (the idea being the new copy wouldn't get a copy of the capability, just get the inventory), neither seem to work
    } 
  
  	//...
}

 

Am I missing something obvious?

Posted (edited)

I use .ifPresent always just because IntelliJ bugs you if you just use .get without .isPresent (and ifPresent is just like both of them in one)

Copying the stacks directly during PlayerEvent.Clone is what I thought would work in the beginning as well, but it seems the original copy just has an empty inventory, even if the LivingDropsEvent was canceled, that's why I started looking into capabilities

I thought about looking at the KEEPINVENTORY rule, but I absolutely don't understand where it is, it's regitered in the GameRules class but it's just a boolean, I'm sure there is a check for it somewhere, but IntelliJ finds no usages of the variable and I couldn't find it manually

Edited by Whistler

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • How do I download mods for minecraft pe
    • I've tried multiple ways to get the chunk coordinates and none of them have worked, so I'm guessing it may be getChunk().  Is there another way to forcibly load a chunk on that same tick? If all else fails I guess I can just delay my action by a tick, but that's a pain in the butt and makes it more complex to provide appropriate feedback... Also welcome, Zander.  If you're searching for a similar solution, I hope this (eventually) helps you too 😅
    • My guess would be that you are not getting the chunk coordinates right, or getChunk dosnt work.
    • Hello, Recently I have been hosting an RLcraft server on Minecraft Forge Version 1.12.2 - 14.23.5.2860 I originally had lag issues and I chalked it up to playing on less than perfect hardware so I moved the server to better hardware(i9-9900k with 64GB of RAM for 1 player.) After lots of troubleshooting I discovered a couple of things. World saving was causing the MSPT to spike to over 2000Miliseconds and was happening every minute. This kind of makes sense as the world was pre-generated and is over 100GB. I learned that I could turn off world saving and just schedule that and that resolved that issue. After that issue was resolved I discovered another issue. It appears that when exploring chunks, any chunks explored stay loaded. They persist. I was originally thinking that this had to be a mod issue, but it didn't seem like anyone was talking about it. This isn't really a problem on packs with few worldgen mods or just few mods in general, but on beefier packs it becomes a problem. I went through forge changelogs and found this line. Build: 1.12.2-14.23.5.2841 - Thu Aug 29 01:58:50 GMT 2019 bs2609: Improve performance of persistent chunk checks (#5706) Is this a related item to what I am dealing with or just something else?   I went ahead and created a new dedicated server with Just Forge 14.23.5.2860, spark reforged, and it's core mods. I was able to replicate the issue and log it with spark. Hopefully you're able to see this spark profile. It basically shows that as I explored the chunk loading persisted(2000 chunks loaded over 10 minutes with one player)The view distance is set to 10 so that should be 200 chunks per player. If I don't move the loaded chunks are 200. I was however able to fix the persistent chunk issue if I save the world more frequently. My question is, is this intended function of the game or is this a bug? Everywhere I read seems to indicate Minecraft servers save every 5 minutes and not every minute. Can chunks not unload if the world does not autosave. Additionally. Autosave specifically appears to fix this issue. Manually running save-all does not resolve the issue.   I realize this is kind of a log one, sorry. Please let me know if you require further information. Thanks in advance for your time. https://spark.lucko.me/NvlMtC39Yt https://imgur.com/a/K0oyukx https://pastebin.com/z0qGu1Vh  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.