Twitt4j Integration Issue

Discussion in 'Plugin Development' started by Booshayy, Apr 5, 2015.

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

    Booshayy

    Hey guys.

    I'm writing a plugin that sends a tweet by using a command, the tweet formats are pulled from a config.

    However, Twitt4j keeps behaving interestingly and I wanted some help from you guys.

    Here's the code.

    Main:

    Code:
    package com.pierce.uhctweet;
    
    import java.io.File;
    import java.util.HashMap;
    
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.pierce.uhctweet.commands.TweetCommand;
    
    /* Copyright UHCHub, 2015
    * Written by Pierce (last name confidential) and Joey Gallegos
    * Code not for use unless authorized in writing
    * by one of the authors
    */
    
    public class UHCTweet extends JavaPlugin {
       
        private static UHCTweet plugin;
        private static HashMap<String, String> loadedTweets = new HashMap<>();
        private static Twitter twitter = null;
       
       
        public void onEnable() {
            plugin = this;
            if (!(new File(getDataFolder(), "config.yml")).exists()) {
                saveDefaultConfig();
            }   
           
            HashMap<String, String> tweets = new HashMap<>();
            tweets.put("barebones", getConfig().getString("tweets.barebones"));
            tweets.put("cutclean", getConfig().getString("tweets.cutclean"));
            tweets.put("rush", getConfig().getString("tweets.rush"));
            tweets.put("tripleores", getConfig().getString("tweets.tripleores"));
           
            for (String tweet : tweets.values()) {
                if (tweet.length() > 140) {
                    getLogger().info("This tweet is longer than 140 characters!");
                    getLogger().info(tweet);
                    tweets.remove(tweet);
                }
            }
           
            setLoadedTweets(tweets);
           
            boolean debug = getConfig().getBoolean("debug");
            String username = getConfig().getString("username");
            String password = getConfig().getString("password");
            getCommand("sendtweet").setExecutor(new TweetCommand());
           
            ConfigurationBuilder cb = new ConfigurationBuilder();
            cb.setDebugEnabled(debug);
            cb.setUser(username);
            cb.setPassword(password);
           
            TwitterFactory tf = new TwitterFactory(cb.build());
            twitter = tf.getInstance();
        }
       
        public void onDisable() {
            plugin = null;
        }
       
        public static Plugin getInstance() {
            return plugin;
        }
       
        public static Twitter getTwitter() {
            return twitter;
        }
    
        public static HashMap<String, String> getLoadedTweets() {
            return loadedTweets;
        }
    
        public static void setLoadedTweets(HashMap<String, String> loadedTweets) {
            UHCTweet.loadedTweets = loadedTweets;
        }
    }
    
    Command Class:

    Code:
    package com.pierce.uhctweet.commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.scheduler.BukkitRunnable;
    
    import com.avaje.ebeaninternal.server.lib.sql.DataSourcePool.Status;
    import com.pierce.uhctweet.UHCTweet;
    
    public class TweetCommand implements CommandExecutor {
       
        private static String prefix = ChatColor.GRAY + "[" + ChatColor.AQUA + "UHC" + ChatColor.GRAY + "] ";
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (!sender.isOp()) {
                sender.sendMessage(prefix + ChatColor.DARK_RED + "You don't have permission to execute this!");
                return true;
            }
           
            if (args.length == 0) {
                sender.sendMessage(prefix + ChatColor.RED + "Enter a type of tweet: Barebones, CutClean, Rush, or TripleOres.");
               
            } else {
                String game = args[0];
                if (UHCTweet.getLoadedTweets().keySet().contains(game)) {
                    new BukkitRunnable() {
    
                        @Override
                        public void run() {
                            try {
                                Status status = UHCTweet.getTwitter().updateStatus(new StatusUpdate(UHCTweet.getLoadedTweets().get(game)));
                                UHCTweet.getInstance().getLogger().info("You just tweeted! - " + status.getText());
                            } catch (TwitterException e) {
                                e.printStackTrace();
                            }
                           
                        }
                    }.runTaskAsynchronously(UHCTweet.getInstance());
                } else {
                    sender.sendMessage(prefix + ChatColor.DARK_RED  + "That game mode doesn't exist!");
                }
            }
           
            return true;
        }
    
    }
    
    plugin.yml:

    Code:
    main: com.pierce.uhctweet.UHCTweet
    name: UHCTweet
    authors: [Pierce (last name confidential), Joey Gallegos]
    version: 1.0
    description: An auto-tweet plugin for UHC hosting.
    commands:
      sendtweet:
        usage: /sendtweet <gamemode>
        descripton: Sends the tweet.
    
    Twitt4j Dependency (pom.xml):

    Code:
       <dependencies>
          <dependency>
               <groupId>org.twitter4j</groupId>
               <artifactId>twitter4j-core</artifactId>
               <version>[3.0,)</version>
           </dependency>
       </dependencies>
    Referenced libraries: http://prntscr.com/6psz19


    Final note: None of the imports will well.. import. Before I added twitt4j as a dependency everything imported fine, but now it's all messed up, and the things that I wrote depending on the twitt4j library are showing as errors. What did I do wrong?

    Thanks anyone for your help.

    Pierce
     
  2. Offline

    mine-care

    i see quite some un-needed static over there, remove it please.
    What is the odd behaviour you faced? Also external libs have a problem, they cannot be compiled to the same jar as the plugin but when placed in the plugins folder with the plugin bukkits attempts to load them as plugins and there is an error in console...
     
  3. Offline

    WeeSkilz

    Try using a manifest and placing the twitter4j JAR in your plugins folder
     
  4. Offline

    Booshayy

    How do I use a manifest? I'm pretty new to using dependencies.
     
  5. Offline

    mythbusterma

    @Booshayy

    Don't bother with that.

    Use Maven shade plugin. Maven doesn't export the dependencies by default.
     
  6. Offline

    Booshayy

    @mythbusterma
    How do I set up the plugin? Do you have a webpage that explains it simply?
     
  7. Offline

    mythbusterma

    @Booshayy

    Just include the plugin in your plugins section of your pom.xml.

    The snippet you need is on the website of the plugin.
     
Thread Status:
Not open for further replies.

Share This Page