Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.88 KB | None | 0 0
  1. --
  2. -- Created by dev0
  3. --
  4.  
  5. Plants = {}
  6.  
  7. function Plants.new(...)
  8.     local o = setmetatable({}, { __index = Plants })
  9.     if o.constructor then
  10.         o:constructor(...)
  11.     end
  12.     return o
  13. end
  14.  
  15. function Plants:constructor()
  16.     self._tblAllPlants = {}
  17.     self._tblPlantTimer = {}
  18.  
  19.     setTimer(function() self:createPlants() end, 1000, 1)
  20.  
  21.     addEventHandler("onPlayerClick", root, function(...) self:onPlantClick(...) end)
  22. end
  23.  
  24. function Plants:createPlants()
  25.     local SQL = dbQuery(connection, "SELECT * FROM plants")
  26.     if (SQL) then
  27.         local tblResult, iRows = dbPoll(SQL, -1)
  28.         if (iRows >= 1) then
  29.             for _, value in ipairs(tblResult) do
  30.                 self._plantID = tonumber(value["id"])
  31.                 self._plantOwner = tostring(value["owner"])
  32.                 self._plantTime = tonumber(value["time"])
  33.                 self._plantModelID = tonumber(value["type"])
  34.  
  35.                 if (not (isElement(self._tblAllPlants[self._plantID]))) then
  36.                     self._tblAllPlants[self._plantID] = Object(tonumber(value["type"]), tonumber(value["posX"]), tonumber(value["posY"]), tonumber(value["posZ"]) - 0.9)
  37.  
  38.                     self._tblAllPlants[self._plantID]:setData("id", self._plantID)
  39.                     self._tblAllPlants[self._plantID]:setData("owner", self._plantOwner)
  40.                     self._tblAllPlants[self._plantID]:setData("time", self._plantTime)
  41.                     self._tblAllPlants[self._plantID]:setData("isPlant", true)
  42.  
  43.                     if (self._plantModelID == 3409) then
  44.                         self._PlantPlaceHolder = Object(2991, 0, 0, 0, 0, 0, 0)
  45.                         self._PlantPlaceHolder1 = Object(2991, 0, 0, 0, 0, 0, 0)
  46.                         self._PlantPlaceHolderPosition = Vector3(-2588.5119628906 - (-2589.3125), 330.2961730957 - (330.35571289063), -0.5)
  47.                         self._PlantPlaceHolderPosition1 = Vector3(-2588.5119628906 - (-2587.8278808594), 330.2961730957 - (330.31005859375), -0.5)
  48.  
  49.                         self._PlantPlaceHolder:attach(self._tblAllPlants[self._plantID], self._PlantPlaceHolderPosition.x, self._PlantPlaceHolderPosition.y, self._PlantPlaceHolderPosition.z, 0, 0, 270)
  50.                         self._PlantPlaceHolder1:attach(self._tblAllPlants[self._plantID], self._PlantPlaceHolderPosition1.x, self._PlantPlaceHolderPosition1.y, self._PlantPlaceHolderPosition1.z, 0, 0, 270)
  51.  
  52.                         self._PlantPlaceHolder:setParent(self._tblAllPlants[self._plantID])
  53.                         self._PlantPlaceHolder1:setParent(self._tblAllPlants[self._plantID])
  54.  
  55.                         self._PlantPlaceHolder:setAlpha(0)
  56.                         self._PlantPlaceHolder1:setAlpha(0)
  57.  
  58.                         self._PlantPlaceHolder:setData("weedObject", self._tblAllPlants[self._plantID])
  59.                         self._PlantPlaceHolder1:setData("weedObject", self._tblAllPlants[self._plantID])
  60.                     end
  61.  
  62.                     if (self._plantModelID == 3409) then
  63.                         self._PlantGrowTime = 120000
  64.                     else
  65.                         self._PlantGrowTime = 240000
  66.                     end
  67.  
  68.                     if (self._plantTime < 100) then
  69.                         self._tblPlantTimer[self._tblAllPlants[self._plantID]] = setTimer(function(uPlant)
  70.                             if (isElement(uPlant) and uPlant:getData("time") < 100) then
  71.                                 uPlant:setData("time", uPlant:getData("time") + 1)
  72.                             end
  73.                         end, self._PlantGrowTime, 100 - self._plantTime, self._tblAllPlants[self._plantID])
  74.                     end
  75.                 end
  76.             end
  77.         end
  78.     end
  79. end
  80.  
  81. function Plants:deletePlant(uPlayer, uPlant)
  82.     if (isElement(uPlant)) then
  83.         for key, value in ipairs(self._tblAllPlants) do
  84.             if value == uPlant then
  85.                 table.remove(self._tblAllPlants, key)
  86.                 break
  87.             end
  88.         end
  89.         if (uPlant:getModel() == 3409) then
  90.             if (uPlant:getData("time") == 100 or isAdmin(uPlayer, 1)) then
  91.                 -- uPlayer:setData("WEED", uPlayer:getData("WEED") + math.random(1, 5))
  92.                 dbExec(connection, "DELETE FROM plants WHERE id = '" .. uPlant:getData("id") .. "'")
  93.                 uPlayer:outputChat("Du hast das Hanf abgeerntet!", 0, 120, 0)
  94.                 if isElement(uPlant) then uPlant:destroy() end
  95.             else
  96.                 uPlayer:outputChat("Du kannst diese Pflanze noch nicht ernten!", 120, 0, 0)
  97.             end
  98.         end
  99.     end
  100. end
  101.  
  102. function Plants:onPlantClick(strButton, strState, uElement)
  103.     if (strButton == "left" and strState == "up") then
  104.         if (uElement) then
  105.             local playerPosition = Vector3(getElementPosition(source))
  106.             if (getDistanceBetweenPoints3D(playerPosition.x, playerPosition.y, playerPosition.z, getElementPosition(uElement)) <= 5) then
  107.                 if (uElement:getData("weedObject")) then
  108.                     local clickedPlant = uElement:getData("weedObject")
  109.                     source:outputChat(string.format("PflanzenID: %s, Pflanzenowner: %s, Pflanzenzeit: %s", clickedPlant:getData("id"), clickedPlant:getData("owner"), clickedPlant:getData("time")), 255, 255, 0)
  110.                 end
  111.             end
  112.         end
  113.     elseif (strButton == "right" and strState == "up") then
  114.         if (uElement) then
  115.             local playerPosition = Vector3(getElementPosition(source))
  116.             if (getDistanceBetweenPoints3D(playerPosition.x, playerPosition.y, playerPosition.z, getElementPosition(uElement)) <= 5) then
  117.                 if (uElement:getData("weedObject")) then
  118.                     local clickedPlant = uElement:getData("weedObject")
  119.                     self:deletePlant(source, clickedPlant)
  120.                 end
  121.             end
  122.         end
  123.     end
  124. end
  125.  
  126. G_Plants = Plants.new()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement