Skip to main content

Creating A Custom Fragment Generator Lua

If you just want to create a new Fragment generator for other events or just a basic one then you can use Lua to create one

First create a new folder in /Isets/ named fragment-generators should be like /Isets/fragment-generators/

Once that is done you should create a new file you can name it what you want for this guide i will name it example-generator.Lua

Creating the generator file

we will create a generator for the BlockBreakEvent it will do the same thing as the generator that comes with the plugin

package.path = pluginFolder .. "\\listeners\\?.lua"
require "BlockBreakEvent"

namespace = "example"
source = "blockBreakEvent"

function handleGeneration(player, settingsMap)
  if settingsMap:getBoolean("Enabled") and math.random() <= settingsMap:getDouble("Chance") then
    local amount = (settingsMap:getDouble("Amount_To_Give") <= 0) and 1 or settingsMap:getDouble("Amount_To_Give")
    local fragment = utils.findFragment(player)
	
	if fragment ~= nil then
	  if settingsMap:getBoolean("Physical") then
	    givePhys(player, amount, fragment)
	  else
	    giveVirtual(player, amount, fragment)
	  end
	end
	
   end
end

function givePhys(player, amount, fragment)
  fragment:giveOrUpdateFragment(player, math.floor(amount), false)
  utils.tell(player, "&2You been given " .. amount .. " &6fragments &2for &6" .. fragment:getArmorSetName())
end

function giveVirtual(player, amount, fragment)
  local cache = utils.getCache(player)
  local currentBalance = cache:getFragmentAmount(fragment:getArmorSetName())
  cache:updateFragmentAmount(fragment.armorSetName, currentVirtualBalance + math.floor(amount))
  utils.tell(player, "&2You been given " .. amount .. " &6fragments &2for &6" .. fragment:getArmorSetName())
end

Registering an event listener

to actually create a listener for the BlockBreakEvent you should first create a folder inside /Isets/ in this example we are calling the folder listeners so /Isets/listeners/ same as before create a new file you can name it as you please, in this case we name it BlockBreak.lua

utils.subscribeToEvent("org.bukkit.event.block.BlockBreakEvent", function(event)
  local player = event:getPlayer()
  local armorSet = utils.findArmorSet(utils.getCache(player):getArmorSetFragmentGen())
  
  if armorSet ~= nil then
    local type = armorSet:getFragmentGeneration():getString("Type")
	local source = armorSet:getFragmentGeneration():getString("Source")
    local fragmentGenerator = utils.findFragmentGenerator(type, source)
	
	if fragmentGenerator == nil then 
	  return
	end
	if type == namespace then
	  local disabledWorlds = armorSet:getFragmentGeneration():getStringList("Disabled_Worlds")
	  local worldName = player:getWorld():getName()
	  
	  for i = 0, disabledWorlds:size() -1 do
	    if disabledWorlds:get(i) == worldName then
	      return
	    end
	  end
	
      fragmentGenerator:handleGeneration(player, armorSet:getFragmentGeneration())
    end
  end
end)

Utils package

the utils package has a few utility functions

Util to register an event listener

subscribeToEvent("path to event class", function(event)
--Code here
end)

Util to find a armor set if it exists will return nil if none was found

findArmorSet("name of the armor set")

Util to find a fragment generator if it exists will return nil if none was found

findFragmentGenerator("type/namespace", "source")

Util to get the cache of a Player will return nil if the cache cant be found if one is found it will return the cache

getCache(player)

Util to find the current enabled fragment generator a player has enabled will return "none" if one isnt set will return the name of the armor set they enabled the generator for.

findFragment(player)

Util to send a message to a player it will auto translate colors 

tell(player, "message goes here color codes and hex is automaticly translated")