Author Topic: Item Saving  (Read 923 times)

Ok, so I'm trying to make this script from Clockturn's item saving event into just a regular server item saver.  I need it to make the items in a player's inventory auto save when they leave the server, and load when they spawn.  Here's what I found...Can anyone help me fix it up into a server item saver?

Code: [Select]
new ScriptObject(SavedItemList)
{
class = ApSys;
dataFile = "config/server/saveditems.dat";
};
SavedItemList.addValue("Items","string 255");
SavedItemList.loadData();
function Player::saveItems(%player)
{
%client = %player.client;
if(isObject(%client) && isObject(%player))
{
%cdata = SavedItemList.getData(%client.BL_ID,1);
for(%i=0;%i<%player.getDatablock().maxTools;%i++)
{
if(%list $= "" && isObject(%player.tool[%i]))
{
%list = %player.tool[%i].getName();
}
else if(%list !$= "" && isObject(%player.tool[%i]))
{
%list = %list TAB %player.tool[%i].getName();
%cdata.setValue("Items",%list);
}
}
}
}
function Player::loadItems(%player)
{
%client = %player.client;
if(isObject(%client) && isObject(%player))
{
%cdata = SavedItemList.getData(%client.BL_ID,1);
%itemlist = %cdata.getValue("Items");
%itemcount = getFieldCount(%itemlist);
%player.clearTools();
if(%player.getDatablock().maxTools >= %itemcount)
{
for(%i=0;%i<%itemcount;%i++)
{
%itemname = getField(%itemlist,%i);
%itemdb = nameToID(%itemname);
if(isObject(%itemdb))
{
%player.tool[%i] = %itemdb;
%player.weaponCount++;
messageClient(%client,'MsgItemPickup','',%i,%itemdb);
}
}
return;
}
}
}
package Event_ItemSaving
{
function GameConnection::onClientLeaveGame(%client)
{
SavedItemList.saveData();
Parent::onClientLeaveGame(%client);
}
function onExit()
{
SavedItemList.saveData();
Parent::onExit();
}
};
activatePackage(Event_ItemSaving);


Anyone?
You posted 12 hours later, and you weren't even halfway down the first page.


It's a saving system by clockturn.

What's an "ApSys"?

Code: [Select]
// ApSys - Save System
//  Author: Clockturn / BLID 4332
//
// Free use - Please give credit if used

