[Solved] Change a player's gamemode?

I’m just wondering if gamemode implementation has been added yet. I searched the github issues for gamemode related issues, and there appears to be none. I believe the following should work, however when I run it in game on a valid player, nothing happens. Does this mean it’s just not implemented, or am I doing something wrong?

p.getGameModeData().setGameMode(GameModes.SURVIVAL);

I feel like this simply means it’s not implemented yet, however this seems like the kind of thing that would have already been implemented.

Edit: Sorry about all of the questions today, this is just one of the few days I have a lot of time to work on plugins!

You’ve got to offer modified data back – so you’d have to p.offer(p.getGameModeData().setGameMode(GameModes.SURVIVAL));

The returned DataManipulator is a copy, so it won’t affect the player until it’s actually set back on the player.

2 Likes

@zml That’s a really foreign concept to me. Why is it like this? Is this just for DataManipulators? Thanks a ton :smile:

If you programmed with Bukkit you should have encountered it with setting metadata:
item.setItemMeta(metaYouJustGotThenModifiedAndNowAreSetting);

(Although I’ve never confirmed that the retrieved meta is actually a copy so I may be wrong, although then the setter would be redundant so It’s not as bad to assume it was similar to the above concept).

The Data API can have a pretty steep learning curve, since it’s quite different (and much more powerful) than how Bukkit worekd.

It’s currently undergoing a rewrite. Once the changes land, the docs will be updated for the new changes. It’ll become much easier to get started using it.

Now that the DataAPI has been updated, what would changing a gamemode look like? I don’t really understand how it works now. Can anyone clear up some confusion? @Aaron1011 @wingz @zml

I’ve been looking for a while, but the only routes I see require something along the lines of Keys.GameMode, which doesn’t exist…

See: How to use Data API 2.0

Sorry, spilt water on my keys so I cant expain :cry:

I already looked there, but I can’t figure out how to use gamemodes with it, which is why I asked here :frowning: @wingz

Also, sorry to hear about your keyboard!

looks like this is now (6.0.0) just:

p.offer(Keys.GAME_MODE, GameModes.SURVIVAL);

It’s either or. You can still retrieve the GameModeData and offer it back to the player.

Please don’t necro old threads in the future :wink: Otherwise, the docs have some good documentation on how to do this and the comparison of using the manipulator versus using keys. It’s frequent to just use keys these days as setting simple things, but when complex things need to be processed, can use a manipulator (especially if for example you’re transforming like a schematic).