hassansyyid

Untitled

Aug 4th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.84 KB | None | 0 0
  1. package io.github.hsyyid.adminshop;
  2.  
  3. import io.github.hsyyid.adminshop.cmdexecutors.SetItemShopExecutor;
  4. import io.github.hsyyid.adminshop.utils.AdminShop;
  5. import io.github.hsyyid.adminshop.utils.LocationAdapter;
  6. import io.github.hsyyid.adminshop.utils.ShopItem;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.BufferedWriter;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileReader;
  13. import java.io.FileWriter;
  14. import java.io.IOException;
  15. import java.math.BigDecimal;
  16. import java.util.ArrayList;
  17. import java.util.Arrays;
  18.  
  19. import ninja.leaping.configurate.ConfigurationNode;
  20. import ninja.leaping.configurate.commented.CommentedConfigurationNode;
  21. import ninja.leaping.configurate.loader.ConfigurationLoader;
  22.  
  23. import org.slf4j.Logger;
  24. import org.spongepowered.api.Game;
  25. import org.spongepowered.api.block.BlockTypes;
  26. import org.spongepowered.api.block.tileentity.Sign;
  27. import org.spongepowered.api.data.manipulator.tileentity.SignData;
  28. import org.spongepowered.api.entity.player.Player;
  29. import org.spongepowered.api.event.Subscribe;
  30. import org.spongepowered.api.event.block.tileentity.SignChangeEvent;
  31. import org.spongepowered.api.event.entity.player.PlayerBreakBlockEvent;
  32. import org.spongepowered.api.event.entity.player.PlayerInteractBlockEvent;
  33. import org.spongepowered.api.event.state.ServerStartedEvent;
  34. import org.spongepowered.api.event.state.ServerStoppingEvent;
  35. import org.spongepowered.api.plugin.Plugin;
  36. import org.spongepowered.api.service.config.DefaultConfig;
  37. import org.spongepowered.api.text.Texts;
  38. import org.spongepowered.api.text.format.TextColors;
  39. import org.spongepowered.api.util.command.args.GenericArguments;
  40. import org.spongepowered.api.util.command.spec.CommandSpec;
  41. import org.spongepowered.api.world.Location;
  42. import org.spongepowered.api.world.TeleportHelper;
  43.  
  44. import com.erigitic.config.AccountManager;
  45. import com.erigitic.main.TotalEconomy;
  46. import com.google.gson.Gson;
  47. import com.google.gson.GsonBuilder;
  48. import com.google.inject.Inject;
  49.  
  50. @Plugin(id = "AdminShop", name = "AdminShop", version = "0.1", dependencies = "required-after:TotalEconomy")
  51. public class Main
  52. {
  53.     public static Game game = null;
  54.     public static ConfigurationNode config = null;
  55.     public static ConfigurationLoader<CommentedConfigurationNode> configurationManager;
  56.     public static TeleportHelper helper;
  57.     public static ArrayList<AdminShop> adminShops = new ArrayList<AdminShop>();
  58.     public static ArrayList<AdminShop> buyAdminShops = new ArrayList<AdminShop>();
  59.     public static ArrayList<ShopItem> items = new ArrayList<ShopItem>();
  60.     private Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Location.class, new LocationAdapter()).create();
  61.  
  62.     @Inject
  63.     private Logger logger;
  64.  
  65.     public Logger getLogger()
  66.     {
  67.         return logger;
  68.     }
  69.  
  70.     @Inject
  71.     @DefaultConfig(sharedRoot = true)
  72.     private File dConfig;
  73.  
  74.     @Inject
  75.     @DefaultConfig(sharedRoot = true)
  76.     private ConfigurationLoader<CommentedConfigurationNode> confManager;
  77.  
  78.     @Subscribe
  79.     public void onServerStart(ServerStartedEvent event)
  80.     {
  81.         getLogger().info("AdminShop loading...");
  82.  
  83.         String json = null;
  84.         String line = null;
  85.         try
  86.         {
  87.             // FileReader reads text files in the default encoding.
  88.             FileReader fileReader = new FileReader("AdminShops.json");
  89.  
  90.             // Always wrap FileReader in BufferedReader.
  91.             BufferedReader bufferedReader = new BufferedReader(fileReader);
  92.  
  93.             while ((line = bufferedReader.readLine()) != null)
  94.             {
  95.                 json = line;
  96.             }
  97.  
  98.             // Always close files.
  99.             bufferedReader.close();
  100.         }
  101.         catch (FileNotFoundException ex)
  102.         {
  103.  
  104.         }
  105.         catch (IOException ex)
  106.         {
  107.  
  108.         }
  109.  
  110.         adminShops = new ArrayList<AdminShop>(Arrays.asList(gson.fromJson(json, AdminShop[].class)));
  111.        
  112.         String j = null;
  113.         String l = null;
  114.         try
  115.         {
  116.             // FileReader reads text files in the default encoding.
  117.             FileReader fileReader = new FileReader("BuyAdminShops.json");
  118.  
  119.             // Always wrap FileReader in BufferedReader.
  120.             BufferedReader bufferedReader = new BufferedReader(fileReader);
  121.  
  122.             while ((l = bufferedReader.readLine()) != null)
  123.             {
  124.                 j = l;
  125.             }
  126.  
  127.             // Always close files.
  128.             bufferedReader.close();
  129.         }
  130.         catch (FileNotFoundException ex)
  131.         {
  132.  
  133.         }
  134.         catch (IOException ex)
  135.         {
  136.  
  137.         }
  138.  
  139.         buyAdminShops = new ArrayList<AdminShop>(Arrays.asList(gson.fromJson(j, AdminShop[].class)));
  140.  
  141.         game = event.getGame();
  142.         helper = game.getTeleportHelper();
  143.  
  144.         // Config File
  145.         try
  146.         {
  147.             if (!dConfig.exists())
  148.             {
  149.                 dConfig.createNewFile();
  150.                 config = confManager.load();
  151.                 confManager.save(config);
  152.             }
  153.             configurationManager = confManager;
  154.             config = confManager.load();
  155.  
  156.         }
  157.         catch (IOException exception)
  158.         {
  159.             getLogger().error("The default configuration could not be loaded or created!");
  160.         }
  161.  
  162.         CommandSpec setItemShopCommandSpec = CommandSpec.builder()
  163.             .description(Texts.of("Sets Item for a AdminShop"))
  164.             .permission("adminshop.setitem")
  165.             .arguments(GenericArguments.onlyOne(GenericArguments.string(Texts.of("item ID"))))
  166.             .executor(new SetItemShopExecutor())
  167.             .build();
  168.  
  169.         game.getCommandDispatcher().register(this, setItemShopCommandSpec, "setitem");
  170.  
  171.         getLogger().info("-----------------------------");
  172.         getLogger().info("AdminShop was made by HassanS6000!");
  173.         getLogger().info("Please post all errors on the Sponge Thread or on GitHub!");
  174.         getLogger().info("Have fun, and enjoy! :D");
  175.         getLogger().info("-----------------------------");
  176.         getLogger().info("AdminShop loaded!");
  177.     }
  178.  
  179.     @Subscribe
  180.     public void onServerStopping(ServerStoppingEvent event)
  181.     {
  182.         String json = gson.toJson(adminShops);
  183.         String j = gson.toJson(buyAdminShops);
  184.         try
  185.         {
  186.             // Assume default encoding.
  187.             FileWriter fileWriter = new FileWriter("AdminShops.json");
  188.  
  189.             // Always wrap FileWriter in BufferedWriter.
  190.             BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
  191.  
  192.             bufferedWriter.write(json);
  193.  
  194.             // Always close files.
  195.             bufferedWriter.close();
  196.         }
  197.         catch (IOException ex)
  198.         {
  199.             getLogger().error("Could not save JSON file!");
  200.         }
  201.  
  202.         try
  203.         {
  204.             // Assume default encoding.
  205.             FileWriter fileWriter = new FileWriter("BuyAdminShops.json");
  206.  
  207.             // Always wrap FileWriter in BufferedWriter.
  208.             BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
  209.  
  210.             bufferedWriter.write(j);
  211.  
  212.             // Always close files.
  213.             bufferedWriter.close();
  214.         }
  215.         catch (IOException ex)
  216.         {
  217.             getLogger().error("Could not save JSON file!");
  218.         }
  219.     }
  220.  
  221.     @Subscribe
  222.     public void onSignChange(SignChangeEvent event)
  223.     {
  224.         Player player = null;
  225.         if (event.getCause().isPresent() && event.getCause().get().getCause() instanceof Player)
  226.         {
  227.             player = (Player) event.getCause().get().getCause();
  228.         }
  229.  
  230.         Sign sign = event.getTile();
  231.         Location signLocation = sign.getBlock();
  232.         SignData signData = event.getNewData();
  233.         String line0 = Texts.toPlain(signData.getLine(0));
  234.         String line1 = Texts.toPlain(signData.getLine(1));
  235.         String line2 = Texts.toPlain(signData.getLine(2));
  236.         String line3 = Texts.toPlain(signData.getLine(3));
  237.  
  238.         if (line0.equals("[AdminShop]"))
  239.         {
  240.             if (player != null && player.hasPermission("adminshop.create"))
  241.             {
  242.                 int itemAmount = Integer.parseInt(line1);
  243.                 double price = Double.parseDouble(line2);
  244.                 String itemName = line3;
  245.                 AdminShop shop = new AdminShop(itemAmount, price, itemName, signLocation);
  246.                 adminShops.add(shop);
  247.                 signData.setLine(0, Texts.of(TextColors.DARK_BLUE, "[AdminShop]"));
  248.                 player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "Successfully created AdminShop!"));
  249.             }
  250.             else if (player != null)
  251.             {
  252.                 player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You do not have permission to create an AdminShop!"));
  253.             }
  254.         }
  255.         else if (line0.equals("[AdminShopSell]"))
  256.         {
  257.             if (player != null && player.hasPermission("adminshop.create"))
  258.             {
  259.                 int itemAmount = Integer.parseInt(line1);
  260.                 double price = Double.parseDouble(line2);
  261.                 String itemName = line3;
  262.                 AdminShop shop = new AdminShop(itemAmount, price, itemName, signLocation);
  263.                 buyAdminShops.add(shop);
  264.                 signData.setLine(0, Texts.of(TextColors.DARK_BLUE, "[AdminShopSell]"));
  265.                 player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "Successfully created AdminShop!"));
  266.             }
  267.             else if (player != null)
  268.             {
  269.                 player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You do not have permission to create an AdminShop!"));
  270.             }
  271.         }
  272.  
  273.         event.setNewData(signData);
  274.     }
  275.  
  276.     @Subscribe
  277.     public void onPlayerBreakBlock(PlayerBreakBlockEvent event)
  278.     {
  279.         if (event.getBlock().getBlock() != null && (event.getBlock().getBlock().getType() == BlockTypes.WALL_SIGN || event.getBlock().getBlock().getType() == BlockTypes.STANDING_SIGN))
  280.         {
  281.             AdminShop thisShop = null;
  282.             for (AdminShop shop : adminShops)
  283.             {
  284.                 if (shop.getSignLocation().getX() == event.getBlock().getX() && shop.getSignLocation().getY() == event.getBlock().getY() && shop.getSignLocation().getZ() == event.getBlock().getZ())
  285.                 {
  286.                     thisShop = shop;
  287.                 }
  288.             }
  289.  
  290.             if (thisShop != null && event.getEntity().hasPermission("adminshop.remove"))
  291.             {
  292.                 event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]:", TextColors.GREEN, " AdminShop successfully removed!"));
  293.                 adminShops.remove(thisShop);
  294.             }
  295.             else if (thisShop != null)
  296.             {
  297.                 event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: Error!", TextColors.RED, " you do not have permission to destroy AdminShops!"));
  298.                 event.setCancelled(true);
  299.             }
  300.             else
  301.             {
  302.                 AdminShop thisBuyShop = null;
  303.                 for (AdminShop shop : buyAdminShops)
  304.                 {
  305.                     if (shop.getSignLocation().getX() == event.getBlock().getX() && shop.getSignLocation().getY() == event.getBlock().getY() && shop.getSignLocation().getZ() == event.getBlock().getZ())
  306.                     {
  307.                         thisBuyShop = shop;
  308.                     }
  309.                 }
  310.  
  311.                 if (thisBuyShop != null && event.getEntity().hasPermission("adminshop.remove"))
  312.                 {
  313.                     event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]:", TextColors.GREEN, " AdminShop successfully removed!"));
  314.                     buyAdminShops.remove(thisBuyShop);
  315.                 }
  316.                 else if (thisBuyShop != null)
  317.                 {
  318.                     event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: Error!", TextColors.RED, " you do not have permission to destroy AdminShops!"));
  319.                     event.setCancelled(true);
  320.                 }
  321.             }
  322.         }
  323.     }
  324.  
  325.     @Subscribe
  326.     public void onPlayerInteractBlock(PlayerInteractBlockEvent event)
  327.     {
  328.         if (event.getBlock().getBlock() != null && (event.getBlock().getBlock().getType() == BlockTypes.WALL_SIGN || event.getBlock().getBlock().getType() == BlockTypes.STANDING_SIGN))
  329.         {
  330.             AdminShop thisShop = null;
  331.             for (AdminShop chestShop : adminShops)
  332.             {
  333.                 if (chestShop.getSignLocation().getX() == event.getBlock().getX() && chestShop.getSignLocation().getY() == event.getBlock().getY() && chestShop.getSignLocation().getZ() == event.getBlock().getZ())
  334.                 {
  335.                     thisShop = chestShop;
  336.                 }
  337.             }
  338.  
  339.             if (thisShop != null)
  340.             {
  341.                 ShopItem item = null;
  342.                 for (ShopItem i : items)
  343.                 {
  344.                     if (i.getPlayer().getUniqueId() == event.getEntity().getUniqueId())
  345.                     {
  346.                         item = i;
  347.                         break;
  348.                     }
  349.                 }
  350.  
  351.                 if (item != null)
  352.                 {
  353.                     adminShops.remove(thisShop);
  354.                     thisShop.setItemName(item.getItemID());
  355.                     adminShops.add(thisShop);
  356.                     items.remove(item);
  357.                     event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GREEN, "Successfully set new item ID."));
  358.                 }
  359.                 else
  360.                 {
  361.                     int itemAmount = thisShop.getItemAmount();
  362.                     double price = thisShop.getPrice();
  363.                     String itemName = thisShop.getItemName();
  364.  
  365.                     Player player = event.getEntity();
  366.                     TotalEconomy totalEconomy = (TotalEconomy) game.getPluginManager().getPlugin("TotalEconomy").get().getInstance();
  367.                     AccountManager accountManager = totalEconomy.getAccountManager();
  368.                     BigDecimal amount = new BigDecimal(price);
  369.  
  370.                     if (accountManager.getBalance(player).intValue() > amount.intValue())
  371.                     {
  372.                         accountManager.removeFromBalance(player, amount);
  373.                         player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "You have just bought " + itemAmount + " " + itemName + " for " + price + " dollars."));
  374.                         game.getCommandDispatcher().process(game.getServer().getConsole(), "give" + " " + player.getName() + " " + itemName + " " + itemAmount);
  375.                     }
  376.                     else
  377.                     {
  378.                         player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You don't have enough money to do that!"));
  379.                     }
  380.                 }
  381.             }
  382.             else
  383.             {
  384.                 AdminShop thisBuyShop = null;
  385.                 for (AdminShop chestShop : buyAdminShops)
  386.                 {
  387.                     if (chestShop.getSignLocation().getX() == event.getBlock().getX() && chestShop.getSignLocation().getY() == event.getBlock().getY() && chestShop.getSignLocation().getZ() == event.getBlock().getZ())
  388.                     {
  389.                         thisBuyShop = chestShop;
  390.                     }
  391.                 }
  392.  
  393.                 if (thisBuyShop != null)
  394.                 {
  395.                     ShopItem item = null;
  396.                     for (ShopItem i : items)
  397.                     {
  398.                         if (i.getPlayer().getUniqueId() == event.getEntity().getUniqueId())
  399.                         {
  400.                             item = i;
  401.                             break;
  402.                         }
  403.                     }
  404.  
  405.                     if (item != null)
  406.                     {
  407.                         buyAdminShops.remove(thisBuyShop);
  408.                         thisBuyShop.setItemName(item.getItemID());
  409.                         buyAdminShops.add(thisBuyShop);
  410.                         items.remove(item);
  411.                         event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GREEN, "Successfully set new item ID."));
  412.                     }
  413.                     else
  414.                     {
  415.                         int itemAmount = thisBuyShop.getItemAmount();
  416.                         double price = thisBuyShop.getPrice();
  417.                         String itemName = thisBuyShop.getItemName();
  418.  
  419.                         Player player = event.getEntity();
  420.                         TotalEconomy totalEconomy = (TotalEconomy) game.getPluginManager().getPlugin("TotalEconomy").get().getInstance();
  421.                         AccountManager accountManager = totalEconomy.getAccountManager();
  422.                         BigDecimal amount = new BigDecimal(price);
  423.  
  424.                         if (player.getItemInHand().isPresent() && player.getItemInHand().get().getItem().getName().equals(itemName) && player.getItemInHand().get().getQuantity() == itemAmount)
  425.                         {
  426.                             player.setItemInHand(null);
  427.                             accountManager.addToBalance(player, amount, true);
  428.                             player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "You have just sold " + itemAmount + " " + itemName + " for " + price + " dollars."));
  429.                         }
  430.                         else
  431.                         {
  432.                             player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You're not holding this item or the right quantity of this item!"));
  433.                         }
  434.                     }
  435.                 }
  436.             }
  437.         }
  438.     }
  439.  
  440.     public static ConfigurationLoader<CommentedConfigurationNode> getConfigManager()
  441.     {
  442.         return configurationManager;
  443.     }
  444. }
Advertisement
Add Comment
Please, Sign In to add comment