Add privs to items?

Post Reply
User avatar
christoferlevich
Member
Posts: 324
Joined: Thu Dec 01, 2016 23:44
GitHub: ChristoferL
Location: Athol, Massachusetts

Add privs to items?

by christoferlevich » Post

Is it possible to add Privs to an Item? My desire is to add privs to modified versions of armor that would grant a 'faster turbo' - or 'flight' - or teleport - perhaps enhance damage - add a healing ability - noclip - etc. If I can make the items degrade (like a tool) it would be an awesome thing for my students shoot for. They all want privs but I only want to grant them as a goal.
everything can be a learning experience...

User avatar
maikerumine
Member
Posts: 1403
Joined: Mon Aug 04, 2014 14:27
GitHub: maikerumine
In-game: maikerumine

Re: Add privs to items?

by maikerumine » Post

christoferlevich wrote:Is it possible to add Privs to an Item? My desire is to add privs to modified versions of armor that would grant a 'faster turbo' - or 'flight' - or teleport - perhaps enhance damage - add a healing ability - noclip - etc. If I can make the items degrade (like a tool) it would be an awesome thing for my students shoot for. They all want privs but I only want to grant them as a goal.
Great idea! I tried messing around with feature but lack skill in coding. I be interested in your outcome.
Extreme Survival Minetest-->viewtopic.php?f=10&t=12959

User avatar
stu
Member
Posts: 923
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11
Location: United Kingdom

Re: Add privs to items?

by stu » Post

christoferlevich wrote:Is it possible to add Privs to an Item? My desire is to add privs to modified versions of armor that would grant a 'faster turbo' - or 'flight' - or teleport - perhaps enhance damage - add a healing ability - noclip - etc. If I can make the items degrade (like a tool) it would be an awesome thing for my students shoot for. They all want privs but I only want to grant them as a goal.
Most of this is already possible with my 3d_armor mod, see the 'Speed Boots' example I have given. https://github.com/stujones11/minetest- ... E.txt#L101

Currently armor is only damaged when the player takes damage or is 'punched' by an appropriate tool, however, you could use the on_equip callback to apply damage every time an item is equipped, eg. (untested)

Code: Select all

on_equip = function(player, index, stack)
	stack:add_wear(100)
	armor:set_inventory_stack(player, index, stack)
end,
Note that these features are only available in the current git master version of minetest-3d_armor the forum post links to the current stable version-0.4.8

Edit: I have just pushed some changes that are important if you read this earlier, note the order of index and stack, please bear in mind these are in-development features, after all :)

User avatar
christoferlevich
Member
Posts: 324
Joined: Thu Dec 01, 2016 23:44
GitHub: ChristoferL
Location: Athol, Massachusetts

Re: Add privs to items?

by christoferlevich » Post

stu wrote:Most of this is already possible with my 3d_armor mod, see the 'Speed Boots' example I have given. https://github.com/stujones11/minetest- ... E.txt#L101

Currently armor is only damaged when the player takes damage or is 'punched' by an appropriate tool, however, you could use the on_equip callback to apply damage every time an item is equipped, eg. (untested)
I was actually looking at your mod as a starting point as the students LOVE your armor. THANK YOU FOR THE AWESOME MOD! I didn't realize there were speed boots... going to check out your link now!
everything can be a learning experience...

User avatar
stu
Member
Posts: 923
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11
Location: United Kingdom

Re: Add privs to items?

by stu » Post

christoferlevich wrote:I was actually looking at your mod as a starting point as the students LOVE your armor. THANK YOU FOR THE AWESOME MOD! I didn't realize there were speed boots... going to check out your link now!
You are very welcome, the speed boots example shows how to use the in built-in physics overrides but I guess there is nothing stopping you from setting player privileges directly, with a little care. Here is another example of how I would do that, again untested.

Code: Select all

local player_privs = {}

armor:register_armor("mod_name:fly_boots", {
	description = "Flying Boots",
	inventory_image = "mod_name_fly_boots_inv.png",
	texture = "mod_name_fly_boots.png",
	preview = "blank.png",
	groups = {armor_feet=1, armor_use=0},
	armor_groups = {fleshy=5},
	on_punched = function()
		-- override punch damage effects
		return false
	end,
	on_equip = function(player, index, stack)
		local name = player:get_player_name()
		local privs = minetest.get_player_privs(name)
		if not player_privs[name] then
			-- store initial player privs
			player_privs[name] = table.copy(privs)
		end
		privs.fly = true
		minetest.set_player_privs(name, privs)
		armor:damage(player, index, stack, 1000) -- approx 65 uses
	end,
	on_unequip = function(player, index, stack)
		local name = player:get_player_name()
		local privs = minetest.get_player_privs(name)
		-- restore initial fly priv
		privs.fly = player_privs[name].fly
		minetest.set_player_privs(name, privs)
	end,
})

