Solved Need help with plugin

Discussion in 'Spigot Plugin Development' started by ThatGuyLogi, Jan 14, 2020.

  1. I am making a plugin for my upcoming server, where if you die, thunder strikes at the death location of the player, and their items go into a spawned chest on the death block location. I also want the chat to say that player was killed. How can I do this?
     
  2. First of all, I don't recommend to use titles like that.

    If you want to start to code that, you need to work with PlayerDeathEvent.
    1. For the message use setDeathMessage("Message here");
    2. To spawn a stroke use the World Location and strikeLightning(Location);
    3. The chest you need to get the player inventory and get all the contents (you can use foreach), then get the block Location and set a Chest, then you get the chest Inventory and put in all the items of the player.

    Code (Java):
    @EventHandler
        public void onDeath(PlayerDeathEvent event){
            Player player = event.getEntity();
            Location location = player.getLocation();

            ////////////////
            //Death Message
            event.setDeathMessage("A message");

            //////////
            //Thunder
            location.getWorld().strikeLightning(location);

            //////////
            //Chest
            location.getBlock().setType(Material.CHEST);
            Chest chest = (Chest) location.getBlock().getState();

            Inventory playerInventory = player.getInventory();
            for (ItemStack itemStack : playerInventory.getContents()){
                if (itemStack == null) return;
                chest.getInventory().addItem(itemStack);
            }
            event.getDrops().clear();
        }
     
    #2 irvinglink, Jan 14, 2020
    Last edited: Jan 14, 2020
    • Like Like x 1
  3. Strahan

    Benefactor

    Fairly simple, though I assume you meant you want lightning to strike. Thunder doesn't strike :)
    That's a good start, but it has a flaw. The player will lose items if they are loaded up. You are only creating a single chest, and that holds 27 items whereas as player may have 41 items. You'd want to loop in the areas around the chest looking for air so you can make it a double. Then you have to account for if there are no free spots then run some more tests to determine a nearby location where it can be placed and that could get tricky. Imagine a player falling down a 1 block wide shaft to their death for example.

    PS unless a single chest block can have a larger inventory size set on it, I have no idea if that is possible or not. Be pretty sweet it you can.
     
    • Like Like x 1
  4. Or just use getInventory().getContents() and getInventory().setContents instead of using a for loop
     
  5. Use the PlayerDeathEvent
    get the player location and summon a lightning there.
    Then check if event.getKeepInventory() and if the inv should not be kept you just get the content as a ItemStack[] and clear the players inventory.
    At last you need to create a chest (get the inventory via the blockstate) and put the ItemStack[] in there.
    The PlayerDeathEvent also has a method called setDeathMessage(String)

    PS a chest only has 27 slots so you might need to create your own handler for that.
    fortunately chest is a TileState so you can store stuff in the PersistentDataContainer
    Just serialize the ItemStack[] as a json or Base64 String or something and save it.
    Create a new Inventory when the chunk with the chest is loaded (i think there is some Chunk#getTileStates method) and make sure that the custom inv opens when a player clicks the chest.
    Ah and either make the chest indestructable or handle explosions, block breaks, etc by yourself (dropping the content and so on)
     
    #5 7smile7, Jan 14, 2020
    Last edited: Jan 14, 2020
  6. Strahan

    Benefactor

    That won't work for the same reason the player loses items; the inv amounts are disparate.
     
    • Agree Agree x 1
  7. Thank you dude. Massive thanks. Will mark this as solved!
     
  8. Strahan

    Benefactor

    Don't forget my warning though, unless you want some pissed off players lol
     
  9. This was before i got the notification of you lol
     
  10. Yeah don't forget that warning. I make it a simple code. To fix that problem you need to check the total of items in the Inventory, if the total of items are more than 27 you will need to make a double chest.
     
    • Agree Agree x 1