Advertisement
Guest User

2012-05-01 07:33:31 [SEVERE] Error occurred while enabling G

a guest
May 1st, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.63 KB | None | 0 0
  1. package com.oneofthesevenbillion.ziah.Gravity;
  2.  
  3. import java.io.File;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.logging.Logger;
  7.  
  8. import lib.PatPeter.SQLibrary.SQLite;
  9.  
  10. import org.bukkit.Location;
  11. import org.bukkit.Material;
  12. import org.bukkit.Server;
  13. import org.bukkit.block.Block;
  14. import org.bukkit.command.Command;
  15. import org.bukkit.command.CommandSender;
  16. import org.bukkit.entity.Player;
  17. import org.bukkit.event.block.BlockBreakEvent;
  18. import org.bukkit.event.block.BlockPlaceEvent;
  19. import org.bukkit.plugin.Plugin;
  20. import org.bukkit.plugin.PluginManager;
  21. import org.bukkit.plugin.java.JavaPlugin;
  22.  
  23. import com.sk89q.worldedit.IncompleteRegionException;
  24. import com.sk89q.worldedit.LocalPlayer;
  25. import com.sk89q.worldedit.LocalWorld;
  26. import com.sk89q.worldedit.bukkit.BukkitPlayer;
  27. import com.sk89q.worldedit.bukkit.WorldEditPlugin;
  28. import com.sk89q.worldedit.regions.Region;
  29.  
  30. public class Gravity extends JavaPlugin {
  31.     Logger log;
  32.     Server server = getServer();
  33.     File pFolder = new File("plugins" + File.separator + "Gravity");
  34.     SQLite sqlite;
  35.     Plugin WorldEdit;
  36.     public void onEnable() {
  37.         PluginManager pluginManager = server.getPluginManager();
  38.         WorldEdit = pluginManager.getPlugin("WorldEdit");
  39.         log = getLogger();
  40.         log.info("Gravity has been enabled");
  41.         sqlite = new SQLite(log, "", "Gravity", pFolder.getPath());
  42.         sqlite.open();
  43.         if (!sqlite.checkTable("blocks")) {
  44.             log.info("Creating table blocks");
  45.             sqlite.createTable("CREATE TABLE blocks (x INT, y INT, z INT, world STRING)");
  46.         }
  47.         if (WorldEdit != null) {
  48.             log.info("WorldEdit found.");
  49.         }else{
  50.             log.info("WorldEdit not found you will be unable to use the enable and disable commands.");
  51.         }
  52.     }
  53.     public void onDisable() {
  54.         log.info("Gravity has been disabled");
  55.     }
  56.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
  57.         if ((cmd.getName().equalsIgnoreCase("gravity")) || (cmd.getName().equalsIgnoreCase("g"))) {
  58.             if (args[0].equalsIgnoreCase("help")) {
  59.                 sender.sendMessage("\"/gravity disable\" Disables gravity in the WorldEdit selection\n\"/gravity enable\" Enables gravity in the WorldEdit selection");
  60.             }else
  61.             if (args[0].equalsIgnoreCase("disable")) {
  62.                 if (WorldEdit == null) {
  63.                     sender.sendMessage("WorldEdit is not installed, Install it to use the enable and disable commands.");
  64.                     return true;
  65.                 }
  66.                 Region region = null;
  67.                 try {
  68.                     LocalPlayer lp = new BukkitPlayer((WorldEditPlugin) WorldEdit, ((WorldEditPlugin) WorldEdit).getServerInterface(), (Player) sender);
  69.                     LocalWorld lw = lp.getWorld();
  70.                     region = ((WorldEditPlugin) WorldEdit).getSession((Player) sender).getSelection(lw);
  71.                 } catch (IncompleteRegionException e) {
  72.                     sender.sendMessage("Complete your selection before enabling or disabling gravity in an area.");
  73.                     return true;
  74.                 }
  75.                 for (Integer x = region.getMinimumPoint().getBlockX(); x <= region.getMaximumPoint().getBlockX(); x++) {
  76.                     for (Integer y = region.getMinimumPoint().getBlockY(); y <= region.getMaximumPoint().getBlockY(); y++) {
  77.                         for (Integer z = region.getMinimumPoint().getBlockZ(); z <= region.getMaximumPoint().getBlockZ(); z++) {
  78.                             Block block = ((Player) sender).getWorld().getBlockAt(x, y, z);
  79.                             ResultSet result = sqlite.query("SELECT COUNT(*) as 'Count' FROM blocks WHERE x = '" + block.getX() + "' AND y = '" + block.getY() + "' AND z = '" + block.getZ() + "' AND world = '" + block.getWorld().getName() + "'");
  80.                             try {
  81.                                 if (result.getInt("Count") > 0) {
  82.                                     sqlite.query("DELETE FROM blocks WHERE x = '" + block.getX() + "' AND y = '" + block.getY() + "' AND z = '" + block.getZ() + "' AND world = '" + block.getWorld().getName() + "'");
  83.                                 }
  84.                             } catch (SQLException e) {
  85.                                 log.warning(e.getMessage());
  86.                             }
  87.                         }
  88.                     }
  89.                 }
  90.             }else
  91.             if (args[0].equalsIgnoreCase("enable")) {
  92.                 if (WorldEdit == null) {
  93.                     sender.sendMessage("WorldEdit is not installed, Install it to use the enable and disable commands.");
  94.                     return true;
  95.                 }
  96.                 Region region = null;
  97.                 try {
  98.                     LocalPlayer lp = new BukkitPlayer((WorldEditPlugin) WorldEdit, ((WorldEditPlugin) WorldEdit).getServerInterface(), (Player) sender);
  99.                     LocalWorld lw = lp.getWorld();
  100.                     region = ((WorldEditPlugin) WorldEdit).getSession((Player) sender).getSelection(lw);
  101.                 } catch (IncompleteRegionException e) {
  102.                     sender.sendMessage("Complete your selection before enabling or disabling gravity in an area.");
  103.                     return true;
  104.                 }
  105.                 for (Integer x = region.getMinimumPoint().getBlockX(); x <= region.getMaximumPoint().getBlockX(); x++) {
  106.                     for (Integer y = region.getMinimumPoint().getBlockY(); y <= region.getMaximumPoint().getBlockY(); y++) {
  107.                         for (Integer z = region.getMinimumPoint().getBlockZ(); z <= region.getMaximumPoint().getBlockZ(); z++) {
  108.                             Block block = ((Player) sender).getWorld().getBlockAt(x, y, z);
  109.                             ResultSet result = sqlite.query("SELECT COUNT(*) as 'Count' FROM blocks WHERE x = '" + block.getX() + "' AND y = '" + block.getY() + "' AND z = '" + block.getZ() + "' AND world = '" + block.getWorld().getName() + "'");
  110.                             try {
  111.                                 if (result.getInt("Count") < 1) {
  112.                                     sqlite.query("INSERT INTO blocks (x, y, z, world) VALUES ('" + block.getX() + "', '" + block.getY() + "', '" + block.getZ() + "', '" + block.getWorld().getName() + "'");
  113.                                 }
  114.                             } catch (SQLException e) {
  115.                                 log.warning(e.getMessage());
  116.                             }
  117.                         }
  118.                     }
  119.                 }
  120.             }
  121.             return true;
  122.         }
  123.         return false;
  124.     }
  125.     public void onBlockPlace(BlockPlaceEvent event) {
  126.         ResultSet result = sqlite.query("SELECT COUNT(*) as 'Count' FROM blocks WHERE x = '" + event.getBlock().getX() + "' AND y = '" + event.getBlock().getY() + "' AND z = '" + event.getBlock().getZ() + "' AND world = '" + event.getBlock().getWorld().getName() + "'");
  127.         try {
  128.             if (result.getInt("Count") < 1) {
  129.                 sqlite.query("INSERT INTO blocks (x, y, z, world) VALUES ('" + event.getBlock().getX() + "', '" + event.getBlock().getY() + "', '" + event.getBlock().getZ() + "', '" + event.getBlock().getWorld().getName() + "'");
  130.             }else{
  131.                 sqlite.query("DELETE FROM blocks WHERE x = '" + event.getBlock().getX() + "' AND y = '" + event.getBlock().getY() + "' AND z = '" + event.getBlock().getZ() + "' AND world = '" + event.getBlock().getWorld().getName() + "'");
  132.                 sqlite.query("INSERT INTO blocks (x, y, z, world) VALUES ('" + event.getBlock().getX() + "', '" + event.getBlock().getY() + "', '" + event.getBlock().getZ() + "', '" + event.getBlock().getWorld().getName() + "'");
  133.             }
  134.         } catch (SQLException e) {
  135.             log.warning(e.getMessage());
  136.         }
  137.         if (!isStable(event.getPlayer(), event.getBlock())) {
  138.             fall(event.getPlayer(), event.getBlock());
  139.         }
  140.     }
  141.     public void onBlockBreak(BlockBreakEvent event) {
  142.         sqlite.query("DELETE FROM blocks WHERE x = '" + event.getBlock().getX() + "' AND y = '" + event.getBlock().getY() + "' AND z = '" + event.getBlock().getZ() + "' AND world = '" + event.getBlock().getWorld().getName() + "'");
  143.         update(event.getPlayer(), event.getBlock());
  144.     }
  145.     public void update(Player player, Block block) {
  146.         if (block.getType() != Material.AIR) {
  147.             if (!isStable(player, block) && hasGravity(block)) {
  148.                 fall(player, block);
  149.             }
  150.         }
  151.         if (player.getWorld().getBlockAt(block.getX() + 1, block.getY(), block.getZ()).getType() != Material.AIR && !isStable(player, player.getWorld().getBlockAt(block.getX() + 1, block.getY(), block.getZ())) && hasGravity(player.getWorld().getBlockAt(block.getX() + 1, block.getY(), block.getZ()))) {
  152.             update(player, player.getWorld().getBlockAt(block.getX() + 1, block.getY(), block.getZ()));
  153.             fall(player, player.getWorld().getBlockAt(block.getX() + 1, block.getY(), block.getZ()));
  154.         }
  155.         if (player.getWorld().getBlockAt(block.getX() - 1, block.getY(), block.getZ()).getType() != Material.AIR && !isStable(player, player.getWorld().getBlockAt(block.getX() - 1, block.getY(), block.getZ())) && hasGravity(player.getWorld().getBlockAt(block.getX() - 1, block.getY(), block.getZ()))) {
  156.             update(player, player.getWorld().getBlockAt(block.getX() - 1, block.getY(), block.getZ()));
  157.             fall(player, player.getWorld().getBlockAt(block.getX() - 1, block.getY(), block.getZ()));
  158.         }
  159.         if (player.getWorld().getBlockAt(block.getX(), block.getY() + 1, block.getZ()).getType() != Material.AIR && !isStable(player, player.getWorld().getBlockAt(block.getX(), block.getY() + 1, block.getZ())) && hasGravity(player.getWorld().getBlockAt(block.getX(), block.getY() + 1, block.getZ()))) {
  160.             update(player, player.getWorld().getBlockAt(block.getX(), block.getY() + 1, block.getZ()));
  161.             fall(player, player.getWorld().getBlockAt(block.getX(), block.getY() + 1, block.getZ()));
  162.         }
  163.         if (player.getWorld().getBlockAt(block.getX(), block.getY() - 1, block.getZ()).getType() != Material.AIR && !isStable(player, player.getWorld().getBlockAt(block.getX(), block.getY() - 1, block.getZ())) && hasGravity(player.getWorld().getBlockAt(block.getX(), block.getY() - 1, block.getZ()))) {
  164.             update(player, player.getWorld().getBlockAt(block.getX(), block.getY() - 1, block.getZ()));
  165.             fall(player, player.getWorld().getBlockAt(block.getX(), block.getY() - 1, block.getZ()));
  166.         }
  167.         if (player.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ() + 1).getType() != Material.AIR && !isStable(player, player.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ() + 1)) && hasGravity(player.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ() + 1))) {
  168.             update(player, player.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ() + 1));
  169.             fall(player, player.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ() + 1));
  170.         }
  171.         if (player.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ() - 1).getType() != Material.AIR && !isStable(player, player.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ() - 1)) && hasGravity(player.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ() - 1))) {
  172.             update(player, player.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ() - 1));
  173.             fall(player, player.getWorld().getBlockAt(block.getX(), block.getY(), block.getZ() - 1));
  174.         }
  175.     }
  176.     public boolean isStable(Player player, Block block) {
  177.         Location location = block.getLocation();
  178.         if (hasGravity(block)) {
  179.             return true;
  180.         }
  181.         if (block.getType() == Material.AIR) {
  182.             return false;
  183.         }
  184.         if ((player.getWorld().getBlockAt(location.getBlockX() + 1, location.getBlockY(), location.getBlockZ()).getType() != Material.AIR || player.getWorld().getBlockAt(location.getBlockX() - 1, location.getBlockY(), location.getBlockZ()).getType() != Material.AIR || player.getWorld().getBlockAt(location.getBlockX(), location.getBlockY() + 1, location.getBlockZ()).getType() != Material.AIR || player.getWorld().getBlockAt(location.getBlockX(), location.getBlockY() - 1, location.getBlockZ()).getType() != Material.AIR || player.getWorld().getBlockAt(location.getBlockX(), location.getBlockY(), location.getBlockZ() + 1).getType() != Material.AIR || player.getWorld().getBlockAt(location.getBlockX(), location.getBlockY(), location.getBlockZ() - 1).getType() != Material.AIR) && isStable(player, player.getWorld().getBlockAt(location.getBlockX() + 1, location.getBlockY(), location.getBlockZ())) || isStable(player, player.getWorld().getBlockAt(location.getBlockX() - 1, location.getBlockY(), location.getBlockZ())) || isStable(player, player.getWorld().getBlockAt(location.getBlockX(), location.getBlockY() + 1, location.getBlockZ())) || isStable(player, player.getWorld().getBlockAt(location.getBlockX(), location.getBlockY() - 1, location.getBlockZ())) || isStable(player, player.getWorld().getBlockAt(location.getBlockX(), location.getBlockY(), location.getBlockZ() + 1)) || isStable(player, player.getWorld().getBlockAt(location.getBlockX(), location.getBlockY(), location.getBlockZ() - 1))) {
  185.             return true;
  186.         }else{
  187.             return false;
  188.         }
  189.     }
  190.     public void fall(Player player, Block block) {
  191.         if (block.getType() == Material.AIR) {
  192.             return;
  193.         }
  194.         while (player.getWorld().getBlockAt(block.getLocation().getBlockX(), block.getLocation().getBlockY() - 1, block.getLocation().getBlockZ()).getType() == Material.AIR) {
  195.             player.getWorld().getBlockAt(block.getLocation().getBlockX(), block.getLocation().getBlockY() - 1, block.getLocation().getBlockZ()).setType(block.getType());
  196.             player.getWorld().getBlockAt(block.getLocation().getBlockX(), block.getLocation().getBlockY() - 1, block.getLocation().getBlockZ()).setData(block.getData());
  197.             block.setType(Material.AIR);
  198.             block = player.getWorld().getBlockAt(block.getLocation().getBlockX(), block.getLocation().getBlockY() - 1, block.getLocation().getBlockZ());
  199.             wait(5);
  200.         }
  201.     }
  202.     public boolean hasGravity(Block block) {
  203.         ResultSet result = sqlite.query("SELECT COUNT(*) as 'Count' FROM blocks WHERE x = '" + block.getX() + "' AND y = '" + block.getY() + "' AND z = '" + block.getZ() + "' AND world = '" + block.getWorld().getName() + "'");
  204.         try {
  205.             if (result.getInt("Count") < 1) {
  206.                 return true;
  207.             }
  208.         } catch (SQLException e) {
  209.             log.warning(e.getMessage());
  210.         }
  211.         return false;
  212.     }
  213.     public static void wait(int n) {
  214.         long t0, t1;
  215.         t0 = System.currentTimeMillis();
  216.         do {
  217.             t1 = System.currentTimeMillis();
  218.         }
  219.         while (t1 - t0 < n);
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement