Custom Shop NPC
A basic custom shop NPC with a command "stack 123" to restock the shop. Only supports items with basic coin prices.
function onCreated() {
this.onUpdated();
}
function onUpdated() {
this.itemid = "lootoldshoe";
// Copy item attributes
let item = Server.getitemconfig(this.itemid);
this.itemname = item.name;
let keys = Object.keys(item);
for (let i=0; i<keys.length; i++)
this[keys[i]] = item[keys[i]];
this.stackcount = 5;
this.updateDisplay();
}
function updateDisplay() {
this.chat = "";
this.name = this.stackcount + " x " + this.itemname;
}
function onPlayerSays(pl) {
if (pl.adminlevel <= 0)
return;
// Set a new stack count by typing "stack nr"
if (pl.chat.startsWith("stack ")) {
let newnr = pl.chat.substring("stack ".length) * 1;
if (newnr >= 1 && newnr <= 100) {
this.stackcount = newnr;
this.updateDisplay();
}
pl.chat = "";
}
}
function onMouseDown(player) {
this.onPlayerAttacks(player);
}
function onPlayerAttacks(player) {
if (this.stackcount <= 0) {
player.showmessage("Sorry sold out!");
return;
}
let item = Server.getitemconfig(this.itemid);
if (player.coins < item.price) {
// Show the coins dialog or a message
// player.showmessage("You don't have enought coins!");
player.buycoins(item.price);
return;
}
let hasnr = player.getitemnr(this.itemid);
if (hasnr > 0 && (!item.stackable || hasnr >= item.stacklimit)) {
player.showmessage("You cannot buy more of this!");
return;
}
let self = this;
player.buyitem(item, (pl, itemid) => {
if (itemid == self.itemid)
self.sellItemToPlayer(pl);
});
}
function sellItemToPlayer(player) {
// Check some stuff again
let item = Server.getitemconfig(this.itemid);
if (this.stackcount <= 0)
return;
if (player.coins < item.price)
return;
// Remove coins and add the item
player.removecoins(item.price, "buy " + this.itemid);
player.additem(this.itemid, 1);
this.stackcount--;
this.updateDisplay();
}