Skip to main content

Creating A Custom Fragment Generator Lua

IfIt youis just wantpossible to create a new Fragment generator for other events or just a basic one then you can use Lua to createdevelop onea simple or new fragment generator for additional events.

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

. 

Once that is donedone, you should create a new filefile. youYou can name it whatwhatever you wantwant. forFor this guideguide, iI will name it example-generator.Lua.

Creating the generator file

weWe will create a generator for the BlockBreakEvent. itIt will dobe similar to the samedefault thinggenerator asprovided by the generator that comes with the pluginplugin.

package.path = pluginFolder .. "\\fragment-generators\\listeners\\?.lua"

namespace = "example"
source = "blockBreakEvent"
require "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

toTo actually create a listener for the BlockBreakEvent, you should first create a folder inside /Isets/. inIn this exampleexample, we are callingname the folder listeners so'listeners'; /Isets/fragment-generators/listeners/. same as before createCreate a new file you can name it as you please,and in this casecase, 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

theThe utils package has a few utility functionsfunctions.

Util to register an event listenerlistener.

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

Util to findretrieve aan armor setset. ifIf itnone existswas located, the function will return nil if none was foundnil.

findArmorSet("name of the armor set")

Util to findretrieve a fragment generator. If no fragment generator ifwas itdiscovered, existsthe function will return nil if none was foundnil.

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

Util to get the cache ofretrieve a Playerplayer's cache. It will return nil if the cache cantcannot be foundlocated; if oneit is foundcan, it will return the cachecache.

getCache(player)

UtilUse this to finddetermine the current enabledwhich fragment generator a player currently has enabledenabled. will returnIf "none" ifis onereturned, isntthen setthey willdid returnnot theenable one. The name of the armor set for which they enabledactivated the generator for.for will be returned.

findFragment(player)

Util to send a message to a playerplayer. itIt will auto translate colors colors.

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