09-24-2007 07:37
A while back, I needed a bartender for a party. I shopped around and finally spent far too much on a "full permissions bartender script". When I got and read the script code, I thought it was some of the worst trash it's ever been my misfortune to waste money on. Annoyed beyond belief, I went and wrote my own script.

Since I have no intention in going into the bartender business, I thought I'd pay back all of the scriptors who have so generously contributed to this forum, by putting my script out into the public domain. Hopefully it'll save someone else from wasting a lot of money the way I did.

CODE

//
// Bartender Script
// by Midori Mikazuki
//
// Copyright 2007. This script is hearby released into the public domain.
//
// Drop this script into an object that will act as a bartender.
// The script assumes that all objects contained within the bartender
// are drinks to be given out. When someone touches the bartender,
// (s)he is presented with a list of the installed drinks; if there
// are more than 10 drinks in the bartender, the menu will page through
// them. Clicking on one of the drink names in the menu will cause the
// bartender to give the avatar a copy of that drink.
//
// The bartender menu listens on a randomly generated channel, so it
// should be fine to have several bartenders in close proximity without
// worrying that they will all give out a drink at the same time.

integer CHANNEL;
integer handle;
key touched;

list DRINKS;

string TITLE = "Touch me for a free drink";

RebuildDrinkList()
{
DRINKS = [];

integer count = llGetInventoryNumber(INVENTORY_OBJECT);
integer i;

for (i = 0; i < count; ++i)
{
string name = llGetInventoryName(INVENTORY_OBJECT, i);
name = llGetSubString(name, 0, 24);
DRINKS = (DRINKS=[]) + DRINKS + [ name ];
}
}

DisplayMenu(integer page)
{
integer length = llGetListLength(DRINKS);
integer start = page * 9;
integer end = start + 9;
list menu = [];

page += 1;

if (start > 0) {
menu = [ "Page " + (string)(page - 1) ];
}
else {
menu = [ " " ];
}

menu = (menu=[]) + menu + llList2List(DRINKS, start, start);

if (end < length) {
menu = (menu=[]) + menu + [ "Page " + (string) (page + 1) ];
}
else {
menu = (menu=[]) + menu + [ " " ];
}

menu = (menu=[]) + menu + llList2List(DRINKS, start + 1, end);

llDialog(touched, "\nDrink Menu: ", menu , CHANNEL);
}

default
{
state_entry()
{
llSetText(TITLE, <1.0, 1.0, 1.0>, 1.0);

RebuildDrinkList();

llListenRemove(handle);
CHANNEL = (integer)llFrand(2000000000.0);
llListen(CHANNEL, "", NULL_KEY, "");
}

on_rez(integer start)
{
llResetScript();
}

changed(integer change)
{
if (change == CHANGED_INVENTORY)
{
llOwnerSay("Rebuilding drink list");
RebuildDrinkList();
llOwnerSay("Done.");
}
}

touch_start(integer total_number)
{
touched = llDetectedKey(0);
if (touched)
{
DisplayMenu(0);
}
}

listen(integer channel, string name, key id, string message)
{
if (llSubStringIndex(message, "Page ") != -1) {
string page = llGetSubString(message, 5, -1);
DisplayMenu(((integer) page) - 1);
}
else {
integer drink = llListFindList(DRINKS, [ message ]);
if (drink != -1) {
llGiveInventory(id, llGetInventoryName(INVENTORY_OBJECT, drink));
}
}
}
}