Solved Knock an entity up

Discussion in 'Plugin Development' started by Bobit, Apr 15, 2014.

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

    Bobit

    This code is supposed to do: when a player right clicks with a blaze rod, if the cooldown is ready, get the air block the player is looking at, then spawn particle flames in a horizontal square around it that damage and ignite entities it hits. The square gets smaller and smaller. When the square is one block, it spawns falling lava for the special effects and deals damage, ignites, and knocks targets up.

    Phew.

    What it actually does: Er...all of the System.out.println() statements work. The cooldown works too, but IT'S A COOLDOWN FOR NOTHING.

    No errors from netbeans or minecraft. Ignore the lack of imports, I just didn't bother to copy-and-paste those. My best guess is that target has some sort of messed up value.


    Code:java
    1. public final class ReignRpg extends JavaPlugin implements Listener {
    2.  
    3. public final Plugin main = this;
    4. Skills Skillz = new Skills(); /*Skillz is an instance of the Skills class.
    5.   * Skills is really just a library of skills, but it is made into an object because
    6.   * it's always best to keep it there just in case. Cooldowns and methods
    7.   * using cooldowns could easily be made static, but why bother?
    8.   * That only messes with multi-server operations.
    9.   */
    10.  
    11.  
    12. @Override
    13. public void onEnable() {
    14. System.out.println("Plugin [ReignRpg] has been enabled!");
    15. getServer().getPluginManager().registerEvents(this, this);
    16. this.saveDefaultConfig();
    17. }
    18.  
    19. @Override
    20. public void onDisable() {
    21. }
    22.  
    23. @EventHandler
    24. public void interaction(PlayerInteractEvent event) {
    25. //whenever the player right-clicks, this code is done. It should lead to all other "on-right-click" spells, with their conditions inside them.
    26. Player player = event.getPlayer();
    27. /*it's as simple as:
    28.   * Skillz.{skillname}(this, [player]);
    29.   * for almost every single skill you want to maybe activate on right-click.
    30.   */
    31. if ((player.getItemInHand().getType()) == Material.BLAZE_ROD) {
    32. Skillz.eruption(this, player);
    33. }
    34. }
    35.  
    36. public static Block getTargetAirBlock(final Player player) {
    37. BlockIterator bit = new BlockIterator(player, 300);
    38. Block prev = null;
    39. Block next = null;
    40. while (bit.hasNext()) {
    41. prev = next;
    42. next = bit.next();
    43. }//we want the air block before the target block. This should do it.
    44. assert (prev != null) : "Somehow, getTargetBlock returned null";
    45. return (prev);
    46. }
    47.  
    48. public static void trueDamageTarget(final LivingEntity target, final double damage) {
    49. double x = target.getHealth();
    50. if ((x - damage) < 0) {//we can't have negative health!
    51. target.damage(0);//to give the impression of damage
    52. target.setHealth(0);
    53. } else {
    54. target.damage(0);//to give the impression of damage
    55. target.setHealth(x - damage);
    56.  
    57. }
    58. }
    59.  
    60. public static void overheat(final Player player, final double damage) {
    61. player.sendMessage(ChatColor.RED + "Overheated for " + damage + " true damage! Try not casting that spell for a little.");
    62. trueDamageTarget(player, damage);
    63. player.getWorld().playEffect(player.getLocation(), Effect.POTION_BREAK, 0);
    64. player.damage(0);//to give the impression of damage
    65. }
    66.  
    67. public Player getPlayerByUuid(UUID uuid) {
    68. for (Player p : getServer().getOnlinePlayers()) {
    69. if (p.getUniqueId().equals(uuid)) {
    70. return p;
    71. }
    72. }
    73. //if there are no players with that UUID, which probably shouldn't happen.
    74. System.out.println("[WARNING] [ReignRPG] A player was not returned by getPlayerByUuid");
    75. return null;
    76. }
    77.  
    78. }


    Code:java
    1. public class Skills {
    2.  
    3. public Skills() {
    4. }
    5.  
    6. public WeakHashMap<UUID, Long> eruptionCooldown = new WeakHashMap<UUID, Long>();
    7.  
    8.  
    9.  
    10. public void eruption(final Plugin main, final Player player) {//have fire converge on the target area, and then spawn lava at the target, and knock up targets.
    11. if (eruptionCooldown.get(player.getUniqueId()) == null) {
    12. eruptionCooldown.put(player.getUniqueId(), 0L);//it's a long, so we have to put L after it
    13. }
    14. System.out.println("reach skillz.eruption");
    15. long ticksSinceCast = ((Bukkit.getServer().getWorld("world").getFullTime()) - (eruptionCooldown.get(player.getUniqueId())));
    16. if (ticksSinceCast >= main.getConfig().getInt("EruptionCooldown")) {//if it's ready to cast
    17. Location targetLocation = ReignRpg.getTargetAirBlock(player).getLocation();// one block before the "target" block.
    18. System.out.println("cooldown ready.");
    19. if (targetLocation.getBlock().getType() == Material.AIR) {//make sure it's empty, or it'll just be weird.
    20. System.out.println("the block is indeed empty.");
    21. eruptionCooldown.put(player.getUniqueId(), (Bukkit.getServer().getWorld("world").getFullTime()));
    22. recursiveEruptionTick(main, main.getConfig().getInt("EruptionInitialRadius"), targetLocation);
    23. } else {
    24. player.sendMessage(ChatColor.RED + "You can't cast eruption on a wall.");
    25. }
    26. } else {
    27. ReignRpg.overheat(player, main.getConfig().getInt("EruptionOverheat"));
    28. }
    29. }
    30.  
    31. private void recursiveEruptionTick(final Plugin main, final int radius, final Location target) {
    32. if (radius > 1) {//play a fire effect, damage, and ignite targets at the blocks that are the given radius from the target horizontally, and adjacent vertically. Then, after the delay, call this method with the current radius -1.
    33. System.out.println("that's one flame tick");
    34. int xCoordModifier;
    35. int zCoordModifier;
    36. xCoordModifier = radius * -1;
    37. for (zCoordModifier = radius * -1; zCoordModifier <= radius; zCoordModifier++) {
    38. spawnEruptionFlame(main, target, xCoordModifier, zCoordModifier);
    39. }
    40. xCoordModifier = radius * 1;
    41. for (zCoordModifier = radius * -1; zCoordModifier <= radius; zCoordModifier++) {
    42. spawnEruptionFlame(main, target, xCoordModifier, zCoordModifier);
    43. }
    44. zCoordModifier = radius * -1;
    45. for (xCoordModifier = radius * -1; xCoordModifier <= radius; xCoordModifier++) {
    46. spawnEruptionFlame(main, target, xCoordModifier, zCoordModifier);
    47. }
    48. zCoordModifier = radius * 1;
    49. for (xCoordModifier = radius * -1; xCoordModifier <= radius; xCoordModifier++) {
    50. spawnEruptionFlame(main, target, xCoordModifier, zCoordModifier);
    51. }
    52.  
    53. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    54.  
    55. @Override
    56. public void run() {
    57. recursiveEruptionTick(main, radius - 1, target);
    58. }
    59. }, main.getConfig().getInt("EruptionDelay"));
    60.  
    61.  
    62. } else {//perform the animation, damage, and knockup at the focus.
    63. System.out.println("that's the focus.");
    64. System.out.println("The focus should be at " + target.getX() + ", " + target.getY() + ", " + target.getZ() + ".");
    65. final FallingBlock fallb = target.getWorld().spawnFallingBlock(target, Material.LAVA, (byte) 9);//spawn falling lava that won't do anything, theoretically.
    66. target.getWorld().playSound(target, Sound.GHAST_FIREBALL, 1, 1);
    67. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
    68.  
    69. @Override
    70. public void run() {
    71. fallb.remove();
    72. target.getWorld().playSound(target, Sound.EXPLODE, 1, 1);
    73. List<Entity> near = target.getWorld().getEntities();
    74. for (Entity e : near) {
    75. LivingEntity targetEntity = null;
    76. if (e instanceof LivingEntity) {
    77. targetEntity = (LivingEntity) e;
    78. }
    79. if (targetEntity != null) {
    80. if ((targetEntity.getLocation().distance(target)) <= .5) {//if it's a living entity and it's close enough
    81. targetEntity.damage(main.getConfig().getInt("EruptionFocusDamage"));
    82. int longerIgniteLength = 0;
    83. if (targetEntity.getFireTicks() >= main.getConfig().getInt("EruptionFocusIgniteLength")) {
    84. longerIgniteLength = targetEntity.getFireTicks();
    85. } else if (targetEntity.getFireTicks() < main.getConfig().getInt("EruptionFocusIgniteLength")) {
    86. longerIgniteLength = main.getConfig().getInt("EruptionFocusIgniteLength");
    87. }
    88. targetEntity.setFireTicks(longerIgniteLength);
    89. Vector velocity = new Vector(0, main.getConfig().getInt("EruptionKnockup"), 0);
    90. targetEntity.setVelocity(velocity);
    91. }
    92. }
    93. }
    94. }
    95. }, main.getConfig().getInt("EruptionFocusDuration"));
    96. }
    97. }
    98.  
    99. private void spawnEruptionFlame(Plugin main, Location target, int xCoordModifier, int zCoordModifier) {
    100. System.out.println("spawning an eruption flame...");
    101. Location flameLocation = new Location(target.getWorld(), (target.getBlockX() + xCoordModifier), target.getBlockY(), (target.getBlockZ() + zCoordModifier));
    102. target.getWorld().playEffect(target, Effect.SMOKE, 1);
    103.  
    104. List<Entity> near = target.getWorld().getEntities();
    105. for (Entity e : near) {
    106. LivingEntity targetEntity = null;
    107. if (e instanceof LivingEntity) {
    108. targetEntity = (LivingEntity) e;
    109. }
    110. if (targetEntity != null) {
    111. if ((targetEntity.getLocation().distance(target)) <= .5) {//if it's a living entity and it's close enough
    112. targetEntity.damage(main.getConfig().getInt("EruptionFlameDamage"));
    113.  
    114. int longerIgniteLength = 0;
    115. if (targetEntity.getFireTicks() >= main.getConfig().getInt("EruptionFlameIgniteLength")) {
    116. longerIgniteLength = targetEntity.getFireTicks();
    117. } else if (targetEntity.getFireTicks() < main.getConfig().getInt("EruptionFlameIgniteLength")) {
    118. longerIgniteLength = main.getConfig().getInt("EruptionFlameIgniteLength");
    119. }
    120. targetEntity.setFireTicks(longerIgniteLength);
    121. }
    122. }
    123. }
    124. }
    125. }
     
  2. Offline

    BillyGalbreath

    Which System.out.println is "the last one?"
     
  3. Offline

    Bobit

    Line 98 of the second batch of code. Sorry about that, my first post randomly got deleted before it was posted, so I was kind've rushing.
     
  4. Offline

    BillyGalbreath

    Code:java
    1.  
    2. for (zCoordModifier = radius * -1; zCoordModifier <= radius; zCoordModifier++) {
    3.  


    Output those values in a println right before the loop to see what the actual values are and see if the conditions for the loop are even met for it to run.
     
  5. Offline

    Bobit

    Apparently it is reaching the last System.out.println(). But it's still not doing anything. Another weird part is that, even on the last recursiveeruptiontick, it claims to spawn around 20 flames, which is more than there should be.

    Edit: yeah, the conditions are right.

    Also, don't worry about the overheat line. That part works. It basically punishes the player for trying to cast the spell too soon.

    lol, 42 views already and nobody knows.
    I'm legitimately considering giving up on this code.

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

    RawCode

  7. Offline

    Bobit

    Rawcode, you're telling me everything I already know.
    My cooldowns and delays are not the problem, they work on the other 5 skills I have like this.
    I have formatted it, but it's kind of difficult to copy and paste in perfect format.
    I have traced it, as evident from the System.out.println()'s, and it went through every single one.

    If you're not going to tell me something I certainly didn't already know, please, just say you're not sure. The people who can actually tell me something I didn't know were deterred because you "answered my question," while your solution was probably something I already tried because you didn't read that I already did a bug trace.

    I don't know how to host my code though. Nor do I understand what your article is supposed to do.
     
  8. Offline

    Bobit

    bump?
     
  9. Offline

    Bobit

    Last bump :(
     
  10. Offline

    TomFromCollege

    RawCode is right, attempting to read your code as it's currently sitting is horrible. If you can't get bukkit to format it, can you at least paste it to PasteBin?
    Can we get a console output?
    Do you ever test the values you're using?
    Code:
    System.out.println("the block is indeed empty.");
    That line may call absolutely fine - But why not output extra detail such as block material, Anything you're using to find out what's not setting correctly.
     
    Bobit likes this.
  11. Offline

    RawCode

    Bobit
    I tell you to use code hosting and you answer "i know this" but still not fixed your posts, are you kidding or what?
     
  12. Offline

    Bobit

    Alright, I'm fixing the formatting.
    Also, rawcode, I wrote "I don't know how to host though." What I do know is what part of my code works, and I know that the part you were suggesting didn't work DOES work. That's what I was saying.
    As for more detailed System.out.println()'s, I might look into that soon, but I'll have to find special methods for everything.

    Alright, format fixed. Thanks to tom for telling me about pastebin.

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

    TomFromCollege

    The part that you think DOES work, may not actually work, you're probably passing incorrect variables somewhere, hence my saying: Test them.
     
    Bobit likes this.
  14. Offline

    Bobit

    Okay, I threw in System.out.println("The focus should be at " + target.getX() + ", " + target.getY() + ", " + target.getZ() + ".").
    I also made it less laggy on large servers by not permanently storing the player (which would cause a ton of lag), but rather his UUID.
    I'll test it tomorrow, sorry for all the formatting fails.

    Alright, I know what's wrong now. It's the getTargetAirBlock() method. This method returned values nowhere near what it was supposed to, which makes sense because I'm not entirely sure how block iterators work, and I wrote that code. Where the value should've been something like 113, 69, 211, it instead returned 326, 115, 229 :eek:

    (That's pretty bad [sheep] )

    Basically, I need someone to help me rewrite that method, or point me in the right direction. It's supposed to return the block that the player would have placed a block on, had they been closer and holding a block.

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

    RawCode

    Bobit
    protip about things you dont know http://lmgtfy.com/?q=code+hosting

    proper direction:
    System.out.println("Some expression " + (some expression));
    if (some expression)

    Will provide you with trace about all conditions in your code.

    If some condition pass when it shoud not - you will have chance to know about this.

    Personally i just copypaste code as strings for debugging, but it's possible via annotation preprocessors to make things more effiicient.
     
  16. Offline

    Bobit

    See my post before rawcode's smartass response.
    (That link was pretty awesome though XD)

    Edit: I've got it up on github for now, but this is alright as-is.

    2nd Edit: To recap, I need a method that returns the air block the player is looking at.
     
  17. Offline

    Bobit

    Bump.

    To summarize: I need a method that returns the air block the player is looking at.
    (Meaning the place that he would place a block at if he were close enough and holding a block and right-clicked.)
     
  18. Offline

    Garris0n

    There's a getLastTwoTargetBlocks(). Alternatively, a BlockIterator.
     
  19. Offline

    Bobit

    Well, Garris0n what's wrong with the current one?
    Code:java
    1. public static Block getTargetAirBlock(final Player player) {
    2. BlockIterator bit = new BlockIterator(player, 300);
    3. Block prev = null;
    4. Block next = null;
    5. while (bit.hasNext()) {
    6. prev = next;
    7. next = bit.next();
    8. }
    9. assert (prev != null) : "Somehow, getTargetBlock returned null";
    10. return (prev);
    11. }

    Shouldn't that return the secondtolast block? Or would that go through walls?
     
  20. Offline

    Garris0n

    BlockIterators don't stop. You have to check if the type is air (or really, if the last type is air).
     
    Bobit likes this.
  21. Offline

    Bobit

    (That bukkit auto-format though. Never edit without first putting the code into pastebin)

    So, like this? (ignore stupid syntax errors, I don't have an IDE with me atm)

    Code:java
    1. public static Block getTargetAirBlock(final Player player) {
    2. BlockIterator bit = new BlockIterator(player, 300);
    3. Block prev = null;
    4. Block current = null;
    5.  
    6. if(bit.hasNext){
    7. current=bit.next();
    8. while (bit.hasNext() && (current.getType() == Material.AIR)) {
    9. prev = current;
    10. current = bit.next();
    11. }
    12. if(current.getType()==Material.AIR){
    13. prev=null;
    14. }
    15. }
    16.  
    17. return (prev);
    18. }

    Outside of this, there would be a null-check that printed to the player it's out of range in cases where the player activated this.
     
  22. Offline

    Garris0n

    Why don't you try it?
     
  23. Offline

    Bobit

    I did, sorry. It had some errors, but I fixed it up and it's working now.
    But now I have another problem: the .setVelocity() doesn't seem to work at all.
    It should knock up entities at the target block, but it doesn't. Everything else is working fine.
    Doesn't a new vector (0,5,0) point straight up?

    EDIT: Yes! But my falling block that was put there for the special effects was pushing the player out of the way when it fell, so the knockup didn't target it.
     
  24. Offline

    Bobit

    Bump.
    To summarize: shouldn't
    Code:java
    1. Vector x = new Vector(0, 5, 0);
    2. p.setVelocity(x);

    knock the player up?

    Edit: after looking at the tutorial for vectors, it seems more like x should alter the upward velocity. I'll test that out soon.
     
Thread Status:
Not open for further replies.

Share This Page