Solved Need help on getting players rank

Discussion in 'Plugin Development' started by TinyTom38, May 18, 2014.

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

    TinyTom38

    Code:java
    1. package me.tinytom38.Plugin;
    2.  
    3. import org.anjocaido.groupmanager.data.User;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    8. import static org.bukkit.ChatColor.*;
    9.  
    10. public class Event implements Listener {
    11.  
    12. public Main plugin;
    13.  
    14. @EventHandler
    15. public void onEntityDamage(EntityDamageByEntityEvent e) {
    16. if (!(e.getDamager() instanceof Player)) {
    17. return;
    18. }
    19. if (!(e.getEntity() instanceof Player)) {
    20. return;
    21. }
    22.  
    23. Player player = (Player) e.getDamager();
    24. Player targetPlayer = (Player) e.getEntity();
    25.  
    26. if (player instanceof User) {
    27. if ((((User) player).getGroup()) == ((User) targetPlayer)
    28. .getGroup()) {
    29. player.sendMessage(GOLD + "You can not hurt a fellow "
    30. + ((User) player).getGroupName() + GOLD + "!");
    31. e.setCancelled(true);
    32. }
    33. }
    34. }
    35. }


    I have this which doesn't work, unfortunately im new to coding java plugins and sorta new to actual coding.
    Any help on how to do this?
    Btw im trying to get the groupmanager group of the player and see if they are the same then cancel the damage and say they cant hurt a fellow <rankname>

    I did do this another way which i think is quite messy though:
    Code:java
    1. package me.tinytom38.Plugin;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    7. import static org.bukkit.ChatColor.*;
    8.  
    9. public class Event implements Listener {
    10.  
    11. public Main plugin;
    12.  
    13. @EventHandler
    14. public void onEntityDamage(EntityDamageByEntityEvent e) {
    15. if (!(e.getDamager() instanceof Player)) {
    16. return;
    17. }
    18. if (!(e.getEntity() instanceof Player)) {
    19. return;
    20. }
    21. Player player = (Player) e.getDamager();
    22. Player targetPlayer = (Player) e.getEntity();
    23.  
    24. if ((player.hasPermission(new Permissions().permTTAS))
    25. && (targetPlayer.hasPermission(new Permissions().permTTAS))) {
    26. e.setCancelled(true);
    27. player.sendMessage(GOLD + "You Can Not Damage Fellow Assassins!");
    28. }
    29.  
    30. if ((player.hasPermission(new Permissions().permTTHU))
    31. && (targetPlayer.hasPermission(new Permissions().permTTHU))) {
    32. e.setCancelled(true);
    33. player.sendMessage(GOLD + "You Can Not Damage Fellow Hunters!");
    34. }
    35.  
    36. if ((player.hasPermission(new Permissions().permTTAD))
    37. && (targetPlayer.hasPermission(new Permissions().permTTAD))) {
    38. e.setCancelled(true);
    39. player.sendMessage(GOLD + "You Can Not Damage Fellow Adventurers!");
    40. }
    41. }
    42. }


    If nobody can help ill just stick to this as its just personal use
    As this works

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

    TinyTom38

    Though i have got it too work the other way someone helping with the other one would be nice :)
     
  3. Offline

    TinyTom38

    I think i have figured it out, quite simple now that i see it XD
    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageByEntityEvent event) {
    3. if (!(event.getDamager() instanceof Player)
    4. || !(event.getEntity() instanceof Player))
    5. return;
    6. User player = (User) event.getDamager();
    7. User targetPlayer = (User) event.getEntity();
    8.  
    9. if (player.getGroup() == targetPlayer.getGroup()) {
    10. event.setCancelled(true);
    11. player.getBukkitPlayer().sendMessage(
    12. ChatColor.GOLD + "You Can Not Hurt Fellow "
    13. + player.getGroupName() + ChatColor.GOLD + "s");
    14. }
    15. }

    I think that will work, havent tested yet (about to do). Just thought id post it on here incase anyone else comes looking
     
  4. Offline

    TinyTom38

    Doesn't work :( anyone know how to do this?
     
  5. Offline

    xTigerRebornx

  6. Offline

    TinyTom38

    Okay ill try XD

    Okay sorry but how do i use this?
    I tried using the getGroup() example but i just don't know how to make it check if the player does what the player does

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

    xTigerRebornx

  8. Offline

    TinyTom38

    On the page is has an example method of getGroup(), i tried adding this in but i don't know what to do with it...
     
  9. Offline

    xTigerRebornx

    TinyTom38 It returns a String, which I believe is the groups name.. You are able to compare Strings using the methods it contains.
     
  10. Offline

    TinyTom38

    like how though as it doesn't allow me to do player.getRank() or player.getRankName() which is what i need right?

    Wait i think i got it XD sorry im new to coding n such
    Is it this by any chance?

    Code:java
    1. public class Event implements Listener {
    2.  
    3. public Main plugin;
    4. private GroupManager groupManager;
    5.  
    6. @EventHandler
    7. public void onEntityDamage(EntityDamageByEntityEvent event) {
    8. if (!(event.getDamager() instanceof Player)
    9. || !(event.getEntity() instanceof Player))
    10. return;
    11. Player player = (Player) event.getDamager();
    12. Player targetPlayer = (Player) event.getEntity();
    13. if (getGroup(player) == getGroup(targetPlayer)) {
    14. event.setCancelled(true);
    15. player.sendMessage(
    16. ChatColor.GOLD + "You Can Not Hurt Fellow "
    17. + getGroup(player) + ChatColor.GOLD + "s");
    18. }
    19. }
    20.  
    21. public String getGroup(final Player base) {
    22. final AnjoPermissionsHandler handler = groupManager.getWorldsHolder()
    23. .getWorldPermissions(base);
    24. if (handler == null) {
    25. return null;
    26. }
    27. return handler.getGroup(base.getName());
    28. }
    29. }

    So doing if (getGroup(player) == getGroup(targetPlayer) should work i think

    EDIT: This doesn't work :( any help please?

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

    samosaara

    Sorry if it sounds like hater comment, but why not use vault instead? You have nothing to loose Vault is more stable, easier, and compatible with GroupManager, AND PEX, AND bPermissions AND other permissions systems why hook directly on group manager?
    VaultAPI: https://github.com/MilkBowl/VaultAPI
     
  12. Offline

    TinyTom38

    I have no idea how to do that
     
  13. Offline

    samosaara

    You using the GroupManager API to get the player group
    Code:java
    1. if (getGroup(player) == getGroup(targetPlayer))

    the method getGroup(); from the group manager User class
    import org.anjocaido.groupmanager.data.User
    you cloud use the getPrimaryGroup(); method from the Vault Permissions class -- How to? 2 ways, if you have maven at your project Add the Vault dependency and repository to your pom.xml if you don't use maven you can download the vault jar from Vault bukkit dev Page and add vault for your project class path (like you did to bukkit). And them just follow the instructions how to hook on the vault bukkit dev page so your main class will basically look like something like this:
    Code:java
    1. package com.example.plugin;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import net.milkbowl.vault.chat.Chat;
    6. import net.milkbowl.vault.economy.Economy;
    7. import net.milkbowl.vault.economy.EconomyResponse;
    8. import net.milkbowl.vault.permission.Permission;
    9.  
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.plugin.RegisteredServiceProvider;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class ExamplePlugin extends JavaPlugin {
    17.  
    18. private static final Logger log = Logger.getLogger("Minecraft");
    19. public static Economy econ = null;
    20. public static Permission perms = null;
    21. public static Chat chat = null;
    22.  
    23. @Override
    24. public void onDisable() {
    25. log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion()));
    26. }
    27.  
    28. @Override
    29. public void onEnable() {
    30. setupPermissions();
    31. }
    32.  
    33. private boolean setupPermissions() {
    34. RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
    35. perms = rsp.getProvider();
    36. return perms != null;
    37. }
    38. }

    And when you need to get the group you use the vault method instead, like in line 13, it is:
    if (getGroup(player) == getGroup(targetPlayer))
    switch to:
    if (plugin.perms.getPrimaryGroup(player) == plugin.perms.getPrimaryGroup(player))
    Use the getPrimaryGroup(player) for group queries and if you need any other group operation check the github! https://github.com/MilkBowl/VaultAPI
     
  14. Offline

    TinyTom38


    Omg thanks <3 I've got it working now :)
     
Thread Status:
Not open for further replies.

Share This Page