Basic Store Plugin
#1
Here's the store plugin I've been working on, I finally got the ini file writing to work.

The basics - credits are earned by mining, one credit per block mined, doesn't matter the type. When a method to detect playerkills is added, I will be adding in 100 credits for killing a player and losing 100 credits for being killed. When mobs and monsters are implemented, I will be adding a credit value system to that too Smile

You can buy:
64 torches - 64 credits
wood shovel, axe, pickaxe, sword - 0 credits (new players joining the server automatically get access to basic tools)
stone shovel, axe, pickaxe, sword - 100 credits
iron shovel, axe, pickaxe, sword - 500 credits
gold shovel, axe, pickaxe, sword - 800 credits
diamond shovel, axe, pickaxe, sword - 1000 credits

This is easily expandable to include other items as I've added a fairly easy to expand store api with a function to check and see if I player can afford an item Smile

Filename:Kit.lua
PHP Code:
local KitPlugin = {}
KitPlugin.__index = KitPlugin

function KitPlugin:new()
   
local t = {}
   
setmetatable(t, KitPlugin)
   
local w = Lua__cPlugin:new()
   
tolua.setpeer(w, t)
   
w:tolua__set_instance(w)
   return 
w
end

function KitPlugin:Initialize()
    
self:AddCommand("/store", " - tells you what's currently for sale and how many credits you have.", "Kit.lua")
    
self:BindCommand("/store", PricesCommand)
    
self:BindCommand("/buywoodkit", BuyWoodKit)
    
self:BindCommand("/buystonekit", BuyStoneKit)
    
self:BindCommand("/buyironkit", BuyIronKit)
    
self:BindCommand("/buygoldkit", BuyGoldKit)
    
self:BindCommand("/buydiamondkit", BuyDiamondKit)
    
self:BindCommand("/buytorches", Buy64Torches)
    
    
PluginManager = cRoot:Get():GetPluginManager()
    
PluginManager:AddHook( self, cPluginManager.E_PLUGIN_BLOCK_DIG )
    return 
true
end


function KitPlugin:OnBlockDig( PacketData, Player )
  
local PlayerName = Player:GetName()
  
local World = cRoot:Get():GetWorld()
  
local X = PacketData.m_PosX
  local Y 
= PacketData.m_PosY
  local Z 
= PacketData.m_PosZ

  
-- status 0 = started digging; 2 = block broken
  
if(PacketData.m_Status == 2) then
   
   local IniFile 
= cIniFile("score.ini")
if ( 
IniFile:ReadFile() == true ) then
    PlayerScore 
= IniFile:GetValueI(PlayerName, "score" )
    
NewScore = PlayerScore+1
    IniFile
:DeleteKey(PlayerName)
    
IniFile:WriteFile()
    
IniFile:AddKeyName(PlayerName)
    
IniFile:SetValueI(PlayerName, "score", NewScore)
    
IniFile:WriteFile()
    
Log(NewScore);
    
end
end
return false
end

function CanAfford(money, Player)
local PlayerName = Player:GetName()
local IniFile = cIniFile("score.ini")
if ( 
IniFile:ReadFile() == true ) then
    PlayerScore 
= IniFile:GetValueI(PlayerName, "score" )
    
Log(PlayerScore);
    if(
PlayerScore>=money) then
    
if(money>0) then
    NewScore 
= PlayerScore-money
    Log
("Sold item");
    else
    
NewScore = PlayerScore
    Log
("Gave away freebie");
    
end
    IniFile
:DeleteKey(PlayerName)
    
IniFile:WriteFile()
    
IniFile:SetValueI(PlayerName, "score", NewScore)
    
IniFile:WriteFile()
    
Log(NewScore);
    
end
return true
else
return 
false
end
end

-- store values --
function 
BuyWoodKit(Split, Player)
Server = cRoot:Get():GetServer()
if(
CanAfford(0, Player)) then
Server
:SendMessage(cChatColor.Red .. "The wood kit has been added to your inventory")
    
Player:GetInventory():AddItem( cItem( 268, 1 ) )
    
Player:GetInventory():AddItem( cItem( 269, 1 ) )
    
Player:GetInventory():AddItem( cItem( 270, 1 ) )
    
Player:GetInventory():AddItem( cItem( 271, 1 ) )
else
Server:SendMessage(cChatColor.Red .. "You cannot afford the wood kit!")
end
return true
end

function BuyStoneKit(Split, Player)
Server = cRoot:Get():GetServer()
if(
CanAfford(100, Player)) then
Server
:SendMessage(cChatColor.Red .. "The stone kit has been added to your inventory")
    
Player:GetInventory():AddItem( cItem( 272, 1 ) )
    
Player:GetInventory():AddItem( cItem( 273, 1 ) )
    
Player:GetInventory():AddItem( cItem( 274, 1 ) )
    
Player:GetInventory():AddItem( cItem( 275, 1 ) )
else
Server:SendMessage(cChatColor.Red .. "You cannot afford the iron kit!")
end
return true
end

function BuyIronKit(Split, Player)
Server = cRoot:Get():GetServer()
if(
CanAfford(500, Player)) then
Server
:SendMessage(cChatColor.Red .. "The iron kit has been added to your inventory")
    
Player:GetInventory():AddItem( cItem( 267, 1 ) )
    
Player:GetInventory():AddItem( cItem( 256, 1 ) )
    
Player:GetInventory():AddItem( cItem( 257, 1 ) )
    
Player:GetInventory():AddItem( cItem( 258, 1 ) )
else
Server:SendMessage(cChatColor.Red .. "You cannot afford the iron kit!")
end
return true
end

