Skip to content

Commit

Permalink
Updated to Events 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hsyyid committed Sep 8, 2015
1 parent 377d56c commit d0e738c
Showing 1 changed file with 113 additions and 104 deletions.
217 changes: 113 additions & 104 deletions src/main/java/io/github/hsyyid/chestshop/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void onServerStart(GameStartedServerEvent event)
}

String json = null;

try
{
json = readFile("ChestShops.json", StandardCharsets.UTF_8);
Expand All @@ -108,7 +108,7 @@ public void onServerStart(GameStartedServerEvent event)
{
getLogger().error("Could not read JSON file!");
}

if(json != null)
{
chestShops = new ArrayList<ChestShop>(Arrays.asList(gson.fromJson(json, ChestShop[].class)));
Expand All @@ -117,24 +117,24 @@ public void onServerStart(GameStartedServerEvent event)
{
getLogger().error("No JSON data read.");
}

getLogger().info("-----------------------------");
getLogger().info("ChestShop 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("ChestShop loaded!");
}

static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}

@Listener

public void onServerStoppingEvent(GameStoppingServerEvent event)
{
String json = gson.toJson(chestShops);
Expand All @@ -157,142 +157,151 @@ public void onServerStoppingEvent(GameStoppingServerEvent event)
getLogger().error("Could not save JSON file!");
}
}

@Listener
public void onSignChange(ChangeSignEvent.SourcePlayer event)
public void onSignChange(ChangeSignEvent event)
{
Player owner = event.getSourceEntity();
Sign sign = event.getTargetTile();
Location<World> signLocation = sign.getLocation();
double y = signLocation.getY() - 1;
Location<World> chestLocation = new Location<World>(signLocation.getExtent(), signLocation.getX(), y, signLocation.getZ());
SignData signData = event.getText();

String line0 = Texts.toPlain(signData.getValue(Keys.SIGN_LINES).get().get(0));
String line1 = Texts.toPlain(signData.getValue(Keys.SIGN_LINES).get().get(1));
String line2 = Texts.toPlain(signData.getValue(Keys.SIGN_LINES).get().get(2));
String line3 = Texts.toPlain(signData.getValue(Keys.SIGN_LINES).get().get(3));

if (line0.equals("[ChestShop]"))
if(event.getCause().first(Player.class).isPresent())
{
if (chestLocation.getBlock() != null && chestLocation.getBlock().getType().equals(BlockTypes.CHEST))
Player owner = (Player) event.getCause().first(Player.class).get();
Sign sign = event.getTargetTile();
Location<World> signLocation = sign.getLocation();
double y = signLocation.getY() - 1;
Location<World> chestLocation = new Location<World>(signLocation.getExtent(), signLocation.getX(), y, signLocation.getZ());
SignData signData = event.getText();

String line0 = Texts.toPlain(signData.getValue(Keys.SIGN_LINES).get().get(0));
String line1 = Texts.toPlain(signData.getValue(Keys.SIGN_LINES).get().get(1));
String line2 = Texts.toPlain(signData.getValue(Keys.SIGN_LINES).get().get(2));
String line3 = Texts.toPlain(signData.getValue(Keys.SIGN_LINES).get().get(3));

if (line0.equals("[ChestShop]"))
{
signData.set(signData.getValue(Keys.SIGN_LINES).get().set(0, Texts.of(TextColors.DARK_BLUE, "[ChestShop]")));
if (owner != null)
if (chestLocation.getBlock() != null && chestLocation.getBlock().getType().equals(BlockTypes.CHEST))
{
int itemAmount = Integer.parseInt(line1);
double price = Double.parseDouble(line2);
String itemName = line3;
ChestShop shop = new ChestShop(itemAmount, price, itemName, signLocation, owner.getUniqueId().toString());
chestShops.add(shop);
owner.sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.GREEN, "ChestShop successfully created!"));
signData.set(signData.getValue(Keys.SIGN_LINES).get().set(0, Texts.of(TextColors.DARK_BLUE, "[ChestShop]")));
if (owner != null)
{
int itemAmount = Integer.parseInt(line1);
double price = Double.parseDouble(line2);
String itemName = line3;
ChestShop shop = new ChestShop(itemAmount, price, itemName, signLocation, owner.getUniqueId().toString());
chestShops.add(shop);
owner.sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.GREEN, "ChestShop successfully created!"));

}
}
else
{
signData.set(signData.getValue(Keys.SIGN_LINES).get().set(0, Texts.of(TextColors.DARK_RED, "[ChestShop]")));
}
}
else
{
signData.set(signData.getValue(Keys.SIGN_LINES).get().set(0, Texts.of(TextColors.DARK_RED, "[ChestShop]")));
}
}
}

