summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/bungeeBans/listeners/BanJoinListener.java
blob: 4ef297b8a1c40f28e32bc7d49944edc52b347244 (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
package com.redstoner.bungeeBans.listeners;

import com.mojang.api.profiles.Profile;
import com.redstoner.bungeeBans.BanManager;
import com.redstoner.bungeeBans.Util;
import com.redstoner.bungeeBans.json.Ban;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.connection.PendingConnection;
import net.md_5.bungee.api.event.PreLoginEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.event.EventPriority;

public class BanJoinListener<T extends Ban> implements Listener {
	private String        name;
	private BanManager<T> bm;

	public BanJoinListener(String name, BanManager<T> bm) {
		this.name = name;
		this.bm = bm;
	}

	@EventHandler (priority = EventPriority.HIGHEST)
	public void onJoin(PreLoginEvent event) {
		event.setCancelled(true);

		T ban;

		PendingConnection conn = event.getConnection();

		switch (this.name) {
			case "player":
				String name = conn.getName();

				Profile[] profiles = Util.findProfilesByNames(name);

				if (profiles.length != 1) {
					event.setCancelReason(
							new ComponentBuilder(ChatColor.RED + "Server error occured while joining: ")
									.append(ChatColor.AQUA + "The mojang API does not know your UUID!")
									.create()
					);

					return;
				}

				ban = bm.getBan(Util.dashUUID(profiles[0].getId()));
				break;
			case "IP":
				String address = conn.getAddress().getAddress().getHostAddress();
				System.out.println("IP connection: " + address);
				ban = bm.getBan(address);
				break;
			default:
				throw new UnsupportedOperationException();
		}

		if (ban != null) {
			event.setCancelReason(
					new ComponentBuilder(ChatColor.RED + "You were " + this.name + " banned by ")
							.append(ChatColor.AQUA + ban.getSource())
							.append(ChatColor.RED + " for ")
							.append(ChatColor.AQUA + ban.getReason())
							.append(ChatColor.RED + " on ")
							.append(ChatColor.AQUA + ban.getCreated())
							.append(ChatColor.RED + " until ")
							.append(ChatColor.AQUA + ban.getExpires())
							.create()
			);
		} else {
			event.setCancelled(false);
		}
	}
}