[UNSOLVED] Tweet on ban.

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

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

    XxZHALO13Xx

    I need help making a plugin that when a player is banned it posts a tweet that looks like The player PLAYERNAME has been banned by PLAYERNAME for TIMEPERIOD in the reasoning of REASON

    How would i do this. Please list and guide me through all the steps. Thanks so much. I've tried to do this for a long time and i cant figure it out. Thanks for ur helping

    I do know i need to use a java twitter api like twitter4j but i dont know how to static link it or something
     
  2. Offline

    XxZHALO13Xx

    Assist ok and how does one use it lol
     
  3. Offline

    xTigerRebornx

  4. Offline

    XxZHALO13Xx

  5. Offline

    mrCookieSlime

    XxZHALO13Xx
    There is a detailed explanation on that Page, also providing you with examples...
    Just read the ReadMe...
     
  6. Offline

    XxZHALO13Xx

    mrCookieSlime how would i make it post a tweet.. i cant find that part. or how to download
     
  7. Offline

    mrCookieSlime

  8. Offline

    XxZHALO13Xx

    mrCookieSlime how do i download the thing... im very confused on the download and importing... I don't use maven
     
  9. Offline

    Skionz

    XxZHALO13Xx This is forum is for help with the bukkit api not twitters api.
     
  10. Offline

    XxZHALO13Xx

    Skionz um this is for bukkit.........
     
  11. Offline

    RainoBoy97

    You are asking for help with the Twitter API, not Bukkit.
     
    Skionz likes this.
  12. Offline

    madaeq1234

    Anyone got one for FB
     
  13. Offline

    Gamesareme

    XxZHALO13Xx To download go to the GitHub page, and look for the "download zip" located on the right of your screen. Hope that helps.
     
  14. Offline

    rbrick

  15. Offline

    XxZHALO13Xx

    RainoBoy97 madaeq1234 Gamesareme rbrick like i wanna use twitter4j... i get errors

    heres the test class
    Code:java
    1. package me.XxZHALO13Xx.Test;
    2.  
    3.  
    4. import twitter4j.Status;
    5. import twitter4j.Twitter;
    6. import twitter4j.TwitterException;
    7. import twitter4j.TwitterFactory;
    8. import twitter4j.auth.AccessToken;
    9. import twitter4j.auth.RequestToken;
    10. import twitter4j.conf.ConfigurationBuilder;
    11.  
    12. import javax.swing.*;
    13. import java.io.BufferedReader;
    14. import java.io.IOException;
    15. import java.io.InputStreamReader;
    16. import java.net.URI;
    17.  
    18. /**
    19. * Created by ZH on 10/1/2014.
    20. */
    21. public class TestClass {
    22.  
    23.  
    24.  
    25. public static void main(String args[]) throws Exception {
    26. // The factory instance is re-useable and thread safe.
    27. Twitter twitter = new TwitterFactory().getInstance();
    28.  
    29. //insert the appropriate consumer key and consumer secret here
    30.  
    31. twitter.setOAuthConsumer("-----------------",
    32. "------------------");
    33. RequestToken requestToken = twitter.getOAuthRequestToken("[url]https://api.twitter.com/1.1/[/url]");
    34. AccessToken accessToken = null;
    35. while (null == accessToken) {
    36. System.out.println("Open the following URL and grant access to your account:");
    37. System.out.println(requestToken.getAuthorizationURL());
    38. System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
    39. String pin = br.readLine();
    40. try {
    41.  
    42.  
    43. if (pin.length() > 0) {
    44. accessToken = twitter.getOAuthAccessToken(requestToken, pin);
    45. } else {
    46. accessToken = twitter.getOAuthAccessToken();
    47. }
    48. } catch (TwitterException te) {
    49. if (401 == te.getStatusCode()) {
    50. System.out.println("Unable to get the access token.");
    51. } else {
    52. te.printStackTrace();
    53. }
    54. }
    55. }
    56. //persist to the accessToken for future reference.
    57. System.out.println(twitter.verifyCredentials().getId());
    58. System.out.println("token : " + accessToken.getToken());
    59. System.out.println("tokenSecret : " + accessToken.getTokenSecret());
    60. //storeAccessToken(twitter.verifyCredentials().getId() , accessToken);
    61. Status status = twitter.updateStatus(args[0]);
    62. System.out.println("Successfully updated the status to [" + status.getText() + "].");
    63. System.exit(0);
    64. }
    65. }
    66.  



    heres the error

    PHP:
    "C:\Program Files\Java\jdk1.7.0_45\bin\java" -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 13.1.2\bin" -Dfile.encoding=UTF--classpath "C:\Program Files\Java\jdk1.7.0_45\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_45\jre\lib\ext\zipfs.jar;C:\Users\ZH\IdeaProjects\Banner\out\production\Banner;C:\Users\ZH\Desktop\Plugins\Server\craftbukkit.jar;C:\Users\ZH\Desktop\twitter4j-core-4.0.2.jar;Z:\Downloads\Twitter\twitter4j-appengine-4.0.2.jar;Z:\Downloads\Twitter\twitter4j-async-4.0.2.jar;Z:\Downloads\Twitter\twitter4j-examples-4.0.2.jar;Z:\Downloads\Twitter\twitter4j-media-support-4.0.2.jar;Z:\Downloads\Twitter\twitter4j-spdy-support-4.0.2.jar;Z:\Downloads\Twitter\twitter4j-stream-4.0.2.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 13.1.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain me.XxZHALO13Xx.Test.TestClass
    Exception in thread 
    "main" java.lang.NoClassDefFoundErrorcom/google/appengine/api/urlfetch/HTTPRequest
        at twitter4j
    .AlternativeHttpClientImpl.handleRequest(AlternativeHttpClientImpl.java:43)
        
    at twitter4j.HttpClientBase.request(HttpClientBase.java:53)
        
    at twitter4j.HttpClientBase.post(HttpClientBase.java:82)
        
    at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:107)
        
    at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:92)
        
    at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:263)
        
    at me.XxZHALO13Xx.Test.TestClass.main(TestClass.java:33)
        
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        
    at java.lang.reflect.Method.invoke(Method.java:606)
        
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
    Caused byjava.lang.ClassNotFoundExceptioncom.google.appengine.api.urlfetch.HTTPRequest
        at java
    .net.URLClassLoader$1.run(URLClassLoader.java:366)
        
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        
    at java.security.AccessController.doPrivileged(Native Method)
        
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        ... 
    12 more
     
    Process finished with 
    exit code 1
     
  16. Offline

    MajorSkillage

    Code:
                AccessToken accessToken = null;
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                while (null == accessToken)
    wouldn't accessToken = null and while(null == accessToken) so pretty much while accessToken is null it will spam the hell out of you, i suggest using a for loop :I Ive had better experience with them
     
  17. Offline

    XxZHALO13Xx

  18. Offline

    MajorSkillage

    StringBuilder str = new StringBuilder();
    lookup how to use StringBuilders :D
     
  19. Offline

    XxZHALO13Xx

    Assist in tweeter it has a error
    LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

    getFactory() cannot be resolved to a method
     
  20. Offline

    XxZHALO13Xx

  21. XxZHALO13Xx
    Could you show me how you used it? I tested it and it worked completely fine. My code was really just an example, I wrote it in like 5 minutes because I was bored. You'd be better off using a real api, but mine works too.
     
  22. Offline

    XxZHALO13Xx



    package me.XxZHALO13Xx.Test;

    public class TestClass {

    public static void main(String[] args) {
    Tweeter tweeter = new Tweeter("----ontrol", "-----");

    boolean successfulLogin = false;

    try {
    successfulLogin = tweeter.login();
    } catch (Exception e) {
    }

    if (successfulLogin) {
    System.out.println("Logged in.");

    boolean successfulTweet = false;

    try {
    successfulTweet = tweeter.tweet("Test tweet.");
    } catch (Exception e) {
    }

    if (successfulTweet) {
    System.out.println("Tweeted");
    }
    }
    }
    }

    package me.XxZHALO13Xx.Test;

    import com.gargoylesoftware.htmlunit.BrowserVersion;
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.*;
    import com.mysql.jdbc.log.LogFactory;

    import java.util.logging.Level;


    public class Tweeter {

    private WebClient webClient;;

    private String username;
    private String password;

    private boolean loggedIn = false;

    public Tweeter(String username, String password) {
    webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setPrintContentOnFailingStatusCode(false);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setRedirectEnabled(true);
    webClient.getOptions().setCssEnabled(true);
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getCookieManager().setCookiesEnabled(true);

    org.apache.commons.logging.LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
    java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
    java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
    new Shutit(webClient);

    this.username = username;
    this.password = password;
    }

    public boolean login() throws Exception {
    HtmlPage page = webClient.getPage("https://mobile.twitter.com/login");

    final HtmlForm form = page.getForms().get(0);
    final HtmlTextInput userField = form.getInputByName("username");
    final HtmlPasswordInput passField = form.getInputByName("password");

    userField.setValueAttribute(this.username);
    passField.setValueAttribute(this.password);

    final HtmlSubmitInput button = form.getInputByValue("Sign in");
    final HtmlPage resultPage = button.click();

    webClient.closeAllWindows();

    String title = resultPage.getUrl().toExternalForm();

    loggedIn = true;
    return title.equals("https://mobile.twitter.com/");
    }

    public boolean tweet(String text) throws Exception {
    if (!loggedIn || text.length() > 140 || text.length() < 1) {
    return false;
    }

    HtmlPage page = webClient.getPage("https://mobile.twitter.com/compose/tweet");

    final HtmlForm form = page.getForms().get(0);
    final HtmlTextArea userField = form.getTextAreaByName("tweet[text]");

    userField.setText(text);

    final HtmlSubmitInput button = form.getInputByValue("Tweet");
    final HtmlPage resultPage = button.click();

    webClient.closeAllWindows();

    String title = resultPage.getUrl().toExternalForm();
    return title.equals("https://mobile.twitter.com/");
    }
    }

    package me.XxZHALO13Xx.Test;

    import java.net.MalformedURLException;
    import java.net.URL;

    import org.w3c.css.sac.CSSException;
    import org.w3c.css.sac.CSSParseException;
    import org.w3c.css.sac.ErrorHandler;

    import com.gargoylesoftware.htmlunit.IncorrectnessListener;
    import com.gargoylesoftware.htmlunit.ScriptException;
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;
    import com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener;

    public class Shutit {

    WebClient webClient = null;

    public Shutit(WebClient client) {
    webClient = client;
    call();
    }

    private void call() {
    webClient.setIncorrectnessListener(new IncorrectnessListener() {

    @Override
    public void notify(String arg0, Object arg1) {
    // TODO Auto-generated method stub

    }
    });

    webClient.setCssErrorHandler(new ErrorHandler() {

    @Override
    public void warning(CSSParseException exception) throws CSSException {
    // TODO Auto-generated method stub

    }

    @Override
    public void fatalError(CSSParseException exception) throws CSSException {
    // TODO Auto-generated method stub

    }

    @Override
    public void error(CSSParseException exception) throws CSSException {
    // TODO Auto-generated method stub

    }
    });

    webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {

    @Override
    public void timeoutError(HtmlPage arg0, long arg1, long arg2) {
    // TODO Auto-generated method stub

    }

    @Override
    public void scriptException(HtmlPage arg0, ScriptException arg1) {
    // TODO Auto-generated method stub

    }

    @Override
    public void loadScriptError(HtmlPage arg0, URL arg1, Exception arg2) {
    // TODO Auto-generated method stub

    }

    @Override
    public void malformedScriptURL(HtmlPage arg0, String arg1, MalformedURLException arg2) {
    // TODO Auto-generated method stub

    }
    });
    }
    }
     
  23. XxZHALO13Xx
    I see you have a lot to learn. The code in the Main class was just a usage example, obviously in a plugin you're not supposed to have the "public static void main(String[] args)" method. You want to run this on plugin enable:
    Code:
    Tweeter tweeter = new Tweeter("user", "pass");
     
    boolean successfulLogin = false;
     
    try {
    successfulLogin = tweeter.login();
    } catch (Exception e) {
    }
    and when someone is banned, use the tweeter.tweet() method.

    EDIT: You might want to delete or edit your post, we can see your twitter username and password.
     
  24. Offline

    XxZHALO13Xx

    Assist hank u so much that would have been really bad... and what would the whole class look like..? i cant think rn
     
  25. XxZHALO13Xx
    Well in your main class (the one that extends JavaPlugin), you would do something like this
    Code:java
    1. private static Tweeter tweeter;
    2.  
    3. public void onEnable() {
    4. tweeter = new Tweeter("user", "pass");
    5.  
    6. try {
    7. tweeter.login();
    8. } catch (Exception e) {
    9. e.printStackTrace();
    10. }
    11. }

    And when a player is banned, you get the Tweeter class like this, then call the tweet() method.
    Code:java
    1. YourMainClass.getTweeter().tweet("Some player was banned.");

    For example, if you have a custom ban command, then you use that after you've banned the player.
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if (cmd.getName().equalsIgnoreCase("ban")) {
    3. if (args.length > 0) {
    4. Player player = Bukkit.getPlayerExact(args[0]);
    5.  
    6. if (player == null) {
    7. sender.sendMessage("Can't find player: " + args[0]);
    8. return true;
    9. }
    10.  
    11. String reason = null;
    12.  
    13. if (args.length > 1) {
    14. reason = StringUtils.join(args, " ", 1, args.length);
    15. }
    16.  
    17. if (reason != null) {
    18. try {
    19. YourMainClass.getTweeter().tweet(player.getName() + " was banned for: " + reason);
    20. } catch (Exception e) {
    21. e.printStackTrace();
    22. }
    23. } else {
    24. try {
    25. YourMainClass.getTweeter().tweet(player.getName() + " was banned.");
    26. } catch (Exception e) {
    27. e.printStackTrace();
    28. }
    29. }
    30. }
    31. }
    32. return false;
    33. }

    If not, you'll have to figure out some other way to determine when a player is banned.
     
  26. Offline

    XxZHALO13Xx

    Assist do i remove all the files when exporting or keep them?
     
  27. XxZHALO13Xx
    What files? You shouldn't remove anything.
     
Thread Status:
Not open for further replies.

Share This Page