|
| 1 | +package io.github.hsyyid.adminshop; |
| 2 | + |
| 3 | +import io.github.hsyyid.adminshop.utils.AdminShop; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.math.BigDecimal; |
| 8 | +import java.util.ArrayList; |
| 9 | + |
| 10 | +import ninja.leaping.configurate.ConfigurationNode; |
| 11 | +import ninja.leaping.configurate.commented.CommentedConfigurationNode; |
| 12 | +import ninja.leaping.configurate.loader.ConfigurationLoader; |
| 13 | + |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.spongepowered.api.Game; |
| 16 | +import org.spongepowered.api.block.BlockTypes; |
| 17 | +import org.spongepowered.api.block.tileentity.Sign; |
| 18 | +import org.spongepowered.api.data.manipulator.tileentity.SignData; |
| 19 | +import org.spongepowered.api.entity.player.Player; |
| 20 | +import org.spongepowered.api.event.Subscribe; |
| 21 | +import org.spongepowered.api.event.block.tileentity.SignChangeEvent; |
| 22 | +import org.spongepowered.api.event.entity.player.PlayerBreakBlockEvent; |
| 23 | +import org.spongepowered.api.event.entity.player.PlayerInteractBlockEvent; |
| 24 | +import org.spongepowered.api.event.state.ServerStartedEvent; |
| 25 | +import org.spongepowered.api.plugin.Plugin; |
| 26 | +import org.spongepowered.api.service.config.DefaultConfig; |
| 27 | +import org.spongepowered.api.text.Texts; |
| 28 | +import org.spongepowered.api.text.format.TextColors; |
| 29 | +import org.spongepowered.api.world.Location; |
| 30 | +import org.spongepowered.api.world.TeleportHelper; |
| 31 | + |
| 32 | +import com.erigitic.config.AccountManager; |
| 33 | +import com.erigitic.main.TotalEconomy; |
| 34 | +import com.google.inject.Inject; |
| 35 | + |
| 36 | +@Plugin(id = "AdminShop", name = "AdminShop", version = "0.1", dependencies = "required-after:TotalEconomy") |
| 37 | +public class Main |
| 38 | +{ |
| 39 | + public static Game game = null; |
| 40 | + public static ConfigurationNode config = null; |
| 41 | + public static ConfigurationLoader<CommentedConfigurationNode> configurationManager; |
| 42 | + public static TeleportHelper helper; |
| 43 | + public static ArrayList<AdminShop> adminShops = new ArrayList<AdminShop>(); |
| 44 | + |
| 45 | + @Inject |
| 46 | + private Logger logger; |
| 47 | + |
| 48 | + public Logger getLogger() |
| 49 | + { |
| 50 | + return logger; |
| 51 | + } |
| 52 | + |
| 53 | + @Inject |
| 54 | + @DefaultConfig(sharedRoot = true) |
| 55 | + private File dConfig; |
| 56 | + |
| 57 | + @Inject |
| 58 | + @DefaultConfig(sharedRoot = true) |
| 59 | + private ConfigurationLoader<CommentedConfigurationNode> confManager; |
| 60 | + |
| 61 | + @Subscribe |
| 62 | + public void onServerStart(ServerStartedEvent event) |
| 63 | + { |
| 64 | + getLogger().info("AdminShop loading..."); |
| 65 | + game = event.getGame(); |
| 66 | + helper = game.getTeleportHelper(); |
| 67 | + // Config File |
| 68 | + try |
| 69 | + { |
| 70 | + if (!dConfig.exists()) |
| 71 | + { |
| 72 | + dConfig.createNewFile(); |
| 73 | + config = confManager.load(); |
| 74 | + confManager.save(config); |
| 75 | + } |
| 76 | + configurationManager = confManager; |
| 77 | + config = confManager.load(); |
| 78 | + |
| 79 | + } |
| 80 | + catch (IOException exception) |
| 81 | + { |
| 82 | + getLogger().error("The default configuration could not be loaded or created!"); |
| 83 | + } |
| 84 | + |
| 85 | + getLogger().info("-----------------------------"); |
| 86 | + getLogger().info("AdminShop was made by HassanS6000!"); |
| 87 | + getLogger().info("Please post all errors on the Sponge Thread or on GitHub!"); |
| 88 | + getLogger().info("Have fun, and enjoy! :D"); |
| 89 | + getLogger().info("-----------------------------"); |
| 90 | + getLogger().info("AdminShop loaded!"); |
| 91 | + } |
| 92 | + |
| 93 | + @Subscribe |
| 94 | + public void onSignChange(SignChangeEvent event) |
| 95 | + { |
| 96 | + Player player = null; |
| 97 | + if(event.getCause().isPresent() && event.getCause().get().getCause() instanceof Player) |
| 98 | + { |
| 99 | + player = (Player) event.getCause().get().getCause(); |
| 100 | + } |
| 101 | + |
| 102 | + Sign sign = event.getTile(); |
| 103 | + Location signLocation = sign.getBlock(); |
| 104 | + SignData signData = event.getNewData(); |
| 105 | + String line0 = Texts.toPlain(signData.getLine(0)); |
| 106 | + String line1 = Texts.toPlain(signData.getLine(1)); |
| 107 | + String line2 = Texts.toPlain(signData.getLine(2)); |
| 108 | + String line3 = Texts.toPlain(signData.getLine(3)); |
| 109 | + |
| 110 | + if (line0.equals("[AdminShop]")) |
| 111 | + { |
| 112 | + if(player != null && player.hasPermission("adminshop.create")) |
| 113 | + { |
| 114 | + int itemAmount = Integer.parseInt(line1); |
| 115 | + double price = Double.parseDouble(line2); |
| 116 | + String itemName = line3; |
| 117 | + AdminShop shop = new AdminShop(itemAmount, price, itemName, signLocation); |
| 118 | + adminShops.add(shop); |
| 119 | + signData.setLine(0, Texts.of(TextColors.DARK_BLUE, "[AdminShop]")); |
| 120 | + player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "Successfully created AdminShop!")); |
| 121 | + } |
| 122 | + else if(player != null) |
| 123 | + { |
| 124 | + player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You do not have permission to create an AdminShop!")); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + event.setNewData(signData); |
| 129 | + } |
| 130 | + |
| 131 | + @Subscribe |
| 132 | + public void onPlayerBreakBlock(PlayerBreakBlockEvent event) |
| 133 | + { |
| 134 | + if (event.getBlock().getBlock() != null && event.getBlock().getBlock().getType() == BlockTypes.WALL_SIGN) |
| 135 | + { |
| 136 | + AdminShop thisShop = null; |
| 137 | + for (AdminShop shop : adminShops) |
| 138 | + { |
| 139 | + if (shop.getSignLocation().getX() == event.getBlock().getX() && shop.getSignLocation().getY() == event.getBlock().getY() && shop.getSignLocation().getZ() == event.getBlock().getZ()) |
| 140 | + { |
| 141 | + thisShop = shop; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if (thisShop != null) |
| 146 | + { |
| 147 | + adminShops.remove(thisShop); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Subscribe |
| 153 | + public void onPlayerInteractBlock(PlayerInteractBlockEvent event) |
| 154 | + { |
| 155 | + if (event.getBlock().getBlock() != null && (event.getBlock().getBlock().getType() == BlockTypes.WALL_SIGN || event.getBlock().getBlock().getType() == BlockTypes.STANDING_SIGN)) |
| 156 | + { |
| 157 | + AdminShop thisShop = null; |
| 158 | + for (AdminShop chestShop : adminShops) |
| 159 | + { |
| 160 | + if (chestShop.getSignLocation().getX() == event.getBlock().getX() && chestShop.getSignLocation().getY() == event.getBlock().getY() && chestShop.getSignLocation().getZ() == event.getBlock().getZ()) |
| 161 | + { |
| 162 | + thisShop = chestShop; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (thisShop != null) |
| 167 | + { |
| 168 | + int itemAmount = thisShop.getItemAmount(); |
| 169 | + double price = thisShop.getPrice(); |
| 170 | + String itemName = thisShop.getItemName(); |
| 171 | + |
| 172 | + Player player = event.getEntity(); |
| 173 | + TotalEconomy totalEconomy = (TotalEconomy) game.getPluginManager().getPlugin("TotalEconomy").get().getInstance(); |
| 174 | + AccountManager accountManager = totalEconomy.getAccountManager(); |
| 175 | + BigDecimal amount = new BigDecimal(price); |
| 176 | + |
| 177 | + if (accountManager.getBalance(player).intValue() > amount.intValue()) |
| 178 | + { |
| 179 | + accountManager.removeFromBalance(player, amount); |
| 180 | + player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GOLD, "You have just bought " + itemAmount + " " + itemName + " for " + price + " dollars.")); |
| 181 | + game.getCommandDispatcher().process(game.getServer().getConsole(), "give" + " " + player.getName() + " " + itemName + " " + itemAmount); |
| 182 | + } |
| 183 | + else |
| 184 | + { |
| 185 | + player.sendMessage(Texts.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You don't have enough money to do that!")); |
| 186 | + } |
| 187 | + |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + public static ConfigurationLoader<CommentedConfigurationNode> getConfigManager() |
| 193 | + { |
| 194 | + return configurationManager; |
| 195 | + } |
| 196 | +} |
0 commit comments