function ApSys::onAdd(%this)
{
if(%this.getName() $= "")
{
%errorStack = ((%errorstack $= "") ? "No name specified." : %errorStack NL "No name specified.");
}
if(%this.dataFile $= "")
{
%errorStack = ((%errorstack $= "") ? "No dataFile variable." : %errorStack NL "No dataFile variable.");
}
if(%errorstack !$= "")
{
echo(%this SPC "- errors:\n" @ %errorstack);
%this.schedule(0,"delete");
return;
}
%this.fileObject = new FileObject();
%this.dataCount = 0;
%this.values = 0;
if(isFile(%this.dataFile))
{
echo(%this.getName() @ ": Previous save found.");
%this.loadData();
%this.foundDataFile = 1;
}
}
function ApSys::onRemove(%this)
{
for(%i=0;%i<%this.dataCount;%i++)
{
%this.dataByIndex[%i].delete();
}
%this.fileObject.close();
%this.fileObject.delete();
}
function ApSys::AddValue(%this,%name,%type,%default)
{
%name = strreplace(%name," ","");
for(%i=0;%i<%this.values;%i++)
{
if(%this.valueName[%i] $= %name)
{
echo(%this.getName() SPC "- Value" SPC %name SPC "already exists.");
return;
}
}
%typeargs = getWords(%type,1);
%type = getWord(%type,0);
switch$(%type)
{
case "string":
%typeargs = getWord(%typeargs,0);
case "int":
%typeargs = getWord(%typeargs,0) SPC getWord(%typeargs,1);
case "float":
%typeargs = getWord(%typeargs,0) SPC getWord(%typeargs,1);
case "array":
%typeargs = getWord(%typeargs,0) SPC getWord(%typeargs,1);
default:
echo(%this.getName() SPC "- Invalid type" SPC %type  @ ".");
return;
}
%this.valueName[%this.values] = %name;
%this.valueType[%this.values] = %type;
%this.valueTypeArgs[%this.values] = %typeargs;
%this.valueDefault[%this.values] = %default;
%this.values++;
for(%i=0;%i<%this.dataCount;%i++)
{
if(%type $= "array")
{
for(%y=0;%y<getWord(%typeargs,1);%y++)
{
for(%x=0;%x<getWord(%typeargs,0);%x++)
{
%this.dataByIndex[%i].setValue(%name SPC %x SPC %y,%default);
}
}
} else {
%this.dataByIndex[%i].setValue(%name,%default);
}
}
}
function ApSys::RemoveValue(%this,%name)
{
for(%i=0;%i<%this.values;%i++)
{
if(%this.valueName[%i] $= %name)
{
%type = %this.valueType[%i];
%args = %this.valueTypeArgs[%i];
%x = 1;
}
if(%x)
{
%this.valueName[%i] = %this.valueName[%i+1];
%this.valueType[%i] = %this.valueType[%i+1];
%this.valueTypeArgs[%i] = %this.valueTypeArgs[%i+1];
%this.valueDefault[%i] = %this.valueDefault[%i+1];
}
}
if(%x)
{
%this.values--;
for(%i=0;%i<%this.dataCount;%i++)
{
%data = %this.dataByIndex[%i];
if(%type $= "array")
{
for(%y=0;%y<getWord(%args,1);%y++)
{
for(%x=0;%x<getWord(%args,0);%x++)
{
%data.value[%name,%x SPC %y] = "";
}
}
} else {
%data.value[%name] = "";
}
}
} else {
echo(%this.getName() SPC "- Value" SPC %name SPC "does not exist.");
}
}
function ApSys::AddData(%this,%id)
{
if(isObject(%this.dataByID[%id]))
{
echo(%this.getName() SPC "- Data already exists for ID" SPC %id @ ".");
return;
}
%data = new ScriptObject()
{
class = ApSysData;
ID = %id;
parent = %this;
index = %this.dataCount;
};
%this.dataByIndex[%this.dataCount] = %data;
%this.dataByID[%id] = %data;
%this.dataCount++;
}
function ApSys::RemoveData(%this,%id)
{
%data = %this.dataByID[%id];
if(!isObject(%data))
{
echo(%this.getName() SPC "- Data does not exist for ID" SPC %id @ ".");
return;
}
for(%i=%data.index;%i<%this.dataCount;%i++)
{
%this.dataByIndex[%i] = %this.dataByIndex[%i+1];
}
%this.dataByID[%id] = "";
%data.delete();
%this.dataCount--;
}
function ApSys::LoadData(%this)
{
if(%this.saveInProgress)
{
return;
}
%FO = %this.fileObject;
%FO.openForRead(%this.dataFile);
%mode = 0;
while(!%FO.isEOF())
{
%line = %FO.readLine();
if(%line $= "end")
{
%mode = 0;
continue;
}
switch(%mode)
{
case 0:
switch$(getWord(%line,0))
{
case "values":
%mode = 1;
case "data":
%data = %this.getData(getWord(%line,1),1);
%mode = 2;
}
continue;
case 1:
%name = getWord(%line,0);
%type = getWord(%line,1);
switch$(%type)
{
case "string":
%type = %type SPC getWord(%line,2);
%default = getWords(%line,3);
case "int":
%type = %type SPC getWord(%line,2) SPC getWord(%line,3);
%default = getWord(%line,4);
case "float":
%type = %type SPC getWord(%line,2) SPC getWord(%line,3);
%default = getWord(%line,4);
case "array":
%type = %type SPC getWord(%line,2) SPC getWord(%line,3);
%default = getWords(%line,4);
}
%this.addValue(%name,%type,%default);
case 2:
%name = getWord(%line,0);
%args = getWords(%line,1);
if(%args $= "array")
{
%mode = 3;
for(%i=0;%i<%this.values;%i++)
{
if(%this.valueName[%i] $= %name)
{
%arraysizex = getWord(%this.valueTypeArgs[%i],0);
%arraysizey = getWord(%this.valueTypeArgs[%i],1);
%y = 0;
break;
}
}
continue;
} else {
%data.setValue(%name,%args);
}
case 3:
%pos = strpos(%line,"[",0);
%x = 0;
while(%pos != -1)
{
%endpos = strpos(%line,"]",%pos);
%cell = strreplace(strreplace(getsubstr(%line,%pos+1,%endpos-(%pos+1)),"br<","["),">br","]");
%data.setValue(%name SPC %x SPC %y,%cell);
%x++;
if(%x >= %arraysizex)
{
break;
}
%pos = strpos(%line,"[",%endpos);
}
%y++;
if(%y >= %arraysizey)
{
%mode = 2;
continue;
}
}
}
%FO.close();
%this.loadedsavefile = 1;
}
function ApSys::SaveData(%this)
{
if(!%this.saveInProgress)
{
%this.fileObject.openForWrite(%this.dataFile);
%this.saveProgress = 1;
%this.saveInProgress = 1;
%this.fileObject.writeLine("values");
for(%i=0;%i<%this.values;%i++)
{
%this.fileObject.writeLine(%this.valueName[%i] SPC %this.valueType[%i] SPC %this.valueTypeArgs[%i] SPC %this.valueDefault[%i]);
}
%this.fileObject.writeLine("end");
%this.fileObject.writeLine("");
%this.OnSaveLoop(0);
}
%FO = %this.fileObject;
%FO.openForWrite(%this.dataFile);
%FO.writeLine("values");
for(%i=0;%i<%this.values;%i++)
{
%FO.writeLine(%this.valueName[%i] SPC %this.valueType[%i] SPC %this.valueTypeArgs[%i] SPC %this.valueDefault[%i]);
}
%FO.writeLine("end");
%FO.writeLine("");
for(%dc=0;%dc<%this.dataCount;%dc++)
{
%data = %this.dataByIndex[%dc];
%FO.writeLine("data" SPC %data.ID);
for(%i=0;%i<%this.values;%i++)
{
if(%this.valueType[%i] $= "array")
{
%FO.writeLine(%this.valueName[%i] SPC "array");
for(%y=0;%y<getWord(%this.valueTypeArgs[%i],1);%y++)
{
%line = "[";
for(%x=0;%x<getWord(%this.valueTypeArgs[%i],0);%x++)
{
%line = %line @ strreplace(strreplace(%data.value[%this.valueName[%i],%x SPC %y],"[","br<"),"]",">br") @ "][";
}
%line = getsubstr(%line,0,strlen(%line)-1);
%FO.writeLine(%line);
}
} else {
%FO.writeLine(%this.valueName[%i] SPC %data.value[%this.valueName[%i]]);
}
}
%FO.writeLine("end");
%FO.writeLine("");
}
%FO.close();
}
function ApSys::OnSaveLoop(%this,%record)
{
if(!%this.saveInProgress)
{
return;
}
if(%this.saveProgress >= %this.dataCount)
{
%this.OnSaveFinish();
return;
}
%data = %this.dataByIndex[%record];
%data.saveRecord();
%this.saveProgress++;
%this.saveLoopSchedule = %this.schedule(10,"OnSaveLoop",%record++);
}
function ApSys::OnSaveFinish(%this)
{
if(!%this.saveInProgress)
{
return;
}
%this.fileObject.close();
%this.saveProgress = 1;
cancel(%this.saveLoopSchedule);
%this.saveLoopSchedule = "";
%this.saveInProgress = 0;
}
function ApSysData::onAdd(%this)
{
if(!isObject(%this.parent))
{
echo("ApSysData - No parent.");
%this.schedule(0,"delete");
return;
}
if(%this.ID $= "")
{
echo("ApSysData - No ID.");
%this.schedule(0,"delete");
return;
}
for(%i=0;%i<%this.parent.values;%i++)
{
if(%this.parent.valueType[%i] $= "array")
{
for(%y=0;%y<getWord(%this.parent.valueTypeArgs[%i],1);%y++)
{
for(%x=0;%x<getWord(%this.parent.valueTypeArgs[%i],0);%x++)
{
%this.setValue(%this.parent.valueName[%i] SPC %x SPC %y,%this.parent.valueDefault[%i]);
}
}
} else {
%this.setValue(%this.parent.valueName[%i],%this.parent.valueDefault[%i]);
}
}
}
function ApSysData::saveRecord(%this)
{
if(!%this.parent.saveInProgress)
{
return;
}
%this.parent.fileObject.writeLine("data" SPC %this.ID);
for(%i=0;%i<%this.parent.values;%i++)
{
if(%this.parent.valueType[%i] $= "array")
{
%this.parent.fileObject.writeLine(%this.parent.valueName[%i] SPC "array");
for(%y=0;%y<getWord(%this.parent.valueTypeArgs[%i],1);%y++)
{
%line = "[";
for(%x=0;%x<getWord(%this.parent.valueTypeArgs[%i],0);%x++)
{
%line = %line @ strreplace(strreplace(%this.value[%this.parent.valueName[%i],%x SPC %y],"[","br<"),"]",">br") @ "][";
}
%line = getsubstr(%line,0,strlen(%line)-1);
%this.parent.fileObject.writeLine(%line);
}
} else {
%this.parent.fileObject.writeLine(%this.parent.valueName[%i] SPC %this.value[%this.parent.valueName[%i]]);
}
}
%this.parent.fileObject.writeLine("end");
%this.parent.fileObject.writeLine("");
}
function ApSysData::getValue(%this,%value)
{
%arrayspace = getWord(%value,1) SPC getWord(%value,2);
%value = getWord(%value,0);
for(%i=0;%i<%this.parent.values;%i++)
{
if(%this.parent.valueName[%i] $= %value)
{
%index = %i;
break;
}
}
if(%index $= "")
{
echo("ApSysData - Invalid value" SPC %value @ ".");
return -1;
}
if(%this.parent.valueType[%index] $= "array")
{
%ax = getWord(%arrayspace,0);
%ay = getWord(%arrayspace,1);
if(%ax < 0 || %ay < 0 || %ax >= getWord(%this.parent.valueTypeArgs[%index],0) || %ay >= getWord(%this.parent.valueTypeArgs[%index],1))
{
echo("ApSysData - Array space out of bounds.");
return -1;
}
return %this.value[%this.parent.valueName[%index],%arrayspace];
} else {
return %this.value[%this.parent.valueName[%index]];
}
}
function ApSysData::setValue(%this,%value,%set)
{
%arrayspace = getWord(%value,1) SPC getWord(%value,2);
%value = getWord(%value,0);
for(%i=0;%i<%this.parent.values;%i++)
{
if(%this.parent.valueName[%i] $= %value)
{
%index = %i;
break;
}
}
if(%index $= "")
{
echo("ApSysData - Invalid value" SPC %value @ ".");
return "";
}
switch$(%this.parent.valueType[%index])
{
case "string":
%set = getSubStr(%set,0,%this.parent.valueTypeArgs[%index]);
%this.value[%this.parent.valueName[%index]] = %set;
return %this.value[%this.parent.valueName[%index]];
case "int":
%set -= 1;
%set += 1;
if(strpos(%set,".") != -1)
{
%set = getSubStr(%set,0,strpos(%set,"."));
}
%set = mClampF(%set,getWord(%this.parent.valueTypeArgs[%index],0),getWord(%this.parent.valueTypeArgs[%index],1));
%this.value[%this.parent.valueName[%index]] = %set;
return %this.value[%this.parent.valueName[%index]];
case "float":
%set -= 1;
%set += 1;
%set = mClampF(%set,getWord(%this.parent.valueTypeArgs[%index],0),getWord(%this.parent.valueTypeArgs[%index],1));
%this.value[%this.parent.valueName[%index]] = %set;
return %this.value[%this.parent.valueName[%index]];
case "array":
%ax = getWord(%arrayspace,0);
%ay = getWord(%arrayspace,1);
if(%ax < 0 || %ay < 0 || %ax >= getWord(%this.parent.valueTypeArgs[%index],0) || %ay >= getWord(%this.parent.valueTypeArgs[%index],1))
{
echo("ApSysData - Array space out of bounds.");
return 0;
}
%this.value[%this.parent.valueName[%index],%arrayspace] = %set;
return %this.value[%this.parent.valueName[%index],%arrayspace];
}
}
function ApSys::getData(%this,%id,%create)
{
%data = %this.dataByID[%id];
if(!isObject(%data))
{
if(%create)
{
%this.addData(%id);
%data = %this.dataByID[%id];
} else {
%data = -1;
}
}
return %data;
}

