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(tKitPlugin)
   
local w Lua__cPlugin:new()
   
tolua.setpeer(wt)
   
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:AddHookselfcPluginManager.E_PLUGIN_BLOCK_DIG )
    return 
true
end


function KitPlugin:OnBlockDigPacketDataPlayer )
  
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 diggingblock broken
  
if(PacketData.m_Status == 2then
   
   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(moneyPlayer)
local PlayerName Player:GetName()
local IniFile cIniFile("score.ini")
if ( 
IniFile:ReadFile() == true then
    PlayerScore 
IniFile:GetValueI(PlayerName"score" )
    
Log(PlayerScore);
    if(
PlayerScore>=moneythen
    
if(money>0then
    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(SplitPlayer)
Server cRoot:Get():GetServer()
if(
CanAfford(0Player)) then
Server
:SendMessage(cChatColor.Red .. "The wood kit has been added to your inventory")
    
Player:GetInventory():AddItemcItem268) )
    
Player:GetInventory():AddItemcItem269) )
    
Player:GetInventory():AddItemcItem270) )
    
Player:GetInventory():AddItemcItem271) )
else
Server:SendMessage(cChatColor.Red .. "You cannot afford the wood kit!")
end
return true
end

function BuyStoneKit(SplitPlayer)
Server cRoot:Get():GetServer()
if(
CanAfford(100Player)) then
Server
:SendMessage(cChatColor.Red .. "The stone kit has been added to your inventory")
    
Player:GetInventory():AddItemcItem272) )
    
Player:GetInventory():AddItemcItem273) )
    
Player:GetInventory():AddItemcItem274) )
    
Player:GetInventory():AddItemcItem275) )
else
Server:SendMessage(cChatColor.Red .. "You cannot afford the iron kit!")
end
return true
end

function BuyIronKit(SplitPlayer)
Server cRoot:Get():GetServer()
if(
CanAfford(500Player)) then
Server
:SendMessage(cChatColor.Red .. "The iron kit has been added to your inventory")
    
Player:GetInventory():AddItemcItem267) )
    
Player:GetInventory():AddItemcItem256) )
    
Player:GetInventory():AddItemcItem257) )
    
Player:GetInventory():AddItemcItem258) )
else
Server:SendMessage(cChatColor.Red .. "You cannot afford the iron kit!")
end
return true
end

function BuyGoldKit(SplitPlayer)
Server cRoot:Get():GetServer()
if(
CanAfford(800Player)) then
Server
:SendMessage(cChatColor.Red .. "The gold kit has been added to your inventory")
    
Player:GetInventory():AddItemcItem283) )
    
Player:GetInventory():AddItemcItem284) )
    
Player:GetInventory():AddItemcItem285) )
    
Player:GetInventory():AddItemcItem286) )
else
Server:SendMessage(cChatColor.Red .. "You cannot afford the gold kit!")
end
return true
end

function BuyDiamondKit(SplitPlayer)
Server cRoot:Get():GetServer()
if(
CanAfford(1000Player)) then
Server
:SendMessage(cChatColor.Red .. "The diamond kit has been added to your inventory")
    
Player:GetInventory():AddItemcItem276) )
    
Player:GetInventory():AddItemcItem277) )
    
Player:GetInventory():AddItemcItem278) )
    
Player:GetInventory():AddItemcItem279) )
else
Server:SendMessage(cChatColor.Red .. "You cannot afford the diamond kit!")
end
return true
end

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

function 
KitCommand1(SplitPlayer)
    
Player:GetInventory():AddItemcItem283) )
    
Player:GetInventory():AddItemcItem284) )
    
Player:GetInventory():AddItemcItem285) )
    
Player:GetInventory():AddItemcItem286) )
    
Player:GetInventory():AddItemcItem314) )
    
Player:GetInventory():AddItemcItem315) )
    
Player:GetInventory():AddItemcItem316) )
    
Player:GetInventory():AddItemcItem317) )
    
Server cRoot:Get():GetServer()
    
Server:SendMessage(cChatColor.Red .. "Get your kit on!")
    return 
true
end

function PricesCommand(SplitPlayer)
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():AddPluginPlugin 

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: 4 Guest(s)