[UNSOLVED] Tweet on ban.

Discussion in 'Plugin Development' started by XxZHALO13Xx, Oct 1, 2014.

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

    XxZHALO13Xx

    Assist when i execute the command nothing happens

    Assist
    Code:java
    1. package me.XxZHALO13Xx.Test;
    2.  
    3. import org.apache.commons.lang3.StringUtils;
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. /**
    12. * Created by ZH on 10/4/2014.
    13. */
    14. public class Core extends JavaPlugin implements Listener {
    15.  
    16. private static Tweeter tweeter;
    17.  
    18. public void onEnable(){
    19. tweeter = new Tweeter("%%%%%", "[USER=90712312]@@@@[/USER]@@@");
    20. }
    21.  
    22. public static Tweeter getTweeter(){
    23. return tweeter;
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    27. if (cmd.getName().equalsIgnoreCase("ban")) {
    28. if (args.length > 0) {
    29. Player player = Bukkit.getPlayerExact(args[0]);
    30.  
    31. if (player == null) {
    32. sender.sendMessage("Can't find player: " + args[0]);
    33. return true;
    34. }
    35.  
    36. String reason = null;
    37.  
    38. if (args.length > 1) {
    39. reason = StringUtils.join(args, " ", 1, args.length);
    40. }
    41.  
    42. if (reason != null) {
    43. try {
    44. tweeter.tweet(player.getName() + " was banned for: " + reason);
    45. } catch (Exception e) {
    46. e.printStackTrace();
    47. }
    48. } else {
    49. try {
    50. tweeter.tweet(player.getName() + " was banned.");
    51. } catch (Exception e) {
    52. e.printStackTrace();
    53. }
    54. }
    55. }
    56. }
    57. return false;
    58. }
    59. }
    60.  


    Code:java
    1. package me.XxZHALO13Xx.Test;
    2.  
    3. import com.gargoylesoftware.htmlunit.BrowserVersion;
    4. import com.gargoylesoftware.htmlunit.WebClient;
    5. import com.gargoylesoftware.htmlunit.html.*;
    6. import com.mysql.jdbc.log.LogFactory;
    7.  
    8. import java.util.logging.Level;
    9.  
    10.  
    11. public class Tweeter {
    12.  
    13. private WebClient webClient;;
    14.  
    15. private String username;
    16. private String password;
    17.  
    18. private boolean loggedIn = false;
    19.  
    20. public Tweeter(String username, String password) {
    21. webClient = new WebClient(BrowserVersion.CHROME);
    22. webClient.getOptions().setThrowExceptionOnScriptError(false);
    23. webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    24. webClient.getOptions().setPrintContentOnFailingStatusCode(false);
    25. webClient.getOptions().setJavaScriptEnabled(true);
    26. webClient.getOptions().setRedirectEnabled(true);
    27. webClient.getOptions().setCssEnabled(true);
    28. webClient.getOptions().setUseInsecureSSL(true);
    29. webClient.getCookieManager().setCookiesEnabled(true);
    30.  
    31. org.apache.commons.logging.LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
    32. java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
    33. java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
    34. new Shutit(webClient);
    35.  
    36. this.username = username;
    37. this.password = password;
    38. }
    39.  
    40. public boolean login() throws Exception {
    41. HtmlPage page = webClient.getPage("[URL]https://mobile.twitter.com/login[/URL]");
    42.  
    43. final HtmlForm form = page.getForms().get(0);
    44. final HtmlTextInput userField = form.getInputByName("username");
    45. final HtmlPasswordInput passField = form.getInputByName("password");
    46.  
    47. userField.setValueAttribute(this.username);
    48. passField.setValueAttribute(this.password);
    49.  
    50. final HtmlSubmitInput button = form.getInputByValue("Sign in");
    51. final HtmlPage resultPage = button.click();
    52.  
    53. webClient.closeAllWindows();
    54.  
    55. String title = resultPage.getUrl().toExternalForm();
    56.  
    57. loggedIn = true;
    58. return title.equals("[URL]https://mobile.twitter.com/[/URL]");
    59. }
    60.  
    61. public boolean tweet(String text) throws Exception {
    62. if (!loggedIn || text.length() > 140 || text.length() < 1) {
    63. return false;
    64. }
    65.  
    66. HtmlPage page = webClient.getPage("[URL]https://mobile.twitter.com/compose/tweet[/URL]");
    67.  
    68. final HtmlForm form = page.getForms().get(0);
    69. final HtmlTextArea userField = form.getTextAreaByName("tweet[text]");
    70.  
    71. userField.setText(text);
    72.  
    73. final HtmlSubmitInput button = form.getInputByValue("Tweet");
    74. final HtmlPage resultPage = button.click();
    75.  
    76. webClient.closeAllWindows();
    77.  
    78. String title = resultPage.getUrl().toExternalForm();
    79. return title.equals("[URL]https://mobile.twitter.com/[/URL]");
    80. }
    81. }


    Code:java
    1. package me.XxZHALO13Xx.Test;
    2.  
    3. import java.net.MalformedURLException;
    4. import java.net.URL;
    5.  
    6. import org.w3c.css.sac.CSSException;
    7. import org.w3c.css.sac.CSSParseException;
    8. import org.w3c.css.sac.ErrorHandler;
    9.  
    10. import com.gargoylesoftware.htmlunit.IncorrectnessListener;
    11. import com.gargoylesoftware.htmlunit.ScriptException;
    12. import com.gargoylesoftware.htmlunit.WebClient;
    13. import com.gargoylesoftware.htmlunit.html.HtmlPage;
    14. import com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener;
    15.  
    16. public class Shutit {
    17.  
    18. WebClient webClient = null;
    19.  
    20. public Shutit(WebClient client) {
    21. webClient = client;
    22. call();
    23. }
    24.  
    25. private void call() {
    26. webClient.setIncorrectnessListener(new IncorrectnessListener() {
    27.  
    28. @Override
    29. public void notify(String arg0, Object arg1) {
    30. // TODO Auto-generated method stub
    31.  
    32. }
    33. });
    34.  
    35. webClient.setCssErrorHandler(new ErrorHandler() {
    36.  
    37. @Override
    38. public void warning(CSSParseException exception) throws CSSException {
    39. // TODO Auto-generated method stub
    40.  
    41. }
    42.  
    43. @Override
    44. public void fatalError(CSSParseException exception) throws CSSException {
    45. // TODO Auto-generated method stub
    46.  
    47. }
    48.  
    49. @Override
    50. public void error(CSSParseException exception) throws CSSException {
    51. // TODO Auto-generated method stub
    52.  
    53. }
    54. });
    55.  
    56. webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {
    57.  
    58. @Override
    59. public void timeoutError(HtmlPage arg0, long arg1, long arg2) {
    60. // TODO Auto-generated method stub
    61.  
    62. }
    63.  
    64. @Override
    65. public void scriptException(HtmlPage arg0, ScriptException arg1) {
    66. // TODO Auto-generated method stub
    67.  
    68. }
    69.  
    70. @Override
    71. public void loadScriptError(HtmlPage arg0, URL arg1, Exception arg2) {
    72. // TODO Auto-generated method stub
    73.  
    74. }
    75.  
    76. @Override
    77. public void malformedScriptURL(HtmlPage arg0, String arg1, MalformedURLException arg2) {
    78. // TODO Auto-generated method stub
    79.  
    80. }
    81. });
    82. }
    83. }
    84.  


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

    Assist

    XxZHALO13Xx
    Yeah sorry, I edited my post earlier, you probably didn't see it. You haven't logged in yet. In the onEnable, do this
    Code:java
    1. tweeter = new Tweeter("user", "pass");
    2.  
    3. try {
    4. tweeter.login();
    5. } catch (Exception e) {
    6. e.printStackTrace();
    7. }
     
  3. Offline

    XxZHALO13Xx

    Assist still nothing D: lol
     
  4. Offline

    Assist

    XxZHALO13Xx
    Well I'd assume it's because you either have another plugin with the command "ban", or you haven't added the command to your plugin.yml. As I said earlier, you have to find your own way to detect the ban. If you want to check if my code even works, you can just call tweeter.tweet("test") in the onEnable after you've logged in.
     
  5. Offline

    XxZHALO13Xx

    Assist
    PHP:
    [14:44:53 INFO]: [BanTweetEnabling BanTweet v1.0
    [14:44:53 WARN]: java.lang.NullPointerException
    [14:44:53 WARN]:        at me.XxZHALO13Xx.Test.Core.onEnable(Core.java:22)
    [
    14:44:53 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
    in
    .java:250)
    [
    14:44:53 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(
    JavaPluginLoader.java:324)
    [
    14:44:53 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(Si
    mplePluginManager
    .java:404)
    [
    14:44:53 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin
    (CraftServer.java:448)
    [
    14:44:53 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlug
    ins
    (CraftServer.java:382)
    [
    14:44:53 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.reload(Cra
    ftServer
    .java:801)
    [
    14:44:53 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:288)
    [
    14:44:53 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(Rel
    oadCommand
    .java:23)
    [
    14:44:53 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap
    .java:180)
    [
    14:44:53 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCo
    mmand
    (CraftServer.java:703)
    [
    14:44:53 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchSe
    rverCommand
    (CraftServer.java:690)
    [
    14:44:53 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.aB(Dedic
    atedServer
    .java:296)
    [
    14:44:53 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.v(Dedica
    tedServer
    .java:261)
    [
    14:44:53 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.u(Minecr
    aftServer
    .java:558)
    [
    14:44:53 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.run(Mine
    craftServer
    .java:469)
    [
    14:44:53 WARN]:        at net.minecraft.server.v1_7_R3.ThreadServerApplication.
    run(SourceFile:628)
    [
    14:44:54 ERROR]: Error occurred while enabling BanTweet v1.0 (Is it up to date?
    )
    java.lang.ExceptionInInitializerError
            at org
    .cyberneko.html.HTMLScanner.scanEntityRef(HTMLScanner.java:1415) ~
    [?:?]
            
    at org.cyberneko.html.HTMLScanner$ContentScanner.scanAttribute(HTMLScann
    er
    .java:3025) ~[?:?]
            
    at org.cyberneko.html.HTMLScanner$ContentScanner.scanAttribute(HTMLScann
    er
    .java:2851) ~[?:?]
            
    at org.cyberneko.html.HTMLScanner$ContentScanner.scanStartElement(HTMLSc
    anner
    .java:2700) ~[?:?]
            
    at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2
    110
    ) ~[?:?]
            
    at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:920) ~[?
    :?]
            
    at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:499
    ) ~[?:?]
            
    at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:452
    ) ~[?:?]
            
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) ~[?:?]
            
    at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.pars
    e
    (HTMLParser.java:965) ~[?:?]
            
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:2
    47
    ) ~[?:?]
            
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.ja
    va
    :193) ~[?:?]
            
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(Defau
    ltPageCreator
    .java:268) ~[?:?]
            
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPa
    geCreator
    .java:156) ~[?:?]
            
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient
    .java:468) ~[?:?]
            
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:342) ~
    [?:?]
            
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:407) ~
    [?:?]
            
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:392) ~
    [?:?]
            
    at me.XxZHALO13Xx.Test.Tweeter.login(Tweeter.java:41) ~[?:?]
            
    at me.XxZHALO13Xx.Test.Core.onEnable(Core.java:28) ~[?:?]
            
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:250) ~[c
    raftbukkit
    .jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:324) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r
    .java:404) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin(CraftServer.jav
    a
    :448) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlugins(CraftServer.
    java:382) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.reload(CraftServer.java:80
    1
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.Bukkit.reload(Bukkit.java:288) [craftbukkit.jar:git-Bukkit
    -1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:
    23) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r
    .java:703) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchServerCommand(Craf
    tServer
    .java:690) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.DedicatedServer.aB(DedicatedServer.java:
    296) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    61
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
    Caused byjava.lang.NullPointerException
            at java
    .util.Properties$LineReader.readLine(Unknown Source) ~[?:1.7.0_51
    ]
            
    at java.util.Properties.load0(Unknown Source) ~[?:1.7.0_51]
            
    at java.util.Properties.load(Unknown Source) ~[?:1.7.0_51]
            
    at org.cyberneko.html.HTMLEntities.load0(HTMLEntities.java:101) ~[?:?]
            
    at org.cyberneko.html.HTMLEntities.<clinit>(HTMLEntities.java:53) ~[?:?]
     
            ... 
    36 more
    Assist
    Code:java
    1. package me.XxZHALO13Xx.Test;
    2.  
    3. import me.XxZHALO13Xx.Test.Banner.Banner;
    4. import org.apache.commons.lang3.StringUtils;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. /**
    13. * Created by ZH on 10/4/2014.
    14. */
    15. public class Core extends JavaPlugin implements Listener {
    16.  
    17. private static Tweeter tweeter;
    18.  
    19. public void onEnable(){
    20. getCommand("kick").setExecutor(new Banner());
    21. try {
    22. tweeter.tweet("Test");
    23. } catch (Exception e) {
    24. e.printStackTrace();
    25. }
    26. tweeter = new Tweeter("%%%%%%", "%%%%%%%");
    27. try {
    28. tweeter.login();
    29. } catch (Exception e) {
    30. e.printStackTrace();
    31. }
    32. }
    33.  
    34. public static Tweeter getTweeter(){
    35. return tweeter;
    36. }
    37.  
    38. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    39. if (cmd.getName().equalsIgnoreCase("ban")) {
    40. if (args.length > 0) {
    41. Player player = Bukkit.getPlayerExact(args[0]);
    42.  
    43. if (player == null) {
    44. sender.sendMessage("Can't find player: " + args[0]);
    45. return true;
    46. }
    47.  
    48. String reason = null;
    49.  
    50. if (args.length > 1) {
    51. reason = StringUtils.join(args, " ", 1, args.length);
    52. }
    53.  
    54. if (reason != null) {
    55. try {
    56. tweeter.tweet(player.getName() + " was banned for: " + reason);
    57. } catch (Exception e) {
    58. e.printStackTrace();
    59. }
    60. } else {
    61. try {
    62. tweeter.tweet(player.getName() + " was banned.");
    63. } catch (Exception e) {
    64. e.printStackTrace();
    65. }
    66. }
    67. }
    68. }
    69. return false;
    70. }
    71. }
    72.  


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

    Assist

    XxZHALO13Xx
    You need to call tweeter.tweet() after you've initialized the variable and logged in.
     
  7. Offline

    XxZHALO13Xx

    Assist i did that and nothing... can u tryt adn make the plugin then send me it..? ive tried to make this forever.. over 2 months lol no jokle
     
  8. Offline

    madaeq1234

    Hey well i was just asking not saying use facebook.... So... Does anybody know where a facebook one is?
     
  9. Offline

    Regablith

    [quote uid=90954848 name="madaeq1234" post=2833399]Hey well i was just asking not saying use facebook.... So... Does anybody know where a facebook one is?[/quote]

    Try this: <Edit by Moderator: Redacted bit url>
     
    Last edited by a moderator: Feb 11, 2017
  10. Offline

    madaeq1234

  11. Offline

    ChipDev

    If it is not solved, it is unsolved. no need for the tag.
     
  12. Offline

    rtainc

    I would suggest basic OOP and Java knowledge before trying to use APIs; especially Bukkit. From what you have demonstrated, it seems you should try and take it one step at the time.
     
Thread Status:
Not open for further replies.

Share This Page