Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Eclipse Workspace Patch 1.0
- #P aCis_gameserver
- Index: java/net/sf/l2j/gameserver/handler/AdminCommandHandler.java
- ===================================================================
- --- java/net/sf/l2j/gameserver/handler/AdminCommandHandler.java (revision 2)
- +++ java/net/sf/l2j/gameserver/handler/AdminCommandHandler.java (working copy)
- @@ -19,6 +19,7 @@
- import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminEffects;
- import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminEnchant;
- import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminExpSp;
- +import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminFacebook;
- import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminGeoEngine;
- import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminGm;
- import net.sf.l2j.gameserver.handler.admincommandhandlers.AdminGmChat;
- @@ -103,6 +104,7 @@
- registerAdminCommandHandler(new AdminTarget());
- registerAdminCommandHandler(new AdminTeleport());
- registerAdminCommandHandler(new AdminZone());
- + registerAdminCommandHandler(new AdminFacebook());
- }
- public void registerAdminCommandHandler(IAdminCommandHandler handler)
- Index: java/net/sf/l2j/facebook/fb.java
- ===================================================================
- --- java/net/sf/l2j/facebook/fb.java (nonexistent)
- +++ java/net/sf/l2j/facebook/fb.java (working copy)
- @@ -0,0 +1,467 @@
- +package net.sf.l2j.facebook;
- +
- +import com.restfb.DefaultFacebookClient;
- +import com.restfb.FacebookClient;
- +import com.restfb.Parameter;
- +import com.restfb.exception.FacebookOAuthException;
- +import com.restfb.types.Page;
- +import com.restfb.types.Post;
- +
- +import java.sql.Connection;
- +import java.sql.PreparedStatement;
- +import java.sql.ResultSet;
- +import java.util.ArrayList;
- +import java.util.List;
- +import java.util.Set;
- +import java.util.concurrent.ConcurrentHashMap;
- +import java.util.concurrent.ScheduledFuture;
- +import java.util.logging.Logger;
- +
- +import net.sf.l2j.commons.concurrent.ThreadPool;
- +
- +import net.sf.l2j.Config;
- +import net.sf.l2j.L2DatabaseFactory;
- +import net.sf.l2j.gameserver.data.xml.AdminData;
- +import net.sf.l2j.gameserver.model.World;
- +import net.sf.l2j.gameserver.model.actor.instance.Player;
- +import net.sf.l2j.gameserver.network.clientpackets.Say2;
- +import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
- +
- +/**
- + * @author Melron
- + */
- +public class fb
- +{
- + protected static final Logger LOG = Logger.getLogger(fb.class.getName());
- +
- + private static List<String> FACEBOOK_POSTS = new ArrayList<>();
- +
- + private static final String Facebook_Restore_Users_Ids = "SELECT facebook_user_id FROM characters where facebook_user_id IS NOT NULL";
- + private static final String Facebook_Restore = "SELECT * FROM facebook";
- +
- + private static final Set<String> FACEBOOK_USERS_IDS = ConcurrentHashMap.newKeySet();
- + private AutomaticReward _automaticReward;
- +
- + private static final int Normal = 0;
- + private static final int Busy = 1;
- + private static final int Error = 2;
- + private static int position = Normal;
- + protected ScheduledFuture<?> autoAnnounce = null;
- +
- + public enum State
- + {
- + Active,
- + Inactive
- + }
- +
- + public State state = State.Active;
- +
- + public static fb getInstance()
- + {
- + return SingletonHolder.INSTANCE;
- + }
- +
- + private static class SingletonHolder
- + {
- + protected static final fb INSTANCE = new fb();
- + }
- +
- + public fb()
- + {
- + load();
- + }
- +
- + public void load()
- + {
- + state = State.Active;
- + position = Normal;
- + loadLinksAndToken();
- + LOG.warning("Loadded " + FACEBOOK_USERS_IDS.size() + " Facebook Users Ids");
- +
- + }
- +
- + public void setFbState(State _state)
- + {
- + state = _state;
- + }
- +
- + public AutomaticReward setAutomaticReward(AutomaticReward reward)
- + {
- + _automaticReward = reward;
- + return _automaticReward;
- + }
- +
- + public AutomaticReward getAutomaticReward()
- + {
- + return _automaticReward;
- + }
- +
- +
- + public void reload(Player activeChar)
- + {
- + try
- + {
- + FACEBOOK_POSTS.clear();
- + FACEBOOK_USERS_IDS.clear();
- + load();
- + if (activeChar != null)
- + activeChar.sendPacket(new CreatureSay(0, Say2.CRITICAL_ANNOUNCE, "Facebook", "Facebook: Successfully reloaded."));
- + }
- + catch (Exception e)
- + {
- + if (activeChar != null)
- + activeChar.sendPacket(new CreatureSay(0, Say2.CRITICAL_ANNOUNCE, "Facebook", "Facebook: There was an error while restoring facebook data."));
- + }
- + }
- +
- +
- + public List<String> getFacebookPages()
- + {
- + return FACEBOOK_POSTS;
- + }
- +
- +
- + private static void loadLinksAndToken()
- + {
- + try (Connection con = L2DatabaseFactory.getInstance().getConnection())
- + {
- + PreparedStatement statement = con.prepareStatement(Facebook_Restore);
- + ResultSet rset = statement.executeQuery();
- +
- + while (rset.next())
- + {
- + if (FACEBOOK_POSTS.contains(rset.getString("enc_link")))
- + System.out.println("WARNING! Facebook ERROR! Double link found in database (facebook table)");
- + else
- + FACEBOOK_POSTS.add(rset.getString("enc_link"));
- +
- + }
- + rset.close();
- + statement.close();
- +
- + statement = con.prepareStatement(Facebook_Restore_Users_Ids);
- + rset = statement.executeQuery();
- +
- + while (rset.next())
- + {
- + if (FACEBOOK_USERS_IDS.contains(rset.getString("facebook_user_id")))
- + System.out.println("WARNING! Facebook ERROR! Double user id {" + rset.getString("facebook_user_id") + "}");
- + else
- + FACEBOOK_USERS_IDS.add(rset.getString("facebook_user_id"));
- + }
- +
- + rset.close();
- + statement.close();
- + Config.loadFacebook();
- + }
- + catch (Exception e)
- + {
- + System.out.println("Facebook Error: " + e.getMessage());
- + e.printStackTrace();
- + }
- +
- + System.out.println("Facebook settings has been successfully loaded.");
- + }
- +
- + /**
- + * @param player
- + * @param fbName
- + * @return true/false
- + */
- + public boolean acceptName(Player player, String fbName)
- + {
- + if (fbName != null && fbName.length() > 0)
- + {
- + player.setFacebookName(fbName, true);
- + return true;
- + }
- + return false;
- + }
- +
- + /**
- + * @param activeChar
- + */
- + public void executeFacebookCheck(Player activeChar)
- + {
- + if (activeChar == null)
- + return;
- +
- + if (activeChar.getFacebookName() == null)
- + {
- + activeChar.sendMessage("You must set your facebook user name first!");
- + return;
- + }
- +
- + if (state == State.Inactive)
- + {
- + activeChar.sendMessage("Facebook system is currently under maintenance..");
- + return;
- + }
- + if (position == Busy)
- + {
- + activeChar.sendMessage("Do not move so fast...");
- + return;
- + }
- + if (position == Error)
- + {
- + activeChar.sendMessage("Something is going wrong. Please wait until the administrator solve this problem.");
- + return;
- + }
- +
- + position = Busy;
- +
- + int currentLikes = 0;
- + int WantedLikes = getFacebookPages().size();
- + if (WantedLikes == 0)
- + {
- + activeChar.sendMessage("Something is going wrong. please contact with the administrator!");
- + position = Error;
- + return;
- + }
- + try
- + {
- + @SuppressWarnings("deprecation")
- + FacebookClient fClient = new DefaultFacebookClient(Config.FACEBOOK_API_TOKEN);
- +
- + for (String page : getFacebookPages())
- + {
- + com.restfb.Connection<Page> result = fClient.fetchConnection(page + "/likes", Page.class);
- +
- + for (List<Page> pages : result)
- + for (Page likePage : pages)
- + if (likePage.getName().equals(activeChar.getFacebookName()))
- + {
- + if (likePage.getId().equals(activeChar.getFacebookUserId()))
- + {
- + currentLikes++;
- + break;
- + }
- + if (activeChar.getFacebookUserId() == null && !FACEBOOK_USERS_IDS.contains(likePage.getId()))
- + {
- + FACEBOOK_USERS_IDS.add(likePage.getId());
- + activeChar.setFacebookUserId(likePage.getId(), true);
- + currentLikes++;
- + break;
- + }
- + }
- +
- + }
- + }
- + catch (FacebookOAuthException e)
- + {
- + position = Error;
- + LOG.warning(e.getErrorMessage());
- + e.printStackTrace();
- + }
- + if (currentLikes == WantedLikes)
- + {
- + activeChar.saveLikes();
- + activeChar.sendMessage("You have successfully liked all the facebook posts!");
- + activeChar.addItem("Fb-Reward", Config.FACEBOOK_REWARD_ID, Config.FACEBOOK_REWARD_COUNT, null, true);
- + }
- + else
- + {
- + activeChar.sendMessage(currentLikes == 0 ? "You haven't liked any post..." : "You have to like " + (WantedLikes - currentLikes) + " more posts in order to take your reward!");
- + }
- + position = Normal;
- + }
- +
- + public int getTotalLikes()
- + {
- + int likes = 0;
- + try
- + {
- + @SuppressWarnings("deprecation")
- + FacebookClient fClient = new DefaultFacebookClient(Config.FACEBOOK_API_TOKEN);
- +
- + com.restfb.Connection<Post> result = fClient.fetchConnection(Config.FACEBOOK_PAGE_NAME + "/feed", Post.class);
- +
- + for (List<Post> page : result)
- + {
- + for (Post aPost : page)
- + if (FACEBOOK_POSTS.contains(aPost.getId()))
- + {
- + aPost = fClient.fetchObject(aPost.getId(), Post.class, Parameter.with("fields", "from,actions,message,story,to,likes.limit(0).summary(true),comments.limit(0).summary(true),shares.limit(0).summary(true)"));
- + likes += aPost.getLikesCount();
- + }
- + }
- +
- + }
- + catch (FacebookOAuthException e)
- + {
- + LOG.warning(e.getErrorMessage());
- + e.printStackTrace();
- + }
- + return likes;
- + }
- +
- + /**
- + * @return
- + */
- + public State getState()
- + {
- + return state;
- + }
- +
- + /**
- + * @return
- + */
- + public List<Post> getAllPosts()
- + {
- + List<Post> allPosts = new ArrayList<>();
- + String token = Config.FACEBOOK_API_TOKEN;
- + try
- + {
- +
- + @SuppressWarnings("deprecation")
- + FacebookClient fClient = new DefaultFacebookClient(token);
- +
- + com.restfb.Connection<Post> result = fClient.fetchConnection(Config.FACEBOOK_PAGE_NAME + "/feed", Post.class);
- +
- + for (List<Post> page : result)
- + {
- + for (Post aPost : page)
- + {
- + allPosts.add(aPost);
- +
- + }
- + }
- +
- + }
- + catch (Exception e)
- + {
- + e.printStackTrace();
- +
- + }
- + return allPosts;
- + }
- +
- + public void savePost(String postId, String postName, Player activeChar)
- + {
- + try (Connection con = L2DatabaseFactory.getInstance().getConnection())
- + {
- + PreparedStatement statement = con.prepareStatement("INSERT INTO facebook VALUES (?)");
- + statement.setString(1, postId);
- + statement.execute();
- + statement.close();
- + FACEBOOK_POSTS.add(postId);
- + if (activeChar != null)
- + activeChar.sendMessage("New post has been added.");
- + }
- + catch (Exception e)
- + {
- + LOG.warning("Could not insert new post in db: " + e);
- + }
- + }
- +
- + public void removePost(String postId, String postMessage, Player activeChar)
- + {
- + try (Connection con = L2DatabaseFactory.getInstance().getConnection())
- + {
- + PreparedStatement statement = con.prepareStatement("DELETE FROM facebook WHERE enc_link=?");
- + statement.setString(1, postId);
- + statement.execute();
- + statement.close();
- + FACEBOOK_POSTS.remove(postId);
- + activeChar.sendMessage("Post has been deleted.");
- + }
- + catch (Exception e)
- + {
- + LOG.warning("could not delete post from db: " + e);
- + }
- +
- + }
- +
- + /**
- + * @param name
- + * @return
- + */
- + public boolean postExists(String name)
- + {
- + return FACEBOOK_POSTS.contains(name);
- + }
- +
- + public void cleanFacebookData()
- + {
- + try (Connection con = L2DatabaseFactory.getInstance().getConnection())
- + {
- + final String[] statements =
- + {
- + "TRUNCATE TABLE facebook",
- + "UPDATE characters SET facebook_name = null,facebook_user_id = null,facebookliked = 0"
- + };
- + PreparedStatement statement;
- + for (String statementStr : statements)
- + {
- + statement = con.prepareStatement(statementStr);
- + statement.execute();
- + statement.close();
- + }
- +
- + for (Player player : World.getInstance().getPlayers())
- + {
- + if (player != null)
- + {
- + player.setFacebookName(null, false);
- + player.setFacebookUserId(null, false);
- + }
- + }
- + AdminData.getInstance().broadcastMessageToGMs("Facebook session has reset.");
- + }
- + catch (Exception e)
- + {
- + LOG.warning("could not reset facebook data in characters from db: " + e);
- + e.printStackTrace();
- + AdminData.getInstance().broadcastMessageToGMs("Facebook session reset failed.");
- + }
- + }
- +
- + public void turn(boolean on)
- + {
- + state = on ? State.Active : State.Inactive;
- + AdminData.getInstance().broadcastMessageToGMs("Facebook system is now " + (on ? "On" : "Off"));
- + }
- +
- + public void setAutoAnnounce(boolean val)
- + {
- + if (val)
- + {
- + if (autoAnnounce != null)
- + {
- + AdminData.getInstance().broadcastMessageToGMs("Error, AutoAnnounce is running");
- + return;
- + }
- + AdminData.getInstance().broadcastMessageToGMs("Facebook Auto-Announcements started.");
- + autoAnnounce = ThreadPool.scheduleAtFixedRate(new Runnable()
- + {
- + @Override
- + public void run()
- + {
- + for (Player player : World.getInstance().getPlayers())
- + if (player != null && getState() == State.Active)
- + player.sendPacket(new CreatureSay(0, Say2.CRITICAL_ANNOUNCE, "Facebook", "Dont forget to like in order to get our reward!"));
- +
- + }
- +
- + }, 5000, 5000);
- + }
- + else
- + {
- + if (autoAnnounce != null)
- + {
- + AdminData.getInstance().broadcastMessageToGMs("Facebook Auto-Announcements canceled.");
- + autoAnnounce.cancel(true);
- + autoAnnounce = null;
- + }
- + }
- +
- + }
- +
- + /**
- + * @return
- + */
- + public boolean isAutoAnnounceOn()
- + {
- + return autoAnnounce != null;
- + }
- +}
- \ No newline at end of file
- Index: java/net/sf/l2j/facebook/AutomaticReward.java
- ===================================================================
- --- java/net/sf/l2j/facebook/AutomaticReward.java (nonexistent)
- +++ java/net/sf/l2j/facebook/AutomaticReward.java (working copy)
- @@ -0,0 +1,104 @@
- +package net.sf.l2j.facebook;
- +
- +import java.util.concurrent.ScheduledFuture;
- +
- +import net.sf.l2j.commons.concurrent.ThreadPool;
- +
- +import net.sf.l2j.gameserver.data.xml.AdminData;
- +import net.sf.l2j.gameserver.model.World;
- +import net.sf.l2j.gameserver.model.actor.instance.Player;
- +
- +/**
- + * @author Melron
- + */
- +public class AutomaticReward
- +{
- + private int itemId;
- + private int itemCnt;
- + private int likes;
- + private boolean enabled = false;
- + protected boolean rewarded = false;
- +
- + protected ScheduledFuture<?> checkTask;
- +
- + public AutomaticReward(int id, int count, int whenLikes)
- + {
- + itemId = id;
- + itemCnt = count;
- + likes = whenLikes;
- + enabled = true;
- + }
- +
- + public boolean isRewarded()
- + {
- + return rewarded;
- + }
- +
- + public int getItemId()
- + {
- + return itemId;
- + }
- +
- + public int getItemCnt()
- + {
- + return itemCnt;
- + }
- +
- + public int getLikesCount()
- + {
- + return likes;
- + }
- +
- + public boolean isEnabled()
- + {
- + return enabled;
- + }
- +
- + public void start()
- + {
- + enabled = true;
- + checkTask = ThreadPool.scheduleAtFixedRate(new Runnable()
- + {
- + @Override
- + public void run()
- + {
- + if (reward())
- + {
- + rewarded = true;
- + end();
- + }
- +
- + }
- +
- + }, 5000, 5000);
- + }
- +
- + protected void end()
- + {
- + if (checkTask != null)
- + {
- + checkTask.cancel(true);
- + checkTask = null;
- + }
- + AdminData.getInstance().broadcastMessageToGMs("Automatic facebook reward successfully launched and rewarded.");
- + }
- +
- + protected boolean reward()
- + {
- + if (fb.getInstance().getTotalLikes() >= likes)
- + {
- + rewarded = true;
- + for (Player player : World.getInstance().getPlayers())
- + {
- + if (player != null)
- + {
- + player.addItem("fb-reward", itemId, itemCnt, null, true);
- + player.sendMessage("Facebook Reward!!");
- + }
- + }
- + return rewarded;
- + }
- + return false;
- + }
- +
- +}
- Index: java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminFacebook.java
- ===================================================================
- --- java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminFacebook.java (nonexistent)
- +++ java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminFacebook.java (working copy)
- @@ -0,0 +1,265 @@
- +package net.sf.l2j.gameserver.handler.admincommandhandlers;
- +
- +import com.restfb.types.Post;
- +
- +import java.util.List;
- +import java.util.StringTokenizer;
- +
- +import net.sf.l2j.commons.concurrent.ThreadPool;
- +import net.sf.l2j.commons.lang.StringUtil;
- +
- +import net.sf.l2j.facebook.fb;
- +import net.sf.l2j.facebook.fb.State;
- +import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
- +import net.sf.l2j.gameserver.model.actor.instance.Player;
- +import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
- +
- +/**
- + * This class handles following admin commands:
- + * <ul>
- + * <li>ban_acc [account_name] = changes account access level to -100 and logs him off. If no account is specified target's account is used.</li>
- + * <li>ban_char [char_name] = changes a characters access level to -100 and logs him off. If no character is specified target is used.</li>
- + * <li>ban_chat [char_name] [duration] = chat bans a character for the specified duration. If no name is specified the target is chat banned indefinitely.</li>
- + * <li>unban_acc [account_name] = changes account access level to 0.</li>
- + * <li>unban_char [char_name] = changes specified characters access level to 0.</li>
- + * <li>unban_chat [char_name] = lifts chat ban from specified player. If no player name is specified current target is used.</li>
- + * <li>jail [char_name] [penalty_time] = jails character. Time specified in minutes. For ever if no time is specified.</li>
- + * <li>unjail [char_name] = Unjails player, teleport him to Floran.</li>
- + * </ul>
- + */
- +public class AdminFacebook implements IAdminCommandHandler
- +{
- + private static final String[] ADMIN_COMMANDS =
- + {
- + "admin_facebook",
- + "admin_showPosts",
- + "admin_delPost",
- + "admin_addPost",
- + "admin_turnFb",
- + "admin_fbreload",
- + "admin_fb_auto_announce",
- + "admin_clean_facebook_database"
- + };
- +
- + @Override
- + public boolean useAdminCommand(String command, Player activeChar)
- + {
- + if (command.startsWith("admin_facebook"))
- + {
- + pendingHtml(activeChar);
- + ThreadPool.schedule(() -> sendHtml(activeChar), 1);
- + }
- + else if (command.startsWith("admin_clean_facebook_database"))
- + {
- + fb.getInstance().cleanFacebookData();
- + }
- + else if (command.startsWith("admin_fbreload"))
- + {
- + fb.getInstance().reload(activeChar);
- + }
- + else if (command.startsWith("admin_fb_auto_announce"))
- + {
- + StringTokenizer st = new StringTokenizer(command);
- + st.nextToken();
- + boolean choice = Boolean.parseBoolean(st.nextToken());
- + fb.getInstance().setAutoAnnounce(choice);
- + ThreadPool.schedule(() -> sendHtml(activeChar), 1);
- + }
- + else if (command.startsWith("admin_turnFb "))
- + {
- + boolean choice = Boolean.parseBoolean(command.substring(13));
- + fb.getInstance().turn(choice);
- + pendingHtml(activeChar);
- + ThreadPool.schedule(() -> sendHtml(activeChar), 1);
- + }
- + else if (command.startsWith("admin_showPosts"))
- + {
- + ThreadPool.schedule(() -> showPosts(activeChar), 1);
- + }
- + else if (command.startsWith("admin_addPost"))
- + {
- + StringTokenizer st = new StringTokenizer(command, " ");
- + st.nextToken();
- +
- + if (!st.hasMoreTokens())
- + {
- + activeChar.sendMessage("Error in command bypass " + command);
- + return false;
- + }
- + String postId = st.nextToken();
- + String postName = st.nextToken();
- + if (fb.getInstance().postExists(postId))
- + {
- + activeChar.sendMessage("post already added.");
- + return false;
- + }
- +
- + fb.getInstance().savePost(postId, postName, activeChar);
- + ThreadPool.schedule(() -> showPosts(activeChar), 1);
- + }
- + else if (command.startsWith("admin_delPost"))
- + {
- + StringTokenizer st = new StringTokenizer(command, " ");
- + st.nextToken();
- +
- + if (!st.hasMoreTokens())
- + {
- + activeChar.sendMessage("Error in command bypass " + command);
- + return false;
- + }
- + String postId = st.nextToken();
- + String postName = st.nextToken();
- + fb.getInstance().removePost(postId, postName, activeChar);
- + ThreadPool.schedule(() -> showPosts(activeChar), 1);
- + }
- + return true;
- + }
- +
- + private static void pendingHtml(Player activeChar)
- + {
- + if (activeChar != null)
- + {
- + final NpcHtmlMessage html = new NpcHtmlMessage(0);
- + html.setFile("data/html/facebook/gmFB-pending.htm");
- + activeChar.sendPacket(html);
- + }
- + }
- +
- + /**
- + * @param activeChar
- + */
- + private static void showPosts(Player activeChar)
- + {
- + if (activeChar == null)
- + return;
- +
- + final NpcHtmlMessage html = new NpcHtmlMessage(0);
- + html.setFile("data/html/facebook/posts.htm");
- + html.replace("%posts%", makePosts(fb.getInstance().getAllPosts()));
- + activeChar.sendPacket(html);
- + }
- +
- + /**
- + * @param allPosts
- + * @return
- + */
- + private static String makePosts(List<Post> allPosts)
- + {
- + if (allPosts.isEmpty())
- + return "";
- +
- + final StringBuilder sb = new StringBuilder();
- +
- + for (Post post : allPosts)
- + {
- + if (fb.getInstance().postExists(post.getId()))
- + StringUtil.append(sb, "<tr><td>", checkForBigString(post.getMessage()), "</td><td><a action=\"bypass admin_delPost ", post.getId(), " ", post.getMessage(), "\"><font color=\"CB440A\">Remove</font></a></td></tr>");
- + else
- + StringUtil.append(sb, "<tr><td>", checkForBigString(post.getMessage()), "</td><td><a action=\"bypass admin_addPost ", post.getId(), " ", post.getMessage(), "\"><font color=\"0E772C\">Add</font></a></td></tr>");
- +
- + }
- +
- + return sb.toString();
- + }
- +
- + /**
- + * @param message
- + * @return
- + */
- + private static String checkForBigString(String message)
- + {
- + if (message.length() > 25)
- + {
- + String[] words = message.split(" ");
- + if (words.length > 4)
- + return new String(words[0] + " " + words[1] + " " + words[2] + " ... " + words[words.length - 1]);
- + return message;
- + }
- + return message;
- + }
- +
- + private static void sendHtml(Player activeChar)
- + {
- + if (activeChar == null || !activeChar.isGM())
- + return;
- +
- + final NpcHtmlMessage html = new NpcHtmlMessage(0);
- + html.setFile("data/html/facebook/gmFB.htm");
- + if (fb.getInstance().getState() == State.Active)
- + {
- + html.replace("%facebookState%", "<img src=L2UI_CH3.radar_party width=8 height=8>");
- + html.replace("%isEnabled%", "<button value=\"\" action=\"bypass admin_turnFb false\" width=16 height=16 fore=\"l2ui.CheckBox_checked\" back=\"l2ui.CheckBox_unable\">");
- +
- + }
- + else
- + {
- + html.replace("%facebookState%", "<img src=L2UI_CH3.radar_target width=8 height=8>");
- + html.replace("%isEnabled%", "<button value=\"\" action=\"bypass admin_turnFb true\" width=16 height=16 fore=\"l2ui.CheckBox_unable\" back=\"l2ui.CheckBox_checked\">");
- + }
- + html.replace("%totalLikes%", fb.getInstance().getTotalLikes());
- + html.replace("%pages%", fb.getInstance().getFacebookPages().size());
- + if (fb.getInstance().isAutoAnnounceOn())
- + html.replace("%autoAnnounce%", "<button value=\"\" action=\"bypass admin_fb_auto_announce false\" width=16 height=16 fore=\"l2ui.CheckBox_checked\" back=\"l2ui.CheckBox_unable\">");
- + else
- + html.replace("%autoAnnounce%", "<button value=\"\" action=\"bypass admin_fb_auto_announce true\" width=16 height=16 fore=\"l2ui.CheckBox_unable\" back=\"l2ui.CheckBox_checked\">");
- +
- + if (fb.getInstance().getAutomaticReward() == null)
- + {
- + html.replace("%automaticRewardState%", "<font color=\"DE320C\"> Disabled </font>");
- + html.replace("%automaticRewardTick%", "<img src=l2ui.CheckBox_unable width=16 height=16>");
- + html.replace("%likesNum%", 0);
- + html.replace("%idNum%", 0);
- + html.replace("%cntNum%", 0);
- + }
- + else
- + {
- + boolean enabled = fb.getInstance().getAutomaticReward().isEnabled();
- + boolean rewarded = enabled && fb.getInstance().getAutomaticReward().isRewarded();
- +
- + int likes = fb.getInstance().getAutomaticReward().getLikesCount();
- + int id = fb.getInstance().getAutomaticReward().getItemId();
- + int cnt = fb.getInstance().getAutomaticReward().getItemCnt();
- +
- + if (enabled)
- + {
- + if (rewarded)
- + {
- + html.replace("%automaticRewardState%", "<font color=\"CBCB04\"> Rewarded </font>");
- + html.replace("%automaticRewardTick%", "<img src=l2ui.CheckBox_checked width=16 height=16>");
- +
- + html.replace("%likesNum%", "<font color=\"CBCB04\">" + likes + "</font>");
- + html.replace("%idNum%", "<font color=\"CBCB04\">" + id + "</font>");
- + html.replace("%cntNum%", "<font color=\"CBCB04\">" + cnt + "</font>");
- + }
- + else
- + {
- + html.replace("%automaticRewardState%", "<font color=\"10D548\"> Enabled </font>");
- + html.replace("%automaticRewardTick%", "<img src=l2ui.CheckBox_checked_unable width=16 height=16>");
- +
- + html.replace("%likesNum%", "<font color=\"10D548\">" + likes + "</font>");
- + html.replace("%idNum%", "<font color=\"10D548\">" + id + "</font>");
- + html.replace("%cntNum%", "<font color=\"10D548\">" + cnt + "</font>");
- +
- + }
- + }
- + else
- + {
- + html.replace("%automaticRewardState%", "<font color=\"DE320C\"> Disabled </font>");
- + html.replace("%automaticRewardTick%", "<img src=l2ui.CheckBox_unable width=16 height=16>");
- + html.replace("%likesNum%", 0);
- + html.replace("%idNum%", 0);
- + html.replace("%cntNum%", 0);
- + }
- +
- + }
- +
- + activeChar.sendPacket(html);
- +
- + }
- +
- + @Override
- + public String[] getAdminCommandList()
- + {
- + return ADMIN_COMMANDS;
- + }
- +}
- \ No newline at end of file
- Index: java/net/sf/l2j/Config.java
- ===================================================================
- --- java/net/sf/l2j/Config.java (revision 2)
- +++ java/net/sf/l2j/Config.java (working copy)
- @@ -37,7 +37,11 @@
- public static final String PLAYERS_FILE = "./config/players.properties";
- public static final String SERVER_FILE = "./config/server.properties";
- public static final String SIEGE_FILE = "./config/siege.properties";
- + public static final String FACEBOOK_FILE = "./config/facebook.properties";
- + public static String FACEBOOK_API_TOKEN;
- + public static String FACEBOOK_PAGE_NAME;
- + public static int FACEBOOK_REWARD_ID;
- + public static int FACEBOOK_REWARD_COUNT;
- +
- // --------------------------------------------------
- // Clans settings
- // --------------------------------------------------
- @@ -718,6 +722,17 @@
- /**
- * Loads clan and clan hall settings.
- */
- + public static final void loadFacebook()
- + {
- + final ExProperties fb = initProperties(FACEBOOK_FILE);
- +
- + FACEBOOK_API_TOKEN = fb.getProperty("FacebookApiToken", "123");
- + FACEBOOK_PAGE_NAME = fb.getProperty("FacebookPageName" , "L2Tests");
- + FACEBOOK_REWARD_ID = fb.getProperty("FacebookRewardId" , 57);
- + FACEBOOK_REWARD_COUNT= fb.getProperty("FacebookRewardCount" , 150);
- + }
- +
- + /**
- + * Loads clan and clan hall settings.
- + */
- private static final void loadClans()
- {
- final ExProperties clans = initProperties(CLANS_FILE);
- @@ -1363,6 +1378,7 @@
- {
- _log.info("Loading gameserver configuration files.");
- + loadFacebook();
- // clans settings
- loadClans();
- Index: java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java
- ===================================================================
- --- java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java (revision 2)
- +++ java/net/sf/l2j/gameserver/network/clientpackets/RequestBypassToServer.java (working copy)
- @@ -4,7 +4,12 @@
- import java.util.logging.Level;
- import java.util.logging.Logger;
- +import net.sf.l2j.commons.concurrent.ThreadPool;
- +
- import net.sf.l2j.Config;
- +import net.sf.l2j.facebook.AutomaticReward;
- +import net.sf.l2j.facebook.fb;
- +import net.sf.l2j.facebook.fb.State;
- import net.sf.l2j.gameserver.communitybbs.CommunityBoard;
- import net.sf.l2j.gameserver.data.xml.AdminData;
- import net.sf.l2j.gameserver.handler.AdminCommandHandler;
- @@ -83,6 +88,81 @@
- {
- playerHelp(activeChar, _command.substring(12));
- }
- + else if (_command.startsWith("facebook"))
- + {
- + if (activeChar.getFacebookName() == null)
- + return;
- + if (activeChar.hasLikedAllThePhotos())
- + {
- + activeChar.sendMessage("You have already rewarded.");
- + return;
- + }
- + ThreadPool.execute(() -> fb.getInstance().executeFacebookCheck(activeChar));
- + }
- + else if (_command.startsWith("setFbReward"))
- + {
- + StringTokenizer st = new StringTokenizer(_command);
- + st.nextToken();
- + if (!activeChar.isGM())
- + return;
- + if (!st.hasMoreTokens())
- + {
- + activeChar.sendMessage("Fill all fields and then try to save.");
- + return;
- + }
- +
- + if (st.countTokens() == 1)
- + {
- + String value = st.nextToken();
- + if (value.equals("0"))
- + {
- + activeChar.sendMessage("Automatic Reward cleaned.");
- + fb.getInstance().setAutomaticReward(null);
- + }
- + else
- + activeChar.sendMessage("Invalid parameter");
- + }
- +
- + else if (fb.getInstance().getState() == State.Inactive)
- + activeChar.sendMessage("Facebook system is offline!");
- + else if (st.countTokens() == 3)
- + {
- + String id = st.nextToken();
- + String itemCnt = st.nextToken();
- + String likes = st.nextToken();
- +
- + if (!parseElements(id, itemCnt, likes))
- + activeChar.sendMessage("Wrong values.");
- + else
- + {
- + activeChar.sendMessage("New Automatic Method has been setted.");
- + fb.getInstance().setAutomaticReward(new AutomaticReward(Integer.parseInt(id), Integer.parseInt(itemCnt), Integer.parseInt(likes))).start();
- + }
- + }
- +
- + IAdminCommandHandler ach = AdminCommandHandler.getInstance().getAdminCommandHandler("admin_facebook");
- + if (ach == null)
- + {
- + activeChar.sendMessage("Something is wrong with your command");
- + return;
- + }
- + ach.useAdminCommand("admin_facebook", activeChar);
- +
- + }
- + else if (_command.startsWith("fbname "))
- + {
- + String name = _command.substring(7);
- + if (name == null || name.length() < 1)
- + {
- + activeChar.sendMessage("Something is wrong with your name. please fill a new one.");
- + return;
- + }
- +
- + if (fb.getInstance().acceptName(activeChar, name))
- + activeChar.sendMessage("Your facebook name (" + activeChar.getFacebookName() + ") has been accepted.");
- + else
- + activeChar.sendMessage("Invalid facebook name!");
- + }
- else if (_command.startsWith("npc_"))
- {
- if (!activeChar.validateBypass(_command))
- @@ -176,6 +256,36 @@
- }
- }
- + private static boolean parseElements(String id, String count, String whenLikes)
- + {
- + if (id == null || count == null || whenLikes == null || id.length() < 1 || count.length() < 1 || whenLikes.length() < 1)
- + return false;
- +
- + try
- + {
- + int _id = Integer.parseInt(id);
- +
- + if (_id == 0)
- + return false;
- +
- + int _cnt = Integer.parseInt(count);
- + if (_cnt == 0)
- + return false;
- +
- + int _likes = Integer.parseInt(whenLikes);
- +
- + if (_likes == 0 || _likes <= fb.getInstance().getTotalLikes())
- + return false;
- +
- + }
- + catch (Exception e)
- + {
- + return false;
- + }
- +
- + return true;
- + }
- +
- private static void playerHelp(Player activeChar, String path)
- {
- if (path.indexOf("..") != -1)
- Index: java/net/sf/l2j/gameserver/handler/usercommandhandlers/FaceBook.java
- ===================================================================
- --- java/net/sf/l2j/gameserver/handler/usercommandhandlers/FaceBook.java (nonexistent)
- +++ java/net/sf/l2j/gameserver/handler/usercommandhandlers/FaceBook.java (working copy)
- @@ -0,0 +1,70 @@
- +package net.sf.l2j.gameserver.handler.usercommandhandlers;
- +
- +import com.restfb.types.Post;
- +
- +import java.util.List;
- +
- +import net.sf.l2j.commons.lang.StringUtil;
- +
- +import net.sf.l2j.facebook.fb;
- +import net.sf.l2j.facebook.fb.State;
- +import net.sf.l2j.gameserver.handler.IUserCommandHandler;
- +import net.sf.l2j.gameserver.model.actor.instance.Player;
- +import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
- +
- +/**
- + * @author Melron
- + */
- +public class FaceBook implements IUserCommandHandler
- +{
- + private static final int[] COMMAND_IDS =
- + {
- + 116
- + };
- +
- + @Override
- + public boolean useUserCommand(int id, Player activeChar)
- + {
- + if (fb.getInstance().getState() == State.Inactive)
- + {
- + activeChar.sendMessage("Facebook system is currently under maintenance..");
- + return false;
- + }
- + String Html = activeChar.getFacebookName() == null ? "index" : activeChar.hasLikedAllThePhotos() ? "alreadyLiked" : "passed";
- + final NpcHtmlMessage html = new NpcHtmlMessage(0);
- + html.setFile("data/html/facebook/" + Html + ".htm");
- + html.replace("%charName%", activeChar.getName());
- + html.replace("%needLikes%", fb.getInstance().getFacebookPages().size());
- + html.replace("%sites%", makePosts(fb.getInstance().getAllPosts()));
- + activeChar.sendPacket(html);
- + return true;
- + }
- +
- + private static String makePosts(List<Post> allPosts)
- + {
- + if (allPosts.isEmpty())
- + return "";
- +
- + final StringBuilder sb = new StringBuilder();
- + allPosts.forEach(post -> StringUtil.append(sb, checkForBigString(post.getMessage()), "<br1>"));
- + return sb.toString();
- + }
- +
- + private static String checkForBigString(String message)
- + {
- + if (message.length() > 25)
- + {
- + String[] words = message.split(" ");
- + if (words.length > 2)
- + return new String(words[0] + " " + words[1] + " " + words[2] + " ... " + words[words.length - 1]);
- + return message;
- + }
- + return message;
- + }
- +
- + @Override
- + public int[] getUserCommandList()
- + {
- + return COMMAND_IDS;
- + }
- +}
- \ No newline at end of file
- Index: java/net/sf/l2j/gameserver/model/actor/instance/Player.java
- ===================================================================
- --- java/net/sf/l2j/gameserver/model/actor/instance/Player.java (revision 2)
- +++ java/net/sf/l2j/gameserver/model/actor/instance/Player.java (working copy)
- @@ -310,7 +310,7 @@
- private static final String INSERT_CHARACTER = "INSERT INTO characters (account_name,obj_Id,char_name,level,maxHp,curHp,maxCp,curCp,maxMp,curMp,face,hairStyle,hairColor,sex,exp,sp,karma,pvpkills,pkkills,clanid,race,classid,deletetime,cancraft,title,accesslevel,online,isin7sdungeon,clan_privs,wantspeace,base_class,nobless,power_grade,last_recom_date) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
- private static final String UPDATE_CHARACTER = "UPDATE characters SET level=?,maxHp=?,curHp=?,maxCp=?,curCp=?,maxMp=?,curMp=?,face=?,hairStyle=?,hairColor=?,sex=?,heading=?,x=?,y=?,z=?,exp=?,expBeforeDeath=?,sp=?,karma=?,pvpkills=?,pkkills=?,rec_have=?,rec_left=?,clanid=?,race=?,classid=?,deletetime=?,title=?,accesslevel=?,online=?,isin7sdungeon=?,clan_privs=?,wantspeace=?,base_class=?,onlinetime=?,punish_level=?,punish_timer=?,nobless=?,power_grade=?,subpledge=?,last_recom_date=?,lvl_joined_academy=?,apprentice=?,sponsor=?,varka_ketra_ally=?,clan_join_expiry_time=?,clan_create_expiry_time=?,char_name=?,death_penalty_level=? WHERE obj_id=?";
- - private static final String RESTORE_CHARACTER = "SELECT account_name, obj_Id, char_name, level, maxHp, curHp, maxCp, curCp, maxMp, curMp, face, hairStyle, hairColor, sex, heading, x, y, z, exp, expBeforeDeath, sp, karma, pvpkills, pkkills, clanid, race, classid, deletetime, cancraft, title, rec_have, rec_left, accesslevel, online, char_slot, lastAccess, clan_privs, wantspeace, base_class, onlinetime, isin7sdungeon, punish_level, punish_timer, nobless, power_grade, subpledge, last_recom_date, lvl_joined_academy, apprentice, sponsor, varka_ketra_ally,clan_join_expiry_time,clan_create_expiry_time,death_penalty_level FROM characters WHERE obj_id=?";
- + private static final String RESTORE_CHARACTER = "SELECT account_name, obj_Id, char_name, level, maxHp, curHp, maxCp, curCp, maxMp, curMp, face, hairStyle, hairColor, sex, heading, x, y, z, exp, expBeforeDeath, sp, karma, pvpkills, pkkills, clanid, race, classid, deletetime, cancraft, title, rec_have, rec_left, accesslevel, online, char_slot, lastAccess, clan_privs, wantspeace, base_class, onlinetime, isin7sdungeon, punish_level, punish_timer, nobless, power_grade, subpledge, last_recom_date, lvl_joined_academy, apprentice, sponsor, varka_ketra_ally,clan_join_expiry_time,clan_create_expiry_time,death_penalty_level,facebook_name,facebook_user_id FROM characters WHERE obj_id=?";
- private static final String RESTORE_CHAR_SUBCLASSES = "SELECT class_id,exp,sp,level,class_index FROM character_subclasses WHERE char_obj_id=? ORDER BY class_index ASC";
- private static final String ADD_CHAR_SUBCLASS = "INSERT INTO character_subclasses (char_obj_id,class_id,exp,sp,level,class_index) VALUES (?,?,?,?,?,?)";
- @@ -5280,6 +5280,9 @@
- player.setName(rset.getString("char_name"));
- player._lastAccess = rset.getLong("lastAccess");
- + player.setFacebookName(rset.getString("facebook_name"), false);
- +
- + player.setFacebookUserId(rset.getString("facebook_user_id"), false);
- player.getStat().setExp(rset.getLong("exp"));
- player.setExpBeforeDeath(rset.getLong("expBeforeDeath"));
- player.getStat().setLevel(rset.getByte("level"));
- @@ -10536,4 +10539,121 @@
- }
- }
- }
- +
- + /**
- + * @return
- + */
- + public boolean hasLikedAllThePhotos()
- + {
- + int liked = 0;
- + try (Connection con = L2DatabaseFactory.getInstance().getConnection())
- + {
- + PreparedStatement statement = con.prepareStatement("SELECT facebookliked FROM characters WHERE obj_id=?");
- + statement.setInt(1, getObjectId());
- + ResultSet rset = statement.executeQuery();
- + while (rset.next())
- + liked = rset.getInt("facebookliked");
- +
- + rset.close();
- + statement.close();
- + }
- + catch (Exception e)
- + {
- + _log.warning("could not restore facebook likes: " + e);
- + }
- + return liked == 1;
- + }
- +
- + public void clearAllLikes()
- + {
- + try (Connection con = L2DatabaseFactory.getInstance().getConnection())
- + {
- + PreparedStatement statement = con.prepareStatement("UPDATE characters SET facebookliked=? WHERE obj_Id=?");
- + statement.setInt(1, 0);
- + statement.setInt(2, getObjectId());
- + statement.executeUpdate();
- + statement.close();
- + }
- + catch (Exception e)
- + {
- + _log.log(Level.WARNING, "Could not update " + getName() + " facebook status: " + e.getMessage(), e);
- + }
- + }
- +
- + public void saveLikes()
- + {
- + try (Connection con = L2DatabaseFactory.getInstance().getConnection())
- + {
- + PreparedStatement statement = con.prepareStatement("UPDATE characters SET facebookliked=? WHERE obj_Id=?");
- + statement.setInt(1, 1);
- + statement.setInt(2, getObjectId());
- + statement.executeUpdate();
- + statement.close();
- + }
- + catch (Exception e)
- + {
- + _log.log(Level.WARNING, "Could not update " + getName() + " facebook status: " + e.getMessage(), e);
- + }
- + }
- +
- + private String fbName = null;
- + private String fbId = null;
- +
- + /**
- + * @return
- + */
- + public String getFacebookName()
- + {
- + return fbName;
- + }
- +
- + public void setFacebookName(String name, boolean store)
- + {
- + fbName = name;
- + if (store)
- + {
- + try (Connection con = L2DatabaseFactory.getInstance().getConnection())
- + {
- + PreparedStatement statement = con.prepareStatement("UPDATE characters SET facebook_name=? WHERE obj_id=?");
- + statement.setString(1, name);
- + statement.setInt(2, this.getObjectId());
- + statement.execute();
- + statement.close();
- + }
- + catch (Exception e)
- + {
- + _log.warning("Error could not update char facebook name: " + e);
- + }
- + }
- + }
- +
- + /**
- + * @return
- + */
- + public String getFacebookUserId()
- + {
- + return fbId;
- + }
- +
- + /**
- + * @param id
- + * @param store
- + */
- + public void setFacebookUserId(String id, boolean store)
- + {
- + fbId = id;
- + if (store)
- + try (Connection con = L2DatabaseFactory.getInstance().getConnection())
- + {
- + PreparedStatement statement = con.prepareStatement("UPDATE characters SET facebook_user_id=? WHERE obj_id=?");
- + statement.setString(1, id);
- + statement.setInt(2, getObjectId());
- + statement.execute();
- + statement.close();
- + }
- + catch (Exception e)
- + {
- + _log.warning("Error could not update char facebook user id: " + e);
- + }
- + }
- }
- \ No newline at end of file
- Index: java/net/sf/l2j/gameserver/GameServer.java
- ===================================================================
- --- java/net/sf/l2j/gameserver/GameServer.java (revision 2)
- +++ java/net/sf/l2j/gameserver/GameServer.java (working copy)
- @@ -18,6 +18,7 @@
- import net.sf.l2j.Config;
- import net.sf.l2j.L2DatabaseFactory;
- +import net.sf.l2j.facebook.fb;
- import net.sf.l2j.gameserver.cache.CrestCache;
- import net.sf.l2j.gameserver.cache.HtmCache;
- import net.sf.l2j.gameserver.communitybbs.Manager.ForumsBBSManager;
- @@ -280,6 +281,8 @@
- _log.config("ItemHandler: Loaded " + ItemHandler.getInstance().size() + " handlers.");
- _log.config("SkillHandler: Loaded " + SkillHandler.getInstance().size() + " handlers.");
- _log.config("UserCommandHandler: Loaded " + UserCommandHandler.getInstance().size() + " handlers.");
- + StringUtil.printSection("Facebook");
- + fb.getInstance();
- StringUtil.printSection("System");
- Runtime.getRuntime().addShutdownHook(Shutdown.getInstance());
- Index: java/net/sf/l2j/gameserver/handler/UserCommandHandler.java
- ===================================================================
- --- java/net/sf/l2j/gameserver/handler/UserCommandHandler.java (revision 2)
- +++ java/net/sf/l2j/gameserver/handler/UserCommandHandler.java (working copy)
- @@ -10,6 +10,7 @@
- import net.sf.l2j.gameserver.handler.usercommandhandlers.ClanWarsList;
- import net.sf.l2j.gameserver.handler.usercommandhandlers.DisMount;
- import net.sf.l2j.gameserver.handler.usercommandhandlers.Escape;
- +import net.sf.l2j.gameserver.handler.usercommandhandlers.FaceBook;
- import net.sf.l2j.gameserver.handler.usercommandhandlers.Loc;
- import net.sf.l2j.gameserver.handler.usercommandhandlers.Mount;
- import net.sf.l2j.gameserver.handler.usercommandhandlers.OlympiadStat;
- @@ -41,6 +42,7 @@
- registerUserCommandHandler(new PartyInfo());
- registerUserCommandHandler(new SiegeStatus());
- registerUserCommandHandler(new Time());
- + registerUserCommandHandler(new FaceBook());
- }
- public void registerUserCommandHandler(IUserCommandHandler handler)
Add Comment
Please, Sign In to add comment