Racetrack? *Cringe*

Discussion in 'Plugin Development' started by TheHandfish, Apr 14, 2014.

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

    TheHandfish

    I'm a little reluctant to ask this, but I'm wondering how I'd accomplish the following things:

    1. Placing (1st, 2nd, 3rd, 4th, 5th, 6th, 7th, and 8th).

    2. One-way blocks (Blocks that only allow you to go SOUTH.)

    If I think of more ideas I'll post them here.
     
  2. Offline

    TryB4

    TheHandfish
    1. You can see who has passed certain locations.
    2. You can add metadata to blocks, and see if they have it. Then, if it has it and then see what direction the player is looking.




    (may not be efficient, or work anyways. It's too late for me to be on here)
     
  3. Offline

    TheHandfish

    TryB4:

    Okay! I'm actually making a lot of progress.

    My race track method: I'm placing different golden blocks with different item datas. They mark the track. The item data increases steadily, so I'll be running along, and while doing so, an event checks to see when I pass one golden block with a value of, say, 2, then I'll go further and pass one with "3". However, the problem I now face is my blocks aren't retaining their item data when placed! Which means if I place a gold block with an item data of 2, it automatically defaults to zero. So I put this code in to set the item data when the block is placed:

    Code:javascript
    1.  
    2. public static void onPlace(BlockPlaceEvent event)
    3. {
    4. Block b = event.getBlock();
    5. if(b.getType() == Material.GOLD_BLOCK)
    6. {
    7. int arg = Integer.parseInt(event.getPlayer().getItemInHand().getData());
    8. b.setData((byte) arg);
    9. }
    10. }
    11.  


    I'm getting an error at "b.setData((byte) arg);". Am I writing this wrong?

    Thanks.
     
  4. Offline

    TryB4

    TheHandfish
    b.setMetadata("trackPlace", new FixedMetadataValue(this, <number>));

    Replace 'this' with your main class
     
  5. Offline

    TheHandfish

    I put that with my onPlace method or...?

    Because I tried it, putting in a sendMessage so when I walk past blocks it tells me its data.

    p.sendMessage("§4Detected golden block! §a" + b.getMetadata("trackPlace"));

    But I just get a bunch of confusing phrases, no one number.

    TryB4:

    To give you an idea of what I'm talking about when I say "confusing phrases":

    http://gyazo.com/0cec682bb8495c834332b7923c1789c9

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

    xTigerRebornx

    TheHandfish getMetadata() returns a List<MetadataValue>, not a single object, so when you send the List to the player, it gives them that "confusing phrases"
     
  7. Offline

    TheHandfish

    xTigerRebornx:

    Ah, thanks.

    So maybe I should rephrase my question, LOL

    How do I assign a number to a gold block, then when I place it, make it so it retains the number and how do I check that number?
     
  8. Offline

    TryB4

    TheHandfish
    <block>.getMetadata("<taghere>").asInt();
     
    TheHandfish likes this.
  9. Offline

    TheHandfish

    TryB4:

    I'm getting an error at "asInt()".


    Code:
    b.getMetadata("trackPlace").asInt();
    Bump.

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

    TryB4

    TheHandfish
    b.getMetadata("trackPlace").get(0).asInt();
     
  11. Offline

    TheHandfish

    TryB4:

    Now I'm getting errors when the onMove event is called. :-/

     
  12. Offline

    MCForger

    TheHandfish
    Whats your player move event look like?
     
  13. Offline

    TryB4

    TheHandfish
    Be sure to check everything, also. Metadata doesn't stay through restarts, so you need to set that everytime.

    Code:java
    1. if (block.hasMetadata("trackPlace")){
    2. if (block.getMetadata("trackPlace") != null){
    3. if (block.getMetadata("trackPlace").size()>=1){
    4. if (block.getMetadata("trackPlace").get(0) != null) {
    5.  
    6. }
    7. }
    8.  
    9. }
    10.  
    11.  
    12. }
     
  14. Offline

    TheHandfish

    MCForger:

    Here's the full thing.

    It only gets important at line 73.

    Code:javascript
    1. /*
    2. * To change this license header, choose License Headers in Project Properties.
    3. * To change this template file, choose Tools | Templates
    4. * and open the template in the editor.
    5. */
    6.  
    7. package com.hand.Race;
    8.  
    9. import static com.hand.Race.ItemBox.giveReward;
    10. import java.util.HashMap;
    11. import java.util.List;
    12. import org.bukkit.Location;
    13. import org.bukkit.Material;
    14. import org.bukkit.block.Block;
    15. import org.bukkit.entity.EnderCrystal;
    16. import org.bukkit.entity.Entity;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.EventHandler;
    19. import org.bukkit.event.Listener;
    20. import org.bukkit.event.player.PlayerMoveEvent;
    21. import org.bukkit.scheduler.BukkitRunnable;
    22.  
    23. /**
    24. *
    25. * @author Robert
    26. */
    27. public class Move implements Listener
    28. {
    29. public static Main plugin;
    30. public Move(Main ins)
    31. {
    32. Move.plugin = ins;
    33. }
    34.  
    35. @EventHandler
    36. public static void onMove(PlayerMoveEvent event)
    37. {
    38. Player p = event.getPlayer();
    39. List<Entity> nearby = p.getNearbyEntities(0.25,0.25,0.5);
    40. for(Entity cry : nearby)
    41. {
    42. if(cry instanceof EnderCrystal)
    43. {
    44. HashMap<String, Integer> cooldowns = new HashMap<String, Integer>();
    45.  
    46. new BukkitRunnable()
    47. {
    48. public void run()
    49. {
    50. for(String s : cooldowns.keySet())
    51. {
    52. cooldowns.put(s, cooldowns.get(s) - 1);
    53. }
    54. }
    55. }.runTaskTimer(plugin, 1L /* The amount of time until the timer starts */, 20L /* The delay of each call */);
    56.  
    57. // When a player uses that command use:
    58.  
    59. if(!cooldowns.containsValue(p.getName()) || cooldowns.get(p.getName()) == 0)
    60. {
    61. giveReward(p, cry, cry.getLocation().getBlockX(), cry.getLocation().getBlockY(), cry.getLocation().getBlockZ());
    62. cooldowns.put(p.getName(), 5); // The 60 is seconds until they can use it again
    63. }
    64.  
    65. }
    66. }
    67.  
    68.  
    69.  
    70.  
    71.  
    72.  
    73. Location playerLoc = p.getLocation();
    74. double pX = playerLoc.getX();
    75. double pY = playerLoc.getY();
    76. double pZ = playerLoc.getZ();
    77.  
    78.  
    79. for (int x = -3; x <= 3; x ++)
    80. {
    81. for (int y = -3; y <= 3; y ++)
    82. {
    83. for (int z = -3; z <= 3; z ++)
    84. {
    85. Block b = p.getWorld().getBlockAt((int)pX+x, (int)pY+y, (int)pZ+z);
    86. if(b.getType() == Material.GOLD_BLOCK)
    87. {
    88. p.sendMessage("§4Detected golden block! §a" + b.getMetadata("trackPlace").get(0).asInt());
    89. }
    90. }
    91. }
    92. }
    93. }
    94. }
    95.  
     
  15. Offline

    MCForger

    TheHandfish
    Alright for debugging purposes simply before you do the b.getMetadata check to see if there is metadata there and if not send a message to your self. Then your problem might be the setting of metadata not the listening part.
     
  16. Offline

    TheHandfish

    MCForger:

    I did so, and it said it doesn't have metadata... hmm...
     
  17. Offline

    MCForger

    TheHandfish
    Did this occur after a restart/reload?
     
  18. Offline

    TheHandfish

    MCForger: Wait, now it's coming up positive. Let me do some more testing, one sec.

    EDIT: I did it... I DID IT! YAY! :D

    The numbers are returning like they should, now!

    Thanks so much for your help, MCForger and TryB4!
     
    MCForger and TryB4 like this.
  19. Offline

    MCForger

  20. Offline

    TheHandfish

    MCForger, TryB4,

    Is there a way to save the blocks with their meta data when the server closes?
     
  21. Offline

    TryB4

    TheHandfish
    You can save the block's location to a configuration file.
    And the metadata/value too.
     
  22. Offline

    TheHandfish

    TryB4: How would I go about that? (Sorry for so many questions... x_x)
     
  23. Offline

    TryB4

    TheHandfish
    First create a configuration file.
    http://pastebin.com/BZVT0qWR
    in your main class:
    public static storage s;

    in onEnable:

    s = new storage();
    s.setup(this);

    Then you can save it in there.
     
    TheHandfish likes this.
Thread Status:
Not open for further replies.

Share This Page