-
Notifications
You must be signed in to change notification settings - Fork 17
/
LWCMCPCSupport.java
161 lines (132 loc) · 4.52 KB
/
LWCMCPCSupport.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
package com.griefcraft.listeners;
import com.griefcraft.lwc.LWC;
import com.griefcraft.model.LWCPlayer;
import com.griefcraft.model.Protection;
import com.griefcraft.scripting.JavaModule;
import com.griefcraft.scripting.event.LWCProtectionDestroyEvent;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class LWCMCPCSupport extends JavaModule implements Listener {
/**
* The shared multiplayer block id used by transport pipes
*/
public static final int TRANSPORT_PIPE_ID = 166;
/**
* The LWC object
*/
private LWC lwc;
/**
* A set of blacklisted blocks
*/
private final Set<Integer> blacklistedBlocks = new HashSet<Integer>();
/**
* A set of blacklisted players that are blocked from destroying any blocks protected by LWC. Mainly useful for MCPC
* where mods can remove blocks and try to break the block by sending an event first (e.g turtle)
*/
private final Set<String> blacklistedPlayers = new HashSet<String>();
public LWCMCPCSupport(LWC lwc) {
this.lwc = lwc;
loadAndProcessConfig();
registerEvents();
}
/**
* Register the MCPC events
*/
private void registerEvents() {
Bukkit.getServer().getPluginManager().registerEvents(this, lwc.getPlugin());
}
/**
* Load and process the configuration
*/
public void loadAndProcessConfig() {
blacklistedBlocks.clear();
blacklistedPlayers.clear();
for (String player : lwc.getConfiguration().getStringList("optional.blacklistedPlayers", new ArrayList<String>())) {
blacklistedPlayers.add(player.toLowerCase());
}
List<String> ids = lwc.getConfiguration().getStringList("optional.blacklistedBlocks", new ArrayList<String>());
for (String sId : ids) {
String[] idParts = sId.trim().split(":");
int id = Integer.parseInt(idParts[0].trim());
int data = 0;
if (idParts.length > 1) {
data = Integer.parseInt(idParts[1].trim());
}
blacklistedBlocks.add(hashCode(id, data));
}
}
/**
* Check if MCPC is installed to the server (which Tekkit uses)
*
* @return
*/
public boolean mcpcInstalled() {
// quickest and easiest check
if (Bukkit.getBukkitVersion().contains("MCPC")) {
return true;
}
// attempt to detect Forge, tekkit definitely contains forge, most other MCPC mods are going to have it ?
try {
Class.forName("forge.MinecraftForge");
return true;
} catch (Exception e) { } // ignore failure
return false; // no mcpc
}
@EventHandler(ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
if (!LWC.ENABLED) {
return;
}
Player player = event.getPlayer();
Block block = event.getBlock();
// check if the block is blacklisted
boolean blockIsBlacklisted = blacklistedBlocks.contains(hashCode(block.getTypeId(), block.getData()));
if (blockIsBlacklisted) {
// it's blacklisted, check for a protected chest
Protection protection = lwc.findAdjacentProtectionOnAllSides(block);
if (protection != null) {
if (!lwc.canAccessProtection(player, protection)) {
// they can't access the protection ..
event.setCancelled(true);
}
}
}
}
/**
* Called when a protection is destroyed
*
* @param event
*/
public void onDestroyProtection(LWCProtectionDestroyEvent event) {
if (event.isCancelled()) {
return;
}
Player bPlayer = event.getPlayer();
LWCPlayer player = lwc.wrapPlayer(bPlayer);
String lowerPlayerName = player.getName().toLowerCase();
if (blacklistedPlayers.contains(lowerPlayerName)) {
event.setCancelled(true);
player.sendLocale("protection.accessdenied");
}
}
/**
* Get the hashcode of two integers
*
* @param int1
* @param int2
* @return
*/
private int hashCode(int int1, int int2) {
int hash = int1 * 17;
hash *= 37 + int2;
return hash;
}
}