Solved Overriding class from different plugin

Discussion in 'Plugin Development' started by brord, Nov 8, 2012.

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

    brord

    How to add/edit a factions command without editing facitons:

    The main plugin class:
    Code:java
    1.  
    2. package net.castegaming.plugins.CasteFactions;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.plugin.java.JavaPlugin;
    6. import com.massivecraft.factions.P;
    7. import com.massivecraft.factions.cmd.FCommand;
    8.  
    9. public class CasteFactions extends JavaPlugin {
    10.  
    11. public static CasteFactions plugin;
    12.  
    13. public void onEnable()
    14. {
    15. P factions = (P)Bukkit.getPluginManager().getPlugin("Factions");
    16. if (factions == null )
    17. {
    18. Bukkit.getLogger().severe("[CasteFactions] Factions has not been found. Disabling plugin");
    19. getServer().getPluginManager().disablePlugin(plugin);
    20. }
    21. else
    22. {
    23. Bukkit.getLogger().info("[CasteFactions] Factions has been found. Enabling plugin");
    24. try
    25. {
    26. FCommand cmdBase = factions.cmdBase;
    27. cmdBase.subCommands.remove(factions.cmdBase.cmdInvite); //you remove the old command here
    28. factions.cmdBase.cmdInvite = new FactionsInviteExtender(); //Links CmdInvite to FactionsInviteExtender() class
    29. cmdBase.addSubCommand(factions.cmdBase.cmdInvite); //You add the new CmdInvite command here
    30. Bukkit.getLogger().info("[CasteFactions] Tried to link with CmdInvite");
    31. }
    32. catch (Exception e)
    33. {
    34. e.printStackTrace();
    35. }
    36. }
    37. }
    38.  
    39. public void onDisable(){
    40. }
    41. }
    42.  


    The new (CmdInvite in my case) class:
    Code:java
    1.  
    2. package net.castegaming.plugins.CasteFactions;
    3.  
    4. import org.bukkit.Bukkit;
    5.  
    6. import com.massivecraft.factions.cmd.*;
    7. import com.massivecraft.factions.Conf;
    8. import com.massivecraft.factions.FPlayer;
    9. import com.massivecraft.factions.struct.FPerm;
    10.  
    11. public class FactionsInviteExtender extends CmdInvite
    12. {
    13. @Override
    14. public void perform()
    15. {
    16. FPlayer you = this.argAsBestFPlayerMatch(0);
    17. if (you == null) return;
    18.  
    19.  
    20. if (myFaction.getFPlayers().size() >= 1 && !fme.getPlayer().hasPermission("factions.unlimitedinvites")){
    21. msg("You do not have the permission to have more then 5 players in your faction :(");
    22. return;
    23. }
    24.  
    25. if (you.getFaction() == myFaction)
    26. {
    27. msg("%s<i> is already a member of %s", you.getName(), myFaction.getTag());
    28. msg("<i>You might want to: " + p.cmdBase.cmdKick.getUseageTemplate(false));
    29. return;
    30. }
    31.  
    32. if (fme != null && ! FPerm.INVITE.has(fme, myFaction)) return;
    33.  
    34. // if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay
    35. if ( ! payForCommand(Conf.econCostInvite, "to invite someone", "for inviting someone")) return;
    36.  
    37. myFaction.invite(you);
    38.  
    39.  
    40. you.msg("%s<i> invited you to %s", fme.describeTo(you, true), myFaction.describeTo(you));
    41. myFaction.msg("%s<i> invited %s<i> to your faction.", fme.describeTo(myFaction, true), you.describeTo(myFaction));
    42. }
    43. }
    44.  
    45.  


    ALL credits for this goes to fireblast709, Love you man <3
    kwek20 or brord
     
  2. Offline

    fireblast709

    How about:
    Code:java
    1. P factions = (P)Bukkit.getPluginManager().getPlugin("Factions");
    2. if(factions != null)
    3. {
    4. factions.cmdBase.cmdInvite = yournewclass;
    5. }


    And to be sure, let your class extend the CmdInvite class from the plugin (using it as a library for the import) to prevent any ClassCastException in case the plugin casts it
     
  3. Offline

    brord


    Thanks!
    tried, probably forgot something really stupid, (or you gave psuedo-code)
    Code:
    factions.cmdBase.cmdInvite = FactionsInviteExtender;
    Which gave me: "FactionsInviteExtender cannot be resolved to a variable"
    I have created a class called FactionsInviteExtender.

    ALso, in that class should i do this?
    Code:
    public class FactionsInviteExtender extends FCommand
    {
        public FactionsInviteExtender()
     
            ...
    }
    Because it used to be:
    Code:
    public class CmdInvite extends  FCommand
    {
        public CmdInvite()
     
            ...
    }
    Or should i extend it to CmdInvite?
     
  4. Offline

    fireblast709

    Because the command is an extension of the CmdInvite, I would recommend extending that, and overriding what is needed to override. Also: my bad, you have to assign it to an instance of the class xD
     
  5. Offline

    SirTyler

    That is because you are giving it just the class name, not an object; change it to
    Code:
    factions.cmdBase.cmdInvite = new FactionsInviteExtender();
     
  6. Offline

    brord

    Thanks for that part, im still a noob as you can see :)

    But its asking for this method:
    Code:
    public static CmdInvite FactionsInviteExtender() {
            // TODO Auto-generated method stub
            return null;
        }
    Im sure the return type links to something, but as long as it needs to be there, sshit wont work :p

    (this is my plugins main class)
    Code:
    package net.castegaming.plugins.CasteFactions;
     
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.massivecraft.factions.P;
    import com.massivecraft.factions.cmd.CmdInvite;
     
    public class CasteFactions extends JavaPlugin {
       
        public static CasteFactions plugin;
       
        public void onEnable(){
            P factions = (P)Bukkit.getPluginManager().getPlugin("Factions");
            if (factions == null  ){
                Bukkit.getLogger().severe("[CasteFactions] Factions has not been found. Disabling plugin");
                getServer().getPluginManager().disablePlugin(plugin);
            } else {
                Bukkit.getLogger().info("[CasteFactions] Factions has been found. Enabling plugin");
               
                factions.cmdBase.cmdInvite = FactionsInviteExtender.FactionsInviteExtender();
            }
        }
       
        public void onDisable(){
        }
    }
    (error: "The method FactionsInviteExtender() is undefined for the type FactionsInviteExtender")

    kwek or brord
     
  7. Offline

    brord

    SirTyler
    fireblast709

    Oookay, so i have this now:

    Code:
    package net.castegaming.plugins.CasteFactions;
     
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.massivecraft.factions.P;
     
    public class CasteFactions extends JavaPlugin {
     
    public static CasteFactions plugin;
     
    public void onEnable(){
    P factions = (P)Bukkit.getPluginManager().getPlugin("Factions");
    if (factions == null ){
    Bukkit.getLogger().severe("[CasteFactions] Factions has not been found. Disabling plugin");
    getServer().getPluginManager().disablePlugin(plugin);
    } else {
    Bukkit.getLogger().info("[CasteFactions] Factions has been found. Enabling plugin");
    factions.cmdBase.cmdInvite = new FactionsInviteExtender();
    }
    }
     
    public void onDisable(){
    }
    }
    And the class "FactionsInviteExtender":

    Code:
    package net.castegaming.plugins.CasteFactions;
     
    import org.bukkit.Bukkit;
     
    import com.massivecraft.factions.cmd.*;
    import com.massivecraft.factions.Conf;
    import com.massivecraft.factions.FPlayer;
    import com.massivecraft.factions.struct.FPerm;
     
    public class FactionsInviteExtender extends CmdInvite
    {
    @Override
    public void perform()
    {
    Bukkit.broadcastMessage("Reached modified CmdInvite");
    FPlayer you = this.argAsBestFPlayerMatch(0);
    if (you == null) return;
     
     
    if (myFaction.getFPlayers().size() >= 5 && !fme.getPlayer().hasPermission("factions.unlimitedinvites")){
    msg("You do not have the permission to have more then 5 players in your faction :(");
    return;
    }
     
    if (you.getFaction() == myFaction)
    {
    msg("%s<i> is already a member of %s", you.getName(), myFaction.getTag());
    msg("<i>You might want to: " + p.cmdBase.cmdKick.getUseageTemplate(false));
    return;
    }
     
    if (fme != null && ! FPerm.INVITE.has(fme, myFaction)) return;
     
    // if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay
    if ( ! payForCommand(Conf.econCostInvite, "to invite someone", "for inviting someone")) return;
     
    myFaction.invite(you);
     
     
    you.msg("%s<i> invited you to %s", fme.describeTo(you, true), myFaction.describeTo(you));
    myFaction.msg("%s<i> invited %s<i> to your faction.", fme.describeTo(myFaction, true), you.describeTo(myFaction));
    }
    }
    
    No compile errors at all, runs fine.
    But as you cna see, I have added a test message.
    Once i run the /f invite [name] command, jsut the normal stuff happends.
    I do not get the message "Reached modified CmdInvite".
    Which means, it still goes to the real CmdInvite instead of my class.

    Also, can you tell me how you make that java code area? [CODE:JAVA] doesnt work, and neither does [JAVA] :(

    Anyone knows how i can do this without editing factions?
    Thansk a lot for reading/helping :)

    Bump

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

    Hoolean

    brord

    Putting in a code tag so it doesn't actually do it:

    Code:
    [syntax=java]/* Java code here */[/syntax]
     
  9. Offline

    brord

    Ok, so no-one knows how to make factions know that it should use my version of CmdInvite (FactionsInviteExtender), instead of the real class?
     
  10. Offline

    HON95

    Maybe Factions assigned factions.cmdBase.cmdInvite to the real CmdInvite instance after you assigned it to your version? Which might be the case if your plugin got enabled before Factions.

    EDIT: Have you added Factions as a dependency in your plugin.yml?
     
  11. Offline

    brord

    I have added a try/catch thingy to test, still rusn fine
    Code:java
    1.  
    2. public void onEnable(){
    3. P factions = (P)Bukkit.getPluginManager().getPlugin("Factions");
    4. if (factions == null ){
    5. Bukkit.getLogger().severe("[CasteFactions] Factions has not been found. Disabling plugin");
    6. getServer().getPluginManager().disablePlugin(plugin);
    7. } else {
    8. Bukkit.getLogger().info("[CasteFactions] Factions has been found. Enabling plugin");
    9. try {
    10. factions.cmdBase.cmdInvite = new FactionsInviteExtender();
    11. Bukkit.getLogger().info("[CasteFactions] Tried to link with CmdInvite");
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. }
    15. }
    16. }
    17.  


    But still nothing :(
    And yes, i have the depend: [Factions], AND it starts after factions

    :S

    I think you guys are thinking too difficult, and is it just a basic hook which i forgot.
    But since i dont know waht todo, i wont notice that :(
     
  12. Offline

    fireblast709

    I will look into this later, as it is too hard to do in a few secs. It probably has something to do with the this.addSubCommand() function
     
  13. Offline

    brord

    Oooh, its something with factions, and not the way i try to override it...
    Explains.
    I will take a look in the FactionsPlus code (still love to hear our try/answer)

    HMmm factionsPlus just recreates factions, so it checks for its own commands, listeners etc.
    That wont help me, because im not going to do that ever..
    Then i will just place a class INSIDE factions :p

    help me @fireblast709 <3 you're my last hope

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  14. Offline

    HON95

    The constructor of the FCmdRoot class calls the method addSubCommand(MCommand<?> subCommand) in its ancestor class MCommand with the cmdInvite var as the parameter. I don't know if calling that method yourself with your version of CmdInvite would work or if it would just mess things up, but you could try I guess.

    What the relevant part of class MCommand looks like
    Code:JAVA
    1. public List<MCommand<?>> subCommands;
    2. public List<MCommand<?>> commandChain = new ArrayList();
    3.  
    4. public void addSubCommand(MCommand<?> subCommand)
    5. {
    6. subCommand.commandChain.addAll(this.commandChain);
    7. subCommand.commandChain.add(this);
    8. this.subCommands.add(subCommand);
    9. }


    Couldn't you just ask one of the Factions devs?
     
  15. Offline

    fireblast709

    brord As I pointed out, and HON95 specified a bit:
    Code:java
    1. public void onEnable()
    2. {
    3. P factions = (P)Bukkit.getPluginManager().getPlugin("Factions");
    4. if (factions == null )
    5. {
    6. Bukkit.getLogger().severe("[CasteFactions] Factions has not been found. Disabling plugin");
    7. getServer().getPluginManager().disablePlugin(plugin);
    8. }
    9. else
    10. {
    11. Bukkit.getLogger().info("[CasteFactions] Factions has been found. Enabling plugin");
    12. try
    13. {
    14. FCommand cmdBase = factions.cmdBase;
    15. cmdBase.subCommands.remove(factions.cmdBase.cmdInvite);
    16. factions.cmdBase.cmdInvite = new FactionsInviteExtender();
    17. cmdBase.addSubCommand(factions.cmdBase.cmdInvite);
    18. Bukkit.getLogger().info("[CasteFactions] Tried to link with CmdInvite");
    19. }
    20. catch (Exception e)
    21. {
    22. e.printStackTrace();
    23. }
    24. }
    25. }

    How about this? (removing the old command and adding it again). Still no idea if it works, but it is worth a try right ;3
     
    brord likes this.
  16. Offline

    brord

    wow


    !!!!



    You saved my day/week :)
    I never saw the .remove part, wasnt looking at that sort of things.
    Now I see it, i get what @HON95 was trying to say!
    Thanks to you for Helping!

    For those wondering, I have edited the main post with the working classes
    Thanks again fire! I will defenitely take a look at how it works, and how I can add more things to it.

    IF you ever visit my server (www.castegaming.net) You will get a special rank :)

    kwek20 or brord
     
  17. Offline

    Retherz_

  18. Offline

    fireblast709

    ['syntax=java]['/syntax], without the '
     
Thread Status:
Not open for further replies.

Share This Page