summaryrefslogtreecommitdiffstats
path: root/EssentialsGroupManager/src/org/anjocaido/groupmanager/dataholder/OverloadedWorldHolder.java
blob: 8e2df5d105e5f5c4846cfe22922da0e31236d840 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.anjocaido.groupmanager.dataholder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.anjocaido.groupmanager.data.User;

/**
 *
 * @author gabrielcouto
 */
public class OverloadedWorldHolder extends WorldDataHolder {

    /**
     *
     */
    protected Map<String, User> overloadedUsers = new HashMap<String, User>();

    /**
     *
     * @param ph
     */
    @SuppressWarnings("deprecation")
    public OverloadedWorldHolder(WorldDataHolder ph) {
        super(ph.getName());
        this.f = ph.f;
        this.groupsFile = ph.groupsFile;
        this.usersFile = ph.usersFile;
        this.defaultGroup = ph.defaultGroup;
        this.groups = ph.groups;
        this.users = ph.users;
    }

    /**
     *
     * @param userName
     * @return user object or a new user if none exists.
     */
    @Override
    public User getUser(String userName) {
        //OVERLOADED CODE
    	String userNameLowered = userName.toLowerCase();
    	if (overloadedUsers.containsKey(userNameLowered)) {
    		return overloadedUsers.get(userNameLowered);
        }
        //END CODE
        if (users.containsKey(userNameLowered)) {
            return users.get(userNameLowered);
        }
        User newUser = createUser(userName);
        haveUsersChanged = true;
        return newUser;
    }

    /**
     *
     * @param theUser
     */
    @Override
    public void addUser(User theUser) {
        if (theUser.getDataSource() != this) {
            theUser = theUser.clone(this);
        }
        if (theUser == null) {
            return;
        }
        if ((theUser.getGroup() == null) || (!groups.containsKey(theUser.getGroupName().toLowerCase()))) {
            theUser.setGroup(defaultGroup);
        }
        //OVERLOADED CODE
        if (overloadedUsers.containsKey(theUser.getName().toLowerCase())) {
            overloadedUsers.remove(theUser.getName().toLowerCase());
            overloadedUsers.put(theUser.getName().toLowerCase(), theUser);
            return;
        }
        //END CODE
        removeUser(theUser.getName());
        users.put(theUser.getName().toLowerCase(), theUser);
        haveUsersChanged = true;
    }

    /**
     *
     * @param userName
     * @return true if removed/false if not found.
     */
    @Override
    public boolean removeUser(String userName) {
        //OVERLOADED CODE
        if (overloadedUsers.containsKey(userName.toLowerCase())) {
            overloadedUsers.remove(userName.toLowerCase());
            return true;
        }
        //END CODE
        if (users.containsKey(userName.toLowerCase())) {
            users.remove(userName.toLowerCase());
            haveUsersChanged = true;
            return true;
        }
        return false;
    }

    @Override
    public boolean removeGroup(String groupName) {
        if (groupName.equals(defaultGroup)) {
            return false;
        }
        for (String key : groups.keySet()) {
            if (groupName.equalsIgnoreCase(key)) {
                groups.remove(key);
                for (String userKey : users.keySet()) {
                    User user = users.get(userKey);
                    if (user.getGroupName().equalsIgnoreCase(key)) {
                        user.setGroup(defaultGroup);
                    }

                }
                //OVERLOADED CODE
                for (String userKey : overloadedUsers.keySet()) {
                    User user = overloadedUsers.get(userKey);
                    if (user.getGroupName().equalsIgnoreCase(key)) {
                        user.setGroup(defaultGroup);
                    }

                }
                //END OVERLOAD
                haveGroupsChanged = true;
                return true;
            }
        }
        return false;
    }

    /**
     *
     * @return Collection of all users
     */
    @Override
    public Collection<User> getUserList() {
        Collection<User> overloadedList = new ArrayList<User>();
        Collection<User> normalList = users.values();
        for (User u : normalList) {
            if (overloadedUsers.containsKey(u.getName().toLowerCase())) {
                overloadedList.add(overloadedUsers.get(u.getName().toLowerCase()));
            } else {
                overloadedList.add(u);
            }
        }
        return overloadedList;
    }

    /**
     *
     * @param userName
     * @return true if user is overloaded.
     */
    public boolean isOverloaded(String userName) {
        return overloadedUsers.containsKey(userName.toLowerCase());
    }

    /**
     *
     * @param userName
     */
    public void overloadUser(String userName) {
        if (!isOverloaded(userName)) {
            User theUser = getUser(userName);
            theUser = theUser.clone();
            if (overloadedUsers.containsKey(theUser.getName().toLowerCase())) {
                overloadedUsers.remove(theUser.getName().toLowerCase());
            }
            overloadedUsers.put(theUser.getName().toLowerCase(), theUser);
        }
    }

    /**
     *
     * @param userName
     */
    public void removeOverload(String userName) {
        overloadedUsers.remove(userName.toLowerCase());
    }

    /**
     * Gets the user in normal state. Surpassing the overload state.
     * It doesn't affect permissions. But it enables plugins change the
     * actual user permissions even in overload mode.
     * 
     * @param userName
     * @return user object
     */
    public User surpassOverload(String userName) {
        if (!isOverloaded(userName)) {
            return getUser(userName);
        }
        if (users.containsKey(userName.toLowerCase())) {
            return users.get(userName.toLowerCase());
        }
        User newUser = createUser(userName);
        return newUser;
    }
}