/TameGrass/src/couk/Adamki11s/TameGrass/TameGrass.java

https://github.com/Yuri4171/TameGrass · Java · 247 lines · 178 code · 49 blank · 20 comment · 57 complexity · f556790f6b5d0cd9154f9255f70ed59d MD5 · raw file

  1. package couk.Adamki11s.TameGrass;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.logging.Logger;
  5. import org.bukkit.ChatColor;
  6. import org.bukkit.Location;
  7. import org.bukkit.Server;
  8. import org.bukkit.World;
  9. import org.bukkit.block.Block;
  10. import org.bukkit.command.Command;
  11. import org.bukkit.command.CommandSender;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.Event;
  14. import org.bukkit.plugin.Plugin;
  15. import org.bukkit.plugin.PluginDescriptionFile;
  16. import org.bukkit.plugin.PluginManager;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18. import com.nijiko.permissions.PermissionHandler;
  19. import com.nijikokun.bukkit.Permissions.Permissions;
  20. public class TameGrass extends JavaPlugin {
  21. public static Logger log = Logger.getLogger("Minecraft");
  22. public static Server server;
  23. String logPrefix = "[TameGrass] ";
  24. String version;
  25. public static PermissionHandler permissionHandler;
  26. public static boolean hasPermissions = false;
  27. @Override
  28. public void onDisable() {
  29. // TODO Auto-generated method stub
  30. log.info(logPrefix + " TameGrass v" + version + " disabled!");
  31. }
  32. @Override
  33. public void onEnable() {
  34. // TODO Auto-generated method stub
  35. server = getServer();
  36. PluginManager pm = server.getPluginManager();
  37. PluginDescriptionFile pdf = this.getDescription();
  38. version = pdf.getVersion();
  39. log.info(logPrefix + " TameGrass v" + version + " enabled!");
  40. }
  41. int notifcount = 0;
  42. int tenthmark;
  43. private void tamegrass(Player player, int radius, World world, Location loc){
  44. String eta = "0";
  45. if(radius >= 100){
  46. if(radius >= 100 && radius <= 150){ eta = "3"; }
  47. if(radius >= 150 && radius <= 200){ eta = "7"; }
  48. if(radius >= 200 && radius <= 250){ eta = "12"; }
  49. if(radius >= 250 && radius <= 300){ eta = "16"; }
  50. if(radius > 300){ eta = "20+"; }
  51. player.sendMessage(ChatColor.DARK_GREEN + "[TameGrass] " + ChatColor.GREEN + "Removing grass. Estimated time < " + ChatColor.AQUA + eta + ChatColor.GREEN + " seconds.");
  52. }
  53. double x1 = Math.round(loc.getX() - radius),
  54. y1 = Math.round(loc.getY() - radius),
  55. z1 = Math.round(loc.getZ() - radius),
  56. x2 = Math.round(loc.getX() + radius),
  57. y2 = Math.round(loc.getY() + radius),
  58. z2 = Math.round(loc.getZ() + radius);
  59. double quartermark = x1 + ((x2 - x1) / 4), halfmark = x1 + ((x2 - x1) / 2), threequartermark = quartermark * 3;
  60. boolean quarternotif = false, halfnotif = false, thirdnotif = false;
  61. tenthmark = radius / 10;
  62. int blocks = 0;
  63. for(int x = 0; (x1 + x) <= x2; x++){
  64. for(int y = 0; (y1 + y) <= y2; y++){
  65. for(int z = 0; (z1 + z) <= z2; z++){
  66. //if(radius > 250){
  67. //notifications(player, (int) (x + x1), tenthmark, (int) x1);
  68. //}
  69. //if(world.getBlockTypeIdAt(new Location(world, x1 + x, y1 + y, z1 + z)) == 2 && world.getBlockTypeIdAt(new Location(world, x1 + x, (y1 + y) + 1, z1 + z)) != 78 &&
  70. // (world.getBlockTypeIdAt(new Location(world, x1 + x, (y1 + y) + 1, z1 + z)) == 0) && world.getBlockTypeIdAt(new Location(world, x1 + x, (y1 + y) + 1, z1 + z)) != 27 ||
  71. //world.getBlockTypeIdAt(new Location(world, x1 + x, (y1 + y) + 1, z1 + z)) == 28 || ){
  72. if(world.getBlockTypeIdAt(new Location(world, x1 + x, y1 + y, z1 + z)) == 31){
  73. world.getBlockAt(new Location(world, x1 + x, y1 + y, z1 + z)).setTypeId(0);
  74. blocks++;
  75. }
  76. }
  77. }
  78. }
  79. player.sendMessage(ChatColor.DARK_GREEN + "[TameGrass] " + ChatColor.GREEN + "Long grass removed from " + ChatColor.AQUA + blocks + ChatColor.GREEN + " blocks.");
  80. if(radius > 200){
  81. server.dispatchCommand((CommandSender) player, "save-all");
  82. }
  83. }
  84. private void spawngrass(Player player, int radius, World world, Location loc){
  85. String eta = "0";
  86. if(radius >= 100){
  87. if(radius >= 100 && radius <= 150){ eta = "3"; }
  88. if(radius >= 150 && radius <= 200){ eta = "7"; }
  89. if(radius >= 200 && radius <= 250){ eta = "12"; }
  90. if(radius >= 250 && radius <= 300){ eta = "16"; }
  91. if(radius > 300){ eta = "20+"; }
  92. player.sendMessage(ChatColor.DARK_GREEN + "[TameGrass] " + ChatColor.GREEN + "Spawning grass. Estimated time < " + ChatColor.AQUA + eta + ChatColor.GREEN + " seconds.");
  93. }
  94. double x1 = Math.round(loc.getX() - radius),
  95. y1 = Math.round(loc.getY() - radius),
  96. z1 = Math.round(loc.getZ() - radius),
  97. x2 = Math.round(loc.getX() + radius),
  98. y2 = Math.round(loc.getY() + radius),
  99. z2 = Math.round(loc.getZ() + radius);
  100. double quartermark = x1 + ((x2 - x1) / 4), halfmark = x1 + ((x2 - x1) / 2), threequartermark = quartermark * 3;
  101. boolean quarternotif = false, halfnotif = false, thirdnotif = false;
  102. tenthmark = radius / 10;
  103. int blocks = 0;
  104. for(int x = 0; (x1 + x) <= x2; x++){
  105. for(int y = 0; (y1 + y) <= y2; y++){
  106. for(int z = 0; (z1 + z) <= z2; z++){
  107. //if(radius > 250){
  108. //notifications(player, (int) (x + x1), tenthmark, (int) x1);
  109. //}
  110. //if(world.getBlockTypeIdAt(new Location(world, x1 + x, y1 + y, z1 + z)) == 2 && world.getBlockTypeIdAt(new Location(world, x1 + x, (y1 + y) + 1, z1 + z)) != 78 &&
  111. // (world.getBlockTypeIdAt(new Location(world, x1 + x, (y1 + y) + 1, z1 + z)) == 0) && world.getBlockTypeIdAt(new Location(world, x1 + x, (y1 + y) + 1, z1 + z)) != 27 ||
  112. //world.getBlockTypeIdAt(new Location(world, x1 + x, (y1 + y) + 1, z1 + z)) == 28 || ){
  113. if(world.getBlockTypeIdAt(new Location(world, x1 + x, y1 + y, z1 + z)) == 2 && world.getBlockTypeIdAt(new Location(world, x1 + x, (y1 + y) + 1, z1 + z)) == 0){
  114. world.getBlockAt(new Location(world, x1 + x, (y1 + y) + 1, z1 + z)).setTypeId(31);
  115. blocks++;
  116. }
  117. }
  118. }
  119. }
  120. player.sendMessage(ChatColor.DARK_GREEN + "[TameGrass] " + ChatColor.GREEN + "Long grass spawned on " + ChatColor.AQUA + blocks + ChatColor.GREEN + " blocks!");
  121. if(radius > 200){
  122. server.dispatchCommand((CommandSender) player, "save-all");
  123. }
  124. }
  125. @SuppressWarnings("static-access")
  126. private void setupPermissions() {
  127. Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
  128. PluginDescriptionFile pdfFile = getDescription();
  129. version = pdfFile.getVersion();
  130. if (this.permissionHandler == null) {
  131. if (permissionsPlugin != null) {
  132. this.permissionHandler = ((Permissions) permissionsPlugin).getHandler();
  133. hasPermissions = true;
  134. log.info(logPrefix + "v" + version +" - Permissions support enabled for MultiGod");
  135. } else {
  136. //log.info("Permission system not detected, defaulting to OP");
  137. }
  138. }
  139. }
  140. //public static Thread executeThread = new Thread();
  141. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  142. Player player = (Player) sender;
  143. if( (hasPermissions && permissionHandler.has(player, "tamer.tamegrass")) || player.isOp()){
  144. if(label.equalsIgnoreCase("tamegrass") || label.equalsIgnoreCase("tg")){
  145. if(args.length == 1){
  146. int radius;
  147. try{
  148. radius = Integer.parseInt(args[0]);
  149. notifcount = 0;
  150. if(radius < 0){
  151. player.sendMessage(ChatColor.DARK_GREEN + "[TameGrass] " + ChatColor.RED + "The radius must be larger than 0!");
  152. return true;
  153. }
  154. if(radius > 1000){
  155. player.sendMessage(ChatColor.DARK_GREEN + "[TameGrass] " + ChatColor.RED + "The radius cannot be larger than 1000!");
  156. return true;
  157. }
  158. /*executeThread = new Thread(new TamingThread(player, radius, player.getWorld()));
  159. executeThread.start();*/
  160. tamegrass(player, radius, player.getWorld(), player.getLocation());
  161. return true;
  162. } catch (NumberFormatException ex){
  163. ex.printStackTrace();
  164. }
  165. }
  166. return true;
  167. }
  168. }
  169. if( (hasPermissions && permissionHandler.has(player, "tamer.spawngrass")) || player.isOp()){
  170. if(label.equalsIgnoreCase("spawngrass") || label.equalsIgnoreCase("sg")){
  171. if(args.length == 1){
  172. int radius;
  173. try{
  174. radius = Integer.parseInt(args[0]);
  175. notifcount = 0;
  176. if(radius < 0){
  177. player.sendMessage(ChatColor.DARK_GREEN + "[TameGrass] " + ChatColor.RED + "The radius must be larger than 0!");
  178. return true;
  179. }
  180. if(radius > 1000){
  181. player.sendMessage(ChatColor.DARK_GREEN + "[TameGrass] " + ChatColor.RED + "The radius cannot be larger than 1000!");
  182. return true;
  183. }
  184. /*executeThread = new Thread(new TamingThread(player, radius, player.getWorld()));
  185. executeThread.start();*/
  186. spawngrass(player, radius, player.getWorld(), player.getLocation());
  187. return true;
  188. } catch (NumberFormatException ex){
  189. ex.printStackTrace();
  190. return true;
  191. }
  192. }
  193. return true;
  194. }
  195. }
  196. return false;
  197. }
  198. }