Solved I don't know how to fix it...

Discussion in 'Spigot Plugin Development' started by wiktor_k, Jan 18, 2020.

  1. Hello, my goal is if player A kills player B he get a gift (magiccoin). But if player A kills player B again within a hour then he don't get a coin. First after one hour but if player A kills player C then he get another one magiccoin. I tried early with Arraylist but it wasn't work. Now it works too not soo how I want. If someone can fix that please.

    Code (Java):
    package pl.tokzen.core.listeners;

    import java.util.HashMap;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;

    import pl.tokzen.core.Main;
    import pl.tokzen.core.utils.ItemBuilder;
    import pl.tokzen.core.utils.Util;

    public class DeathListener implements Listener{
        public static HashMap<Player, Player> map = new HashMap<>();
        public void onDeath(PlayerDeathEvent e) {
            if(e.getEntity().getKiller() instanceof Player) {
                Player killer = (Player)e.getEntity().getKiller();
                Player victim = (Player)e.getEntity();
                if((map.containsKey(killer)) && (map.containsValue(victim))) {
                    killer.sendMessage(Util.fixColor("&8>> &4&oBlad: &cOstatnio zabiles tego samego gracza! Nagroda nie zostala przyznana."));
                } else {
                    killer.sendMessage(Util.fixColor("&8>> &7Zabiles &f" + victim.getName() + " &7i dostales nagrode! &fx1 " + Lang.ITEMS_MAGICCOIN));
                    ItemStack coin = new ItemBuilder(Material.DOUBLE_PLANT, 1).setName(Util.fixColor(Lang.ITEMS_MAGICCOIN)).addEnchant(Enchantment.DURABILITY, 10).toItemStack();
                    map.put(killer, victim);
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getPlugin(), new Runnable() {
                        public void run() {
                            if((map.containsKey(killer)) && (map.containsValue(victim))) {
                                map.remove(killer, victim);
                    }, 3600*20);
  2. Ok. If you understood java properly you should be able to write a class that tracks every data you want.
    For example:
    we have a class called PlayerData
    That just maintains a
    Code (Text):
    Map<UUID, Long> killedPlayers;
    where the key (uuid) is the players ID and the value (long) is the unix time in ms (the time you get by calling System.currentTimeMillis())

    Now you maintain a
    Code (Text):
    Map<Player, PlayerData> playerDataMap;
    where you put every player in when he joins and take him out when he leaves.

    Now if a player kills another then you can simply get the PlayerData from the map
    then check if he has killed a player with this UUID. If yes get the long and check the time difference to currentTimeMillis()

    If you want a simple solution:
    Use a Table<UUID, UUID, Long>
    You can figure out how to use it here
    guava is already part of spigot so you can skip all the importing.

    PPS watch some more Java tutorials