Solved Two things I need help with

Discussion in 'Spigot Plugin Development' started by Igasi, May 21, 2017.

  1. So I am going to alpha release my server soon, but first I need to fix some things up.

    Firstly, PvE. What I want is so that if a player uses a spear to fight, all the mobs in a 1x1 block radius gets damaged. The problem is that I have to do 8 if statements (North, NE, E, Etc) to figure out where the player is looking, so I wanted to use vectors instead, for efficiency's sake and for accuracy to where the player is working.
    **I know this is possible since Wynncraft has a system where the damage goes where the player is looking
    What I've got so far:
    Code (Text):

    public class combatManager extends JavaPlugin implements Listener{
        @EventHandler
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(this, this);
            }
        @EventHandler
        public void onPlayerRightClick(PlayerInteractEvent e){
            if(e.getAction().equals(Action.RIGHT_CLICK_AIR)){
                if(e.getItem().equals(Material.IRON_SPADE)){
                    Vector v = e.getPlayer().getVelocity();
                    v.setY(2);
                      //Set y is there just incase the player is looking downwards or upwards                    
                }
            }  
        }      
    }
     
    Next thing I need help with:

    When an entity dies, if it's name is X, then all the players in the said area have their name broadcasted. Problem: I have no idea how to print a list with a , after names. Also, I want the last name to have no , after it.
    Code so far: **Before someone brings up that I don't know Java, I read Orcales document on the forEach() loop and this is what I got (Also there is an error on the forEach method)
    Code (Text):

    {
    @Override
    public void onEnable(){
        Bukkit.getPluginManager().registerEvents(this, this);
    }
    @EventHandler
    public void onEntityDeath (EntityDeathEvent e){
        List<Entity> l = e.getEntity().getNearbyEntities(15, 10, 15);
        List<String> names = l.forEach(Entity e1: l; ((Entity) e1).getDisplayName());
    }
    }
     
    So yeah if someone can turn that into a list of strings it would be great! Bonus if someone can add a comma after each name w/o the last one having anything


    **This is for my server, and if it goes public, since I don't have a lot of money to give out, I'll give out ranks to helpful people
     
  2. Can't help you with the first one, But store them in a Map / Array List
    Code (Text):
    for(Player all:getServer().getOnlinePlayers()){
    list.add(all.getName();
    }
    Then broadcast that, Its simple java
     
  3. That I can do. It's easy, but I asked for something more specific. A list that gets all the players in said area and prints out their names with commas and stuff
     
  4. Omg Give us your code, You really need to learn java / the spigotAPI
     
  5. Use StringUtils/String.join();
    (String.join() is recommended like @FlyingLiama said some posts later )

    Example:

    Code (Text):

    String[] s = new String[] {"a", "b", "c"};
    String joined = StringUtils/String.join(s, ",");  // "a,b,c"
     
     
    #5 Phloxz, May 21, 2017
    Last edited: May 21, 2017
    • Like Like x 1
  6. You should start by learning java and basic math.

    Ideally, you just get the direction the player is looking, add 1 to it, and use the resulting coordinates to check distance to mobs.
     
    • Like Like x 1
  7. Look, no reliance on bloaty third-party libraries!. While it is unlikely apache commons will ever be removed from spigot, it cant hurt to to avoid loading useless classes.
    Code (Java):
    String.join(",", strings);
     
  8. He already posted his code and basically already did a better Version of what you posted, all he wanted was splitting up a String Array to a String by using commas.
     
  9. Yea that works too i'll edit my post real quick
     
  10. Im not only using a spear which is why I want to use vectors. I am also using swords that are the 3 blocks in front of you. Just why I want to use vectors. Also, I wanna add a mage class based off of magic that has a 3x3 AoE damage infront of it.
     
  11. You can't talk... you barely know it yourself. Sorry for seeming snappy, it's just, don't tell people to learn Java when just a month or so ago you were on here asking simple questions about maps.


    Sent from my iPhone using Tapatalk
     
    • Like Like x 1
    • Agree Agree x 1
  12. To be fair, I had no idea what I was doing a month and a half ago.....

    But yeah I agree with you
     
  13. That is vectors. You should probably look up what vectors mean in the context of a 3D environment, understanding them properly will make all of this very easy.

    They typically consist of 3 values that represent several things, often interchangably:
    A location: x/y/z coordinates
    A velocity or force: x/y/z speed
    A rotational direction: angle on each x/y/z axis
    A normalized vector, which is usually a velocity/force that has a magnitude (speed) or length of 1.

    Normalized basically converting a vector to a specific magnitude/length without changing the direction, for example 10,0,0 could be normalized to 1,0,0 and 20,0,0 would also normalize to 1,0,0 as they represent the same direction (straight forwards along X), just with different amounts of force/distance

    For this, you could get the view direction of the player as a vector, normalize it (it may already be normalized); it represents a point 1 meter forwards along the view of the player, so just multiply it by the needed distance, add it to the location of the player, and you have the center of your AoE.
     
  14. Ok yeah I get that, but I asked if I can highlight an area with vectors, and someone replied with yes. Anyways, I figured our a more broad way of doing it, so yeah.

    Still need to figure out how to get a list of strings from a list of entities...
     
  15. A list of entity names? Just use a loop. This is really stuff you should (and need) to know.
     
  16. I already showed the loop, but it gives an error (Already said it does)

    Here is what I've got so far:
    Code (Text):

    List<Entity> l = e.getEntity().getNearbyEntities(15, 10, 15);
        List<String> names = l.forEach(Entity e1: l; ((Entity) e1).getDisplayName());
     
    sketchy forEach loop
     
  17. not sure how that even works.. i would suggest looking up some basic for loop tutorials or even java 8 stream API.

    Code (Java):

    entity.getNearbyEntities(3, 3, 3).stream().map(Nameable::getCustomName).collect(Collectors.toList()); //returns list of entity's custom names (those in a 3x3 radius)
     
     
  18. Yeah that works. I tried using a forEach loop and thats what I got out of reading one of oracles documents. Thanks guys!

    EDIT: Red underline loaded and nameable doesnt work

    Its just giving me a list of replacements

    Wait what version does Nameable() come in?

    EDIT: Upgraded to 1.11 it works now
     
    #18 Igasi, May 22, 2017
    Last edited: May 22, 2017
  19. forEach is not a loop, it is an aberrant horror that needs to be purged.
    Stream API is even worse in most cases; it is like using a diesel generator to power a lamp.


    Use a for loop (This is untested code, just typed it out here, hopefully i did not make any dumb errors)

    ArrayList<String>
    Code (Java):

    List<Entity> le = entity.getNearbyEntities(3, 3, 3);
    List<String> ls = new ArrayList<>(e.size());
    for(Entity e : le)
        ls.add(e.getName());
    Array
    Code (Java):

    List<Entity> le = entity.getNearbyEntities(3, 3, 3);
    int i=le.size();
    String[] as = new String[i];
    while(--i>-1)
        as[i]=le.get(i).getName();

    String joined = String.join(", ",as);