Force Bomb

Discussion in 'Plugin Development' started by stickman561, Jun 23, 2015.

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

    stickman561

    Hi, I would like to add a method to my plugin that, when called with an integer and location input, sends all entities(including drops) and blocks(using tileID on falling sand entities) within the integers' radius from the location flying away from that location at high speed. I imagine it would require vectors, which always give me trouble. Would anyone mind thinking of some code that might accomplish this? Please reply, as I have been straining over figuring this out for quite some time.
     
  2. what you do is you get all the entities and you check the distance of the entity and the location you provided with the distancesquared (using the squared distance method makes it faster) and if it's withing the location, you get the location of the entity as an vector and subtract the location that you provided as an vector, and normalize that vector. Then you multiply the vector with (1 minus distance of the entity and the location devided with the size of the explosion (from the center to the edge), and clamp that value between 0 and 1), know you can multiply the vector with an number and that number is the intensity of the explosion. then you have the vector that is the velocity of the entity. This should work, but it could be that I have made a little mistake.
     
  3. Offline

    stickman561

    0.o I don't get it. Please give a code example, using code tag. (CODE=Java) (/CODE) using square brackets in place of parenthesis.
     
  4. Offline

    567legodude

    @Bram0101 wanna let you know that someone did a test and with the data that they found, using squared is hardly faster, by ~1 millisecond.
     
  5. that's true, it isn't that much faster, but when being called a lot of times, it will add up. So if it's only a few times it doesn't matter, but with a lot it starts to matter. And I just realized that using a radius for the size of the explosion, using the normal distance method is the one you need.
    This is something I actually don't do, because you then can just copy and paste it, and you don't really learn much. So I'm going to try to explain it in a different way that is a bit similar to examples.
    Code:
    for(entity e : entities){
    
    distance = explosion.distance(e.getLocation());
    if(check distance){
    
    Vector v1 = e.getLocation().toVector().substract(explosion.toVector()).normalize();
    double d1 = distance / explosionSize;
    if(d1 < 0){
    d1 = 0;
    }
    if(d1 > 1){
    d1 = 1;
    }
    v1.multply(1 - d1).multiply(intensity);
    e.setVelocity(v1);
    
    }
    }
    
    this should be clear now.
     
  6. Offline

    stickman561

    I learned to code by copy-pasting. Now I have a 3D multiplayer game in the works. Copy-Paste coding isn't such a bad thing, you know. And thank you.

    I still can't figure this out. I'm sorry if this makes me sound noobish, but I need a solid chunk of code that I can call from a loop statement to detonate bombs. If you feel that I need to learn it on my own, then I'm sorry but you aren't being very helpful to me in this scenario. (Bram, this isn't focused purely on you). You would be better off using this talent in an online classroom, rather than a support forum.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  7. What do you have right now? What kind of codes, and what do you exactly want? I now don't really know what your code looks like, and therefor I can't really help you make the code that fits for that.
     
  8. Offline

    stickman561

    I have it set so that every time someone places a "Force Bomb" block, it logs the location. Whenever the "detonator" item is used, it scrolls through this log and calls THIS:
    Code:
    public void forcewave(int power, Location location) {
    
    }
    for each location before deleting that location from memory. I just need the code to go inside of that method to make the blocks and entities fly away within a radius of the "power" int from the cental "location" Location.
     
  9. Then what I gave earlier should work, but you probably added it differently. entities is the list of entities, so you would do "location.getWorld().getEntities()" to get the entities list. And then you can do the same thing but for fallingBlock entities that you spawn if a block has to go. Do remember that the code that I gave isn't something that you can copy, but you can use it as a template on how to do it, with basic knowledge of java and bukkit you should be able to do this. If you can't find the method, look at the javadoc to what method it should be.
     
  10. Offline

    stickman561

    Ok, I've got this code:
    Code:
      public void detonateforcebombs() {
            if(forcebombs.size() != 0) {
              Location boom = (Location)forcebombs.get(forcebombs.size() - 1);
              boom.getWorld().playSound(boom, Sound.AMBIENCE_CAVE, 10F, 2.78F);
              forcewave(boom, 3, 0.2);
              forcebombs.remove(boom);
              if(forcebombs.size() != 0) {
                detonateforcebombs(detonator);
              }
            }
          }
    When I call the method, blocks do nothing, players don't move at all half the time with a "<NAME> moved to quickly" error in the console, and mobs simply disappear! Some help please?
    Here's the forcewave method:
    Code:
    public void forcewave(Location location, int radius, double power) {
        List<Block> blocklist = new ArrayList<Block>();
       
        for(double x = radius * -1; x <= radius; x++) {
            for(double y = radius * -1; y <= radius; y++) {
              for(double z = radius * -1; z <= radius; z++) {
                Block block = location.getBlock().getRelative((int)x, (int)y, (int)z);
                World w = location.getWorld();
    
                 for (int i = 0; i < blocklist.size();i++){    
                     Block b = blocklist.get(i);
                     Location bLoc = b.getLocation();
                     x = bLoc.getX() - location.getX();
                     y = bLoc.getY() - location.getY() + 0.5;
                     z = bLoc.getZ() - location.getZ();
                FallingBlock fb = w.spawnFallingBlock(bLoc, b.getType(), (byte)b.getData());
                fb.setDropItem(false);
                fb.setVelocity(new Vector(0, 1, 0));
                }
              }
            }
          }
       
        List<Entity> entities = location.getWorld().getEntities();
       
        for(Entity entity : entities) {
            double distance = location.distance(entity.getLocation());
           
            if(distance <= radius) {
                Vector vector = entity.getLocation().toVector().subtract(location.toVector().normalize());
                double distanceroot = distance / radius;
               
                if(distanceroot < 0) {
                    distanceroot = 0;
            }
               
                if(distanceroot > 1) {
                    distanceroot = 1;
            }
               
            vector = vector.multiply(1 - distanceroot).multiply(power);
            entity.setVelocity(vector);
          }
        }
      }
     
  11. *Offtopic*
    You can write things like
    [code=java] [/code]
    using
    [plain][code=java] [/code][/plain]
    ^
    |
    this was made by writing [plain][plain][code=java] [/code][/plain][/plain]
    plain-ception
     
  12. Offline

    stickman561

    About your signature, it's false. Also, those are HTML markup tags.
     
  13. Offline

    stickman561

Thread Status:
Not open for further replies.

Share This Page