Item On Death

0 Downloads
This plugin is currently not working properly, and is not recommended for use. I will release an update as soon as I can to fix the major issue I did not see when I was writing the plugin.

The ItemOnDeath plugin is pretty self explanatory; when a player dies, one (random) item is kept from their inventory and is returned to them when they respawn. There is no configuration for this plugin (yet).

Developed by: Derragon (Scott Stevens) Owned by: Open Source Programming Group Source Code: Open

Features:

  • Keep one (random) item on death.

Notes

  • I am accepting ideas for other server plugins and features for this plugin/other plugin(s).
  • This plugin is tested for major bugs/issues, and updates are not released unless those bugs/issues are fixed.
  • All of my code is open-source, and is meant to help other plugin developers learn.
  • This plugin was designed by the OpenSourceProgramming group, which consists of only myself (Derragon) currently.
  • If you would like to join the OpenSourceProgramming group, please send me a PM with your previous programming experience, along with 1 (one) or more references that can vouch for your previous experience.

To-Do:

  • Create configuration abilities.
  • Add new feature(s).

Source Code:

package ca.bcminecraft.itemondeath;

import java.util.HashMap;
import java.util.Random;
import java.util.logging.Logger;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

/**
 *
 * @author Scott
 */
public class Main extends JavaPlugin implements Listener {
    
    Logger log = Logger.getLogger("Minecraft");
    Random r = new Random();
    HashMap<String, ItemStack> returnItems = new HashMap();
    
    public void onEnable () {
        log.info("Enabled.");
        getServer().getPluginManager().registerEvents(this, this);
    }
    
    public void onDisable () {
        log.info("Disabled.");
    }
    
    @EventHandler
    public boolean onPlayerDeath(PlayerDeathEvent e){
        Player p = e.getEntity();
        Inventory i = p.getInventory();
        int toGet = r.nextInt(i.getContents().length - 1);
        ItemStack item = i.getItem(toGet);
        
        returnItems.put(p.getName(), item);
        if (returnItems.containsKey(p.getName())){
            return true;
        } else {
            return false;
        }
    }
    
    @EventHandler
    public boolean onPlayerRespawn(PlayerRespawnEvent e){
        Player p = e.getPlayer();
        String name = p.getName();
        if (returnItems.containsKey(name)){
            p.setItemInHand(returnItems.get(name));
            if (p.getItemInHand() == returnItems.get(name)){
                returnItems.remove(name);
            }
            else {
                return false;
            }
        }
        return true;
    }
    
}