Solved Disallow certain potions in combat, possible?

Discussion in 'Plugin Development' started by Stupeflip, Mar 26, 2014.

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

    Stupeflip

    Hello,
    I wan't to modify the famous plugin AntiRelog ( http://dev.bukkit.org/bukkit-plugins/anti-relog/ )
    To add a function who disallow the using of Heal and Regeneration potions only in combat.

    Do you think it is possible?
    Here is the code of the plugin:
    https://github.com/r0306/Anti-Relog/tree/master/src/com/github/r0306/AntiRelog

    Sorry for my bad english, please help me if you can by telling me how I can do that, it will be really helpfull for my server ! :)

    No one know just if it's possible at least? ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Stupeflip
    What you can do is when a player takes damage from another player, add the damaged player to an ArrayList. Then in the PlayerItemConsumeEvent make a check to see if the item is a Heal or Regen potion and if so, check the ArrayList to see if the player is contained in it. If they are, cancel the event. Hope this helps :)
     
    Stupeflip likes this.
  3. Offline

    Stupeflip

    If think I will do it by my own plugin, will be maybe more easy, thanks so much, i'm a noob in developement so I probably never found the ArrayList myself, but, can I remove the player from the ArrayList after something like 10 seconds? So that he can heal himself when the fight had stop

    Sorry for my bad english, there is probably lots of errors here :p
     
  4. Offline

    MooshViolet

    Stupeflip Yes. All you have to do is make a scheduler. Put it in ticks (ticks are what your ide reads). After the time, take them out of the arraylist like so:
    Code:java
    1. list.remove(p.getName);
    2. //Also, the "list" is the name of your ArrayList.
    3.  
     
    Stupeflip likes this.
  5. Offline

    xTigerRebornx

    Stupeflip If you are still working on the healing, use this for seeing when an Entity regains health
     
    Stupeflip likes this.
  6. Offline

    Stupeflip

    Thanks you all guys I will try to do it tonight (actually 10pm in France)

    MooshViolet Thanks, but, seems like you do the opposite of your signature :p

    xTigerRebornx Thanks you :)
     
  7. Offline

    Stupeflip

    Hi guys i'm back with my code,
    i request your help to add the scheduler as said MooshViolet, to remove the player from the ArrayList "InFight"

    I think this is the last thing to make the plugin work.
    I've done it alone and i'm a big noob so please tell me if there is any errors here :p
    Code:java
    1. //Plugins function:
    2. //When a player have been hurt by another player, add him in arraylist "in fight"
    3. //Remove him from the arraylist "in fight" after 10 seconds with no damage from another player
    4. //When a player want to consume a potion check if he is in "InFight", if he is, cancel the potion consuming
    5.  
    6. package me.tintinminecraft;
    7.  
    8. import java.util.ArrayList;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Material;
    11. import org.bukkit.entity.Entity;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    16. import org.bukkit.event.player.PlayerItemConsumeEvent;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class PotionsOpium extends JavaPlugin implements Listener {
    21. static ArrayList<Player> InFight = new ArrayList<Player>();
    22.  
    23.  
    24. //Adding the player to the ArrayList "InFight" when he get hurt by another player
    25.  
    26. @EventHandler
    27. public void onEntityDamage(EntityDamageByEntityEvent d) {
    28. Entity entity = d.getEntity();
    29. if (d.getDamager() instanceof Player) {
    30. Player player = (entity instanceof Player) ? (Player)entity : null;
    31. InFight.add(player);
    32. player.sendMessage("Test");
    33. }
    34. }
    35.  
    36.  
    37.  
    38. //Cancelling potion consuming if player is in ArrayList "InFight"
    39.  
    40. @EventHandler
    41. public void onConsume(PlayerItemConsumeEvent c) {
    42. Player player = (c.getPlayer());
    43. ItemStack heal1 = ItemStack (Material.POTION);
    44. if (player.getItemInHand() == heal1){
    45. String cName = c.getPlayer().getName();
    46. if (PotionsOpium.InFight.contains(cName)) {
    47. player.sendMessage(ChatColor.RED + "Vous êtes en combat, les potions sont interdites!");
    48. c.setCancelled(true); }
    49. else {
    50. return;
    51. }
    52. }
    53. }
    54.  
    55.  
    56. // Don't know why I add that but it remove me an error ^^
    57.  
    58. private ItemStack ItemStack(Material potion) {
    59. // TODO Auto-generated method stub
    60. return null;
    61. }
    62. }
     
  8. Offline

    Jalau

    scheduler.scheduleSyncDelayedTask(plugin instance (in your main it's "this"), new Runnable()
    {
    public void run()
    {
    //Remove here
    }
    }, seconds*20L);
     
    Stupeflip likes this.
  9. Offline

    MooshViolet

    Stupeflip Yes do what Jalau says.
    Code:
    scheduler.scheduleSyncDelayedTask(plugin instance (in your main it's "this"), new Runnable()
    {
    public void run()
    {
    InFight.remove(c.getName)
    }
    }, seconds*20L);
     
    Stupeflip likes this.
  10. Offline

    Stupeflip

    Another great thanks for you guys MooshViolet Jalau
    I think i finished the code:
    Code:java
    1. public class PotionsOpium extends JavaPlugin implements Listener {
    2. static ArrayList<Player> InFight = new ArrayList<Player>();
    3.  
    4.  
    5. //Adding the player to the ArrayList "InFight" when he get hurt by another player
    6.  
    7. @EventHandler
    8. public void onEntityDamage(EntityDamageByEntityEvent d) {
    9. Entity entity = d.getEntity();
    10. if (d.getDamager() instanceof Player) {
    11. final Player player = (entity instanceof Player) ? (Player)entity : null;
    12. InFight.add(player);
    13. player.sendMessage("Adding");
    14. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    15. public void run(){
    16. InFight.remove(player);
    17. player.sendMessage("Removing");
    18. }
    19. }, 200L);
    20. }
    21. }
    22.  
    23.  
    24.  
    25. //Cancelling potion consuming if player is in ArrayList "InFight"
    26.  
    27. @EventHandler
    28. public void onConsume(PlayerItemConsumeEvent c) {
    29. Player player = (c.getPlayer());
    30. ItemStack heal1 = ItemStack (Material.POTION);
    31. if (player.getItemInHand() == heal1){
    32. String cName = c.getPlayer().getName();
    33. if (PotionsOpium.InFight.contains(cName)) {
    34. player.sendMessage(ChatColor.RED + "Vous êtes en combat, les potions sont interdites!");
    35. c.setCancelled(true); }
    36. else {
    37. return;
    38. }
    39. }
    40. }
    41.  
    42.  
    43.  
    44. // Don't know why I add that but it remove me an error ^^
    45.  
    46. private ItemStack ItemStack(Material potion) {
    47. // TODO Auto-generated method stub
    48. return null;
    49.  
    50. }
    51. }


    But won't work ^^
    There is no message "Adding" when i get hurt
    There is no message "Removing" after 10 seconds
    I can consume potion :/

    Any ideas?

    Maybe the end:


    Code:java
    1. // Don't know why I add that but it remove me an error ^^
    2.  
    3. private ItemStack ItemStack(Material potion) {
    4. // TODO Auto-generated method stub
    5. return null;
     
  11. Offline

    Jalau

    d.getDamager() change that to event.getDamager(); Also don't save Player instances in ArrayLists, use playernames instead so make a arraylist<string> and add p.getName() ;)
     
    Stupeflip likes this.
  12. Offline

    Stupeflip

    Jalau thanks for the String ArrayList and the .getName,
    But for the d.getDamager(), if i change to event.getDamager it will break the code ^^
    So I just add a ";" at the end like that:
    Code:java
    1. public void onEntityDamage(EntityDamageByEntityEvent d) {
    2. Entity entity = d.getEntity();
    3. if (d.getDamager() instanceof Player); {

    There is no errors too but still not working :S
     
  13. Offline

    MooshViolet

    Stupeflip leave it as d.getEntity. But, use:
    Code:java
    1. Player p = d.getEntity()

    Also, remove your itemmetas. They are unneeded. Use this instead of checking what they are holding:
    Code:java
    1. if (player.getItemInHand().getType() == Material.POTION){

    then check if they are in the string arraylist with:
    Code:java
    1. if(InFight.contains(p.getName)){

    then:
    Code:java
    1. e.setCancelled(true);

    And, change your arraylist to this:
    Code:java
    1. public ArrayList<String> InFight = new ArrayList<String>();
     
    Stupeflip likes this.
  14. Offline

    Stupeflip

    Jalau MooshViolet xTigerRebornx

    Sorry to annoy you guys but still not working with all you recommend to me :/
    Code:java
    1. public class PotionsOpium extends JavaPlugin implements Listener {
    2. static ArrayList<String> InFight = new ArrayList<String>();
    3.  
    4.  
    5. //Adding the player to the ArrayList "InFight" when he get hurt by another player
    6.  
    7. @EventHandler
    8. public void onEntityDamage(EntityDamageByEntityEvent d) {
    9. Entity entity = d.getEntity();
    10. if (d.getDamager() instanceof Player); {
    11. final Player p = (entity instanceof Player) ? (Player)entity : null;
    12. InFight.add(p.getName());
    13. p.sendMessage("Adding");
    14. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    15. public void run(){
    16. InFight.remove(p.getName());
    17. p.sendMessage("Removing");
    18. }
    19. }, 200L);
    20. }
    21. }
    22.  
    23.  
    24. //Cancelling potion consuming if player is in ArrayList "InFight"
    25.  
    26. @EventHandler
    27. public void onConsume(PlayerItemConsumeEvent c) {
    28. Player p = (c.getPlayer());
    29. if (p.getItemInHand().getType() == Material.POTION ){
    30. if (PotionsOpium.InFight.contains(p.getName())) {
    31. p.sendMessage(ChatColor.RED + "Vous êtes en combat, les potions sont interdites!");
    32. c.setCancelled(true); }
    33. else {
    34. return;
    35. }
    36. }
    37. }
    38. }


    Thanks you :/
     
  15. Offline

    Jalau

    Is the event registered? At a debug line right infront of everything in the event so you can see if it's fired? ;)
     
  16. Offline

    Sgt_Tailor

    this.getPluginManger().registerEvents(this,this)
     
  17. Offline

    Stupeflip

    I added line to register as said Sgt_Tailor,
    I added line to broadcast when an event start
    But can't see any broadcast, that mean my events aren't working? But there is no errors in code
    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageByEntityEvent d) {
    3. Bukkit.getPluginManager().registerEvents(this,this);
    4. Bukkit.broadcastMessage("Dommage");
    5. Entity entity = d.getEntity();
    6. if (d.getDamager() instanceof Player); {
    7. final Player p = (entity instanceof Player) ? (Player)entity : null;
    8. InFight.add(p.getName());
    9. p.sendMessage("Adding");
    10. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    11. public void run(){
    12. InFight.remove(p.getName());
    13. p.sendMessage("Removing");
    14. }
    15. }, 200L);
    16. }
    17. }
    18.  
    19.  
    20. //Cancelling potion consuming if player is in ArrayList "InFight"
    21.  
    22. @EventHandler
    23. public void onConsume(PlayerItemConsumeEvent c) {
    24. Bukkit.getPluginManager().registerEvents(this,this);
    25. Bukkit.broadcastMessage("Potion");
    26. Player p = (c.getPlayer());
    27. if (p.getItemInHand().getType() == Material.POTION ){
    28. if (PotionsOpium.InFight.contains(p.getName())) {
    29. p.sendMessage(ChatColor.RED + "Vous êtes en combat, les potions sont interdites!");
    30. c.setCancelled(true); }
    31. else {
    32. return;
    33. }
    34. }
    35. }
     
  18. Offline

    Sgt_Tailor

    Stupeflip my line should go in the onEnable method of your plugin, but as you seem not to be familiar with bukkit I will link you here
     
  19. Offline

    MooshViolet

    Stupeflip Yes. The Register events line should go under you onEnable. So like:
    Code:
    public void onEnable(){
    this.getServer.getPluginManager.registerEvents(this, this);
    and remove the register events in your event handler.
    also I recommended use public arraylist, not static.
     
  20. Offline

    Stupeflip

    Oh yeah big thanks man Sgt_Tailor it work just with a little bug but nothing important!
    Now I must disallow only heal potion and only splash of healing, i'll try myself :)

    Thanks you all

    EDIT: The "little bug" i was talking about generate a dupe bug :S
    When the plugin cancel the consuming, the potion go on the helmet place in inventory
    When i take the potion it dupe the helmet Oo

    Help please ^^'


    Thanks you MooshViolet i've done it, and add some line to clear the list on disable and don't add the damaged to the list if he is already in ^^

    It finally work like that:
    I change the "Consume" part to "Interact" (steal this part in AntiInvisibility plugin :3 )
    Code:java
    1. //Plugins function:
    2. //When a player have been hurt by another player, add him in arraylist "in fight"
    3. //Remove him from the arraylist "in fight" after 10 seconds with no damage from another player
    4. //When a player want to consume a potion check if he is in "InFight", if he is, cancel the potion consuming
    5.  
    6. package me.tintinminecraft;
    7.  
    8. import java.util.ArrayList;
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Material;
    12. import org.bukkit.entity.Entity;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.block.Action;
    17. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    18. import org.bukkit.event.player.PlayerInteractEvent;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21. import org.bukkit.potion.Potion;
    22. import org.bukkit.potion.PotionEffectType;
    23.  
    24. public class PotionsOpium extends JavaPlugin implements Listener {
    25. static ArrayList<String> InFight = new ArrayList<String>();
    26.  
    27. // Send a message and register events
    28. @Override
    29. public void onEnable() {
    30. getServer().getPluginManager().registerEvents(this, this);
    31. getLogger().info("[PotionsOpium] Plugin operationnel.");
    32. }
    33.  
    34.  
    35. // Send a message and clear the list
    36. @Override
    37. public void onDisable() {
    38. getLogger().info("[PotionsOpium] Extinction des feux.");
    39. InFight.clear();
    40. }
    41.  
    42.  
    43. //Add the player to the ArrayList "InFight" when he get hurt by another player
    44. @EventHandler
    45. public void onDamage(EntityDamageByEntityEvent d) {
    46. Entity entity = d.getEntity();
    47. if (d.getDamager() instanceof Player); {
    48. final Player p = (entity instanceof Player) ? (Player)entity : null;
    49. if (PotionsOpium.InFight.contains (p.getName())) {
    50. return;
    51. }
    52. else {
    53. InFight.add(p.getName());
    54. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    55. public void run(){
    56. InFight.remove(p.getName());
    57. }
    58. }, 200);
    59. }
    60. }
    61. }
    62.  
    63. //Cancel potions consuming if player is in ArrayList "InFight"
    64. @EventHandler
    65. public void onDrink(PlayerInteractEvent event) {
    66. Action action = event.getAction();
    67. Player player = event.getPlayer();
    68. if ((action == Action.RIGHT_CLICK_AIR) || (action == Action.RIGHT_CLICK_BLOCK)) {
    69. ItemStack it = player.getItemInHand();
    70. if ((it.getType() == Material.POTION) && (it.getDurability() != 0)) {
    71. Potion potion = Potion.fromItemStack(it);
    72. PotionEffectType effecttype = potion.getType().getEffectType();
    73. if ((effecttype == PotionEffectType.HEAL) || (effecttype == PotionEffectType.REGENERATION)) {
    74. if (PotionsOpium.InFight.contains (player.getName())) {
    75. event.setCancelled(true);
    76. player.sendMessage(ChatColor.RED + ("Vous êtes en combat, les potions sont interdites!"));
    77. }
    78. else {
    79. return;
    80. }
    81. }
    82. }
    83. }
    84. }
    85. }


    Thanks you all guys i appreciate your help! :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  21. Offline

    Sgt_Tailor

    Stupeflip no problem ;) You can now mark this as solved
     
Thread Status:
Not open for further replies.

Share This Page