function BuyGoldKit(Split, Player)
Server = cRoot:Get():GetServer()
if(
CanAfford(800, Player)) then
Server
:SendMessage(cChatColor.Red .. "The gold kit has been added to your inventory")
    
Player:GetInventory():AddItem( cItem( 283, 1 ) )
    
Player:GetInventory():AddItem( cItem( 284, 1 ) )
    
Player:GetInventory():AddItem( cItem( 285, 1 ) )
    
Player:GetInventory():AddItem( cItem( 286, 1 ) )
else
Server:SendMessage(cChatColor.Red .. "You cannot afford the gold kit!")
end
return true
end

function BuyDiamondKit(Split, Player)
Server = cRoot:Get():GetServer()
if(
CanAfford(1000, Player)) then
Server
:SendMessage(cChatColor.Red .. "The diamond kit has been added to your inventory")
    
Player:GetInventory():AddItem( cItem( 276, 1 ) )
    
Player:GetInventory():AddItem( cItem( 277, 1 ) )
    
Player:GetInventory():AddItem( cItem( 278, 1 ) )
    
Player:GetInventory():AddItem( cItem( 279, 1 ) )
else
Server:SendMessage(cChatColor.Red .. "You cannot afford the diamond kit!")
end
return true
end

function Buy64Torches(Split, Player)
Server = cRoot:Get():GetServer()
if(
CanAfford(64, Player)) then
Server
:SendMessage(cChatColor.Red .. "64 torches have been added to your inventory")
    
Player:GetInventory():AddItem( cItem( 50, 64 ) )
else
Server:SendMessage(cChatColor.Red .. "You cannot afford 64 torches!")
end
return true
end
-- end of store values --

function 
KitCommand1(Split, Player)
    
Player:GetInventory():AddItem( cItem( 283, 1 ) )
    
Player:GetInventory():AddItem( cItem( 284, 1 ) )
    
Player:GetInventory():AddItem( cItem( 285, 1 ) )
    
Player:GetInventory():AddItem( cItem( 286, 1 ) )
    
Player:GetInventory():AddItem( cItem( 314, 1 ) )
    
Player:GetInventory():AddItem( cItem( 315, 1 ) )
    
Player:GetInventory():AddItem( cItem( 316, 1 ) )
    
Player:GetInventory():AddItem( cItem( 317, 1 ) )
    
Server = cRoot:Get():GetServer()
    
Server:SendMessage(cChatColor.Red .. "Get your kit on!")
    return 
true
end

function PricesCommand(Split, Player)
local PlayerName = Player:GetName()
    
Server = cRoot:Get():GetServer()


local IniFile = cIniFile("score.ini")
if ( 
IniFile:ReadFile() == true ) then
    PlayerScore 
= IniFile:GetValueI(PlayerName, "score" )
    
Server:SendMessage(cChatColor.Green .. PlayerName)
    
Server:SendMessage(cChatColor.Green .. "You have earned "..PlayerScore.." credits.")
    
Server:SendMessage(cChatColor.Green .. "Keep mining to earn more!")
    
Server:SendMessage(cChatColor.Green .. "Buy 64 torches: 64 credits - use /buytorches")
    
Server:SendMessage(cChatColor.Green .. "Wood Kit: 0 credits - use /buywoodkit")
    
Server:SendMessage(cChatColor.Green .. "Stone Kit: 100 credits - use /buystonekit")
    
Server:SendMessage(cChatColor.Green .. "Iron Kit: 500 credits - use /buyironkit")
    
Server:SendMessage(cChatColor.Green .. "Gold Kit: 800 credits - use /buygoldkit")
    
Server:SendMessage(cChatColor.Green .. "Diamond Kit: 1000 credits - use /buydiamondkit")
end
return true
end

Plugin 
= KitPlugin:new()
cRoot:Get():GetPluginManager():AddPlugin( Plugin ) 

Before I forget, you can access the prices list by typing "/store" into the chat window.

That should be all else you needBig Grin
Reply
Thanks given by:
#2
Thats pretty coolTongue im working on something similar for my rpg server, one thing that i would say though is that you're using commands for each itemset which bugs me, i dont know about anyone elseTongue im just wierd, but what im doing in mine is using Split
so like
Code:
function storecommand(Split, Player)
if(Split[2]=="buy") then
-- do stuff
end
end
but its a nice plugin, more than i've got so farTongue
Reply
Thanks given by:
#3
I considered doing that, but then I'd have to add in a function to detect each type and serve accordingly, and in the end I worked out for what I was doing I would end up having to write more lines of code for that Smile

Thanks for the feedback though!
Reply
Thanks given by:
#4
For optimisation, you only need to IniFile:ReadFile() on plugin loading and IniFile:WriteFile() on server shutdown.

Class IniFile itself is maintained in RAM.
Reply
Thanks given by:
#5
Yeah, if you create the cIniFile in a global scope (global variable) you only have to read it once.
Reply
Thanks given by:
#6
Ah thanks Smile

These things you learn as you go.
Reply
Thanks given by:
#7
What am I doing wrong? I have enabled this in settings.ini and created a directory called "Store" with the LUA in that directory I've even create score.ini in that same directory as the LUA. This is what I get when I launch the server:

[19:17:05] LUA: Plugins/Store/Kit.lua:7: attempt to index global 'Lua__cPlugin' (a nil value)
[19:17:05] Error in plugin Plugins/Store in file Plugins/Store/Kit.lua
Reply
Thanks given by:
#8
You did nothing wrong. Its becouse this plugin is outdated.
Reply
Thanks given by:
#9
This plugin is extremely outdated and won't work with current MCServer versions; it needs to be properly updated and maybe altogether revised.
Reply
Thanks given by:
#10
Aww, and I really wanted a store plugin
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)