Solved It does not work as I wanted

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

  1. Hello, my plugin developer did something like that. Unfortunately it does not work as I wanted. I want someone killing someone and then the same person again, they won't get the reward before 24 hours. That's how it works, but unfortunately if I kill another player, I don't get a prize either.


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

    import java.util.ArrayList;
    import java.util.List;

    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.data.Lang;
    import pl.tokzen.core.tasks.KillTask;
    import pl.tokzen.core.utils.ItemBuilder;
    import pl.tokzen.core.utils.Util;

    public class DeathListener implements Listener{
       
        public static List<String> killers = new ArrayList<String>();
       
        @EventHandler
        public void onDeath(PlayerDeathEvent e) {
            e.getEntity().getWorld().strikeLightningEffect(e.getEntity().getLocation());
            if(e.getEntity().getKiller() instanceof Player) {
                Player killer = (Player)e.getEntity().getKiller();
                Player victim = (Player)e.getEntity();
                if(killers.contains(killer.getName())) {
                    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();
                    killer.getInventory().addItem(coin);
                    killer.updateInventory();
                    killers.add(killer.getName());
                    KillTask.timer(killer);
                }
            }
        }
    }

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

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;

    import pl.tokzen.core.Main;
    import pl.tokzen.core.listeners.DeathListener;

    public class KillTask {
       
        public static void timer(Player killer) {
            Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getPlugin(), new Runnable() {
                @Override
                public void run() {
                    if(DeathListener.killers.contains(killer.getName())) {
                        DeathListener.killers.remove(killer.getName());
                    }
                }      
            }, 86400*20);
        }
    }
     
     
  2. Can you re-explain what is supposed to happen and what the problem is again? Is it that if you killed a player, you get the reward. But if you kill another player (that is not the same player) you don't get a reward even when you should be able to since its a different player?

    You should also make sure you have registered your events and put some debug code to make sure your code gets to the points it should get to.
     
  3. Strahan

    Benefactor

    Well, yea, you are just using a basic List. So as soon as you kill someone, you get put in the list. When you kill the next person, you are in the list so you don't get the prize. The whole point is to be keyed to the person killed, and you aren't doing that.

    Also, using a task is not necessary. Just set the time of last kill and look at that when you kill someone. Plus it is easy to write to config which is good because if you do a reload or stop/start server with what you have now, all the time restrictions are gone.

    Plus learn proper OOP rather than making things static as a crutch for interclass data exchange.

    PS: also you do not need to cast something when the item is already of the same type.
     
    #3 Strahan, Jan 16, 2020
    Last edited: Jan 16, 2020
  4. [​IMG]
     
    • Funny Funny x 5
    • Agree Agree x 1