Library Black holes :0

Discussion in 'Resources' started by Skionz, Dec 27, 2014.

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

    Skionz

    I saw some thread about vector manipulation and I was interested so I made a 'black hole.' Then I wrote a quick class so that you can create your own easily
    Here is a video of what I am talking about

    Class:
    Code:
    public class BlackHole {
        private JavaPlugin plugin;
        private Location loc;
        private int range;
        private float power;
        private List<EntityType> types;
        private boolean isEnabled;
      
        public BlackHole(JavaPlugin plugin, Location loc, int range, float power, List<EntityType> types) {
            this.plugin = plugin;
            this.loc = loc;
            this.range = range;
            this.power = power;
            this.types = types;
            this.isEnabled = true;
            this.control();
        }
      
        private void control() {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this.plugin, new Runnable() {
                public void run() {
                    World world = loc.getWorld();
                    int squared = range * range;
                    if(this.isEnabled()) {
                        for(Entity e : world.getEntities()) {
                            if(types == null || types.contains(e.getType())) {
                                if(e.getLocation().distanceSquared(loc) <= squared) {
                                    Vector entityVector = new Vector(e.getLocation().getX(), e.getLocation().getY(), e.getLocation().getZ());
                                    Vector blackholeVector = new Vector(loc.getX(), loc.getY(), loc.getZ());
                                    e.setVelocity(blackholeVector.subtract(entityVector).multiply(power));
                                }
                            }
                        }
                    }
                }
            }, 1, 1);
        }
        public void enable() {
            this.isEnabled = true;
        }
        public void disable() {
            this.isEnabled = false;
        }
        public void setLocation(Location loc) {
            this.loc = loc;
        }
        public void setRange(int range) {
            this.range = range;
        }
        public void setPower(float power) {
            this.power = power;
        }
        public void setTypes(List<EntityType> types) {
            this.types = types;
        }
        public float getPower() {
            return this.power;
        }
        public int getRange() {
            return this.range;
        }
        public Location getLocation() {
            return this.loc;
        }
        public boolean isEnabled() {
            return this.isEnabled;
        }
    }
    Example usage:
    Code:
    public class SpaceTest extends JavaPlugin implements Listener {
        private BlackHole blackhole;
      
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
        }
      
        @EventHandler
        public void onInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if(event.getAction() == Action.LEFT_CLICK_AIR) {
                if(blackhole == null) {
                    List<EntityType> types = new ArrayList<EntityType>();
                    for(EntityType type : EntityType.values()) {
                        if(type != EntityType.PLAYER) {
                            types.add(type);
                        }
                    }
                    blackhole = new BlackHole(this, player.getLocation(), 500, 0.05F, types);
                    player.sendMessage(ChatColor.GOLD + "Created new black hole");
                }
                else {
                    if(blackhole.isEnabled()) {
                        blackhole.disable();
                        player.sendMessage(ChatColor.GOLD + "Disabled the black hole");
                    }
                    else {
                        blackhole.enable();
                        player.sendMessage(ChatColor.GOLD + "Enabled the black hole");
                    }
                }
            }
        }
    }
    The space generator in the video was just there to make the black hole look cooler.
     
    Last edited: Jan 3, 2015
  2. Offline

    teej107

    Cool! I think a Set would be better for the entity types though.
     
    Experminator and Skionz like this.
  3. Offline

    ChipDev

    Was that recorded on my server?
    :p
    I thought I saw that! Sweet!
    Second: I see that you are smart with vectors. You have the force!
    Get it? :p
     
  4. Offline

    Skionz

    @ChipDev It was on my local one. The space world made it look 10x better haha
     
    ChipDev likes this.
  5. Offline

    xTrollxDudex

    Whoa! That's really cool! You could optimize t a bit by using some NMS magic (the add entity to world method), but it looks fantastic! Good job.

    If you could add some swirly effects, that would be super cool:
    [​IMG]
     
    Last edited by a moderator: Dec 27, 2014
    Experminator, teej107 and Skionz like this.
  6. Offline

    Skionz

    ChipDev likes this.
  7. Offline

    teej107

    Well he did have a tutorial on creating a helix.

    Ninja'D darn it!
     
    TwerkinCraft and Skionz like this.
  8. Offline

    xTrollxDudex

    @Skionz
    I was referring to the disk-surface around the black hole :p
     
  9. Offline

    Monkey_Swag

    @Skionz you g0d. Only someone like you would freely give something like this out! Call me a kiss-ass if you want but this is just really cool. Great job!
     
    Skionz likes this.
  10. Offline

    bwfcwalshy Retired Staff

    I went to the blackhole, I died many times. There was a ball of tnt, shift was jetpack. It was a strange day for man kind.

    @Skionz get's that. I played on the server when he was testing it and it was crazy, good job on it Skionz.
     
    Skionz likes this.
  11. Offline

    JordyPwner

    Last edited: Dec 28, 2014
  12. Offline

    nverdier

    That could be cool.
     
  13. Offline

    Avygeil

    You could divide the power by the distance to make it look smoother when they just get attracted. :) Also normalize the vector before multiplying, or entities will slow down the closer they are. Unless it is intended behaviour, but I think it would look better if you don't get pulled super fast when you just step in range. :p Also, you should use a EnumSet for a Set of EntityType.
     
  14. Offline

    blablubbabc

    Instead of setting the entities velocity directly towards the black hole you could also do something like this:
    Vector toHole = blackholeVector.subtract(entityVector);
    double distance = toHole.length();
    entity.setVelocity(entity.getVelocity().add(toHole.multiply(power / (distance * distance))));

    That way entities won't fly directly towards the black hole but only get pulled in step by step, potentially even flying in circles around the black hole, depending on the entities initial speed and the power of the black hole etc.

    Also the force towards the black hole depends on the distance between the black hole and the entity with this.
     
    Last edited: Dec 28, 2014
  15. Offline

    Skionz

    I tried. It crashed the server :(
    I just tried that and it makes for a really cool effect
    [​IMG]
     
    Last edited: Dec 28, 2014
  16. Offline

    JordyPwner

    How can it crash the server? Just get a max blocks at same time to be attracted then when they are at the blackhole do it again?
     
  17. Offline

    Skionz

    Ah, I will try it later. I'm going to take a wild guess and say it crashed because I turned every block in the range into a falling block.
     
  18. Offline

    JordyPwner

    just have max of like 10 blocks and get those blocks from a random location in a range then attract it to the blackhole and delete those blocks and then do it agian ;)
     
    Skionz likes this.
  19. Offline

    teej107

    @Skionz Now when you go inside the black hole, will it plop you in a saved world backup from the past?
     
    Skionz likes this.
  20. Offline

    Hawktasard

    @Skionz
    This looks amazing, Good job :)
     
    Skionz likes this.
  21. Offline

    bwfcwalshy Retired Staff

    @teej107 That would be amazing, issue is that it would be a pain to do.
     
    teej107 likes this.
  22. Offline

    ChipDev

    Lucky you. all your resources are awesome ;-;
     
    Skionz likes this.
  23. Offline

    Totom3

    I really like it but have a few suggestions for optimizing. Cache the square of the range and check isEnabled() before entering the loop (right now it recalculates the range for each Entity and might loop uselessly if the blackhole isn't enabled).
     
    Skionz likes this.
Thread Status:
Not open for further replies.

Share This Page