Solved Create new Block object

Discussion in 'Plugin Development' started by Max9403, Dec 13, 2013.

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

    Max9403

    I have blocks stored in a database (world, location, type and metadata) how do I create a new Block object from those? (I do not want to directly edit the world as I'm planning to pass a list of Blocks)
     
  2. Offline

    The_Doctor_123

    You can't construct a block by regular means.And they're not supposed to be, it likely would cause errors and such.

    You want to to use getBlockAt(x, y, z) in World or use getBlock() in Location.
     
  3. Offline

    Wingzzz

    Unless you are using world and type information to be fed to a plugin/website or something in order to display them as stats etc then I don't see any need in storing that in a database. Storing a location is all you need really...

    Code:java
    1. Block block = location.getBlock();

    Where 'location' represents of course a Location object in which you've created based off the location information from your database.

    Once you have the block you can of course edit it according to your other information.
     
  4. Offline

    Max9403

    Wingzzz they are stored in database as they are not meant to always be visible to players, but still need to be saved between server restarts

    but I will give location.getBlock() a chance to see if it does not modify the block in the world nope changes the block as well

    Like I said they are NOT for editing the world that's why I need them to read/change data from without getting the block from the world as changing a value will change it in the world. Nether are they stored in the world so methods such as getBlockAt are useless. They will be used to pass data between the plugin, the best I can see at the moment is to create a custom Block class and use that, but I assumed Bukkit had some build in Block class that I could pre modify and ever wanted to dump into the world would be as easy as player.getWorld().replaceBlock(newBlock).

    I don't see how new Block(blockId, location, etc) would create errors at all, or a method in Block that could spit out a blank block when you call it like Block.getBlockById(blockId) which would return a Block object, which is of the id Type (location being 0, 0, 0 and meta being 0)

    To give an example of what I want to achieve:
    Code:java
    1. public static List<Block> getPlayerBlocks(final String playerID, final String worldID) {
    2. List<Block> blockList = new CopyOnWriteArrayList();
    3. Database tempDB = ConfigHandler.getTheDataHub();
    4. if (!tempDB.isOpen() && !tempDB.open()) {
    5. Blueprint.error("Could not work with database");
    6. }
    7. try {
    8. ResultSet result = (tempDB.query("SELECT blockID, blockX, blockY, blockZ, blockMeta FROM blocks WHERE playerID = '" + playerID + "' AND world = '"+worldID+"';"));
    9. do {
    10. blockList.add(new Block(result.get("typeID"), result.get("blockX"), result.get("blockY"), result.get("blockZ"), (byte)result.get("blockMeta"));
    11. } while(result);
    12. } catch (SQLException ex) {
    13. tempDB.close();
    14. Blueprint.error("Couldn't deactivate player", ex);
    15. }
    16. tempDB.close();
    17. return blockList;
    18. }


    P.S. Using .getBlockAt() to modify a block in the world does not seem intuitive to me to be honest (I know you can then call .setTypeId(), etc) but being able to use something .setBlockAt(newBlock)/.setBlockAt(newBlock, blockLocation) would be more intuitive
     
  5. Offline

    Wingzzz

    Max9403
    Okay, so you want to construct blocks so that they may be on a 'standby' so that they do not directly effect the world until you decide to use one, in which case the block can override another block, correct?

    ie: Collection/Array of 'custom' blocks or block 'templates' to be used.

    (Also, I'm just sort of throwing it out there in-case it is anywhere close to what you may want as I await a reply-- Player#sendBlockChange(Location location, Material material, byte data); : fake a block change to the player via packets)
     
  6. Offline

    The_Doctor_123

    Max9403
    You're not making any sense. You want to modify/read blocks without getting them..?
     
  7. Offline

    Max9403

    Wingzzz Yes that is pretty much what I'm trying to achieve just hoping I don't have to make/use a custom class for it, (this is something else from what I'm asking but couldn't get completely right ether) I tried sending a fake sendBlockChange after canceling a BlockPlaceEvent but it did not seem to have an effect (guess it was because the cancelation was send afterward an removing the block again tryed useing protcolLib for it as well, but had some issues with block 32 and try to get a FallingBlock entity to stay inside it)

    The_Doctor_123 Like I said in the previous post I have the needed data for a block stored externally in a database, e.g. like a book would be to ones brain or notes written in class would be to ones textbook, what I want to create is a block object for passing data in my plugin from that external data, therefore I can not get the data from the world as it is not the data in the database, and modifying such would modify the world which is not my direct intent. In other words .getBlockAt(location) != database.getBlockDetails(); because one example would be getBlockAt(location).getType() could return AIR while database.getBlockDetails().getType() could return DIRT
     
  8. Offline

    The_Doctor_123

    Max9403
    You could store the temporary block data in a separate world. That's the only way I can see that you'll get the Block interface.
     
    Max9403 likes this.
  9. Offline

    Wingzzz

    Okay, so how about you create a BlockData class that will hold all the information for a block you would want.
    So you could make one with BlockData blockData = new BlockData(Block block); as well as a bunch of other constructors. It would have methods like getWorld(), getLocation(), getType() etc... You could also throw in a few methods like materialize(); or something of the sort that would then actually modify the world based on the object's data.
     
    Max9403 likes this.
  10. Offline

    Max9403

    Wingzzz I was hopping to avoid having to do that a well >.>

    The_Doctor_123 Thanks for the info but I'm gonna stick with Wingzzz suggestion on this one, though your suggestion would indeed give me the block interface that I'm looking for but would mean loading in an other world, etc which would make the requirements of my plugin go a higher then need be so I'd much rather battle out a new class file then having to spawn worlds (because someone will find a way of "accidentally" getting into that world and (though only probably hilarious to watch) isn't exactly what I'm looking for.

    Thanks for the help everybody, btw would anyone be interested in a copy of the class file that Wingzzz suggested (maybe, if I can get it coded correctly :oops: , make it part of an API)?
     
  11. Offline

    Wingzzz

    Max9403
    Naturally I'm interested, if you're going to make it feel free to message me about it as I will probably sometime go about making my own BlockWrapper. It really shouldn't take long, but would be interested in the outcome none the less for the goal you've described.
     
  12. Offline

    Max9403

    Wingzzz I'll start working on it then, just had a good morning of sleep XD 3 a.m to 1 p.m

    Here is what I have at the moment (I'm using TypeId for backward compatibility):
    Code:java
    1. package com.emberringstudios.blueprint;
    2.  
    3. import java.util.logging.Level;
    4. import java.util.logging.Logger;
    5. import org.bukkit.Material;
    6. import org.bukkit.World;
    7. import org.bukkit.block.Block;
    8.  
    9. /**
    10.  *
    11.  * @author Max9403 <[email][email protected][/email]>
    12.  */
    13. public class BlockData {
    14.  
    15. private int type;
    16. private int locX;
    17. private int locY;
    18. private int locZ;
    19. private byte data;
    20. private World blockWorld;
    21.  
    22. public BlockData(final int blockType, final int locX, final int locY, final int locZ, final byte data) {
    23. this.type = blockType;
    24. this.locX = locX;
    25. this.locY = locY;
    26. this.locZ = locZ;
    27. this.data = data;
    28. }
    29.  
    30. public BlockData(final Material blockType, final int locX, final int locY, final int locZ, final byte data) {
    31. this.type = blockType.getId();
    32. this.locX = locX;
    33. this.locY = locY;
    34. this.locZ = locZ;
    35. this.data = data;
    36. }
    37.  
    38. public BlockData(final Block blockData) {
    39. this.type = blockData.getTypeId();
    40. this.locX = blockData.getX();
    41. this.locY = blockData.getY();
    42. this.locZ = blockData.getZ();
    43. this.data = blockData.getData();
    44. }
    45.  
    46. public BlockData(final int blockType, final int locX, final int locY, final int locZ, final byte data, final World blockWorld) {
    47. this.type = blockType;
    48. this.locX = locX;
    49. this.locY = locY;
    50. this.locZ = locZ;
    51. this.data = data;
    52. this.blockWorld = blockWorld;
    53. }
    54.  
    55. public BlockData(final Material blockType, final int locX, final int locY, final int locZ, final byte data, final World blockWorld) {
    56. this.type = blockType.getId();
    57. this.locX = locX;
    58. this.locY = locY;
    59. this.locZ = locZ;
    60. this.data = data;
    61. this.blockWorld = blockWorld;
    62. }
    63.  
    64. public BlockData(final Block blockData, final World blockWorld) {
    65. this.type = blockData.getTypeId();
    66. this.locX = blockData.getX();
    67. this.locY = blockData.getY();
    68. this.locZ = blockData.getZ();
    69. this.data = blockData.getData();
    70. this.blockWorld = blockWorld;
    71. }
    72.  
    73. /**
    74.   * @return the type
    75.   */
    76. public int getType() {
    77. return type;
    78. }
    79.  
    80. /**
    81.   * @param type the type to set
    82.   */
    83. public void setType(int type) {
    84. this.type = type;
    85. }
    86.  
    87. /**
    88.   * @return the locX
    89.   */
    90. public int getX() {
    91. return locX;
    92. }
    93.  
    94. /**
    95.   * @param locX the locX to set
    96.   */
    97. public void setX(int locX) {
    98. this.locX = locX;
    99. }
    100.  
    101. /**
    102.   * @return the locY
    103.   */
    104. public int getY() {
    105. return locY;
    106. }
    107.  
    108. /**
    109.   * @param locY the locY to set
    110.   */
    111. public void setY(int locY) {
    112. this.locY = locY;
    113. }
    114.  
    115. /**
    116.   * @return the locZ
    117.   */
    118. public int getZ() {
    119. return locZ;
    120. }
    121.  
    122. /**
    123.   * @param locZ the locZ to set
    124.   */
    125. public void setZ(int locZ) {
    126. this.locZ = locZ;
    127. }
    128.  
    129. /**
    130.   * @return the data
    131.   */
    132. public byte getData() {
    133. return data;
    134. }
    135.  
    136. /**
    137.   * @param data the data to set
    138.   */
    139. public void setData(byte data) {
    140. this.data = data;
    141. }
    142.  
    143. /**
    144.   * @return the blockWorld
    145.   */
    146. public World getBlockWorld() {
    147. return blockWorld;
    148. }
    149.  
    150. /**
    151.   * @param blockWorld the blockWorld to set
    152.   */
    153. public void setBlockWorld(World blockWorld) {
    154. this.blockWorld = blockWorld;
    155. }
    156.  
    157. public void loadBlockIntoWorld(World blockWorld) {
    158. setBlockWorld(blockWorld);
    159. try {
    160. loadBlockIntoWorld();
    161. } catch (NoWorldGivenException ex) {
    162. Logger.getLogger("Minecraft").log(Level.SEVERE, "Something went horribly wrong, this method should not give this error", ex);
    163. }
    164. }
    165.  
    166. public void loadBlockIntoWorld() throws NoWorldGivenException {
    167. if (blockWorld == null) {
    168. throw new NoWorldGivenException("Developer forgot to set a world for this block");
    169. }
    170. Block modBlock = this.blockWorld.getBlockAt(locX, locY, locZ);
    171. modBlock.setTypeId(type);
    172. modBlock.setData(data);
    173. }
    174.  
    175. public boolean equalToBlock(Block tempBlock) {
    176. return tempBlock.getTypeId() == type && tempBlock.getX() == locX && tempBlock.getY() == locY && tempBlock.getZ() == locZ && tempBlock.getData() == data;
    177. }
    178. }
    179.  
     
Thread Status:
Not open for further replies.

Share This Page