@Listener
public void onPlayerBreakBlock(BreakBlockEvent.SourcePlayer event)
public void onPlayerBreakBlock(BreakBlockEvent event)
{
for(BlockTransaction transaction : event.getTransactions())
{
if (transaction.getFinalReplacement().getState().getType() != null && transaction.getFinalReplacement().getState().getType() == BlockTypes.WALL_SIGN)
if(event.getCause().first(Player.class).isPresent())
{
ChestShop thisShop = null;
for (ChestShop chestShop : chestShops)
for(BlockTransaction transaction : event.getTransactions())
{
if (chestShop.getSignLocation().getX() == transaction.getFinalReplacement().getLocation().get().getX() && chestShop.getSignLocation().getY() == transaction.getFinalReplacement().getLocation().get().getY() && chestShop.getSignLocation().getZ() == transaction.getFinalReplacement().getLocation().get().getZ())
if (transaction.getFinalReplacement().getState().getType() != null && transaction.getFinalReplacement().getState().getType() == BlockTypes.WALL_SIGN)
{
thisShop = chestShop;
}
}
ChestShop thisShop = null;
for (ChestShop chestShop : chestShops)
{
if (chestShop.getSignLocation().getX() == transaction.getFinalReplacement().getLocation().get().getX() && chestShop.getSignLocation().getY() == transaction.getFinalReplacement().getLocation().get().getY() && chestShop.getSignLocation().getZ() == transaction.getFinalReplacement().getLocation().get().getZ())
{
thisShop = chestShop;
}
}

if (thisShop != null)
{
Player owner = null;
for(Player p : game.getServer().getOnlinePlayers())
{
if(p.getUniqueId().toString().equals(thisShop.getOwnerUUID()))
if (thisShop != null)
{
owner = p;
break;
Player owner = null;
for(Player p : game.getServer().getOnlinePlayers())
{
if(p.getUniqueId().toString().equals(thisShop.getOwnerUUID()))
{
owner = p;
break;
}
}
if(owner != null)
{
owner.sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.GREEN, " ChestShop successfully deleted!"));
}
chestShops.remove(thisShop);
}
}
if(owner != null)
{
owner.sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.GREEN, " ChestShop successfully deleted!"));
}
chestShops.remove(thisShop);
}
}
}
}

