-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hook.java
143 lines (123 loc) · 4.54 KB
/
Hook.java
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
package fi.natroutter.natlibs.handlers;
import fi.natroutter.natlibs.utilities.Colors;
import fi.natroutter.natlibs.utilities.Utilities;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
/**
* Represents a plugin hook.
*/
public class Hook {
/**
* Builder class for creating a Hook instance.
*/
public static class Builder {
private JavaPlugin instance;
private String PluginName;
private boolean isSoftDepend;
private String hookedMessage = " §a+ §7<plugin> hooked succesfully!";
private String hookingFailedMessage = " §4- §7<plugin> hooking failed!";
private String disabledMessage = "§cDisabling plugin because failed to hook <plugin>!";
private boolean disableWhenFailed;
/**
* Constructs a new Builder instance.
* @param instance the JavaPlugin instance
* @param PluginName the name of the plugin to hook
* @param isSoftDepend whether it is a soft dependency
*/
public Builder(JavaPlugin instance, String PluginName, boolean isSoftDepend) {
this.instance = instance;
this.PluginName = PluginName;
this.isSoftDepend = isSoftDepend;
}
/**
* Sets the message to display when the plugin is successfully hooked.
* @param message the hooked message
* @return the Builder instance
*/
public Builder setHookedMessage(String message) {
this.hookedMessage = message;
return this;
}
/**
* Sets the message to display when the plugin hooking fails.
* @param message the hooking failed message
* @return the Builder instance
*/
public Builder setHookingFailedMessage(String message) {
this.hookingFailedMessage = message;
return this;
}
/**
* Sets the message to display when the plugin is disabled due to hooking failure.
* @param message the disable message
* @return the Builder instance
*/
public Builder setDisableMessage(String message) {
this.disabledMessage = message;
return this;
}
/**
* Sets whether the plugin should be disabled when hooking fails.
* @param value true to disable the plugin, false otherwise
* @return the Builder instance
*/
public Builder setDisableWhenFailed(boolean value) {
this.disableWhenFailed = value;
return this;
}
/**
* Builds the Hook instance.
* @return the Hook instance
*/
public Hook build() {
return new Hook(this);
}
}
@Getter private Plugin plugin;
@Getter private boolean Hooked = false;
private final ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
private Builder args;
/**
* Constructs a new Hook instance using the provided Builder.
* @param builder the Builder instance
*/
private Hook(Builder builder) {
this.args = builder;
Component success = Colors.translate(args.hookedMessage,
Placeholder.parsed("plugin", args.PluginName)
);
Component failed = Colors.translate(args.hookingFailedMessage,
Placeholder.parsed("plugin", args.PluginName)
);
Plugin HookPL = Bukkit.getPluginManager().getPlugin(args.PluginName);
ConsoleCommandSender console = Bukkit.getConsoleSender();
if (HookPL != null && HookPL.isEnabled()) {
this.plugin = HookPL;
this.Hooked = true;
console.sendMessage(success);
} else {
console.sendMessage(failed);
if (!args.isSoftDepend) {
disablePlugin();
}
}
}
/**
* Disables the plugin if hooking fails.
*/
protected void disablePlugin() {
String plName = "UNKNOWN_PLUGIN";
if (args.instance != null) {plName = args.instance.getName();}
if (args.disableWhenFailed) {
console.sendMessage(Colors.translate("§4["+plName+"][Hook] " + args.disabledMessage,
Placeholder.parsed("plugin", plName)
));
Bukkit.getServer().getPluginManager().disablePlugin(args.instance);
}
}
}