Solved Trying to make an invisible entity that players can hit please help

Discussion in 'Plugin Development' started by SantaClawz69, Aug 23, 2014.

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

    SantaClawz69

    Hi, I'm trying to make an anti cheat plugin so if a player hits this invisible entity then it would send them a message and eventually leading to a jail. This is the code that I have so far, if I have any errors or something you want to suggest please do so. Basically what happens is that if a player gets killed an entity will spawn near them (in this case because I don't know how to make an invisible entity I'm using a zombie with an invisibility potion effect). I made the zombie not able to attack the player because I want to see if the players hack sees it before he does (I'm not really sure how to make that specific zombie not attack so I made all zombies not being able to attack for now). Please help meh D:


    Code:java
    1. package me.Santa.AntiCheat;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.entity.Zombie;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.EntityDeathEvent;
    12. import org.bukkit.event.entity.EntityTargetEvent;
    13. import org.bukkit.event.entity.EntityTargetEvent.TargetReason;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15. import org.bukkit.potion.PotionEffect;
    16. import org.bukkit.potion.PotionEffectType;
    17.  
    18. public class Main extends JavaPlugin implements Listener {
    19. public void onEnable() {
    20. Bukkit.getPluginManager().registerEvents(this, this);
    21. }
    22.  
    23. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    24. return true;
    25. }
    26.  
    27.  
    28. @EventHandler
    29. public void onKill(EntityDeathEvent e) {
    30. Player player = (Player) e.getEntity();
    31.  
    32. Location inFront = player.getLocation().toVector()
    33. .add(player.getLocation().getDirection().multiply(3))
    34. .toLocation(player.getWorld());
    35. int x = inFront.getBlockX();
    36. int y = player.getLocation().getBlockY();
    37. int z = inFront.getBlockZ();
    38.  
    39. Location finalLocation = new Location(player.getWorld(), x, y, z);
    40. Zombie entity = player.getWorld().spawn(finalLocation, Zombie.class);
    41.  
    42. entity.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1));
    43. }
    44.  
    45. @EventHandler
    46. public void EventTarget(EntityTargetEvent e) {
    47. e.getReason();
    48. if (e.getReason().equals(TargetReason.CLOSEST_PLAYER)) {
    49. e.setCancelled(true);
    50. }
    51.  
    52.  
    53.  
    54.  
    55.  
    56. }
    57. }
    58.  
    59.  
     
  2. Offline

    TheMcScavenger

    Either:
    • Create a custom Entity using NMS
    • Give the Entity properties when it spawns (e.g. setting the target to null), and cancel the event where the entity targets a player.
    I think you could also cancel the Zombie from showing up when the player tries to render it, but I'm not exactly sure about how to do that.. I think NMS would be the "easiest" way to go.
     
    CapitanChewbacca likes this.
  3. Offline

    SantaClawz69

    Thanks so much for the reply :D. I'm pretty new to using NMS because I've only used it about once or twice, so here is the code that I have so far!

    Code:java
    1. package me.Bryan.AntiCheat;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Entity;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.entity.EntityDamageEvent;
    12. import org.bukkit.event.entity.EntityDeathEvent;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. import de.kumpelblase2.remoteentities.EntityManager;
    16. import de.kumpelblase2.remoteentities.RemoteEntities;
    17. import de.kumpelblase2.remoteentities.api.RemoteEntity;
    18. import de.kumpelblase2.remoteentities.api.RemoteEntityType;
    19.  
    20. public class Main extends JavaPlugin implements Listener {
    21. public void onEnable() {
    22. for(Entity e: Bukkit.getWorld("world").getEntities()) {
    23. e.remove();
    24. onKill(null);
    25. Bukkit.getPluginManager().registerEvents(this, this);
    26.  
    27. }
    28. }
    29. public boolean onCommand(CommandSender sender, Command cmd, String label,
    30. String[] args) {
    31. for (Player p : Bukkit.getOnlinePlayers()) {
    32. if (p.hasPermission("Phoenix.AntiHack"))
    33. if (cmd.getName().equalsIgnoreCase("scan")) {
    34.  
    35. Location inFront = p.getLocation().toVector()
    36. .add(p.getLocation().getDirection().multiply(3))
    37. .toLocation(p.getWorld());
    38. int x = inFront.getBlockX();
    39. int y = p.getLocation().getBlockY();
    40. int z = inFront.getBlockZ();
    41.  
    42. Location finalLocation = new Location(p.getWorld(), x, y, z);
    43. EntityManager manager = RemoteEntities.createManager(this);
    44. RemoteEntity entity = manager.createEntity(RemoteEntityType.Silverfish, finalLocation, false);
    45. entity.setPushable(false);
    46. entity.getBukkitEntity().setCustomName("AntiHacker2");
    47. entity.getBukkitEntity().setCustomNameVisible(true);
    48.  
    49. }
    50. }
    51. return true;
    52. }
    53.  
    54. @EventHandler
    55. public void onKill(EntityDeathEvent e) {
    56. Player player = (Player) e.getEntity();
    57. Location inFront = player.getLocation().toVector()
    58. .add(player.getLocation().getDirection().multiply(3))
    59. .toLocation(player.getWorld());
    60. int x = inFront.getBlockX();
    61. int y = player.getLocation().getBlockY();
    62. int z = inFront.getBlockZ();
    63.  
    64. Location finalLocation = new Location(player.getWorld(), x, y, z);
    65. EntityManager manager = RemoteEntities.createManager(this);
    66. RemoteEntity entity = manager.createEntity(RemoteEntityType.Villager,
    67. finalLocation, false);
    68.  
    69. entity.setPushable(false);
    70. entity.getBukkitEntity().setCustomName("AntiHacker");
    71. entity.getBukkitEntity().setCustomNameVisible(true);
    72. }
    73.  
    74. @EventHandler
    75. public void onDamage(EntityDamageEvent e) {
    76. e.setCancelled(false);
    77. }
    78.  
    79. }
    80.  



    Now as you can see some of this stuff may be wrong xD Please tell me if so because I'm really new to NMS. But the big question is how do I make the silverfish invisible to players! Please reply and thanks for the help you've gave me so far :D
     
  4. Offline

    SantaClawz69

  5. Offline

    Anrza

    Maybe you could cancel the EntityDamageByEntity event if it's a the player getting hit by the zombie?

    And also, most cheats have different killAuras/forcefields for mobs and players, consequently, they could be having hacks enabled without hitting the zombie.
     
  6. Offline

    SantaClawz69

    Anrza Sadly you're right but I'm pretty new to NMS code and if I had to make an NPC I would have no clue where to start
     
  7. Offline

    Anrza

    SantaClawz69
    I wish I could help you on that point, but I'm afraid I can't. :/
     
  8. Offline

    SantaClawz69

    :/

    Anrza Do you know any way to notify someone if the player hit the silverfish?

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

    Anrza

    SantaClawz69 I guess you could
    Code:java
    1. @EventHandler
    2. public void onHitSilverfish(EntityDamageByEntityEvent event) {
    3. if (event.getEntity() instanceof Silverfish) {
    4. if (event.getDamager() instanceof Player) {
    5. for (Player peopleToNotify : Bukkit.getServer().getOnlinePlayers()) {
    6. if (peopleToNotify.hasPermission("antiCheatAdmin") {
    7. peopleToNotify.sendMessage(event.getDamager + " hit a silverfish")
    8. }
    9. }
    10. }
    11. }
    12. }

    Or something.
     
  10. Offline

    SantaClawz69

    If you look at my previous source code it will notify if it hit that specific silverfish I spawned correct?
     
  11. Offline

    Anrza

  12. Offline

    SantaClawz69

    Sorry for all the questions Anrza but how would I check if the player hit it more than once :confused:. This NMS stuff is kind of weird if you tell me.

    Updated code:

    Code:java
    1. package me.Santa.AntiCheat;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Entity;
    8. import org.bukkit.entity.HumanEntity;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    13. import org.bukkit.event.entity.EntityDamageEvent;
    14. import org.bukkit.event.entity.EntityDeathEvent;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. import de.kumpelblase2.remoteentities.EntityManager;
    18. import de.kumpelblase2.remoteentities.RemoteEntities;
    19. import de.kumpelblase2.remoteentities.api.RemoteEntity;
    20. import de.kumpelblase2.remoteentities.api.RemoteEntityType;
    21.  
    22. public class Main extends JavaPlugin implements Listener {
    23. public void onEnable() {
    24. for(Entity e: Bukkit.getWorld("world").getEntities()) {
    25. e.remove();
    26. onKill(null);
    27. Bukkit.getPluginManager().registerEvents(this, this);
    28.  
    29. }
    30. }
    31. public boolean onCommand(CommandSender sender, Command cmd, String label,
    32. String[] args) {
    33. for (Player p : Bukkit.getOnlinePlayers()) {
    34. if (p.hasPermission("Phoenix.AntiHack"))
    35. if (cmd.getName().equalsIgnoreCase("scan")) {
    36.  
    37. Location inFront = p.getLocation().toVector()
    38. .add(p.getLocation().getDirection().multiply(3))
    39. .toLocation(p.getWorld());
    40. int x = inFront.getBlockX();
    41. int y = p.getLocation().getBlockY();
    42. int z = inFront.getBlockZ();
    43.  
    44. EntityManager manager = RemoteEntities.createManager(this);
    45. Location finalLocation = new Location(p.getWorld(), x, y, z);
    46. RemoteEntity entity = manager.createEntity(RemoteEntityType.Human, finalLocation, false);
    47. entity.setPushable(false);
    48. entity.getBukkitEntity().setCustomName("AntiHacker2");
    49. entity.getBukkitEntity().setCustomNameVisible(true);
    50.  
    51. }
    52. }
    53. return true;
    54. }
    55.  
    56. @EventHandler
    57. public void onKill(EntityDeathEvent e) {
    58. Player player = (Player) e.getEntity();
    59. Location inFront = player.getLocation().toVector()
    60. .add(player.getLocation().getDirection().multiply(3))
    61. .toLocation(player.getWorld());
    62. int x = inFront.getBlockX();
    63. int y = player.getLocation().getBlockY();
    64. int z = inFront.getBlockZ();
    65.  
    66. Location finalLocation = new Location(player.getWorld(), x, y, z);
    67. EntityManager manager = RemoteEntities.createManager(this);
    68. RemoteEntity entity = manager.createEntity(RemoteEntityType.Human,
    69. finalLocation, false);
    70.  
    71. entity.setPushable(false);
    72. entity.getBukkitEntity().setCustomName("AntiHacker");
    73. entity.getBukkitEntity().setCustomNameVisible(true);
    74. }
    75. @EventHandler
    76. public void onDamage(EntityDamageEvent e) {
    77. e.setCancelled(false);
    78. }
    79.  
    80. @EventHandler
    81. public void onHitHuman(EntityDamageByEntityEvent evnt) {
    82. if(evnt.getEntity() instanceof HumanEntity) {
    83. if(evnt.getEntity() instanceof Player) {
    84. for(Player peopletoNotify : Bukkit.getOnlinePlayers()) {
    85. if(peopletoNotify.hasPermission("antiCheatAdmin")) {
    86. peopletoNotify.sendMessage(evnt.getDamager() + " may be hacking");
    87.  
    88. }
    89. }
    90. }
    91. }
    92. }
    93. }
    94.  
    95.  


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

    Anrza

    Make a hashmap with the player as key and the value increasing every time he hits it.
     
  14. Offline

    SantaClawz69

    Aaaah, you're correct. Thanks sooo much for all your help! I've finally got the custom plugin finished :D Thanks so much. Anrza
     
  15. Offline

    Anrza

Thread Status:
Not open for further replies.

Share This Page