@Listener
public void onPlayerInteractBlock(InteractBlockEvent.SourcePlayer event)
public void onPlayerInteractBlock(InteractBlockEvent event)
{
if (event.getTargetLocation() != null && event.getTargetBlock().getState().getType() == BlockTypes.WALL_SIGN)
if(event.getCause().first(Player.class).isPresent())
{
ChestShop thisShop = null;
for (ChestShop chestShop : chestShops)
Player player = (Player) event.getCause().first(Player.class).get();
if (event.getTargetBlock().getLocation().get() != null && event.getTargetBlock().getState().getType() == BlockTypes.WALL_SIGN)
{
if (chestShop.getSignLocation().getX() == event.getTargetLocation().getX() && chestShop.getSignLocation().getY() == event.getTargetLocation().getY() && chestShop.getSignLocation().getZ() == event.getTargetLocation().getZ())
ChestShop thisShop = null;
for (ChestShop chestShop : chestShops)
{
thisShop = chestShop;
if (chestShop.getSignLocation().getX() == event.getTargetBlock().getLocation().get().getX() && chestShop.getSignLocation().getY() == event.getTargetBlock().getLocation().get().getY() && chestShop.getSignLocation().getZ() == event.getTargetBlock().getLocation().get().getZ())
{
thisShop = chestShop;
}
}
}

if (thisShop != null)
{
if (thisShop.getOwnerUUID().equals(event.getSourceEntity().getUniqueId().toString()))
if (thisShop != null)
{
event.getSourceEntity().sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You cannot purchase things from yourself!"));
}
else
{
int itemAmount = thisShop.getItemAmount();
double price = thisShop.getPrice();
String itemName = thisShop.getItemName();
Location<World> chestLocation = new Location<World>(event.getTargetLocation().getExtent(), event.getTargetLocation().getX(), event.getTargetLocation().getY() - 1, event.getTargetLocation().getZ());

if (chestLocation.getBlock() != null && chestLocation.getBlock().getType().equals(BlockTypes.CHEST))
if (thisShop.getOwnerUUID().equals(player.getUniqueId().toString()))
{
// TODO: Get chest and check if Item is in there - cannot be done until InventoryAPI is implemented..
Player player = event.getSourceEntity();
TotalEconomy totalEconomy = (TotalEconomy) game.getPluginManager().getPlugin("TotalEconomy").get().getInstance();
AccountManager accountManager = totalEconomy.getAccountManager();
BigDecimal amount = new BigDecimal(price);
player.sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You cannot purchase things from yourself!"));
}
else
{
int itemAmount = thisShop.getItemAmount();
double price = thisShop.getPrice();
String itemName = thisShop.getItemName();
Location<World> chestLocation = new Location<World>(event.getTargetBlock().getLocation().get().getExtent(), event.getTargetBlock().getLocation().get().getX(), event.getTargetBlock().getLocation().get().getY() - 1, event.getTargetBlock().getLocation().get().getZ());

if (accountManager.getBalance(player.getUniqueId()).intValue() > amount.intValue())
if (chestLocation.getBlock() != null && chestLocation.getBlock().getType().equals(BlockTypes.CHEST))
{
accountManager.removeFromBalance(player.getUniqueId(), amount);
player.sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.GREEN, "You have just bought " + itemAmount + " " + itemName + " for " + price + " dollars."));
game.getCommandDispatcher().process(game.getServer().getConsole(), "give" + " " + player.getName() + " " + itemName + " " + itemAmount);
accountManager.addToBalance(UUID.fromString(thisShop.getOwnerUUID()), amount, true);
// TODO: Get chest and check if Item is in there - cannot be done until InventoryAPI is implemented..
TotalEconomy totalEconomy = (TotalEconomy) game.getPluginManager().getPlugin("TotalEconomy").get().getInstance();
AccountManager accountManager = totalEconomy.getAccountManager();
BigDecimal amount = new BigDecimal(price);

Player owner = null;
for(Player p : game.getServer().getOnlinePlayers())
if (accountManager.getBalance(player.getUniqueId()).intValue() > amount.intValue())
{
if(p.getUniqueId().toString().equals(thisShop.getOwnerUUID()))
accountManager.removeFromBalance(player.getUniqueId(), amount);
player.sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.GREEN, "You have just bought " + itemAmount + " " + itemName + " for " + price + " dollars."));
game.getCommandDispatcher().process(game.getServer().getConsole(), "give" + " " + player.getName() + " " + itemName + " " + itemAmount);
accountManager.addToBalance(UUID.fromString(thisShop.getOwnerUUID()), amount, true);

Player owner = null;
for(Player p : game.getServer().getOnlinePlayers())
{
if(p.getUniqueId().toString().equals(thisShop.getOwnerUUID()))
{
owner = p;
break;
}
}
if (owner != null)
{
owner = p;
break;
owner.sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.GREEN, player.getName() + " has just bought " + itemAmount + " " + itemName + " from you!"));
}
}
if (owner != null)
else
{
owner.sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.GREEN, player.getName() + " has just bought " + itemAmount + " " + itemName + " from you!"));
player.sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You don't have enough money to do that!"));
}
}
else
{
player.sendMessage(Texts.of(TextColors.BLUE, "[ChestShop]: ", TextColors.DARK_RED, "Error! ", TextColors.RED, "You don't have enough money to do that!"));
}
}
}
}
Expand Down

0 comments on commit d0e738c

Please sign in to comment.