minetest.register_on_leaveplayer(function(player)
	local name = player:get_player_name()
	-- restore initial player privs and clean-up
	if player_privs[name] then
		minetest.set_player_privs(name, player_privs[name])
		player_privs[name] = nil
	end
end)
Naturally, if you have other mods that alter player privs then those effects would also need to be taken into account.

Edit: I have just pushed a change that hopefully fixes a potential problem with the above code. If the armor was initialized before the `on_joinplayer` of this mod is called then the player would receive permanently elevated fly privileges, if they were to join whilst wearing the 'flying boots'

Update: I have now changed the example code so that it would have avoided that problem anyway, also less lines :)

PS: Anyone is welcome to use this as a template for a privilege giving armor mod, you only need the one `on_leaveplayer` registration, simply replace 'fly' with your privilege of choice in the armor definition. I can imagine a no-clip vest or teleporting pants being quite popular ;-)

Example code is CC0 (public domain)
Last edited by stu on Fri Apr 21, 2017 18:48, edited 2 times in total.

User avatar
firefox
Member
Posts: 1597
Joined: Wed Jan 14, 2015 07:34
In-game: Red_Fox
Location: Xanadu

Re: Add privs to items?

by firefox » Post

stu wrote:I can imagine teleporting pants being quite popular ;-)
would be funny if the pants would teleport and leave the player behind :P
✨🏳️‍🌈♣️✨

User avatar
christoferlevich
Member
Posts: 324
Joined: Thu Dec 01, 2016 23:44
GitHub: ChristoferL
Location: Athol, Massachusetts

Re: Add privs to items?

by christoferlevich » Post

Where do I put that code to make it work? I'm scouring your code but not sure how to add this.
everything can be a learning experience...

User avatar
stu
Member
Posts: 923
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11
Location: United Kingdom

Re: Add privs to items?

by stu » Post

christoferlevich wrote:Where do I put that code to make it work? I'm scouring your code but not sure how to add this.
I have attached a working bare-bones mod that demonstrates how to do this. This uses a timer based wear system with the example flying boots lasting about 10 minutes or so.

Please note that there have been some minor changes to the api since the last code I posted, however, I have since amended that post.

Edit: I forgot to include the `on_leaveplayer` registration and it is quite important. Please re-download if you did so earlier.
Last edited by stu on Tue May 23, 2017 18:35, edited 1 time in total.

User avatar
christoferlevich
Member
Posts: 324
Joined: Thu Dec 01, 2016 23:44
GitHub: ChristoferL
Location: Athol, Massachusetts

Re: Add privs to items?

by christoferlevich » Post

This is beautiful. I am truly going to enjoy learning in this. I have a hundred questions but will first try to figure them out myself. :) I am very grateful for your assistance! I am going to play with it right now. :)

"UPDATE: got an error - 2017-04-22 03:56:10: ERROR[Main]: ModError: Failed to load and run script from C:\Users\Admin\Desktop\ACES .4\bin\..\mods\privkit\init.lua:
2017-04-22 03:56:10: ERROR[Main]: C:\Users\Admin\Desktop\ACES .4\bin\..\mods\privkit\init.lua:19: attempt to call method 'register_armor' (a nil value)
2017-04-22 03:56:10: ERROR[Main]: stack traceback:
2017-04-22 03:56:10: ERROR[Main]: C:\Users\Admin\Desktop\ACES .4\bin\..\mods\privkit\init.lua:19: in main chunk"

I guess I should troubleshoot thte 'nil value'? LOL - don't worry if your too busy to look. I am just updating as I go. :)
everything can be a learning experience...

User avatar
texmex
Member
Posts: 1752
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: tacotexmex

Re: Add privs to items?

by texmex » Post

Great topic! Never realized 3d_armor was useful for so much more than just damage reduction.
Mods | Support Mesehub: bc1qluuests9rxmlnvpjrhsnyjg9ucwy6z3r0y3srw

