summaryrefslogtreecommitdiffstats
path: root/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtect.java
blob: 7a7772ea6ff7e4854b35bf7ab61739357d5afa27 (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
package com.earth2me.essentials.protect;

import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.IConf;
import com.earth2me.essentials.IEssentials;
import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import com.earth2me.essentials.protect.data.IProtectedBlock;
import com.earth2me.essentials.protect.data.ProtectedBlockMemory;
import com.earth2me.essentials.protect.data.ProtectedBlockMySQL;
import com.earth2me.essentials.protect.data.ProtectedBlockSQLite;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event.Type;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;


public class EssentialsProtect extends JavaPlugin implements IConf
{
	private EssentialsProtectBlockListener blockListener = null;
	private EssentialsProtectPlayerListener playerListener = null;
	private EssentialsProtectEntityListener entityListener = null;
	private EssentialsProtectWeatherListener weatherListener = null;
	public static final String AUTHORS = Essentials.AUTHORS;
	private static final Logger logger = Logger.getLogger("Minecraft");
	public static HashMap<String, Boolean> genSettings = null;
	public static HashMap<String, String> dataSettings = null;
	public static HashMap<String, Boolean> guardSettings = null;
	public static HashMap<String, Boolean> playerSettings = null;
	public static ArrayList usageList = null;
	public static ArrayList blackListPlace = null;
	public static ArrayList breakBlackList = null;
	public static ArrayList onPlaceAlert = null;
	public static ArrayList onUseAlert = null;
	public static ArrayList onBreakAlert = null;
	private IProtectedBlock storage = null;
	IEssentials ess = null;
	private static EssentialsProtect instance = null;

	public EssentialsProtect()
	{
	}

	public void onEnable()
	{

		PluginManager pm = this.getServer().getPluginManager();
		ess = Essentials.getStatic();
		ess.getDependancyChecker().checkProtectDependancies();
		instance = this;
		reloadConfig();

		playerListener = new EssentialsProtectPlayerListener(this);
		blockListener = new EssentialsProtectBlockListener(this);
		entityListener = new EssentialsProtectEntityListener(this);
		weatherListener = new EssentialsProtectWeatherListener(this);
		pm.registerEvent(Type.PLAYER_INTERACT, playerListener, Priority.Low, this);
		pm.registerEvent(Type.BLOCK_PLACE, blockListener, Priority.Highest, this);
		pm.registerEvent(Type.BLOCK_FROMTO, blockListener, Priority.Highest, this);
		pm.registerEvent(Type.BLOCK_IGNITE, blockListener, Priority.Highest, this);
		pm.registerEvent(Type.BLOCK_BURN, blockListener, Priority.Highest, this);
		pm.registerEvent(Type.ENTITY_EXPLODE, entityListener, Priority.Highest, this);
		pm.registerEvent(Type.ENTITY_DAMAGE, entityListener, Priority.Highest, this);
		pm.registerEvent(Type.BLOCK_BREAK, blockListener, Priority.Highest, this);
		pm.registerEvent(Type.CREATURE_SPAWN, entityListener, Priority.Highest, this);
		pm.registerEvent(Type.LIGHTNING_STRIKE, weatherListener, Priority.Highest, this);
		pm.registerEvent(Type.THUNDER_CHANGE, weatherListener, Priority.Highest, this);
		pm.registerEvent(Type.WEATHER_CHANGE, weatherListener, Priority.Highest, this);

		if (!this.getDescription().getVersion().equals(Essentials.getStatic().getDescription().getVersion()))
		{
			logger.log(Level.WARNING, "Version mismatch! Please update all Essentials jars to the same version.");
		}
		logger.info("Loaded " + this.getDescription().getName() + " build " + this.getDescription().getVersion() + " maintained by " + AUTHORS);
	}

	public static boolean checkProtectionItems(ArrayList itemList, int id)
	{
		return !itemList.isEmpty() && itemList.contains(String.valueOf(id));
	}

	@Override
	public void onDisable()
	{
		genSettings.clear();
		dataSettings.clear();

		blockListener = null;
		playerListener = null;
		entityListener = null;
		genSettings = null;
		dataSettings = null;
		guardSettings = null;
		playerSettings = null;
		usageList = null;
		blackListPlace = null;
		onPlaceAlert = null;
		onUseAlert = null;
		onBreakAlert = null;
	}

	public void alert(User user, String item, String type)
	{
		Location loc = user.getLocation();
		for (Player p : this.getServer().getOnlinePlayers())
		{
			User alertUser = ess.getUser(p);
			if (alertUser.isAuthorized("essentials.protect.alerts"))
				alertUser.sendMessage(Util.format("alertFormat", user.getName(), type, item, formatCoords(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())));
		}
	}

	public static String formatCoords(int x, int y, int z)
	{
		return x + "," + y + "," + z;
	}

	public void reloadConfig()
	{
		dataSettings = ess.getSettings().getEpDBSettings();
		genSettings = ess.getSettings().getEpSettings();
		guardSettings = ess.getSettings().getEpGuardSettings();
		usageList = ess.getSettings().epBlackListUsage();
		blackListPlace = ess.getSettings().epBlackListPlacement();
		breakBlackList = ess.getSettings().epBlockBreakingBlacklist();
		onPlaceAlert = ess.getSettings().getEpAlertOnPlacement();
		onUseAlert = ess.getSettings().getEpAlertOnUse();
		onBreakAlert = ess.getSettings().getEpAlertOnBreak();
		playerSettings = ess.getSettings().getEpPlayerSettings();

		if (dataSettings.get("protect.datatype").equals("mysql"))
		{
			try
			{
				storage = new ProtectedBlockMySQL(dataSettings.get("protect.mysqlDb"), dataSettings.get("protect.username"), dataSettings.get("protect.password"));
			}
			catch (PropertyVetoException ex)
			{
				logger.log(Level.SEVERE, null, ex);
			}
		}
		else
		{
			try
			{
				storage = new ProtectedBlockSQLite("jdbc:sqlite:plugins/Essentials/EssentialsProtect.db");
			}
			catch (PropertyVetoException ex)
			{
				logger.log(Level.SEVERE, null, ex);
			}
		}
		if (genSettings.get("protect.memstore"))
		{
			storage = new ProtectedBlockMemory(storage);
		}
	}

	public static IProtectedBlock getStorage()
	{
		return EssentialsProtect.instance.storage;
	}
}