craftmanager.sk

Created by TPGamesNL

Other available versions. Ordered by newest to oldest versions:

Just so you know, we don't know the file format for every file. If it's just a bunch of random characters, it's probably a .zip or .jar.

options:
	# Don't log errors to console:
	no-log: false # You shouldn't touch this option unless you're getting errors which you want to get rid of and you're unable to fix.
	# Delete custom recipes when the server starts:
	delete-recipes-on-server-start: true # true/false

import:
	ch.njol.skript.Skript
	org.bukkit.inventory.ShapedRecipe
	org.bukkit.inventory.ShapelessRecipe
	org.bukkit.NamespacedKey
	
	# Classes for error printing:
	java.util.logging.Level
	ch.njol.skript.log.SkriptLogger

function char(str: string, i: number) :: string: # Get a specific character in a string, since I'm too lazy to type the substring of thing out
	return substring of {_str} from {_i} to {_i}

function setChar(str: string, i: number, str2: string) :: string: # Set a specific character in a string to some other string, since you can't change substrings using that expression in Skript
	set {_l::*} to split {_str} at ""
	set {_l::%{_i}%} to {_str2}
	return join {_l::*} with ""

effect: # Effect for deleting all custom recipes, making them no longer craftable
	patterns:
		delete [all] custom recipes
		remove [all] custom recipes
	trigger:
		Skript.getInstance().getServer().resetRecipes()
	
effect: # Effect for creating a shaped recipe, with automatic shape detection
	patterns:
		register [new] shaped recipe with [items] %items% [(and|with)] result %item% [(and|with)] id %string%
		create [new] shaped recipe with [items] %items% [(and|with)] result %item% [(and|with)] id %string%
	trigger:
		expression-1, expression-2 and expression-3 are set
		# Getting parameter values and converting the items to org.bukkit.inventory.ItemStack
		set {_items::*} to expression-1
		set {_result} to expression-2
		set {_key} to expression-3
		
		# Getting NamespacedKey (some sort of ID I think)
		set {_nk} to new NamespacedKey(Skript.getInstance(), {_key})
		
		# Preparing shape
		set {_r} to new ShapedRecipe({_nk}, {_result})
		set {_shape} to "123456789"
		loop 9 times where [{_items::%input%} ? air is air]:
			replace every "%loop-num%" in {_shape} with "*"
		set {_l} to {_shape}.split("(?<=\G...)")
		
		# Part that takes care of removing unnecessary air on rows
		if {_l}[0] = "***":
			set {_l}[0] to ""
		if {_l}[2] = "***":
			set {_l}[2] to ""
		if {_l}[1] = "***":
			{_l}[0] or {_l}[2] = "" # Only remove middle if above/below was also removed
			set {_l}[1] to ""
		
		# Actually remove the values (it couldn't be done in the previous part since that would mess up the next conditions)
		loop 3 times where [{_l}[input - 1] = ""]:
			delete {_l}[loop-num - 1]
		loop ...{_l}:
			loop-value is set
			add loop-value to {_l::*}
		set {_l} to [{_l::*}]
		
		# Part that takes care of removing unnecessary air on columns
		if char({_l}[0], 1) ? "*", char({_l}[1], 1) ? "*" and char({_l}[2], 1) ? "*" are "*":
			loop 3 times:
				char({_l}[loop-num - 1], 1) is set
				set {_l}[loop-num - 1] to setChar({_l}[loop-num - 1], 1, "&")
			set {_b} to true
		if char({_l}[0], 3) ? "*", char({_l}[1], 3) ? "*" and char({_l}[2], 3) ? "*" are "*":
			loop 3 times:
				char({_l}[loop-num - 1], 3) is set
				set {_l}[loop-num - 1] to setChar({_l}[loop-num - 1], 3, "&")
			set {_b} to true
		if {_b} is true:
			char({_l}[0], 2) ? "*", char({_l}[1], 2) ? "*" and char({_l}[2], 2) ? "*" are "*"
			loop 3 times:
				char({_l}[loop-num - 1], 2) is set
				set {_l}[loop-num - 1] to setChar({_l}[loop-num - 1], 2, "&")
		
		# Actually remove the values
		loop 3 times:
			replace every "&" in {_l}[loop-num - 1] with ""
		
		# Now the actual creation of the recipe using the shape and items
		{_r}.shape({_l})
		loop 9 times where [{_items::%input%} ? air is not air]:
			{_r}.setIngredient("%loop-num%", {_items::%loop-number%})
		try Skript.getInstance().getServer().addRecipe({_r})
		
		# Error detecting
		error is set
		{@no-log} is false
		if "%error%" is "java.lang.IllegalStateException: Duplicate recipe ignored with ID %{_nk}%":
			SkriptLogger.log(Level.WARNING!, "CraftManager warning: duplicate ID detected!")
		else:
			SkriptLogger.log(Level.SEVERE!, "CraftManager error: %error%")

effect:
	patterns:
		register [new] shapeless recipe with [items] %items% [(and|with)] result %item% [(and|with)] id %string%
		create [new] shapeless recipe with [items] %items% [(and|with)] result %item% [(and|with)] id %string%
	trigger:
		expression-1, expression-2 and expression-3 are set
		# Getting parameter values and converting the items to org.bukkit.inventory.ItemStack
		set {_items::*} to expression-1
		set {_result} to expression-2
		set {_key} to expression-3
		
		# Getting NamespacedKey (some sort of ID I think)
		set {_nk} to new NamespacedKey(Skript.getInstance(), {_key})
		
		# Creating recipe
		set {_r} to new ShapelessRecipe({_nk}, {_result})
		loop {_items::*} where [input is not air]:
			{_r}.addIngredient(loop-value)
		try Skript.getInstance().getServer().addRecipe({_r})
		
		# Error detecting
		error is set
		{@no-log} is false
		if "%error%" is "java.lang.IllegalStateException: Duplicate recipe ignored with ID %{_nk}%":
			SkriptLogger.log(Level.WARNING!, "CraftManager warning: duplicate ID detected!")
		else:
			SkriptLogger.log(Level.SEVERE!, "CraftManager error: %error%")
				
on skript load:
	{@delete-recipes-on-server-start} is true
	delete all custom recipes