Solved [MySQL] Client Crash When Right-Clicking Entity

Discussion in 'Plugin Development' started by krazytraynz, Jun 5, 2014.

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

    krazytraynz

    I'm new to MySQL, so there could easily be a stupid mistake in here. I've been working at fixing this for 2 days, and the only progress I've made is getting less errors, but when I right click a chicken (only entity I'm testing for now) it crashes the client of everyone who's on the server. I'll post all the classes I think are relevant to this, but if I leave anything out let me know (and please ignore the terrible error handling).

    Error: http://pastebin.com/kHBQimXG

    Main:
    Code:java
    1. package com.gmail.oaplugins;
    2.  
    3. import java.lang.reflect.InvocationTargetException;
    4. import java.sql.Connection;
    5. import java.sql.PreparedStatement;
    6. import java.sql.SQLException;
    7. import java.sql.Statement;
    8. import java.util.UUID;
    9.  
    10. import net.minecraft.server.v1_7_R3.EntityInsentient;
    11.  
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.Location;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class PetBukkit extends JavaPlugin {
    17.  
    18. static PetBukkit pb;
    19.  
    20. private FoodRecipes fr;
    21. private Tamer tamer;
    22. MySQLUtils utils;
    23.  
    24. boolean a = true;
    25. String b = "";
    26. String t = "";
    27.  
    28. MySQL sql = null;
    29. Connection c = null;
    30. Statement s = null;
    31.  
    32. public void onEnable(){
    33. pb = this;
    34. tamer = new Tamer();
    35. utils = new MySQLUtils();
    36. fr = new FoodRecipes();
    37.  
    38. fr.chickenFood();
    39. fr.cowFood();
    40. fr.ocelotFood();
    41. fr.pigFood();
    42. fr.sheepFood();
    43.  
    44. CustomEntityType.patch();
    45.  
    46. ConfigurationLoader.load(this);
    47.  
    48. getLogger().info("Initializing MySQL...");
    49. String host = getConfig().getString("mysql.hostname");
    50. String port = getConfig().getString("mysql.port");
    51. String database = getConfig().getString("mysql.database");
    52. String user = getConfig().getString("mysql.username");
    53. String pass = getConfig().getString("mysql.password");
    54. t = getConfig().getString("mysql.tablename");
    55. sql = new MySQL(this, host, port, database, user, pass);
    56. c = sql.openConnection();
    57. String sqls = "CREATE TABLE IF NOT EXISTS " + t + " (pUUID VARCHAR(255) not NULL, eUUID VARCHAR(255) not NULL, PRIMARY KEY (pUUID))";
    58. try {
    59. s = sql.openConnection().createStatement();
    60. s.execute(sqls);
    61. ps = c.prepareStatement("INSERT INTO " + "'" + t + "'" + "('pUUID', 'eUUID') VALUES (?, ?);");
    62. } catch (SQLException e) {
    63. b = e.getMessage();
    64. a = false;
    65. }
    66. if(!a){
    67. getLogger().severe("ERROR INITIALIZING MYSQL: " + b + ". MAKE SURE CONFIG.YML IS PROPERLY CONFIGURED!");
    68. }else{
    69. getLogger().info("MySQL initialized!");
    70. }
    71.  
    72. Bukkit.getServer().getPluginManager().registerEvents(tamer, this);
    73.  
    74. getLogger().info("PetBukkit has been enabled!");
    75. }
    76.  
    77. public void onDisable(){
    78. getLogger().warning("PetBukkit has been disabled!");
    79. }
    80.  
    81. /**
    82.   * Spawn custom entities, 1 at a time and get their UUID
    83.   * @param entity - Custom entity to spawn
    84.   * @param loc - Location to spawn it at
    85.   * @param world - World to spawn it in
    86.   * @throws InstantiationException
    87.   * @throws IllegalAccessException
    88.   * @throws SecurityException
    89.   * @throws NoSuchMethodException
    90.   * @throws InvocationTargetException
    91.   * @throws IllegalArgumentException
    92.   */
    93. public UUID spawnCustomEntity(CustomEntityType entity, Location loc, net.minecraft.server.v1_7_R3.World world) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
    94. EntityInsentient e = (EntityInsentient)(entity.getCustomClass().getDeclaredConstructor(net.minecraft.server.v1_7_R3.World.class).newInstance(world));
    95. e.setPosition(loc.getX(), loc.getY(), loc.getZ());
    96.  
    97. world.addEntity(e);
    98.  
    99. return e.getUniqueID();
    100. }
    101. }


    Tamer.java:
    Code:java
    1. package com.gmail.oaplugins;
    2.  
    3. import java.lang.reflect.InvocationTargetException;
    4. import java.sql.SQLException;
    5. import java.util.UUID;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.Location;
    9. import org.bukkit.World;
    10. import org.bukkit.craftbukkit.v1_7_R3.CraftWorld;
    11. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftChicken;
    12. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftCow;
    13. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftOcelot;
    14. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftPig;
    15. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftSheep;
    16. import org.bukkit.entity.Entity;
    17. import org.bukkit.entity.EntityType;
    18. import org.bukkit.entity.Player;
    19. import org.bukkit.event.EventHandler;
    20. import org.bukkit.event.Listener;
    21. import org.bukkit.event.player.PlayerInteractEntityEvent;
    22. import org.bukkit.inventory.ItemStack;
    23.  
    24. public class Tamer implements Listener{
    25.  
    26. @EventHandler
    27. Player p = e.getPlayer();
    28. if(e.getRightClicked() != null){
    29. if(p.getItemInHand() != null && p.getItemInHand().hasItemMeta()){
    30. ItemStack its = p.getItemInHand();
    31. String s = "";
    32. String name = its.getItemMeta().getDisplayName();
    33. if(name != null){
    34. s = name;
    35. }else{
    36. return;
    37. }
    38.  
    39. switch(s){
    40. case "Chicken Food":
    41. if(e.getRightClicked().getType() == EntityType.CHICKEN){
    42. World world = e.getRightClicked().getWorld();
    43. Location l = e.getRightClicked().getLocation();
    44. CraftWorld w = (CraftWorld)l.getWorld();
    45. e.getRightClicked().setTicksLived(Integer.MAX_VALUE);
    46. UUID uuid = PetBukkit.pb.spawnCustomEntity(CustomEntityType.CHICKEN, l, w.getHandle());
    47. Entity ent = getFromUUID(uuid, world);
    48. ((CraftChicken)ent).getHandle().setHealth(20);
    49. System.out.println(((CraftChicken)ent).getHandle().getHealth());
    50. Bukkit.getServer().getPluginManager().callEvent(new PlayerTameEntityEvent(p, ent));
    51. }
    52. break; //Line 53
    53. case "Seeds":
    54. if(e.getRightClicked() instanceof CustomEntityChicken){
    55. ((CraftChicken)e.getRightClicked()).getHandle().heal(5);
    56. }
    57. break;
    58.  
    59. case "Cow Food":
    60. if(e.getRightClicked().getType() == EntityType.COW){
    61. Location l = e.getRightClicked().getLocation();
    62. CraftWorld w = (CraftWorld)l.getWorld();
    63. e.getRightClicked().setTicksLived(Integer.MAX_VALUE);
    64. UUID uuid = PetBukkit.pb.spawnCustomEntity(CustomEntityType.COW, l, w.getHandle());
    65. Entity ent = getFromUUID(uuid, e.getRightClicked().getWorld());
    66. ((CraftCow)ent).getHandle().setHealth(20);
    67. System.out.println(((CraftCow)ent).getHandle().getHealth());
    68. Bukkit.getServer().getPluginManager().callEvent(new PlayerTameEntityEvent(p, ent));
    69. }
    70. break;
    71. case "Milk":
    72. if(e.getRightClicked() instanceof CustomEntityCow){
    73. ((CraftCow)e.getRightClicked()).getHandle().heal(5);
    74. }
    75. break;
    76.  
    77. case "Ocelot Food":
    78. if(e.getRightClicked().getType() == EntityType.OCELOT){
    79. Location l = e.getRightClicked().getLocation();
    80. CraftWorld w = (CraftWorld)l.getWorld();
    81. e.getRightClicked().setTicksLived(Integer.MAX_VALUE);
    82. UUID uuid = PetBukkit.pb.spawnCustomEntity(CustomEntityType.OCELOT, l, w.getHandle());
    83. Entity ent = getFromUUID(uuid, e.getRightClicked().getWorld());
    84. ((CraftOcelot)ent).getHandle().setHealth(20);
    85. System.out.println(((CraftOcelot)ent).getHandle().getHealth());
    86. Bukkit.getServer().getPluginManager().callEvent(new PlayerTameEntityEvent(p, ent));
    87. }
    88. break;
    89. case "Cooked Fish":
    90. if(e.getRightClicked() instanceof CustomEntityOcelot){
    91. ((CraftOcelot)e.getRightClicked()).getHandle().heal(5);
    92. System.out.println(((CraftOcelot)e.getRightClicked()).getHandle().getHealth());
    93. }
    94. break;
    95. //
    96. case "Pig Food":
    97. if(e.getRightClicked().getType() == EntityType.PIG){
    98. Location l = e.getRightClicked().getLocation();
    99. CraftWorld w = (CraftWorld)l.getWorld();
    100. e.getRightClicked().setTicksLived(Integer.MAX_VALUE);
    101. UUID uuid = PetBukkit.pb.spawnCustomEntity(CustomEntityType.PIG, l, w.getHandle());
    102. Entity ent = getFromUUID(uuid, e.getRightClicked().getWorld());
    103. Bukkit.getServer().getPluginManager().callEvent(new PlayerTameEntityEvent(p, ent));
    104. }
    105. break;
    106. case "Carrot":
    107. if(e.getRightClicked() instanceof CustomEntityPig){
    108. ((CraftPig)e.getRightClicked()).getHandle().heal(5);
    109. }
    110. break;
    111.  
    112. case "Sheep Food":
    113. if(e.getRightClicked().getType() == EntityType.SHEEP){
    114. Location l = e.getRightClicked().getLocation();
    115. CraftWorld w = (CraftWorld)l.getWorld();
    116. e.getRightClicked().setTicksLived(Integer.MAX_VALUE);
    117. UUID uuid = PetBukkit.pb.spawnCustomEntity(CustomEntityType.SHEEP, l, w.getHandle());
    118. Entity ent = getFromUUID(uuid, e.getRightClicked().getWorld());
    119. Bukkit.getServer().getPluginManager().callEvent(new PlayerTameEntityEvent(p, ent));
    120. }
    121. break;
    122. case "Cocoa Beans":
    123. if(e.getRightClicked() instanceof CustomEntitySheep){
    124. ((CraftSheep)e.getRightClicked()).getHandle().heal(5);
    125. }
    126. break;
    127. }
    128. return;
    129. }
    130. if(CustomEntityType.isCustomClass(e.getRightClicked().getType())){
    131. if(MySQLUtils.isProperPlayer(e.getRightClicked().getUniqueId(), e.getPlayer().getUniqueId())){
    132. e.getRightClicked().setPassenger(e.getPlayer());
    133. }
    134. }
    135. }
    136. }
    137.  
    138. @EventHandler
    139. public void setValues(PlayerTameEntityEvent e){
    140. try {
    141. PetBukkit.pb.ps.setString(1, e.getPlayer().getUniqueId().toString());
    142. PetBukkit.pb.ps.setString(2, e.getTamed().getUniqueId().toString());
    143. PetBukkit.pb.ps.executeUpdate();
    144. } catch (SQLException e1) {
    145. e1.printStackTrace(); //Line 146
    146. }
    147. }
    148.  
    149. public Entity getFromUUID(UUID uuid, World w){
    150. Entity e = null;
    151. for(Entity ent : w.getEntities()){
    152. if(ent.getUniqueId().equals(uuid)){
    153. e = ent;
    154. }
    155. }
    156. return e;
    157. }
    158. }


    MySQLUtils.java:
    Code:java
    1. package com.gmail.oaplugins;
    2.  
    3. import java.sql.ResultSet;
    4. import java.sql.SQLException;
    5. import java.util.UUID;
    6.  
    7. public class MySQLUtils {
    8.  
    9. /**
    10.   * Checks if UUID of entity belongs to the UUID of the player
    11.   * @param eUUID - UUID of the entity to try and match player with
    12.   * @param pUUId - UUID of the player to try and match entity with
    13.   * @return Whether or not it matches
    14.   */
    15. public static boolean isProperPlayer(UUID eUUID, UUID pUUID){
    16. try {
    17. String db = PetBukkit.pb.getConfig().getString("mysql.database");
    18. String t = PetBukkit.pb.t;
    19. ResultSet rs = PetBukkit.pb.s.executeQuery("SELECT * FROM " + db + "WHERE" + t + " = '" + pUUID.toString() + "';");
    20. rs.next();
    21.  
    22. if(rs.getString("pUUID") == null){
    23. return false;
    24. }else{
    25. String eString = rs.getString("eUUID");
    26. if(eUUID.toString().equals(eString)){
    27. return true;
    28. }else{
    29. return false;
    30. }
    31. }
    32. } catch (SQLException e) {
    33. e.printStackTrace();
    34. }
    35. return false;
    36. }
    37. }


    Bump

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

    WhatAaCow

    krazytraynz in line 62
    Code:java
    1. ps = c.prepareStatement("INSERT INTO " + "'" + t + "'" + "('pUUID', 'eUUID') VALUES (?, ?);");
    there you use " ' " instead of " ´ " for table names....so try
    Code:java
    1. ps = c.prepareStatement("INSERT INTO " + "´" + t + "´" + "('pUUID', 'eUUID') VALUES (?, ?);");
     
  3. Offline

    fireblast709

    krazytraynz you should be using backticks `` instead of apostrophes ''
     
  4. Offline

    krazytraynz

  5. Offline

    fireblast709

    krazytraynz It's part of the general SQL syntax, thus it counts for every kind of SQL query you create.
     
Thread Status:
Not open for further replies.

Share This Page