Fixed Anti-Noob hammer attack:

Code: [Select]
Package Anti_Hammer
{
function HammerImage::onHitObject(%this, %obj, %slot, %col, %pos, %normal)
{
if(%col.getClassName() $= "Player" && isObject(%col.client))
commandToClient(%obj.client, 'messageBoxOK', "Sorry!", "You are not allowed to hammer players.");
else
parent::onHitObject(%this, %obj, %slot, %col, %pos, %normal);
}
};
activatePackage(Anti_Hammer);

« Last Edit: November 15, 2009, 04:35:32 AM by Tezuni »

Fixed the Hammer.
« Last Edit: November 15, 2009, 04:30:58 AM by Tezuni »

Oh and btw, what's the color shift for white?
Are you kidding me?

Speaking of items, why does this break the hammer?

Code: [Select]
function HammerImage::onHitObject(%this, %obj, %slot, %col, %pos, %normal)
{
if(%col.getClassName() $= "Player" && isObject(%col.client) && !%col.client.getWantedLevel())
commandToClient(%obj.client, 'messageBoxOK', "No!", "Hammering players is not allowed.)");
else
parent::onHitObject(%this, %obj, %slot, %col, %pos, %normal);
}

It makes it so the hammer cannot kill bricks and it does not have sound anymore.  It still moves.
EDIT: and yeah it's packaged.

To explain this we first need to look at the weapon system.
Item: Physical representation of the item.
Image: What you hold.
Projectile: What is fired when you fire a weapon.

Now at functionality.
[Note: the following section is based on pre-v9 information and is used for example only]
When you pick up a Hammer item and equip it, you hold a Hammer image. When you fire, the Hammer image fires a Hammer Projectile which hits whatever is in front of it. HammerImage can not have an "OnHitObject" function because it never hits anything. The projectile does that part of the work. The actual function would be "OnCollision".

Correction to the above: The hammer now uses raycasts, so trying to mod it in this fashion is futile.


Oh and btw, what's the color shift for white?
"R G B A"
"Red Green Blue Alpha"
Think about it.

Thx Rky, but I managed to get it working with OnHitObject.  Can anyone help me with the item saving?
« Last Edit: November 15, 2009, 04:30:28 AM by Tezuni »