Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.45 KB | None | 0 0
  1. package com.mistrix.REST.commands;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.Scanner;
  8.  
  9. import org.json.JSONObject;
  10.  
  11. import com.mistrix.GameServer;
  12. import com.mistrix.REST.configuration.Configuration;
  13. import com.mistrix.engine.task.Task;
  14. import com.mistrix.engine.task.TaskManager;
  15. import com.mistrix.model.Locations.Location;
  16. import com.mistrix.model.PlayerRights;
  17. import com.mistrix.model.Position;
  18. import com.mistrix.world.World;
  19. import com.mistrix.world.content.FountainOfLuck;
  20. import com.mistrix.world.content.PlayerPunishment;
  21. import com.mistrix.world.content.clan.ClanChatManager;
  22. import com.mistrix.world.content.grandexchange.GrandExchangeOffers;
  23. import com.mistrix.world.entity.impl.player.Player;
  24. import com.mistrix.world.entity.impl.player.PlayerSaving;
  25.  
  26. /*
  27.  * @author Luka Furlan
  28.  */
  29. public class Commands {
  30.    
  31.     public static Map<String, String> trivia = new HashMap<>();
  32.  
  33.     public static String getPlayerCount(String idk) {
  34.         String playerCount = World.players.size() + "";
  35.         return playerCount;
  36.     }
  37.    
  38.     public static String getPlayers(String idk) {
  39.         Map<String, String> playerList = new HashMap<>();
  40.         for (Player players : World.getPlayers()) {
  41.             if (players != null) {
  42.                 playerList.put(players.getUsername(), players.getRights().toString());
  43.             }
  44.         }
  45.         JSONObject playerData = new JSONObject(playerList);
  46.         return playerData.toString();
  47.     }
  48.    
  49.     public static String isOnline(String parameters) {
  50.         Map<String, String> params = Configuration.queryToMap(parameters);
  51.         Map<String, String> playerList = new HashMap<>();
  52.         for (Player players : World.getPlayers()) {
  53.             if (players != null) {
  54.                 playerList.put(players.getUsername(), players.getRights().toString());
  55.             }
  56.         }
  57.         if(playerList.get(params.get("playerName")) != null) {
  58.             return "true";
  59.         } else {
  60.             return "false";
  61.         }
  62.     }
  63.    
  64.     public static void saveAll(String idk) {
  65.         World.savePlayers();
  66.         return;
  67.     }
  68.    
  69.     public static void sendGlobalMessage(String parameters) {
  70.         Map<String, String> params = Configuration.queryToMap(parameters);
  71.         World.sendMessage("<img=10><col=2E64FE> "+ params.get("message").replace("-", " "));
  72.     }
  73.    
  74.     public static void teleport(String parameters) {
  75.         Map<String, String> params = Configuration.queryToMap(parameters);
  76.         Player player = World.getPlayerByName(params.get("playerName"));
  77.         int x = Integer.parseInt(params.get("x"));
  78.         int y = Integer.parseInt(params.get("y"));
  79.         player.moveTo(new Position(x, y, 0));
  80.         player.getPacketSender().sendMessage("<img=10><col=2E64FE> You have been teleported by Server");
  81.     }
  82.    
  83.     public static void updateServer(String parameters) {
  84.         Map<String, String> params = Configuration.queryToMap(parameters);
  85.         if(Integer.parseInt(params.get("ticks")) > 0) {
  86.             GameServer.setUpdating(true);
  87.             for (Player players : World.getPlayers()) {
  88.                 if (players == null)
  89.                     continue;
  90.                 players.getPacketSender().sendSystemUpdate(Integer.parseInt(params.get("ticks")));
  91.             }
  92.             TaskManager.submit(new Task(Integer.parseInt(params.get("ticks"))) {
  93.                 @Override
  94.                 protected void execute() {
  95.                     for (Player player : World.getPlayers()) {
  96.                         if (player != null) {
  97.                             World.deregister(player);
  98.                         }
  99.                     }
  100.                     FountainOfLuck.save();
  101.                     GrandExchangeOffers.save();
  102.                     ClanChatManager.save();
  103.                     GameServer.getLogger().info("Update task finished!");
  104.                     System.exit(0);
  105.                     stop();
  106.                 }
  107.             });
  108.         }
  109.     }
  110.    
  111.     public static String giveItem(String parameters) {
  112.         Map<String, String> params = Configuration.queryToMap(parameters);
  113.         Player player = World.getPlayerByName(params.get("playerName"));
  114.         if(player.getInventory().getFreeSlots() >= Integer.parseInt(params.get("quantity"))) {
  115.             return "false";
  116.         } else {
  117.             player.getInventory().add(Integer.parseInt(params.get("item")), Integer.parseInt(params.get("quantity")));
  118.             return "true";
  119.         }
  120.     }
  121.    
  122.     public static void promote(String parameters) {
  123.         Map<String, String> params = Configuration.queryToMap(parameters);
  124.         Player player = World.getPlayerByName(params.get("playerName"));
  125.         switch(params.get("promote")) {
  126.         case "serverSupport":
  127.             player.setRights(PlayerRights.SUPPORT);
  128.             World.deregister(player);
  129.             break;
  130.         case "moderator":
  131.             player.setRights(PlayerRights.MODERATOR);
  132.             World.deregister(player);
  133.             break;
  134.         case "administrator":
  135.             player.setRights(PlayerRights.ADMINISTRATOR);
  136.             World.deregister(player);
  137.             break;
  138.         case "demote":
  139.             player.setRights(PlayerRights.PLAYER);
  140.             World.deregister(player);
  141.             break;
  142.         }
  143.     }
  144.    
  145.    
  146.     public static String getPlayer(String parameters) throws FileNotFoundException {
  147.         Map<String, String> params = Configuration.queryToMap(parameters);
  148.         Scanner scanner = new Scanner( new File("./data/saves/characters/" + params.get("playerName") + ".json") );
  149.         String text = scanner.useDelimiter("\\A").next();
  150.         scanner.close();
  151.         return text;
  152.     }
  153.    
  154.     public static void ban(String parameters) {
  155.         Map<String, String> params = Configuration.queryToMap(parameters);
  156.         String playerToBan = params.get("playerName");
  157.         if(!PlayerSaving.playerExists(playerToBan)) {
  158.             return;
  159.         } else {
  160.             if(PlayerPunishment.banned(playerToBan)) {
  161.                 return;
  162.             }
  163.             PlayerPunishment.ban(playerToBan);
  164.             Player toBan = World.getPlayerByName(playerToBan);
  165.             if(toBan != null) {
  166.                 World.deregister(toBan);
  167.             }
  168.         }
  169.         return;
  170.     }
  171.    
  172.     public static void kick(String parameters) {
  173.         Map<String, String> params = Configuration.queryToMap(parameters);
  174.         String playerToKick = params.get("playerName");
  175.         Player toKick = World.getPlayerByName(playerToKick);
  176.         if(toKick == null) {
  177.             return;
  178.         } else if(toKick.getLocation() != Location.WILDERNESS) {
  179.             World.deregister(toKick);
  180.         }
  181.         return;
  182.     }
  183.    
  184.     public static void mute(String parameters) {
  185.         Map<String, String> params = Configuration.queryToMap(parameters);
  186.         String playerToMute = params.get("playerName");
  187.         Player toMute = World.getPlayerByName(playerToMute);
  188.         if(!PlayerSaving.playerExists(playerToMute)) {
  189.             return;
  190.         } else {
  191.             if(PlayerPunishment.muted(playerToMute)) {
  192.                 return;
  193.             }
  194.             PlayerPunishment.mute(playerToMute);
  195.             toMute.getPacketSender().sendMessage("<img=10><col=2E64FE> You have been muted by Server");
  196.         }
  197.         return;
  198.     }
  199.    
  200.     public static void addTriviaQuestion(String parameters) {
  201.         Map<String, String> params = Configuration.queryToMap(parameters);
  202.         trivia.put(params.get("question").replace("-", " "), params.get("anwser").replace("-", " "));
  203.         World.sendMessage("<img=10><col=2E64FE>[TRIVIA] "+params.get("question").replace("-", " "));
  204.     }
  205.    
  206.     public static void removeTriviaQuestion(String parameters) {
  207.         Map<String, String> params = Configuration.queryToMap(parameters);
  208.         trivia.remove(params.get("question").replace("-", " "));
  209.         World.sendMessage("<img=10><col=2E64FE>[TRIVIA] Question: "+params.get("question").replace("-", " ")+ " is no longer active");
  210.     }
  211.    
  212.     public static String getTriviaQuestions(String parameters) {
  213.         JSONObject triviaObject = new JSONObject(trivia);
  214.         return triviaObject.toString();
  215.     }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement