summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/register/payment/Methods.java
blob: 0ebbcfb84f24f13155103808f6bddbeda90ef622 (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
package com.earth2me.essentials.register.payment;

import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;

import java.util.HashSet;
import java.util.Set;

/**
 * Methods.java
 * Controls the getting / setting of methods & the method of payment used.
 *
 * @author: Nijikokun<nijikokun@gmail.com> (@nijikokun)
 * @copyright: Copyright (C) 2011
 * @license: GNUv3 Affero License <http://www.gnu.org/licenses/agpl-3.0.html>
 */
public class Methods {
    private boolean self = false;
    private Method Method = null;
    private String preferred = "";
    private Set<Method> Methods = new HashSet<Method>();
    private Set<String> Dependencies = new HashSet<String>();
    private Set<Method> Attachables = new HashSet<Method>();

    public Methods() {
        this._init();
    }

    /**
     * Allows you to set which economy plugin is most preferred.
     * 
     * @param preferred - preferred economy plugin
     */
    public Methods(String preferred) {
        this._init();

        if(this.Dependencies.contains(preferred)) {
            this.preferred = preferred;
        }
    }

    private void _init() {
        this.addMethod("iConomy", new com.earth2me.essentials.register.payment.methods.iCo4());
        this.addMethod("iConomy", new com.earth2me.essentials.register.payment.methods.iCo5());
        this.addMethod("BOSEconomy", new com.earth2me.essentials.register.payment.methods.BOSE6());
        this.addMethod("BOSEconomy", new com.earth2me.essentials.register.payment.methods.BOSE7());
    }

    public Set<String> getDependencies() {
        return Dependencies;
    }

    public Method createMethod(Plugin plugin) {
        for (Method method: Methods) {
            if (method.isCompatible(plugin)) {
                method.setPlugin(plugin);
                return method;
            }
        }

        return null;
    }

    private void addMethod(String name, Method method) {
        Dependencies.add(name);
        Methods.add(method);
    }

    public boolean hasMethod() {
        return (Method != null);
    }

    public boolean setMethod(Plugin method) {
        if(hasMethod()) return true;
        if(self) { self = false; return false; }

        int count = 0;
        boolean match = false;
        Plugin plugin;
        PluginManager manager = method.getServer().getPluginManager();

        for(String name: this.getDependencies()) {
            if(hasMethod()) break;
            if(method.getDescription().getName().equals(name)) plugin = method; else  plugin = manager.getPlugin(name);
            if(plugin == null) continue;

            Method current = this.createMethod(plugin);
            if(current == null) continue;

            if(this.preferred.isEmpty())
                this.Method = current;
            else {
                this.Attachables.add(current);
            }
        }

        if(!this.preferred.isEmpty()) {
            do {
                if(hasMethod()) {
                    match = true;
                } else {
                    for(Method attached: this.Attachables) {
                        if(attached == null) continue;

                        if(hasMethod()) {
                            match = true;
                            break;
                        }

                        if(this.preferred.isEmpty()) this.Method = attached;

                        if(count == 0) {
                            if(this.preferred.equalsIgnoreCase(attached.getName()))
                                this.Method = attached;
                        } else {
                            this.Method = attached;
                        }
                    }

                    count++;
                }
            } while(!match);
        }

        return hasMethod();
    }

    public Method getMethod() {
        return Method;
    }

    public boolean checkDisabled(Plugin method) {
        if(!hasMethod()) return true;
        if (Method.isCompatible(method)) Method = null;
        return (Method == null);
    }
}