Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.github.hsyyid.adminshop;
- import io.github.hsyyid.adminshop.cmdexecutors.SetItemShopExecutor;
- import io.github.hsyyid.adminshop.utils.AdminShop;
- import io.github.hsyyid.adminshop.utils.LocationAdapter;
- import io.github.hsyyid.adminshop.utils.ShopItem;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.Arrays;
- import ninja.leaping.configurate.ConfigurationNode;
- import ninja.leaping.configurate.commented.CommentedConfigurationNode;
- import ninja.leaping.configurate.loader.ConfigurationLoader;
- import org.slf4j.Logger;
- import org.spongepowered.api.Game;
- import org.spongepowered.api.block.BlockTypes;
- import org.spongepowered.api.block.tileentity.Sign;
- import org.spongepowered.api.data.manipulator.tileentity.SignData;
- import org.spongepowered.api.entity.player.Player;
- import org.spongepowered.api.event.Subscribe;
- import org.spongepowered.api.event.block.tileentity.SignChangeEvent;
- import org.spongepowered.api.event.entity.player.PlayerBreakBlockEvent;
- import org.spongepowered.api.event.entity.player.PlayerInteractBlockEvent;
- import org.spongepowered.api.event.state.ServerStartedEvent;
- import org.spongepowered.api.event.state.ServerStoppingEvent;
- import org.spongepowered.api.plugin.Plugin;
- import org.spongepowered.api.service.config.DefaultConfig;
- import org.spongepowered.api.text.Texts;
- import org.spongepowered.api.text.format.TextColors;
- import org.spongepowered.api.util.command.args.GenericArguments;
- import org.spongepowered.api.util.command.spec.CommandSpec;
- import org.spongepowered.api.world.Location;
- import org.spongepowered.api.world.TeleportHelper;
- import com.erigitic.config.AccountManager;
- import com.erigitic.main.TotalEconomy;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.inject.Inject;
- @Plugin(id = "AdminShop", name = "AdminShop", version = "0.1", dependencies = "required-after:TotalEconomy")
- public class Main
- {
- public static Game game = null;
- public static ConfigurationNode config = null;
- public static ConfigurationLoader<CommentedConfigurationNode> configurationManager;
- public static TeleportHelper helper;
- public static ArrayList<AdminShop> adminShops = new ArrayList<AdminShop>();
- public static ArrayList<AdminShop> buyAdminShops = new ArrayList<AdminShop>();
- public static ArrayList<ShopItem> items = new ArrayList<ShopItem>();
- private Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(Location.class, new LocationAdapter()).create();
- @Inject
- private Logger logger;
- public Logger getLogger()
- {
- return logger;
- }
- @Inject
- @DefaultConfig(sharedRoot = true)
- private File dConfig;
- @Inject
- @DefaultConfig(sharedRoot = true)
- private ConfigurationLoader<CommentedConfigurationNode> confManager;
- @Subscribe
- public void onServerStart(ServerStartedEvent event)
- {
- getLogger().info("AdminShop loading...");
- String json = null;
- String line = null;
- try
- {
- // FileReader reads text files in the default encoding.
- FileReader fileReader = new FileReader("AdminShops.json");
- // Always wrap FileReader in BufferedReader.
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- while ((line = bufferedReader.readLine()) != null)
- {
- json = line;
- }
- // Always close files.
- bufferedReader.close();
- }
- catch (FileNotFoundException ex)
- {
- }
- catch (IOException ex)
- {
- }
- adminShops = new ArrayList<AdminShop>(Arrays.asList(gson.fromJson(json, AdminShop[].class)));
- String j = null;
- String l = null;
- try
- {
- // FileReader reads text files in the default encoding.
- FileReader fileReader = new FileReader("BuyAdminShops.json");
- // Always wrap FileReader in BufferedReader.
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- while ((l = bufferedReader.readLine()) != null)
- {
- j = l;
- }
- // Always close files.
- bufferedReader.close();
- }
- catch (FileNotFoundException ex)
- {
- }
- catch (IOException ex)
- {
- }
- buyAdminShops = new ArrayList<AdminShop>(Arrays.asList(gson.fromJson(j, AdminShop[].class)));
- game = event.getGame();
- helper = game.getTeleportHelper();
- // Config File
- try
- {
- if (!dConfig.exists())
- {
- dConfig.createNewFile();
- config = confManager.load();
- confManager.save(config);
- }
- configurationManager = confManager;
- config = confManager.load();
- }
- catch (IOException exception)
- {
- getLogger().error("The default configuration could not be loaded or created!");
- }
- CommandSpec setItemShopCommandSpec = CommandSpec.builder()
- .description(Texts.of("Sets Item for a AdminShop"))
- .permission("adminshop.setitem")
- .arguments(GenericArguments.onlyOne(GenericArguments.string(Texts.of("item ID"))))
- .executor(new SetItemShopExecutor())
- .build();
- game.getCommandDispatcher().register(this, setItemShopCommandSpec, "setitem");
- getLogger().info("-----------------------------");
- getLogger().info("AdminShop was made by HassanS6000!");
- getLogger().info("Please post all errors on the Sponge Thread or on GitHub!");
- getLogger().info("Have fun, and enjoy! :D");
- getLogger().info("-----------------------------");
- getLogger().info("AdminShop loaded!");
- }
- @Subscribe
- public void onServerStopping(ServerStoppingEvent event)
- {
- String json = gson.toJson(adminShops);
- String j = gson.toJson(buyAdminShops);
- try
- {
- // Assume default encoding.
- FileWriter fileWriter = new FileWriter("AdminShops.json");
- // Always wrap FileWriter in BufferedWriter.
- BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
- bufferedWriter.write(json);
- // Always close files.
- bufferedWriter.close();
- }
- catch (IOException ex)
- {
- getLogger().error("Could not save JSON file!");
- }
- try
- {
- // Assume default encoding.
- FileWriter fileWriter = new FileWriter("BuyAdminShops.json");
- // Always wrap FileWriter in BufferedWriter.
- BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
- bufferedWriter.write(j);
- // Always close files.
- bufferedWriter.close();
- }
- catch (IOException ex)
- {
- getLogger().error("Could not save JSON file!");
- }
- }
- @Subscribe
- public void onSignChange(SignChangeEvent event)
- {
- Player player = null;
- if (event.getCause().isPresent() && event.getCause().get().getCause() instanceof Player)
- {
- player = (Player) event.getCause().get().getCause();
- }
- Sign sign = event.getTile();
- Location signLocation = sign.getBlock();
- SignData signData = event.getNewData();
- String line0 = Texts.toPlain(signData.getLine(0));
- String line1 = Texts.toPlain(signData.getLine(1));
- String line2 = Texts.toPlain(signData.getLine(2));
- String line3 = Texts.toPlain(signData.getLine(3));
- if (line0.equals("[AdminShop]"))
- {
- if (player != null && player.hasPermission("adminshop.create"))
- {
- int itemAmount = Integer.parseInt(line1);
- double price = Double.parseDouble(line2);
- String itemName = line3;
- AdminShop shop = new AdminShop(itemAmount, price, itemName, signLocation);
- adminShops.add(shop);
- signData.setLine(0, Texts.of(TextColors.DARK_BLUE, "[AdminShop]"));
- player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "Successfully created AdminShop!"));
- }
- else if (player != null)
- {
- player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You do not have permission to create an AdminShop!"));
- }
- }
- else if (line0.equals("[AdminShopSell]"))
- {
- if (player != null && player.hasPermission("adminshop.create"))
- {
- int itemAmount = Integer.parseInt(line1);
- double price = Double.parseDouble(line2);
- String itemName = line3;
- AdminShop shop = new AdminShop(itemAmount, price, itemName, signLocation);
- buyAdminShops.add(shop);
- signData.setLine(0, Texts.of(TextColors.DARK_BLUE, "[AdminShopSell]"));
- player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "Successfully created AdminShop!"));
- }
- else if (player != null)
- {
- player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You do not have permission to create an AdminShop!"));
- }
- }
- event.setNewData(signData);
- }
- @Subscribe
- public void onPlayerBreakBlock(PlayerBreakBlockEvent event)
- {
- if (event.getBlock().getBlock() != null && (event.getBlock().getBlock().getType() == BlockTypes.WALL_SIGN || event.getBlock().getBlock().getType() == BlockTypes.STANDING_SIGN))
- {
- AdminShop thisShop = null;
- for (AdminShop shop : adminShops)
- {
- if (shop.getSignLocation().getX() == event.getBlock().getX() && shop.getSignLocation().getY() == event.getBlock().getY() && shop.getSignLocation().getZ() == event.getBlock().getZ())
- {
- thisShop = shop;
- }
- }
- if (thisShop != null && event.getEntity().hasPermission("adminshop.remove"))
- {
- event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]:", TextColors.GREEN, " AdminShop successfully removed!"));
- adminShops.remove(thisShop);
- }
- else if (thisShop != null)
- {
- event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: Error!", TextColors.RED, " you do not have permission to destroy AdminShops!"));
- event.setCancelled(true);
- }
- else
- {
- AdminShop thisBuyShop = null;
- for (AdminShop shop : buyAdminShops)
- {
- if (shop.getSignLocation().getX() == event.getBlock().getX() && shop.getSignLocation().getY() == event.getBlock().getY() && shop.getSignLocation().getZ() == event.getBlock().getZ())
- {
- thisBuyShop = shop;
- }
- }
- if (thisBuyShop != null && event.getEntity().hasPermission("adminshop.remove"))
- {
- event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]:", TextColors.GREEN, " AdminShop successfully removed!"));
- buyAdminShops.remove(thisBuyShop);
- }
- else if (thisBuyShop != null)
- {
- event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: Error!", TextColors.RED, " you do not have permission to destroy AdminShops!"));
- event.setCancelled(true);
- }
- }
- }
- }
- @Subscribe
- public void onPlayerInteractBlock(PlayerInteractBlockEvent event)
- {
- if (event.getBlock().getBlock() != null && (event.getBlock().getBlock().getType() == BlockTypes.WALL_SIGN || event.getBlock().getBlock().getType() == BlockTypes.STANDING_SIGN))
- {
- AdminShop thisShop = null;
- for (AdminShop chestShop : adminShops)
- {
- if (chestShop.getSignLocation().getX() == event.getBlock().getX() && chestShop.getSignLocation().getY() == event.getBlock().getY() && chestShop.getSignLocation().getZ() == event.getBlock().getZ())
- {
- thisShop = chestShop;
- }
- }
- if (thisShop != null)
- {
- ShopItem item = null;
- for (ShopItem i : items)
- {
- if (i.getPlayer().getUniqueId() == event.getEntity().getUniqueId())
- {
- item = i;
- break;
- }
- }
- if (item != null)
- {
- adminShops.remove(thisShop);
- thisShop.setItemName(item.getItemID());
- adminShops.add(thisShop);
- items.remove(item);
- event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GREEN, "Successfully set new item ID."));
- }
- else
- {
- int itemAmount = thisShop.getItemAmount();
- double price = thisShop.getPrice();
- String itemName = thisShop.getItemName();
- Player player = event.getEntity();
- TotalEconomy totalEconomy = (TotalEconomy) game.getPluginManager().getPlugin("TotalEconomy").get().getInstance();
- AccountManager accountManager = totalEconomy.getAccountManager();
- BigDecimal amount = new BigDecimal(price);
- if (accountManager.getBalance(player).intValue() > amount.intValue())
- {
- accountManager.removeFromBalance(player, amount);
- player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "You have just bought " + itemAmount + " " + itemName + " for " + price + " dollars."));
- game.getCommandDispatcher().process(game.getServer().getConsole(), "give" + " " + player.getName() + " " + itemName + " " + itemAmount);
- }
- else
- {
- player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You don't have enough money to do that!"));
- }
- }
- }
- else
- {
- AdminShop thisBuyShop = null;
- for (AdminShop chestShop : buyAdminShops)
- {
- if (chestShop.getSignLocation().getX() == event.getBlock().getX() && chestShop.getSignLocation().getY() == event.getBlock().getY() && chestShop.getSignLocation().getZ() == event.getBlock().getZ())
- {
- thisBuyShop = chestShop;
- }
- }
- if (thisBuyShop != null)
- {
- ShopItem item = null;
- for (ShopItem i : items)
- {
- if (i.getPlayer().getUniqueId() == event.getEntity().getUniqueId())
- {
- item = i;
- break;
- }
- }
- if (item != null)
- {
- buyAdminShops.remove(thisBuyShop);
- thisBuyShop.setItemName(item.getItemID());
- buyAdminShops.add(thisBuyShop);
- items.remove(item);
- event.getEntity().sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GREEN, "Successfully set new item ID."));
- }
- else
- {
- int itemAmount = thisBuyShop.getItemAmount();
- double price = thisBuyShop.getPrice();
- String itemName = thisBuyShop.getItemName();
- Player player = event.getEntity();
- TotalEconomy totalEconomy = (TotalEconomy) game.getPluginManager().getPlugin("TotalEconomy").get().getInstance();
- AccountManager accountManager = totalEconomy.getAccountManager();
- BigDecimal amount = new BigDecimal(price);
- if (player.getItemInHand().isPresent() && player.getItemInHand().get().getItem().getName().equals(itemName) && player.getItemInHand().get().getQuantity() == itemAmount)
- {
- player.setItemInHand(null);
- accountManager.addToBalance(player, amount, true);
- player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "You have just sold " + itemAmount + " " + itemName + " for " + price + " dollars."));
- }
- else
- {
- 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!"));
- }
- }
- }
- }
- }
- }
- public static ConfigurationLoader<CommentedConfigurationNode> getConfigManager()
- {
- return configurationManager;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment