Combat plugin

Discussion in 'Plugin Development' started by KarimAKL, Apr 8, 2018.

Thread Status:
Not open for further replies.
  1. Offline

    KarimAKL

    @timtower Oh, i just thought you could give me the name for it(like what people call the method), anyway thanks. :)
     
  2. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Not really needed anymore when I posted the actual method with how to call it.
     
  3. Offline

    KarimAKL

    @timtower I just tried looking up the method "System.currentTimeMillis()" but i can only find it used for getting the milliseconds between times and stuff like that. I don't understand how i should use it. :/ Any other methods or do you only recommend this one? If you recommend only this one then you think you can tell me where i can find more information for more uses for it?
     
  4. Online

    timtower Administrator Administrator Moderator

    @KarimAKL Was about cooldowns, so if you search I suggest you include that.
    I personally only recommend this one at the time.
     
  5. Offline

    KarimAKL

    @timtower Okay, thanks. :) I'll try including that and then looking it up again now. :) EDIT: I just looked it up(not finished tho) and i found out you can't tell the amount of time left, is that right? If you aren't able to get the amount of seconds left then is there another way to get that without it not being accurate when the server is lagging? Or is that impossible?
     
    Last edited by a moderator: Apr 17, 2018
  6. Online

    timtower Administrator Administrator Moderator

    @KarimAKL You can get the current time, you subtract the start time from it, that is the time left.
     
  7. Offline

    KarimAKL

    @timtower Okay, thanks for letting me know. :)
    EDIT: Okay so i've tried but i just can't seem to find out what i need to do. :/
    I haven't added anything new to my code but here it is:
    Code:
    public class OnDamageEvent implements Listener {
       
        public static HashMap<UUID, Integer> combat = new HashMap<UUID, Integer>();
       
        @SuppressWarnings("unused")
        private Main plugin;
       
        public OnDamageEvent(Main plugin) {
            this.plugin = plugin;
           
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onDamage(EntityDamageByEntityEvent e) {
            if (e.getDamager() instanceof Player && e.getEntity() instanceof Player) {
                Player victim = (Player) e.getEntity();
                Player attacker = (Player) e.getDamager();
                World victimWorld = victim.getWorld();
                World attackerWorld = attacker.getWorld();
               
                if (victimWorld.getName().equals("world1") && attackerWorld.getName().equals("world1")) {
                    if (combat.containsKey(victim.getUniqueId())) {
                        combat.remove(victim.getUniqueId());
                        combat.put(victim.getUniqueId(), 20*10);
                    } else {
                        combat.put(victim.getUniqueId(), 20*10);
                        victim.sendMessage(ChatColorUtil.chat("you have been attacked by " + attacker + ", don't log out!"));
                    }
                   
                    if (combat.containsKey(attacker.getUniqueId())) {
                        combat.remove(attacker.getUniqueId());
                        combat.put(attacker.getUniqueId(), 20*10);
                    } else {
                        combat.put(attacker.getUniqueId(), 20*10);
                        attacker.sendMessage(ChatColorUtil.chat("you have just attacked " + victim + ", don't log out!"));
                    }
                } else if (victimWorld.getName().equals("world2") && attackerWorld.getName().equals("world2")) {
                    if (combat.containsKey(victim.getUniqueId())) {
                        combat.remove(victim.getUniqueId());
                        combat.put(victim.getUniqueId(), 20*10);
                    } else {
                        combat.put(victim.getUniqueId(), 20*10);
                        victim.sendMessage(ChatColorUtil.chat("you have been attacked by " + attacker + ", don't log out!"));
                    }
                   
                    if (combat.containsKey(attacker.getUniqueId())) {
                        combat.remove(attacker.getUniqueId());
                        combat.put(attacker.getUniqueId(), 20*10);
                    } else {
                        combat.put(attacker.getUniqueId(), 20*10);
                        attacker.sendMessage(ChatColorUtil.chat("you have just attacked " + victim + ", don't log out!"));
                    }
                }
            }
        }
    
    }
    
    I don't quite understand what i should do. :/ Could you tell me some steps of what i should do (without code)? Also, this code is just me guessing that i could do it like that. :p I don't know(or think) that this would actually work but whatever, i'm just trying things out. :p
     
    Last edited by a moderator: Apr 17, 2018
  8. Offline

    KarimAKL

  9. Offline

    KarimAKL

    bump, i deleted the whole project to start from scratch, i've come to this:
    Class: EntityDamageByEntityEvent
    Code:Java
    1.  
    2. public class EntityDamage implements Listener {
    3.  
    4. private Main plugin;
    5.  
    6. public EntityDamage(Main plugin) {
    7. this.plugin = plugin;
    8. Bukkit.getPluginManager().registerEvents(this, plugin);
    9. }
    10.  
    11. @EventHandler
    12. public void onDamage(EntityDamageByEntityEvent e) {
    13. if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
    14. if (e.getEntity() != e.getDamager()) {
    15. if (!e.isCancelled()) {
    16. Player victim = (Player) e.getEntity();
    17. Player attacker = (Player) e.getDamager();
    18. UUID vid = victim.getUniqueId();
    19. UUID aid = attacker.getUniqueId();
    20. String vwn = victim.getWorld().getName();
    21. Integer ct = plugin.getConfig().getInt("Combat-time");
    22. for (String worlds : plugin.getConfig().getConfigurationSection("Worlds").getKeys(false)) {
    23. if (worlds.contains(vwn)) {
    24. if (Combat.log.containsKey(vid)) {
    25. Combat.log.remove(vid);
    26. Combat.log.put(vid, ct);
    27. } else {
    28. Combat.log.put(vid, ct);
    29. victim.sendMessage("Someone attacked you!");
    30. }
    31. if (Combat.log.containsKey(aid)) {
    32. Combat.log.remove(aid);
    33. Combat.log.put(aid, ct);
    34. } else {
    35. Combat.log.put(aid, ct);
    36. attacker.sendMessage("You attacked someone!");
    37. }
    38. }
    39. }
    40. }
    41. }
    42. }
    43. }
    44. }
    45.  

    The thing i am wondering about is how to start using System.currentTimeMillis(), it returns a long but my HashMap is <UUID, Integer> and not <UUID, Long>, should i change it to that or should i convert the long to an integer? If i should convert the long to an integer then how would i do that? Maybe i should do something like this?:
    Code:Java
    1.  
    2. Integer time = (int) System.currentTimeMillis();
    3.  

    Or maybe not? Anyway, how would i get started with this?
     
  10. Online

    timtower Administrator Administrator Moderator

  11. Offline

    KarimAKL

    @timtower Okay, thanks. :) Now what should i change this:
    Code:Java
    1.  
    2. Integer ct = plugin.getConfig().getInt("Combat-time");
    3.  

    to? I want to have the "Combat-time" as an integer for seconds in the config. Maybe convert it from long to integer and integer to long again? :7
     
  12. Online

    timtower Administrator Administrator Moderator

  13. Offline

    KarimAKL

    @timtower If you mean this:
    Code:Java
    1.  
    2. Long ct = plugin.getConfig().getLong("Combat-time");
    3.  

    Then i know i can do that but would i be able to just use an int for that? Example:
    Code:
    Combat-time: 10
    
    Or not? :7
     
  14. Online

    timtower Administrator Administrator Moderator

  15. Offline

    KarimAKL

    @timtower Can't because i have an error when i attack someone. :7
    Error (open)

    Code:
    [22:17:22 ERROR]: Could not pass event EntityDamageByEntityEvent to CombatLog v0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEvent(CraftEventFactory.java:87) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callEntityDamageEvent(CraftEventFactory.java:553) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleEntityDamageEvent(CraftEventFactory.java:466) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.handleLivingEntityDamageEvent(CraftEventFactory.java:585) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityLiving.d(EntityLiving.java:1102) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityHuman.d(EntityHuman.java:859) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityLiving.damageEntity(EntityLiving.java:743) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityHuman.damageEntity(EntityHuman.java:800) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityPlayer.damageEntity(EntityPlayer.java:496) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityHuman.attack(EntityHuman.java:1001) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.EntityPlayer.attack(EntityPlayer.java:1063) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1355) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInUseEntity.a(SourceFile:52) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInUseEntity.a(SourceFile:11) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_161]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_161]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Caused by: java.lang.NullPointerException
            at me.karim.combatlog.listeners.EntityDamage.onDamage(EntityDamage.java:35) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_161]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_161]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            ... 26 more
    
    Do you know what is wrong? "NullPointerException", EntityDamage class line 35 is:
    Code:Java
    1.  
    2. for (String worlds : plugin.getConfig().getConfigurationSection("Worlds").getKeys(false)) {
    3.  

    I guess that code is wrong. :7 This is the config that i'm trying to get at that part:
    Code:
    Worlds:
      - world
    
    EDIT: Nvm fixed the error by changing it to this:
    Code:Java
    1.  
    2. List<String> worlds = plugin.getConfig().getStringList("Worlds");
    3.  

    EDIT2: I tried this:
    Code:Java
    1.  
    2. Player victim = (Player) e.getEntity();
    3. Player attacker = (Player) e.getDamager();
    4. UUID vid = victim.getUniqueId();
    5. UUID aid = attacker.getUniqueId();
    6. String vwn = victim.getWorld().getName();
    7. Long ct = plugin.getConfig().getLong("Combat-time");
    8. List<String> worlds = plugin.getConfig().getStringList("Worlds");
    9. if (worlds.contains(vwn)) {
    10. if (Combat.log.containsKey(vid)) {
    11. Combat.log.remove(vid);
    12. Combat.log.put(vid, ct);
    13. } else {
    14. Combat.log.put(vid, ct);
    15. victim.sendMessage("Someone attacked you!");
    16. }
    17. if (Combat.log.containsKey(aid)) {
    18. Combat.log.remove(aid);
    19. Combat.log.put(aid, ct);
    20. attacker.sendMessage(Color.here("Combat-time: "+ct));
    21. } else {
    22. Combat.log.put(aid, ct);
    23. attacker.sendMessage("You attacked someone!");
    24. }
    25. }
    26.  

    And the output is "Combat-time: 10" all the time. It should be that, right?
     
    Last edited by a moderator: Jun 3, 2018
  16. Offline

    KarimAKL

    Bump, i've been trying to get this working and i currently have this:
    Code:Java
    1. public class EntityDamage implements Listener {
    2.  
    3. private Main plugin;
    4.  
    5. public EntityDamage(Main plugin) {
    6. this.plugin = plugin;
    7. Bukkit.getPluginManager().registerEvents(this, plugin);
    8. }
    9.  
    10. Player attacker = null;
    11.  
    12. @EventHandler
    13. public void onDamage(EntityDamageByEntityEvent e) {
    14. if (!e.isCancelled()) {
    15. if (e.getEntity() instanceof Player) {
    16. Player victim = (Player) e.getEntity();
    17. if (e.getDamager() instanceof Player) {
    18. attacker = (Player) e.getDamager();
    19. } else if (e.getDamager() instanceof Projectile) {
    20. Projectile entity = (Projectile) e.getDamager();
    21. attacker = (Player) entity.getShooter();
    22. }
    23. if (attacker != null && victim != attacker) {
    24. if (plugin.getConfig().getStringList("Worlds").contains(victim.getWorld().getName())) {
    25. int time = plugin.getConfig().getInt("Combat-time");
    26. if (!plugin.Combat().inCombat(victim)) {
    27. plugin.Combat().setCombatTime(victim, time);
    28. victim.sendMessage(plugin.Color(plugin.messages.getString("Combat.someone-attacked-you").replace("{attacker}", attacker.getName())));
    29. } else {
    30. plugin.Combat().removeFromCombat(victim);
    31. plugin.Combat().setCombatTime(victim, time);
    32. }
    33. if (!plugin.Combat().inCombat(attacker)) {
    34. plugin.Combat().setCombatTime(attacker, plugin.getConfig().getInt("Combat-time"));
    35. attacker.sendMessage(plugin.Color(plugin.messages.getString("Combat.you-attacked-someone").replace("{victim}", victim.getName())));
    36. } else {
    37. plugin.Combat().removeFromCombat(attacker);
    38. plugin.Combat().setCombatTime(attacker, time);
    39. }
    40. new BukkitRunnable() {
    41. public void run() {
    42. if (victim.isOnline()) {
    43. if (plugin.Combat().getCombatTime(victim) > 0) {
    44. plugin.Combat().setCombatTime(victim, plugin.Combat().getCombatTime(victim)-1);
    45. } else {
    46. cancel();
    47. plugin.Combat().removeFromCombat(victim);
    48. victim.sendMessage(plugin.Color(plugin.messages.getString("Combat.no-longer-combat")));
    49. }
    50. } else {
    51. cancel();
    52. }
    53. if (attacker.isOnline()) {
    54. if (plugin.Combat().getCombatTime(attacker) > 0) {
    55. plugin.Combat().setCombatTime(attacker, plugin.Combat().getCombatTime(attacker)-1);
    56. } else {
    57. cancel();
    58. plugin.Combat().removeFromCombat(attacker);
    59. attacker.sendMessage(plugin.Color(plugin.messages.getString("Combat.no-longer-combat")));
    60. }
    61. } else {
    62. cancel();
    63. }
    64. }
    65. }.runTaskTimer(plugin, 20, 20);
    66. }
    67. }
    68. }
    69. }
    70. }
    71. }

    But it seems to be acting weird when hitting/getting hit multiple times. (The time goes faster sometimes and not the same time for both players) Am i doing something wrong with the BukkitRunnable? I would like to get this fixed. Other than this i don't have any problems. (At least not yet)
    Btw here is the combat class:
    Code:Java
    1. public class Combat {
    2.  
    3. private HashMap<UUID, Integer> time = new HashMap<UUID, Integer>();
    4.  
    5. public boolean inCombat(Player p) {
    6. return this.time.containsKey(p.getUniqueId());
    7. }
    8.  
    9. public void setCombatTime(Player p, Integer integer) {
    10. this.time.put(p.getUniqueId(), integer);
    11. }
    12.  
    13. public void removeFromCombat(Player p) {
    14. this.time.remove(p.getUniqueId());
    15. }
    16.  
    17. public Integer getCombatTime(Player p) {
    18. return this.time.get(p.getUniqueId());
    19. }
    20. }

    The "plugin.Color("String")" just returns "ChatColor.translateAlternateColorCodes('&', string)".
    EDIT: Here is the Main class:
    Code:Java
    1. public class Main extends JavaPlugin {
    2.  
    3. public String Color(String s) {
    4. return ChatColor.translateAlternateColorCodes('&', s);
    5. }
    6.  
    7. public File messagesFile = new File(getDataFolder()+"/messages.yml");
    8. public FileConfiguration messages = YamlConfiguration.loadConfiguration(messagesFile);
    9.  
    10. private Combat combat;
    11.  
    12. public Combat Combat() {
    13. return combat;
    14. }
    15.  
    16. public void onEnable() {
    17. if (!getDataFolder().exists()) {
    18. getDataFolder().mkdirs();
    19. }
    20. if (!messagesFile.exists()) {
    21. saveResource("messages.yml", false);
    22. }
    23. saveDefaultConfig();
    24. this.combat = new Combat();
    25. new CommandCombatTime(this);
    26. new CommandPreprocess(this);
    27. new EntityDamage(this);
    28. }
    29. }
     
Thread Status:
Not open for further replies.

Share This Page