-
Notifications
You must be signed in to change notification settings - Fork 16
/
SignInteractListener.java
199 lines (178 loc) · 7.63 KB
/
SignInteractListener.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
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
package io.github.niestrat99.advancedteleport.listeners;
import io.github.niestrat99.advancedteleport.api.ATPlayer;
import io.github.niestrat99.advancedteleport.api.Warp;
import io.github.niestrat99.advancedteleport.commands.home.HomeCommand;
import io.github.niestrat99.advancedteleport.commands.spawn.SpawnCommand;
import io.github.niestrat99.advancedteleport.commands.teleport.Tpr;
import io.github.niestrat99.advancedteleport.commands.warp.WarpCommand;
import io.github.niestrat99.advancedteleport.commands.warp.WarpsCommand;
import io.github.niestrat99.advancedteleport.config.CustomMessages;
import io.github.niestrat99.advancedteleport.config.NewConfig;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import java.util.HashMap;
public class SignInteractListener implements Listener {
private final HashMap<String, ATSign> signRegistry;
public SignInteractListener() {
signRegistry = new HashMap<>();
signRegistry.put("warps", new ATSign("Warps", NewConfig.get().USE_WARPS.get()) {
@Override
public void onInteract(Sign sign, Player player) {
WarpsCommand.sendWarps(player);
}
@Override
public boolean canCreate(Sign sign, Player player) {
return true;
}
});
signRegistry.put("warp", new ATSign("Warp", NewConfig.get().USE_WARPS.get()) {
@Override
public void onInteract(Sign sign, Player player) {
if (!Warp.getWarps().containsKey(sign.getLine(1))) return;
WarpCommand.warp(Warp.getWarps().get(sign.getLine(1)), player, true);
}
@Override
public boolean canCreate(Sign sign, Player player) {
if (sign.getLine(1).isEmpty()) {
CustomMessages.sendMessage(player, "Error.noWarpInput");
return false;
} else {
if (Warp.getWarps().containsKey(sign.getLine(1))){
String warpName = sign.getLine(1);
sign.setLine(0, ChatColor.BLUE + "" + ChatColor.BOLD + "[Warp]");
sign.setLine(1, warpName);
CustomMessages.sendMessage(player, "Info.createdWarpSign");
return true;
} else {
CustomMessages.sendMessage(player, "Error.noSuchWarp");
return false;
}
}
}
});
signRegistry.put("home", new ATSign("Home", NewConfig.get().USE_HOMES.get()) {
@Override
public void onInteract(Sign sign, Player player) {
}
@Override
public boolean canCreate(Sign sign, Player player) {
return false;
}
});
signRegistry.put("homes", new ATSign("Homes", NewConfig.get().USE_HOMES.get()) {
@Override
public void onInteract(Sign sign, Player player) {
}
@Override
public boolean canCreate(Sign sign, Player player) {
return false;
}
});
signRegistry.put("bed", new ATSign("Bed", NewConfig.get().USE_HOMES.get()) {
@Override
public void onInteract(Sign sign, Player player) {
ATPlayer atPlayer = ATPlayer.getPlayer(player);
if (atPlayer.getBedSpawn() == null) return;
HomeCommand.teleport(player, atPlayer.getBedSpawn());
}
@Override
public boolean canCreate(Sign sign, Player player) {
return true;
}
});
signRegistry.put("spawn", new ATSign("Spawn", NewConfig.get().USE_SPAWN.get()) {
@Override
public void onInteract(Sign sign, Player player) {
String world = player.getWorld().getName();
boolean specific = false;
if (!sign.getLine(1).isEmpty()) {
world = sign.getLine(1);
specific = true;
}
SpawnCommand.spawn(player, world, specific);
}
@Override
public boolean canCreate(Sign sign, Player player) {
return true;
}
});
signRegistry.put("randomtp", new ATSign("RandomTP", NewConfig.get().USE_RANDOMTP.get()) {
@Override
public void onInteract(Sign sign, Player player) {
if (!sign.getLine(1).isEmpty()) {
World otherWorld = Bukkit.getWorld(sign.getLine(1));
if (otherWorld != null) {
Tpr.randomTeleport(player, otherWorld);
} else {
CustomMessages.sendMessage(player, "Error.noSuchWorld");
}
} else {
Tpr.randomTeleport(player, player.getWorld());
}
}
@Override
public boolean canCreate(Sign sign, Player player) {
if (!sign.getLine(1).isEmpty()) {
World otherWorld = Bukkit.getWorld(sign.getLine(1));
if (otherWorld == null) {
CustomMessages.sendMessage(player, "Error.noSuchWorld");
return false;
}
}
return true;
}
});
}
@EventHandler
public void onSignInteract(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
Player player = event.getPlayer();
Block clickedBlock = event.getClickedBlock();
BlockState blockState = clickedBlock.getState();
if (!(blockState instanceof Sign)) return;
Sign sign = (Sign) blockState;
String command = ChatColor.stripColor(sign.getLine(0));
if (command.length() > 2) {
command = command.substring(1, command.length() - 1).toLowerCase();
}
if (!signRegistry.containsKey(command)) return;
ATSign atSign = signRegistry.get(command);
if (!atSign.isEnabled()) return;
if (!player.hasPermission(atSign.getRequiredPermission())) return;
atSign.onInteract(sign, player);
}
@EventHandler
public void onSignPlace(SignChangeEvent event){
Block placeBlock = event.getBlock();
BlockState state = placeBlock.getState();
Player player = event.getPlayer();
// Make sure it's a sign
if (!(state instanceof Sign)) return;
Sign sign = (Sign) state;
String command = ChatColor.stripColor(event.getLine(0));
if (command.length() > 2) {
command = command.substring(1, command.length() - 1).toLowerCase();
}
if (!signRegistry.containsKey(command)) return;
ATSign atSign = signRegistry.get(command);
if (!atSign.isEnabled()) return;
if (player.hasPermission(atSign.getAdminPermission())) {
sign.setLine(1, event.getLine(1));
if (!atSign.canCreate(sign, player)) return;
event.setLine(0, ChatColor.BLUE + "" + ChatColor.BOLD + "[" + atSign.getName() + "]");
} else {
CustomMessages.sendMessage(player, "Error.noPermissionSign");
event.setCancelled(true);
}
}
}