Solved [Quick Question] Random tick system for custom blocks

Discussion in 'Plugin Development' started by Dudemister1999, Oct 22, 2014.

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

    Dudemister1999

    Hello! I've got another quick question. I've got a custom block API, and I need to implement a ticking system similar to Minecraft's onUpdate() method for blocks. I've got the base of the API done, and I'll show you what I have now:

    Show Spoiler
    Code:java
    1. import java.util.ArrayList;
    2. import java.util.Arrays;
    3. import java.util.List;
    4. import me.dudemister.ci.CustomItemsPlugin;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.inventory.meta.ItemMeta;
    11. import org.bukkit.metadata.FixedMetadataValue;
    12.  
    13. public abstract class ModBlock
    14. {
    15. private final String id;
    16. private Location location;
    17. private Material material;
    18. private byte data;
    19. private String name;
    20. private List<String> lore = new ArrayList<>();
    21.  
    22. public ModBlock(String id, Material type)
    23. {
    24. this.id = id;
    25. this.material = type;
    26. }
    27.  
    28. public final ModBlock setLocation(Location location)
    29. {
    30. this.location = location;
    31. return this;
    32. }
    33.  
    34. public final ModBlock setType(Material type)
    35. {
    36. this.material = type;
    37. return this;
    38. }
    39.  
    40. public final ModBlock setData(int data)
    41. {
    42. this.data = (byte)data;
    43. return this;
    44. }
    45.  
    46. public final ModBlock setName(String name)
    47. {
    48. this.name = name;
    49. return this;
    50. }
    51.  
    52. public final ModBlock setLore(String... l)
    53. {
    54. lore = Arrays.asList(l);
    55. return this;
    56. }
    57.  
    58. public final ModBlock addLore(String... l)
    59. {
    60. lore.addAll(Arrays.asList(l));
    61. return this;
    62. }
    63.  
    64. public Material getType()
    65. {
    66. return this.material;
    67. }
    68.  
    69. public byte getData()
    70. {
    71. return this.data;
    72. }
    73.  
    74. public String getName()
    75. {
    76. return this.name;
    77. }
    78.  
    79. public List<String> getLore()
    80. {
    81. return this.lore;
    82. }
    83.  
    84. public Location getLocation()
    85. {
    86. return this.location;
    87. }
    88.  
    89. public boolean onInteract(Player player)
    90. {
    91. return true;
    92. }
    93.  
    94. public boolean onHit(Player player)
    95. {
    96. return true;
    97. }
    98.  
    99. public boolean onPlace(Player player)
    100. {
    101. return true;
    102. }
    103.  
    104. public boolean onBreak(Player player)
    105. {
    106. return true;
    107. }
    108.  
    109. public boolean onStep(Player player)
    110. {
    111. return true;
    112. }
    113.  
    114. public void update() {}
    115.  
    116. public final String getID()
    117. {
    118. return this.id;
    119. }
    120.  
    121. public final Block place()
    122. {
    123. Block block = getLocation().getBlock();
    124. block.setType(material);
    125. block.setData(data);
    126. block.setMetadata("name", new FixedMetadataValue(CustomItemsPlugin.instance, name));
    127. for(String lore : this.lore)
    128. {
    129. block.setMetadata(lore, new FixedMetadataValue(CustomItemsPlugin.instance, lore));
    130. }
    131. block.getState().update();
    132. return block;
    133. }
    134.  
    135. public final ItemStack getItem()
    136. {
    137. ItemStack item = new ItemStack(getType(), 1, getData());
    138. ItemMeta itemMeta = item.getItemMeta();
    139.  
    140. itemMeta.setDisplayName(getName());
    141. itemMeta.setLore(getLore());
    142.  
    143. item.setItemMeta(itemMeta);
    144.  
    145. return item;
    146. }
    147. }


    Note it may be a little... thrown together. I've got that update() method, but I need to populate it with a random ticking system. Any ideas?

    Thanks,
    -Hydroxocobalamin (OCHbl)
     
  2. Offline

    fireblast709

    Dudemister1999 a timer task that runs every tick, which invokes update() on all the blocks?
     
  3. Offline

    Dudemister1999

    fireblast709 But that'd be synchronised, and regular. I mean (If you've ever made a Minecraft mod), how Minecraft chooses a few random blocks in a chunk and updates them. That's how fire spreads, plants grow, etc. I need something similar, but since this does not override blocks, just is a "wrapper", I need to implement it from scratch.

    Alright, after looking at my code in wonder and boredom, I had a (sort of) solution. Let me know if there's a better way:

    Step 1: In ModBlock Constructor (Because you HAVE to call super()), create a Random.
    Step 2: On the function place(), which might not be in that version, create an Async Timer (2 seconds) with Bukkit.
    Step 3: When that timer goes off, use the random to have a 1 in three chance to update.
    Step 4: In that one in three, call the update() method.
    Step 6: I don't know how to count.
    Step 5: Profit.

    Resolved this thread as well.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page