elgbar / EnumsK

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EnumsK

With this addon you can declare enums with skript.

Syntax

The only place where you can declare new enums (event)

Enums

Declare a new enum (condition)

[create ][new ]enum %object%

Declare a new sub enum (condition)

[create ][new ] sub enum %object%

Add a value to an enum (condition)

set value %object% to %object%

or

%object%: %object%

Get an enum value (expression)

value %object% (from|of) enum %object%

or

|%object%.%object%|

for sub enums just add a dot and then another object like this

|%object%.%object%.%object%|

Get all values from an enum (expression)

[all] values (from|of) enum %object%

or

|%object%::*|

Get all values from a sub enum (expression)

|%object%.%object%::*|

Example

enums: #You can only declare enums under this event	#Do not name the enum in plural, follow java see http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
	create new enum "Sword": #Skript friendly syntax
		set value "OK" to iron sword #You can use all objects
		set value "COOL" to diamond sword named "&cCool sword bro" #Even objects with spesial features like name
	enum Capability: #A bit more minimalistic
		"STRING": "This is a string"
		455: 42
		PLAYER1: "kh498" parsed as offline player #Do not use a type as an enum name eg player, console, tool as skript parsed them as %player%, %console% or %tool%
		item1 : iron sword named "%|Capability.PLAYER1|%"
		sub enum TEST: #Need to include the word sub when declaring a sub enum
			test1: "test"
			test2: "testing"

command /enum:
	trigger:
		set {_player} to |Capability.PLAYER1|
		set {_item} to |Capability.item1|
		give {_player} {_item} #This works, try it out by editing the Capability.PLAYER1 enum

		#!Note: You cannot do
		#give player |Capability.item1| #Gives no error, but doesnt give the player an item
		#!or
		#give |Capability.PLAYER1| {_item} #throws: 'single enum value can't have anything added to it'

		if {_item} is value item1 from enum Capability: #Works as expected
			send "true"
		#give player value item from enum Capability!
		#BUT you can use it inline like this:
		send "%all values from enum Capability%" #They are listed in the order they were declared
		send "%value ""OK"" from enum ""Sword""%"
		send "%value 455 from enum Capability%"
		send "%|Capability.TEST::*|%"
		send "%|Capability.TEST.test1|%"

Performance

Note: This should be taken with a grain of salt since the code have been changed since when I performed these tests.

My tests shows that accessing enums are slightly faster than accessing normal global variables. In the test you set a local variable to an enum or a global variable. This you do 100 000 times. To further smooth out the score I did the test 10 times each. This gives me:

Enums takes an average of 536.27ms

Global variable takes an average of 608.94ms (72.67ms more)

I ran both commands ten times each and took the average of those two. I did not include the first timing as it was a great gap between its time and the other timings. I have a theory on why it is sush a gap. As the server just started up it did not have the variables loaded into the cache and therefore it took to get it as you have to access the disk.

In my test skript you can see the all the script I used, individual results from each test and the whole log for the test.

But note that performance may vary

What's next?

  • Better usability
  • This will mean you can do "text" contains |enum.sameText| and give player |enum.item|

Workarounds for now

contains

"text" contains "%|enum.sameText|%"

set/add/remove

set {_i} to |enum.item|
give player {_i}

About

License:GNU General Public License v3.0


Languages

Language:Java 100.0%