Solved Banning a Player

Discussion in 'Plugin Development' started by Xp10d3, Sep 18, 2020.

  1. Offline

    Xp10d3

    For some reason, when I try and ban a player for reach (I'm making an anti-reach and soon anti-aura plugin), however ironically upon banning and kicking the player, it crashes the server. I get no errors, it just crashes the server. Completely stops it. I get so much console spam from Paper I can't really send it. Basically this is what I do:
    Code:java
    1.  
    2. if (bantwo.equalsIgnoreCase("ban")) {
    3. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been banned for reach! Amount of checks done: " + checks);
    4. Bukkit.getBanList(Type.NAME).addBan(player.getName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been banned for reach! Amount of checks done: " + checks, null, null);
    5. } else if (bantwo.equalsIgnoreCase("banip")) {
    6. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + "You have been IP banned for reach! Amount of checks done: " + checks);
    7. Bukkit.getBanList(Type.IP).addBan(player.getAddress().getHostName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been IP banned for reach! Amount of checks done: " + checks, null, null);
    8. } else if (bantwo.equalsIgnoreCase("tempban") || ban.equalsIgnoreCase("temp-ban") || ban.equalsIgnoreCase("temp ban")) {
    9. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been temp-banned for reach! Amount of checks done: " + checks + ". You have been banned for " + date + ".");
    10. Bukkit.getBanList(Type.NAME).addBan(player.getName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been temp-banned for reach! Amount of checks done: " + checks + ". You have been banned for " + datetwo + ".", datetwo, null);
    11. }
    12.  

    As you can see, I kick the player first the add them to the ban list. Obviously, that doesn't make sense, but I have also tried not kicking the player, adding the player to the ban list first, kicking them after, etc. I assume there is a better way to ban the player, but I can't find anything on the internet. Everyone says to add the player to the ban list.

    Code:
    Code:java
    1.  
    2. package endran.voide.reach;
    3.  
    4. import java.text.SimpleDateFormat;
    5. import java.util.Date;
    6. import java.util.Random;
    7.  
    8. import org.bukkit.BanList.Type;
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.Listener;
    14. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    15. import org.bukkit.util.Vector;
    16.  
    17. public class PlayerListeners implements Listener {
    18.  
    19. // Get's the core variable.
    20. private Core core;
    21.  
    22. // Constructor
    23. public PlayerListeners(Core core) {
    24. this.core = core;
    25. }
    26.  
    27. // Get's the date + time to then write into core.otherLog()
    28. Date now = new Date();
    29. SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    30. // Hypixel Hit Reg
    31. @EventHandler
    32. public void onDamage(EntityDamageByEntityEvent event) {
    33. if (core.getConfig().getString("enabled").equalsIgnoreCase("true")) {
    34. /*
    35.   if (event.getCause() != DamageCause.ENTITY_ATTACK || event.getCause() != DamageCause.CONTACT) {
    36.   return;
    37.   }
    38.   */
    39.  
    40. // Get the damager
    41. Player player = (Player) event.getDamager();
    42.  
    43. Player entity = (Player) event.getEntity();
    44.  
    45. // If the damager damages the damaged...
    46. if (((event.getDamager() instanceof Player)) && ((event.getEntity() instanceof Player))) {
    47. int upper = 5;
    48. // Random number
    49. Random random = new Random();
    50. // If num is greater than or equal to 5...
    51. int rNum = random.nextInt(5+(upper));
    52. if (rNum >= 5) {
    53. event.setCancelled(true);
    54. } else {
    55. event.setCancelled(false);
    56. }
    57.  
    58. // Test for reach
    59.  
    60. // Get the distance between the player's location and the victim's location
    61. /*
    62.   Vector from = new Vector(player.getLocation().getX(), 1, player.getLocation().getZ());
    63.   Vector to = new Vector(entity.getLocation().getX(), 1, entity.getLocation().getZ());
    64.   */
    65. Vector from = player.getLocation().toVector().setY(1);
    66. Vector to = entity.getLocation().toVector().setY(1);
    67.  
    68. //Vector vector = to.subtract(from);
    69.  
    70. double distance = from.distance(to) - 0.4;
    71.  
    72. /*
    73.   if (distance >= 3.5) {
    74.   event.setCancelled(true);
    75.   }
    76.   */
    77. if (distance > 3) { // Since MC's default reach is 3, anything above that is considered reach
    78. int checks = core.getConfig().getInt(player.getUniqueId().toString()); // Get the amount of checks a player has in the config file
    79. int stageOneChecks = core.getConfig().getInt("stage-one-checks"); // The amount of checks done in stage one
    80. int stageTwoChecks = core.getConfig().getInt("stage-two-checks"); // The amount of checks done in stage two
    81. int stageThreeChecks = core.getConfig().getInt("stage-three-checks"); // The amount of checks done in stage three
    82. int stageTwoC = stageOneChecks + stageTwoChecks;
    83. int stageThree = stageOneChecks + stageTwoChecks + stageThreeChecks; // Same with stageTwo var
    84. String ban = core.getConfig().getString("ban-stage-one"); // What type of ban to do in stage one
    85. String bantwo = core.getConfig().getString("ban-stage-two"); // Same with ban var
    86. String banthree = core.getConfig().getString("ban-stage-three"); // Same with ban var
    87. //String bumper = StringUtils.repeat("\n", 35); // To create custom ban screen
    88. int min = core.getConfig().getInt("minutes-stage-one"); // Amount of minutes to temp-ban a player
    89. int mintwo = core.getConfig().getInt("minutes-stage-two"); // Same with min var
    90. int minthree = core.getConfig().getInt("minutes-stage-three"); // Same with min var
    91. Date date = new Date(System.currentTimeMillis()+min*60*1000); // Calculate the amount of time to ban a player for
    92. Date datetwo = new Date(System.currentTimeMillis()+mintwo*60*1000); // Same with date var
    93. Date datethree = new Date(System.currentTimeMillis()+minthree*60*1000); // Same with date var
    94. /*
    95.   core.getConfig().set(player.getUniqueId().toString(), checks + 1);
    96.   player.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You seem to have reach. Amount of checks so far: " + checks);
    97.   */
    98. if (!core.getConfig().contains(player.getUniqueId().toString())) { // If the player isn't in the config file...
    99. core.getConfig().set(player.getUniqueId().toString(), 1); // Add the player to the config file, then set the amount of checks to 1.
    100. core.saveConfig(); // Save the config file
    101. } else if (checks == stageOneChecks) { // If the checks are not equal to null and the amount of checks equals stage one checks...
    102. // Send a message to the victim
    103. entity.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.GREEN + " " + player + " had reach and was banned. Player is at stage one.");
    104. // Check on what type of ban to execute
    105. if (ban.equalsIgnoreCase("ban")) { // If the stage-one ban is equal to ban...
    106. // Ban the player and send a custom screen message. Type.NAME is a regular ban, Type.IP is an IP ban.
    107. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been banned for reach! Amount of checks done: " + checks);
    108. Bukkit.getBanList(Type.NAME).addBan(player.getName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been banned for reach! Amount of checks done: " + checks, null, null);
    109. } else if (ban.equalsIgnoreCase("banip")) { // Same as above
    110. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + "You have been IP banned for reach! Amount of checks done: " + checks);
    111. Bukkit.getBanList(Type.IP).addBan(player.getAddress().getHostName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been IP banned for reach! Amount of checks done: " + checks, null, null);
    112. } else if (ban.equalsIgnoreCase("tempban") || ban.equalsIgnoreCase("temp-ban") || ban.equalsIgnoreCase("temp ban")) {
    113. // To temp ban a player, execute a regular ban but for a certain amount of time (specified by the date var)
    114. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been temp-banned for reach! Amount of checks done: " + checks + ". You have been banned for " + date + ".");
    115. Bukkit.getBanList(Type.NAME).addBan(player.getName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been temp-banned for reach! Amount of checks done: " + checks + ". You have been banned for " + date + ".", date, null);
    116. }
    117. } else if (checks == stageTwoC) {
    118. // Same as above except fill in all the stage-two values.
    119. entity.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.GREEN + " " + player + " had reach and was banned.");
    120. if (bantwo.equalsIgnoreCase("ban")) {
    121. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been banned for reach! Amount of checks done: " + checks);
    122. Bukkit.getBanList(Type.NAME).addBan(player.getName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been banned for reach! Amount of checks done: " + checks, null, null);
    123. } else if (bantwo.equalsIgnoreCase("banip")) {
    124. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + "You have been IP banned for reach! Amount of checks done: " + checks);
    125. Bukkit.getBanList(Type.IP).addBan(player.getAddress().getHostName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been IP banned for reach! Amount of checks done: " + checks, null, null);
    126. } else if (bantwo.equalsIgnoreCase("tempban") || ban.equalsIgnoreCase("temp-ban") || ban.equalsIgnoreCase("temp ban")) {
    127. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been temp-banned for reach! Amount of checks done: " + checks + ". You have been banned for " + date + ".");
    128. Bukkit.getBanList(Type.NAME).addBan(player.getName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been temp-banned for reach! Amount of checks done: " + checks + ". You have been banned for " + datetwo + ".", datetwo, null);
    129. }
    130. } else if (checks == stageThree) {
    131. // Same as above except fill in all the stage-three values.
    132. entity.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.GREEN + " " + player + " had reach and was banned.");
    133. if (banthree.equalsIgnoreCase("ban")) {
    134. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been banned for reach! Amount of checks done: " + checks);
    135. Bukkit.getBanList(Type.NAME).addBan(player.getName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been banned for reach! Amount of checks done: " + checks, null, null);
    136. } else if (banthree.equalsIgnoreCase("banip")) {
    137. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + "You have been IP banned for reach! Amount of checks done: " + checks);
    138. Bukkit.getBanList(Type.IP).addBan(player.getAddress().getHostName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been IP banned for reach! Amount of checks done: " + checks, null, null);
    139. } else if (banthree.equalsIgnoreCase("tempban") || ban.equalsIgnoreCase("temp-ban") || ban.equalsIgnoreCase("temp ban")) {
    140. player.kickPlayer(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been temp-banned for reach! Amount of checks done: " + checks + ". You have been banned for " + date + ".");
    141. Bukkit.getBanList(Type.NAME).addBan(player.getName(), ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You have been temp-banned for reach! Amount of checks done: " + checks + ". You have been banned for " + datethree + ".", datethree, null);
    142. }
    143. } else {
    144. // If the player is in the config file but doesn't have the same amount of checks as required, add one check to the player.
    145. core.getConfig().set(player.getUniqueId().toString(), checks + 1);
    146. player.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Voide Reach" + ChatColor.WHITE + "]" + ChatColor.RED + " You seem to have reach. Amount of checks so far: " + checks);
    147. }
    148. }
    149. }
    150. }
    151. }
    152. }
    153.  
     
  2. Offline

    Strahan

    There is nothing in your code that should cause that. There are plenty of issues, but nothing so drastic as to cause the whole server to go down in flames as you describe. What are your config values, just out of curiosity?
     
  3. Offline

    Xp10d3

    That might be it:
    Code:yml
    1.  
    2. enabled: true
    3. stage-one-checks: 3
    4. stage-two-checks: 3
    5. stage-three-checks: 3
    6. ban-stage-one: temp-ban
    7. minutes-stage-one: 1
    8. ban-stage-two: temp-ban
    9. minutes-stage-two: 1
    10. ban-stage-three: ban
    11. minutes-stage-three: 1
    12.  

    And yes, I made the whole "stage" thing within like 30 minutes. Rushed it, didn't take the time to think through it :/

    EDIT: alright, I recoded the whole entire class. Made the banning a config thing. Marking as solved. Sorry for wasting everyone's time.
     
    Last edited: Sep 19, 2020

Share This Page