summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandgamemode.java
blob: f5ce82b9517bdf0145c988a41266d50809fe63d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.earth2me.essentials.commands;

import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import java.util.List;
import java.util.Locale;
import org.bukkit.GameMode;
import org.bukkit.Server;
import org.bukkit.entity.Player;


public class Commandgamemode extends EssentialsCommand
{
	public Commandgamemode()
	{
		super("gamemode");
	}

	@Override
	protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		GameMode gameMode;
		if (args.length == 0)
		{
			throw new NotEnoughArgumentsException();
		}
		else if (args.length == 1)
		{
			gameMode = matchGameMode(commandLabel);
			gamemodeOtherPlayers(server, sender, gameMode, args[0]);
		}
		else if (args.length == 2)
		{
			gameMode = matchGameMode(args[0].toLowerCase(Locale.ENGLISH));
			gamemodeOtherPlayers(server, sender, gameMode, args[1]);
		}

	}

	@Override
	protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
	{
		GameMode gameMode;
		if (args.length == 0)
		{
			gameMode = matchGameMode(commandLabel);
		}
		else if (args.length > 1 && args[1].trim().length() > 2 && user.isAuthorized("essentials.gamemode.others"))
		{
			gameMode = matchGameMode(args[0].toLowerCase(Locale.ENGLISH));
			gamemodeOtherPlayers(server, user.getSource(), gameMode, args[1]);
			return;
		}
		else
		{
			try
			{
				gameMode = matchGameMode(args[0].toLowerCase(Locale.ENGLISH));
			}
			catch (NotEnoughArgumentsException e)
			{
				if (user.isAuthorized("essentials.gamemode.others"))
				{
					gameMode = matchGameMode(commandLabel);
					gamemodeOtherPlayers(server, user.getSource(), gameMode, args[0]);
					return;
				}
				throw new NotEnoughArgumentsException();
			}
		}
		if (gameMode == null)
		{
			gameMode = user.getGameMode() == GameMode.SURVIVAL ? GameMode.CREATIVE : user.getGameMode() == GameMode.CREATIVE ? GameMode.ADVENTURE : GameMode.SURVIVAL;
		}
		user.setGameMode(gameMode);
		user.sendMessage(tl("gameMode", tl(user.getGameMode().toString().toLowerCase(Locale.ENGLISH)), user.getDisplayName()));
	}

	private void gamemodeOtherPlayers(final Server server, final CommandSource sender, final GameMode gameMode, final String name) throws NotEnoughArgumentsException, PlayerNotFoundException
	{
		if (name.trim().length() < 2 || gameMode == null)
		{
			throw new NotEnoughArgumentsException(tl("gameModeInvalid"));
		}

		boolean skipHidden = sender.isPlayer() && !ess.getUser(sender.getPlayer()).isAuthorized("essentials.vanish.interact");
		boolean foundUser = false;
		final List<Player> matchedPlayers = server.matchPlayer(name);
		for (Player matchPlayer : matchedPlayers)
		{
			final User player = ess.getUser(matchPlayer);
			if (skipHidden && player.isHidden())
			{
				continue;
			}
			foundUser = true;
			player.setGameMode(gameMode);
			sender.sendMessage(tl("gameMode", tl(player.getGameMode().toString().toLowerCase(Locale.ENGLISH)), player.getDisplayName()));
		}
		if (!foundUser)
		{
			throw new PlayerNotFoundException();
		}
	}

	private GameMode matchGameMode(String modeString) throws NotEnoughArgumentsException
	{
		GameMode mode = null;
		if (modeString.equalsIgnoreCase("gmc") || modeString.equalsIgnoreCase("egmc")
			|| modeString.contains("creat") || modeString.equalsIgnoreCase("1") || modeString.equalsIgnoreCase("c"))
		{
			mode = GameMode.CREATIVE;
		}
		else if (modeString.equalsIgnoreCase("gms") || modeString.equalsIgnoreCase("egms")
				 || modeString.contains("survi") || modeString.equalsIgnoreCase("0") || modeString.equalsIgnoreCase("s"))
		{
			mode = GameMode.SURVIVAL;
		}
		else if (modeString.equalsIgnoreCase("gma") || modeString.equalsIgnoreCase("egma")
				 || modeString.contains("advent") || modeString.equalsIgnoreCase("2") || modeString.equalsIgnoreCase("a"))
		{
			mode = GameMode.ADVENTURE;
		}
		else if (modeString.equalsIgnoreCase("gmt") || modeString.equalsIgnoreCase("egmt")
				 || modeString.contains("toggle") || modeString.contains("cycle") || modeString.equalsIgnoreCase("t"))
		{
			mode = null;
		}
		else
		{
			throw new NotEnoughArgumentsException();
		}
		return mode;
	}
}