Solved Money for gathering

Discussion in 'Plugin Requests' started by ViciousVixen, Jan 10, 2015.

  1. With the plugin I would reduce the amount of money you get for selling items and use gathering to fill in the gap. In theory, it would make the act of gathering more fun because of reward when in reality, you would be getting the same amount of money.

    Thanks for the reply Bombardir!
     
    Last edited by a moderator: Jan 10, 2015
  2. Started working on it. I haven't tested it at all yet and I was having trouble figuring out how to determine if the BaseEntity was a tree or ore/rock. I saw that there was a TreeEntity type, but I'm not sure if I referenced it correctly. Its getting late and I'll have another look at it tomorrow/later today.

    You can see what I've got so far here: https://github.com/HellUnit/oxide-2-plugins/blob/master/money-for-gather/money-for-gather.lua

    I removed the "GatherEnabled" option because the OnGather hook is at the end of the method and the 'item == null' check is before the hook, so I thought setting the item to null might cause an issue, if it had an effect at all.

    EDIT: I also think I got the OnGather function wrong because it is passed a BaseEntity and not a BasePlayer, which is what the Economics Plugin needs even though BasePlayer is a derived type of BaseEntity, so I'm not sure how that would work.
     
  3. BasePlayer is derived of BaseEntity (BaseEntity > BaseCombatEntity > BasePlayer), you just need to check if it's a player (BaseEntity.ToPlayer()).
    Also keep in mind that OnGather is also triggered when you hit a corpse and runs for every item you obtain, so if you're hitting stones and you'll obtain all 3 types the hook will trigger 3 times, once for every item.
    And for getting to the tree, ResourceDispenser is an EntityComponent of BaseEntity, and TreeEntity is derived of BaseEntity so it should just be possible to do it with a GetComponent or GetParentComponent to get to the TreeEntity.
     
  4. I was having the same problem when I tried it. I'm just not that good with programming. Thank you sooo much for working on this!!!
     
  5. Haha! Totally missed the BaseEntity.ToPlayer() method. I went and looked and... there it was. Solves that problem. Now just for the ResourceDispenser entity handling.

    EDIT: Well, I've got it to the point where it will give you different amounts of money for trees... and then everything else. All that's left now is determining the difference between ores and everything else you might gather (corpses, mainly as Mughisi mentioned). I'm thinking I might be able to check the item gathered rather than the type of the dispenser. Trees were easy since there is a specific global type for them, but ore nodes and corpses don't seem so straightforward. So it might be easier to check what the item that is being gathered is... I'm not sure yet, but hey it kind of works now! WOOOOOOOO! Progress can be seen at the github link above. :D
    [DOUBLEPOST=1421017916,1421008099][/DOUBLEPOST]Well, I think I've got it. Publishing it now. There may be a few bugs, but I did some basic testing on it to make sure it works.
    [DOUBLEPOST=1421019111][/DOUBLEPOST]Well, I tried to publish it and all it keeps saying is "There was a problem uploading your file". I tried it a million times and after working on it all day, that makes me wanna put my fist through the screen. Instead of that... here it is. I'll try publishing it another time when the publishing gods decide they like me better.

    Commands:
    Code:
    /setforwood <amount> - changes the amount given for gathering wood
    /setforores <amount> - changes the amount given for ores
    /m4gtoggle - enables/disables the plugin
    
    Default Config:
    Code:
    {
      "Settings": {
      -- Enables/Disables the plugin
      "PluginEnabled": "true",
      -- Amount given for gathering wood
      "WoodAmount": "100",
      -- Amount given for gathering ores
      "OreAmount": "100",
      -- Label used in chat for plugin messages
      "ChatName": "MoneyForGather",
      -- AuthLevel required to chang eplugin settings via chat commands
      -- "0" = Anyone
      -- "1" = Moderator
      -- "2" = Owner
      "AuthLevel": "1",
      -- Whether or not messages are shown to player when money is given when gathering
      "GatherMessagesEnabled": "true"
      },
      "Messages": {
      "PluginEnabled": "MoneyForGather has been enabled.",
      "PluginDisabled": "MoneyForGather has been disabled.",
      "NoPermission": "You do not have permission for that command.",
      "OreAmountChanged": "The ore amount has been changed to %s",
      "WoodAmountChanged": "The wood amount has been changed to %s",
      "ReceivedMoney": "You have received %s for gathering %s."
      }
    }
    
    money-for-gather.lua
    Code:
    PLUGIN.Title = "MoneyForGather"
    PLUGIN.Version = V(0, 0, 1)
    PLUGIN.Description = "Gain money through the Economics API for gathering"
    PLUGIN.Author = "Mr. Bubbles AKA BlazR"
    PLUGIN.Url = "None"
    PLUGIN.ResourceId = 000
    PLUGIN.HasConfig = truelocal API = nil
    local notified = "false"-- Quotesafe function to help prevent unexpected output
    local function QuoteSafe(string)
       return UnityEngine.StringExtensions.QuoteSafe(string)
    endfunction PLUGIN:Init()
       -- Load the default config and set the commands
       self:LoadDefaultConfig()
       command.AddChatCommand("setforwood", self.Object, "cmdSetAmount")
       command.AddChatCommand("setforores", self.Object, "cmdSetAmount")
       -- command.AddChatCommand("gather", self.Object, "cmdGather")
       command.AddChatCommand("m4gtoggle", self.Object, "cmdToggle")
    endfunction PLUGIN:OnServerIntialized()
       pluginsList = plugins.GetAll()
       for i = 0, tonumber(pluginsList.Length) - 1 do
         if pluginsList[i].Object.Title:match("Economics") then  
           API = GetEconomyAPI()
         end
       end
       if API == nil then
         print("Economics plugin not found. MoneyForGather plugin will not function!")
       end
    endfunction PLUGIN:LoadDefaultConfig()
       -- Set/load the default config options
       self.Config.Settings = self.Config.Settings or {
         ChatName = "MoneyForGather",
         PluginEnabled = "true",
         WoodAmount = "100",
         OreAmount = "100",
         GatherMessagesEnabled = "true",
         -- GatherEnabled = "true",
         AuthLevel = "1"
       }
       -- Various messages used by the plugin
       self.Config.Messages = self.Config.Messages or {
         OreAmountChanged = "The ore amount has been changed to %s",
         WoodAmountChanged = "The wood amount has been changed to %s",
         NoPermission = "You do not have permission for that command.",
         PluginEnabled = "MoneyForGather has been enabled.",
         PluginDisabled = "MoneyForGather has been disabled.",
         ReceivedMoney = "You have received %s for gathering %s."
         -- GatherEnabled = "Gathering has been enabled.",
         -- GatherDisabled = "Gathering has been disabled."
       }
       self:SaveConfig()
    endfunction PLUGIN:OnGather(dispenser, player, item)
       if API ~= nil and self.Config.Settings.PluginEnabled == "true" then
         player = player:ToPlayer()
         if player then
           userdata = API:GetUserDataFromPlayer(player)
           if dispenser:GetComponentInParent(global.TreeEntity._type) then
             userdata:Deposit(tonumber(self.Config.Settings.WoodAmount))
             if self.Config.Settings.GatherMessagesEnabled == "true" then
               player:SendConsoleCommand("chat.add " .. QuoteSafe(self.Config.Settings.ChatName) .. " " .. QuoteSafe(self.Config.Messages.ReceivedMoney:format(self.Config.Settings.WoodAmount, item.info.displayname)))
             end
           elseif item.info.displayname == "Metal Ore" or item.info.displayname == "Sulfur Ore" then
             userdata:Deposit(tonumber(self.Config.Settings.OreAmount))
             if self.Config.Settings.GatherMessagesEnabled == "true" then
               player:SendConsoleCommand("chat.add " .. QuoteSafe(self.Config.Settings.ChatName) .. " " .. QuoteSafe(self.Config.Messages.ReceivedMoney:format(self.Config.Settings.OreAmount, item.info.displayname)))
             end
           end
         end
       elseif API == nil and notified == "false" then
         pluginsList = plugins.GetAll()
         for i = 0, tonumber(pluginsList.Length) - 1 do
           if pluginsList[i].Object.Title:match("Economics") then  
             API = GetEconomyAPI()
           end
         end
         if API == nil then
           print("Economics plugin not found. MoneyForGather plugin will not function!")
           notified = "true"
         end
       end
    endfunction PLUGIN:cmdSetAmount(player, cmd, args)
       if player.net.connection.authLevel >= tonumber(self.Config.Settings.AuthLevel) then
         if args then
           if cmd == "setforwood" then
             self.Config.Settings.WoodAmount = tostring(args[0])
             self:SaveConfig()
             player:SendConsoleCommand("chat.add " .. QuoteSafe(self.Config.Settings.ChatName) .. " " .. QuoteSafe(self.Config.Messages.WoodAmountChanged:format(tostring(args[0]))))
           else
             self.Config.Settings.OreAmount = tostring(args[0])
             self:SaveConfig()
             player:SendConsoleCommand("chat.add " .. QuoteSafe(self.Config.Settings.ChatName) .. " " .. QuoteSafe(self.Config.Messages.OreAmountChanged:format(tostring(args[0]))))
           end
         end
       else
         player:SendConsoleCommand("chat.add " .. QuoteSafe(self.Config.Settings.ChatName) .. " " .. QuoteSafe(self.Config.Messages.NoPermission))
       end
    end-- function PLUGIN:cmdGather(player, cmd, args)
       -- Add code to toggle config.Settings.GatherEnabled here
    -- endfunction PLUGIN:cmdToggle(player, cmd, args)
       if player.net.connection.authLevel >= tonumber(self.Config.Settings.AuthLevel) then
         if self.Config.Settings.PluginEnabled == "true" then
           self.Config.Settings.PluginEnabled = "false"
           self:SaveConfig()
           player:SendConsoleCommand("chat.add " .. QuoteSafe(self.Config.Settings.ChatName) .. " " .. QuoteSafe(self.Config.Messages.PluginDisabled))
         else
           self.Config.Settings.PluginEnabled = "true"
           self:SaveConfig()
           player:SendConsoleCommand("chat.add " .. QuoteSafe(self.Config.Settings.ChatName) .. " " .. QuoteSafe(self.Config.Messages.PluginEnabled))
         end
       else
         player:SendConsoleCommand("chat.add " .. QuoteSafe(self.Config.Settings.ChatName) .. " " .. QuoteSafe(self.Config.Messages.NoPermission))
       end
    end
    
     
  6. This is fantastic! Thank you so much for putting so much time into some random request from some random person on a weekend. This community rocks.

    Do I mark this as solved or is this something a moderator does?
     
  7. Wulf

    Wulf Community Admin

    Either works, but it has been done. ;)
     
  8. No problem. It helps you because you get the plugin you wanted and it helps me because I'm still learning Lua and the Oxide API, so its mutually beneficial. :)

    EDIT: Finally got it published. Money For Gather
     
  9. Wulf

    Wulf Community Admin

    Why not ask for options from the other plugin?
     
  10. Wulf

    Wulf Community Admin

    Okay? I wasn't suggesting you to review it, I was suggesting to ask under that plugin's Discussion thread for config options to customize it.
     
  11. I think it'll be easily doable. I'll work on it in a bit. ;) Would you want it to give money for specific items gathered from the corpses/animals or just for the act of gathering from a corspe? As it is now, it gives you money just for the act of gathering from a tree since all trees drop is wood. For ore rocks, it only gives you money for 'Metal Ore' or 'Sulfur Ore' and not 'Stones', meaning it filters by item rather than what the item was gathered from. So there's 2 ways to go about it and I just wanted a little clarification. It may be easier to do it one way or the other. I haven't looked yet.
     
    Last edited by a moderator: Jan 12, 2015
  12. Handle both options and let the users configure it with the config file :)
     
  13. True, but like in the case of Ore Rocks, I couldn't figure out how to get to the 'OreRock' entity, so I ended up just filtering by item name since that was easily discoverable. This may or may not be the case with corpses.

    EDIT: Should be doable either way. ResourceDispenser > EntityComponent<BaseEntity> > BaseEntity > BaseCombatEntity > BaseCorpse . So the BaseCorpse type should be discoverable from the ResourceDispenser passed to the OnGather hook.
     
    Last edited by a moderator: Jan 12, 2015
  14. Can't you just check the ResourceDispenser, just do a print( ResourceDispenser:ToString() ), I think you'll be able to use that :)
     
  15. I would like it for the act of gathering from a corpse.
     
  16. I'll give that a shot. That'd be way easier than a million GetComponent() calls.

    I'll see what I can do. :)