User avatar
christoferlevich
Member
Posts: 324
Joined: Thu Dec 01, 2016 23:44
GitHub: ChristoferL
Location: Athol, Massachusetts

Re: Add privs to items?

by christoferlevich » Post

texmex wrote:Great topic! Never realized 3d_armor was useful for so much more than just damage reduction.
Kind of like a poor man's Iron Man - :)
everything can be a learning experience...

User avatar
texmex
Member
Posts: 1752
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: tacotexmex

Re: Add privs to items?

by texmex » Post

Error when trying out privkit:
+ Spoiler
My server crashes as soon as I start to fly. I don't think I can figure it out myself. I'm on 0.4.15-stable.

Soon:
+ Spoiler
Mods | Support Mesehub: bc1qluuests9rxmlnvpjrhsnyjg9ucwy6z3r0y3srw

User avatar
stu
Member
Posts: 923
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11
Location: United Kingdom

Re: Add privs to items?

by stu » Post

christoferlevich wrote: 2017-04-22 03:56:10: ERROR[Main]: C:\Users\Admin\Desktop\ACES .4\bin\..\mods\privkit\init.lua:19: attempt to call method 'register_armor' (a nil value)
texmex wrote: 2017-04-22 17:23:22: ERROR[Main]: ServerError: Runtime error from mod 'privkit' in callback environment_Step(): ...brary/Application Support/minetest/mods/privkit/init.lua:13: attempt to call method 'damage' (a nil value)
In both cases, it looks like you need to update your 3d_armor mod.
stu wrote: Note that these features are only available in the current git master version of minetest-3d_armor the forum post links to the current stable version-0.4.8

User avatar
texmex
Member
Posts: 1752
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: tacotexmex

Re: Add privs to items?

by texmex » Post

Oh, of course, sorry. It works great now!
Mods | Support Mesehub: bc1qluuests9rxmlnvpjrhsnyjg9ucwy6z3r0y3srw

User avatar
stu
Member
Posts: 923
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11
Location: United Kingdom

Re: Add privs to items?

by stu » Post

Unfortunately I have spotted a potential problem. If a server goes down while a player has privkit armor equipped, they will rejoin with permanent 'fly' privs. I could register an `on_shutdown` to clean up after a legitimate shutdown but you cannot rely on that being the case.

The only possible solution I can think of is to set the relevant privs off by default for all players on joining. Perhaps add a privkit priv where this is done only for those players. Someone else may have a better idea.

If anyone wants to make this into a mod I am happy to help with coding, I just do not have time (or skill) to make nice textures and add all the other things like crafting, balance, documentation, etc.

User avatar
stu
Member
Posts: 923
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11
Location: United Kingdom

Re: Add privs to items?

by stu » Post

Here is an updated and much simplified version that requires the 'privkit' privilege to be effective, granted to singleplayer by default.
Attachments
privkit.zip
(8.53 KiB) Downloaded 46 times

User avatar
texmex
Member
Posts: 1752
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: tacotexmex

Re: Add privs to items?

by texmex » Post

Is there a way to also turn on fly mode when granting the priv? I think it defaults to being off even when granted.

I'd help making this into a proper mod conceptually, but I lack coding skills. I can do textures, recipe and such though.
Mods | Support Mesehub: bc1qluuests9rxmlnvpjrhsnyjg9ucwy6z3r0y3srw

User avatar
stu
Member
Posts: 923
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11
Location: United Kingdom

Re: Add privs to items?

by stu » Post

texmex wrote:Is there a way to also turn on fly mode when granting the priv? I think it defaults to being off even when granted.
Unfortunately not that I am aware of but that would be a nice feature to have.
texmex wrote:I'd help making this into a proper mod conceptually, but I lack coding skills. I can do textures, recipe and such though.
I am not sure if there would be a big demand for such a mod, apart from fly and maybe fast move the other privs don't make much sense as armor items, IMO. The only real advantage is that you can see who has what privilege which can be granted for a limited time.

User avatar
christoferlevich
Member
Posts: 324
Joined: Thu Dec 01, 2016 23:44
GitHub: ChristoferL
Location: Athol, Massachusetts

Re: Add privs to items?

by christoferlevich » Post

unless ALL privs were dependent on armor... lol - that could be an interesting sub game. Constantly having to avoid losing interact, shout, fly, speed, etc. (I guess at 'etc' is where ideas grow empty...)
everything can be a learning experience...

Post Reply

Who is online

Users browsing this forum: Hariox, YaCy [Bot] and 1 guest