use of com.solinia.solinia.Interfaces.ISoliniaNPCMerchant in project solinia3-core by mixxit.
the class EntityManager method getMerchantInventory.
@Override
public Inventory getMerchantInventory(UUID playerUUID, ISoliniaNPC npc, int pageno) {
if (npc.getMerchantid() < 1)
return null;
try {
ISoliniaNPCMerchant soliniaNpcMerchant = StateManager.getInstance().getConfigurationManager().getNPCMerchant(npc.getMerchantid());
merchantInventories.put(playerUUID, Bukkit.createInventory(null, 27, soliniaNpcMerchant.getName() + " Page: " + pageno));
pageno = pageno - 1;
int sizePerPage = 18;
List<ISoliniaNPCMerchantEntry> fullmerchantentries = StateManager.getInstance().getEntityManager().getNPCMerchantCombinedEntries(npc);
List<ISoliniaNPCMerchantEntry> merchantentries = fullmerchantentries.stream().skip(pageno * sizePerPage).limit(sizePerPage).collect(Collectors.toCollection(ArrayList::new));
int lastpage = (int) Math.ceil((float) fullmerchantentries.size() / (float) sizePerPage);
for (int i = 0; i < 27; i++) {
ItemStack itemStack = new ItemStack(Material.BARRIER);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName("EMPTY SLOT #" + i);
itemStack.setItemMeta(itemMeta);
try {
ISoliniaNPCMerchantEntry entry = merchantentries.get(i);
itemStack = StateManager.getInstance().getConfigurationManager().getItem(entry.getItemid()).asItemStack();
ItemMeta meta = itemStack.getItemMeta();
meta.setDisplayName("Display Item: " + itemStack.getItemMeta().getDisplayName());
itemStack.setItemMeta(meta);
itemStack.setAmount(1);
merchantInventories.get(playerUUID).addItem(itemStack);
} catch (IndexOutOfBoundsException eOutOfBounds) {
// Final Row left
if (i == 18) {
if (pageno != 0) {
itemStack = new ItemStack(Material.SKULL_ITEM);
itemStack.setItemMeta(ItemStackAdapter.buildSkull((SkullMeta) itemStack.getItemMeta(), UUID.fromString("1226610a-b7f8-47e5-a15d-126c4ef18635"), "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZjg0ZjU5NzEzMWJiZTI1ZGMwNThhZjg4OGNiMjk4MzFmNzk1OTliYzY3Yzk1YzgwMjkyNWNlNGFmYmEzMzJmYyJ9fX0=", null));
itemStack.setDurability((short) 3);
itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName("<-- PREVIOUS PAGE");
itemStack.setItemMeta(itemMeta);
}
}
// Identifier Block
if (i == 19) {
itemStack = new ItemStack(Material.SKULL_ITEM);
itemStack.setItemMeta(ItemStackAdapter.buildSkull((SkullMeta) itemStack.getItemMeta(), UUID.fromString("9c3bb224-bc6e-4da8-8b15-a35c97bc3b16"), "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZDI5NWE5MjkyMzZjMTc3OWVhYjhmNTcyNTdhODYwNzE0OThhNDg3MDE5Njk0MWY0YmZlMTk1MWU4YzZlZTIxYSJ9fX0=", null));
itemMeta = itemStack.getItemMeta();
List<String> lore = new ArrayList<String>();
Integer merchantid = soliniaNpcMerchant.getId();
Integer npcid = npc.getId();
Integer page = pageno + 1;
Integer nextpage = page + 1;
lore.add(merchantid.toString());
lore.add(npcid.toString());
lore.add(page.toString());
if (lastpage > nextpage) {
lore.add(nextpage.toString());
} else {
lore.add("0");
}
itemMeta.setLore(lore);
itemStack.setDurability((short) 3);
itemMeta.setDisplayName("MERCHANT: " + soliniaNpcMerchant.getName());
itemStack.setItemMeta(itemMeta);
itemStack.addUnsafeEnchantment(Enchantment.DURABILITY, 999);
}
// Final Row right
if (i == 26) {
if ((pageno + 1) < lastpage) {
itemStack = new ItemStack(Material.SKULL_ITEM);
itemStack.setItemMeta(ItemStackAdapter.buildSkull((SkullMeta) itemStack.getItemMeta(), UUID.fromString("94fbab2d-668a-4a42-860a-c357f7acc19a"), "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZmNmZTg4NDVhOGQ1ZTYzNWZiODc3MjhjY2M5Mzg5NWQ0MmI0ZmMyZTZhNTNmMWJhNzhjODQ1MjI1ODIyIn19fQ==", null));
itemMeta = itemStack.getItemMeta();
itemStack.setDurability((short) 3);
itemMeta.setDisplayName("NEXT PAGE -->");
itemStack.setItemMeta(itemMeta);
}
}
merchantInventories.get(playerUUID).addItem(itemStack);
}
}
return merchantInventories.get(playerUUID);
} catch (CoreStateInitException e) {
return null;
}
}
use of com.solinia.solinia.Interfaces.ISoliniaNPCMerchant in project solinia3-core by mixxit.
the class SoliniaNPCMerchantFactory method AddNPCMerchantItem.
public static void AddNPCMerchantItem(int merchantid, int itemid) {
try {
ISoliniaNPCMerchant merchant = StateManager.getInstance().getConfigurationManager().getNPCMerchant(merchantid);
SoliniaNPCMerchantEntry merchantentry = new SoliniaNPCMerchantEntry();
int id = 1;
for (ISoliniaNPCMerchantEntry entry : merchant.getEntries()) {
if (entry.getId() > id)
id = entry.getId() + 1;
}
merchantentry.setId(id);
merchantentry.setMerchantid(merchantid);
merchantentry.setItemid(itemid);
StateManager.getInstance().getConfigurationManager().getNPCMerchant(merchantid).getEntries().add(merchantentry);
} catch (CoreStateInitException e) {
e.printStackTrace();
}
}
use of com.solinia.solinia.Interfaces.ISoliniaNPCMerchant in project solinia3-core by mixxit.
the class Solinia3CorePlayerListener method onMerchantInventoryClick.
private void onMerchantInventoryClick(InventoryClickEvent event) {
int merchantid = 0;
int npcid = 0;
ISoliniaNPCMerchant merchant = null;
ISoliniaNPC npc = null;
int page = 0;
int nextpage = 0;
try {
merchantid = Utils.getInventoryMerchantID(event.getInventory());
npcid = Utils.getInventoryNPCID(event.getInventory());
page = Utils.getInventoryPage(event.getInventory());
nextpage = Utils.getInventoryPage(event.getInventory());
} catch (Exception e) {
event.getView().getPlayer().sendMessage(e.getMessage());
e.printStackTrace();
Utils.CancelEvent(event);
;
return;
}
if (merchantid == 0 || npcid == 0) {
event.getView().getPlayer().sendMessage("Could not find npc " + npcid + " or merchant " + merchantid + " or page " + page);
System.out.println("Could not find npc " + npcid + " or merchant " + merchantid);
Utils.CancelEvent(event);
;
return;
}
try {
merchant = StateManager.getInstance().getConfigurationManager().getNPCMerchant(merchantid);
npc = StateManager.getInstance().getConfigurationManager().getNPC(npcid);
} catch (CoreStateInitException e) {
event.getView().getPlayer().sendMessage("Cannot sell/buy right now");
Utils.CancelEvent(event);
;
return;
}
// event.getView().getPlayer().sendMessage("Detected Merchant Inventory Click for Merchant:" + merchantid + " Slot: " + event.getSlot() + " Raw Slot: " + event.getRawSlot());
if (event.getRawSlot() < 0) {
Utils.CancelEvent(event);
;
return;
}
if (event.getCursor() == null || event.getCursor().getType().equals(Material.AIR)) {
// event.getView().getPlayer().sendMessage("Detected AIR Cursor, assuming picking up");
if (event.getRawSlot() > 26) {
try {
ISoliniaItem item = StateManager.getInstance().getConfigurationManager().getItem(event.getCurrentItem());
if (item == null) {
event.getView().getPlayer().sendMessage("Merchants are not interested in this item");
Utils.CancelEvent(event);
;
return;
}
if (item.isTemporary()) {
event.getView().getPlayer().sendMessage("Merchants are not interested in temporary items");
Utils.CancelEvent(event);
;
return;
}
// Picked up sellable item
} catch (CoreStateInitException e) {
Utils.CancelEvent(event);
;
event.getView().getPlayer().sendMessage("Cannot sell/buy right now");
return;
}
return;
} else {
ItemStack pickingUpItem = event.getCurrentItem();
if (pickingUpItem.getType().equals(Material.BARRIER)) {
// event.getView().getPlayer().sendMessage("Ignoring barrier");
Utils.CancelEvent(event);
;
return;
}
// Do not allow movement of UI movers
if (event.getRawSlot() == 18 || event.getRawSlot() == 26) {
// event.getView().getPlayer().sendMessage("Moving Left or Right");
if (event.getRawSlot() == 18) {
if ((page - 1) > 0) {
event.getView().getPlayer().closeInventory();
npc.sendMerchantItemListToPlayer((Player) event.getView().getPlayer(), page - 1);
}
}
if (event.getRawSlot() == 26) {
if (nextpage != 0) {
event.getView().getPlayer().closeInventory();
npc.sendMerchantItemListToPlayer((Player) event.getView().getPlayer(), nextpage + 1);
}
}
Utils.CancelEvent(event);
;
return;
}
// Do not allow movement of identifiers
if (event.getRawSlot() == 19) {
// event.getView().getPlayer().sendMessage("Ignoring identifier block");
Utils.CancelEvent(event);
;
return;
}
// Picking up merchant item
// event.getView().getPlayer().sendMessage("Picking up merchant item");
event.setCursor(event.getCurrentItem());
Utils.CancelEvent(event);
;
return;
}
}
if (event.getCursor() != null && !event.getCursor().getType().equals(Material.AIR)) {
// Clicking item in cursor onto a slot
if (event.getRawSlot() > 26) {
// Dropping own item or buying
if (event.getCursor().getItemMeta().getDisplayName().startsWith("Display Item: ")) {
try {
if (!event.getCurrentItem().getType().equals(Material.AIR)) {
event.getView().getPlayer().sendMessage("You must place the item you wish to buy on an empty slot");
event.setCursor(new ItemStack(Material.AIR));
Utils.CancelEvent(event);
;
return;
}
ISoliniaItem item = StateManager.getInstance().getConfigurationManager().getItem(event.getCursor());
int individualprice = item.getWorth();
// Total price
int price = individualprice * event.getCursor().getAmount();
if (price > StateManager.getInstance().getEconomy().getBalance((Player) event.getView().getPlayer())) {
event.getView().getPlayer().sendMessage("You do not have sufficient balance to buy this item in that quantity.");
event.setCursor(new ItemStack(Material.AIR));
Utils.CancelEvent(event);
;
return;
}
EconomyResponse responsewithdraw = StateManager.getInstance().getEconomy().withdrawPlayer(Bukkit.getOfflinePlayer(((Player) event.getView().getPlayer()).getUniqueId()), price);
if (responsewithdraw.transactionSuccess()) {
ItemStack purchase = item.asItemStack();
event.setCursor(new ItemStack(Material.AIR));
Utils.CancelEvent(event);
;
event.getClickedInventory().setItem(event.getSlot(), purchase);
event.getView().getPlayer().sendMessage(ChatColor.YELLOW + "* You pay $" + price + " for " + event.getCursor().getAmount() + " " + item.getDisplayname());
return;
} else {
System.out.println("Error withdrawing money from your account " + String.format(responsewithdraw.errorMessage));
event.getView().getPlayer().sendMessage(ChatColor.YELLOW + "* Error withdrawing money from your account " + String.format(responsewithdraw.errorMessage));
event.setCursor(new ItemStack(Material.AIR));
Utils.CancelEvent(event);
;
return;
}
} catch (CoreStateInitException e) {
event.getView().getPlayer().sendMessage("Cannot buy items from the merchant right now");
event.setCursor(new ItemStack(Material.AIR));
Utils.CancelEvent(event);
;
return;
}
} else {
// Dropping own item
return;
}
} else {
// Selling items or dropping item back
if (event.getCursor().getItemMeta().getDisplayName().startsWith("Display Item: ")) {
// Returning store item
event.setCursor(new ItemStack(Material.AIR));
Utils.CancelEvent(event);
;
return;
} else {
try {
ISoliniaItem item = StateManager.getInstance().getConfigurationManager().getItem(event.getCursor());
int individualprice = item.getWorth();
// Total price
int price = individualprice * event.getCursor().getAmount();
EconomyResponse responsedeposit = StateManager.getInstance().getEconomy().depositPlayer((Player) event.getView().getPlayer(), price);
if (responsedeposit.transactionSuccess()) {
// Add to buy back list
// StateManager.getInstance().getEntityManager().addTemporaryMerchantItem(npc.getId(), item.getId(), event.getCursor().getAmount());
event.getView().getPlayer().sendMessage(ChatColor.YELLOW + "* You recieve $" + price + " as payment");
event.setCursor(new ItemStack(Material.AIR));
Utils.CancelEvent(event);
;
return;
} else {
System.out.println("Error depositing money to users account " + String.format(responsedeposit.errorMessage));
event.getView().getPlayer().sendMessage(ChatColor.YELLOW + "* Error depositing money to your account " + String.format(responsedeposit.errorMessage));
Utils.CancelEvent(event);
;
return;
}
} catch (CoreStateInitException e) {
event.getView().getPlayer().sendMessage("Cannot sell item to merchant right now");
Utils.CancelEvent(event);
;
return;
}
}
}
}
event.getView().getPlayer().sendMessage("Please alert an admin of this message code: GMMI1");
Utils.CancelEvent(event);
;
return;
}
use of com.solinia.solinia.Interfaces.ISoliniaNPCMerchant in project solinia3-core by mixxit.
the class JsonNPCMerchantRepository method reload.
@Override
public void reload() {
List<ISoliniaNPCMerchant> file = new ArrayList<ISoliniaNPCMerchant>();
try {
GsonBuilder gsonbuilder = new GsonBuilder();
gsonbuilder.registerTypeAdapterFactory(new ISoliniaNPCMerchantEntryTypeAdapterFactory(SoliniaNPCMerchantEntry.class));
Gson gson = gsonbuilder.create();
BufferedReader br = new BufferedReader(new FileReader(filePath));
file = gson.fromJson(br, new TypeToken<List<SoliniaNPCMerchant>>() {
}.getType());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
npcmerchants.clear();
for (ISoliniaNPCMerchant i : file) {
npcmerchants.put(i.getId(), i);
}
System.out.println("Reloaded " + npcmerchants.size() + " npcmerchants");
}
Aggregations