1 |
torben |
1786 |
package dk.thoerup.bukkit.hoeruputils.web; |
2 |
|
|
|
3 |
|
|
import java.math.BigInteger; |
4 |
|
|
import java.security.MessageDigest; |
5 |
|
|
import java.security.NoSuchAlgorithmException; |
6 |
|
|
|
7 |
|
|
import org.bukkit.command.Command; |
8 |
|
|
import org.bukkit.command.CommandExecutor; |
9 |
|
|
import org.bukkit.command.CommandSender; |
10 |
|
|
import org.bukkit.entity.Player; |
11 |
|
|
import org.bukkit.plugin.Plugin; |
12 |
|
|
|
13 |
torben |
3203 |
import io.ebean.Ebean; |
14 |
|
|
|
15 |
torben |
1786 |
public class WebUserCommand implements CommandExecutor { |
16 |
|
|
|
17 |
|
|
Plugin plugin; |
18 |
|
|
|
19 |
torben |
3203 |
public WebUserCommand(Plugin plugin) { |
20 |
torben |
1786 |
this.plugin = plugin; |
21 |
torben |
3203 |
|
22 |
|
|
Ebean.getDefaultServer().find(WebUser.class).findCount(); |
23 |
|
|
|
24 |
|
|
|
25 |
torben |
1786 |
} |
26 |
|
|
|
27 |
|
|
@Override |
28 |
|
|
public boolean onCommand(CommandSender sender, Command command, |
29 |
|
|
String label, String[] args) { |
30 |
|
|
|
31 |
|
|
if (!(sender instanceof Player)) { |
32 |
|
|
sender.sendMessage("This is not a console command!"); |
33 |
|
|
return true; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
if (args.length != 1) { |
37 |
|
|
sender.sendMessage("Usage: /webuser <new_password>"); |
38 |
|
|
return true; |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
String password = args[0]; |
42 |
|
|
|
43 |
torben |
3203 |
WebUser user = Ebean.getDefaultServer().find(WebUser.class).where() |
44 |
torben |
1786 |
.eq("user", sender.getName()).findUnique(); |
45 |
|
|
if (user == null) { |
46 |
|
|
user = new WebUser(); |
47 |
|
|
user.setUser(sender.getName()); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
MessageDigest md5 = null; |
51 |
|
|
try { |
52 |
|
|
md5 = MessageDigest.getInstance("MD5"); |
53 |
|
|
} catch (NoSuchAlgorithmException e) { |
54 |
|
|
e.printStackTrace(); |
55 |
|
|
} |
56 |
|
|
md5.update(password.getBytes(), 0, password.length()); |
57 |
|
|
String pwd = new BigInteger(1,md5.digest()).toString(16); |
58 |
|
|
if (pwd.length() == 31) { |
59 |
|
|
pwd = "0" + pwd; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
user.setPassword(pwd); |
63 |
torben |
3203 |
Ebean.getDefaultServer().save(user); |
64 |
torben |
1786 |
|
65 |
|
|
sender.sendMessage("Your web password has been set"); |
66 |
|
|
|
67 |
|
|
return true; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
} |