Skip to content

Commit 1e32b20

Browse files
committed
Split up PlayerInteractListener class
1 parent d060a63 commit 1e32b20

File tree

7 files changed

+413
-331
lines changed

7 files changed

+413
-331
lines changed

src/main/java/net/coreprotect/listener/player/PlayerInteractListener.java

Lines changed: 32 additions & 331 deletions
Large diffs are not rendered by default.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package net.coreprotect.listener.player;
2+
3+
import org.bukkit.Location;
4+
import org.bukkit.Material;
5+
import org.bukkit.World;
6+
import org.bukkit.block.Block;
7+
import org.bukkit.entity.Player;
8+
9+
import net.coreprotect.thread.CacheHandler;
10+
import net.coreprotect.utility.WorldUtils;
11+
12+
public final class PlayerInteractUtils {
13+
14+
private PlayerInteractUtils() {
15+
// Utility class, prevent instantiation
16+
}
17+
18+
public static void clickedDragonEgg(Player player, Block block) {
19+
Location location = block.getLocation();
20+
long time = System.currentTimeMillis();
21+
int wid = WorldUtils.getWorldId(location.getWorld().getName());
22+
int x = location.getBlockX();
23+
int y = location.getBlockY();
24+
int z = location.getBlockZ();
25+
String coordinates = x + "." + y + "." + z + "." + wid + "." + Material.DRAGON_EGG.name();
26+
CacheHandler.interactCache.put(coordinates, new Object[] { time, Material.DRAGON_EGG, player.getName() });
27+
}
28+
29+
public static void handleBisectedBlockVisualization(Player player, Block block, World world) {
30+
int x = block.getX();
31+
int y = block.getY();
32+
int z = block.getZ();
33+
34+
int worldMaxHeight = world.getMaxHeight();
35+
if (y < (worldMaxHeight - 1)) {
36+
Block y1 = world.getBlockAt(x, y + 1, z);
37+
player.sendBlockChange(y1.getLocation(), y1.getBlockData());
38+
}
39+
40+
int worldMinHeight = net.coreprotect.bukkit.BukkitAdapter.ADAPTER.getMinHeight(world);
41+
if (y > worldMinHeight) {
42+
Block y2 = world.getBlockAt(x, y - 1, z);
43+
player.sendBlockChange(y2.getLocation(), y2.getBlockData());
44+
}
45+
46+
Block x1 = world.getBlockAt(x + 1, y, z);
47+
Block x2 = world.getBlockAt(x - 1, y, z);
48+
Block z1 = world.getBlockAt(x, y, z + 1);
49+
Block z2 = world.getBlockAt(x, y, z - 1);
50+
player.sendBlockChange(x1.getLocation(), x1.getBlockData());
51+
player.sendBlockChange(x2.getLocation(), x2.getBlockData());
52+
player.sendBlockChange(z1.getLocation(), z1.getBlockData());
53+
player.sendBlockChange(z2.getLocation(), z2.getBlockData());
54+
}
55+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package net.coreprotect.listener.player.inspector;
2+
3+
import java.sql.Connection;
4+
5+
import org.bukkit.entity.Player;
6+
7+
import net.coreprotect.config.ConfigHandler;
8+
import net.coreprotect.database.Database;
9+
import net.coreprotect.language.Phrase;
10+
import net.coreprotect.utility.Color;
11+
12+
public abstract class BaseInspector {
13+
14+
protected void checkPreconditions(Player player) throws InspectionException {
15+
if (ConfigHandler.converterRunning) {
16+
throw new InspectionException(Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.UPGRADE_IN_PROGRESS));
17+
}
18+
19+
if (ConfigHandler.purgeRunning) {
20+
throw new InspectionException(Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.PURGE_IN_PROGRESS));
21+
}
22+
23+
if (ConfigHandler.lookupThrottle.get(player.getName()) != null) {
24+
Object[] lookupThrottle = ConfigHandler.lookupThrottle.get(player.getName());
25+
if ((boolean) lookupThrottle[0] || (System.currentTimeMillis() - (long) lookupThrottle[1]) < 100) {
26+
throw new InspectionException(Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
27+
}
28+
}
29+
}
30+
31+
protected Connection getDatabaseConnection(Player player) throws Exception {
32+
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { true, System.currentTimeMillis() });
33+
34+
Connection connection = Database.getConnection(true);
35+
if (connection == null) {
36+
throw new InspectionException(Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.DATABASE_BUSY));
37+
}
38+
39+
return connection;
40+
}
41+
42+
protected void finishInspection(Player player) {
43+
ConfigHandler.lookupThrottle.put(player.getName(), new Object[] { false, System.currentTimeMillis() });
44+
}
45+
46+
public static class InspectionException extends Exception {
47+
private static final long serialVersionUID = 1L;
48+
49+
public InspectionException(String message) {
50+
super(message);
51+
}
52+
}
53+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package net.coreprotect.listener.player.inspector;
2+
3+
import java.sql.Connection;
4+
import java.sql.Statement;
5+
6+
import org.bukkit.GameMode;
7+
import org.bukkit.block.BlockState;
8+
import org.bukkit.block.Sign;
9+
import org.bukkit.entity.Player;
10+
11+
import net.coreprotect.database.lookup.BlockLookup;
12+
import net.coreprotect.utility.Chat;
13+
14+
public class BlockInspector extends BaseInspector {
15+
16+
public void performBlockLookup(final Player player, final BlockState blockState) {
17+
class BasicThread implements Runnable {
18+
@Override
19+
public void run() {
20+
try {
21+
checkPreconditions(player);
22+
23+
try (Connection connection = getDatabaseConnection(player)) {
24+
Statement statement = connection.createStatement();
25+
26+
String resultData = BlockLookup.performLookup(null, statement, blockState, player, 0, 1, 7);
27+
if (resultData.contains("\n")) {
28+
for (String b : resultData.split("\n")) {
29+
Chat.sendComponent(player, b);
30+
}
31+
}
32+
else if (resultData.length() > 0) {
33+
Chat.sendComponent(player, resultData);
34+
}
35+
36+
statement.close();
37+
38+
if (blockState instanceof Sign && player.getGameMode() != GameMode.CREATIVE) {
39+
Thread.sleep(1500);
40+
Sign sign = (Sign) blockState;
41+
player.sendSignChange(sign.getLocation(), sign.getLines(), sign.getColor());
42+
}
43+
}
44+
}
45+
catch (InspectionException e) {
46+
Chat.sendMessage(player, e.getMessage());
47+
}
48+
catch (Exception e) {
49+
e.printStackTrace();
50+
}
51+
finally {
52+
finishInspection(player);
53+
}
54+
}
55+
}
56+
57+
Runnable runnable = new BasicThread();
58+
Thread thread = new Thread(runnable);
59+
thread.start();
60+
}
61+
62+
public void performAirBlockLookup(final Player player, final BlockState finalBlock) {
63+
class BasicThread implements Runnable {
64+
@Override
65+
public void run() {
66+
try {
67+
checkPreconditions(player);
68+
69+
try (Connection connection = getDatabaseConnection(player)) {
70+
Statement statement = connection.createStatement();
71+
if (finalBlock.getType().name().endsWith("AIR")) {
72+
String blockData = BlockLookup.performLookup(null, statement, finalBlock, player, 0, 1, 7);
73+
74+
if (blockData.contains("\n")) {
75+
for (String b : blockData.split("\n")) {
76+
Chat.sendComponent(player, b);
77+
}
78+
}
79+
else if (blockData.length() > 0) {
80+
Chat.sendComponent(player, blockData);
81+
}
82+
}
83+
else {
84+
String blockData = BlockLookup.performLookup(null, statement, finalBlock, player, 0, 1, 7);
85+
if (blockData.contains("\n")) {
86+
for (String splitData : blockData.split("\n")) {
87+
Chat.sendComponent(player, splitData);
88+
}
89+
}
90+
else if (blockData.length() > 0) {
91+
Chat.sendComponent(player, blockData);
92+
}
93+
}
94+
95+
statement.close();
96+
}
97+
}
98+
catch (InspectionException e) {
99+
Chat.sendMessage(player, e.getMessage());
100+
}
101+
catch (Exception e) {
102+
e.printStackTrace();
103+
}
104+
finally {
105+
finishInspection(player);
106+
}
107+
}
108+
}
109+
110+
Runnable runnable = new BasicThread();
111+
Thread thread = new Thread(runnable);
112+
thread.start();
113+
}
114+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.coreprotect.listener.player.inspector;
2+
3+
import java.sql.Connection;
4+
import java.sql.Statement;
5+
import java.util.List;
6+
7+
import org.bukkit.Location;
8+
import org.bukkit.entity.Player;
9+
10+
import net.coreprotect.database.lookup.ChestTransactionLookup;
11+
import net.coreprotect.utility.Chat;
12+
13+
public class ContainerInspector extends BaseInspector {
14+
15+
public void performContainerLookup(final Player player, final Location finalLocation) {
16+
class BasicThread implements Runnable {
17+
@Override
18+
public void run() {
19+
try {
20+
checkPreconditions(player);
21+
22+
try (Connection connection = getDatabaseConnection(player)) {
23+
Statement statement = connection.createStatement();
24+
List<String> blockData = ChestTransactionLookup.performLookup(null, statement, finalLocation, player, 1, 7, false);
25+
for (String data : blockData) {
26+
Chat.sendComponent(player, data);
27+
}
28+
29+
statement.close();
30+
}
31+
}
32+
catch (InspectionException e) {
33+
Chat.sendMessage(player, e.getMessage());
34+
}
35+
catch (Exception e) {
36+
e.printStackTrace();
37+
}
38+
finally {
39+
finishInspection(player);
40+
}
41+
}
42+
}
43+
44+
Runnable runnable = new BasicThread();
45+
Thread thread = new Thread(runnable);
46+
thread.start();
47+
}
48+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package net.coreprotect.listener.player.inspector;
2+
3+
import java.sql.Connection;
4+
import java.sql.Statement;
5+
6+
import org.bukkit.block.Block;
7+
import org.bukkit.entity.Player;
8+
9+
import net.coreprotect.database.lookup.InteractionLookup;
10+
import net.coreprotect.utility.Chat;
11+
12+
public class InteractionInspector extends BaseInspector {
13+
14+
public void performInteractionLookup(final Player player, final Block finalInteractBlock) {
15+
class BasicThread implements Runnable {
16+
@Override
17+
public void run() {
18+
try {
19+
checkPreconditions(player);
20+
21+
try (Connection connection = getDatabaseConnection(player)) {
22+
Statement statement = connection.createStatement();
23+
String blockData = InteractionLookup.performLookup(null, statement, finalInteractBlock, player, 0, 1, 7);
24+
25+
if (blockData.contains("\n")) {
26+
for (String splitData : blockData.split("\n")) {
27+
Chat.sendComponent(player, splitData);
28+
}
29+
}
30+
else {
31+
Chat.sendComponent(player, blockData);
32+
}
33+
34+
statement.close();
35+
}
36+
}
37+
catch (InspectionException e) {
38+
Chat.sendMessage(player, e.getMessage());
39+
}
40+
catch (Exception e) {
41+
e.printStackTrace();
42+
}
43+
finally {
44+
finishInspection(player);
45+
}
46+
}
47+
}
48+
49+
Runnable runnable = new BasicThread();
50+
Thread thread = new Thread(runnable);
51+
thread.start();
52+
}
53+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package net.coreprotect.listener.player.inspector;
2+
3+
import java.sql.Connection;
4+
import java.sql.Statement;
5+
import java.util.List;
6+
7+
import org.bukkit.Location;
8+
import org.bukkit.entity.Player;
9+
10+
import net.coreprotect.database.lookup.SignMessageLookup;
11+
import net.coreprotect.utility.Chat;
12+
13+
public class SignInspector extends BaseInspector {
14+
15+
public void performSignLookup(final Player player, final Location location) {
16+
class BasicThread implements Runnable {
17+
@Override
18+
public void run() {
19+
try {
20+
checkPreconditions(player);
21+
22+
try (Connection connection = getDatabaseConnection(player)) {
23+
Statement statement = connection.createStatement();
24+
List<String> signData = SignMessageLookup.performLookup(null, statement, location, player, 1, 7);
25+
for (String signMessage : signData) {
26+
String bypass = null;
27+
28+
if (signMessage.contains("\n")) {
29+
String[] split = signMessage.split("\n");
30+
signMessage = split[0];
31+
bypass = split[1];
32+
}
33+
34+
if (signMessage.length() > 0) {
35+
Chat.sendComponent(player, signMessage, bypass);
36+
}
37+
}
38+
39+
statement.close();
40+
}
41+
}
42+
catch (InspectionException e) {
43+
Chat.sendMessage(player, e.getMessage());
44+
}
45+
catch (Exception e) {
46+
e.printStackTrace();
47+
}
48+
finally {
49+
finishInspection(player);
50+
}
51+
}
52+
}
53+
54+
Runnable runnable = new BasicThread();
55+
Thread thread = new Thread(runnable);
56+
thread.start();
57+
}
58+
}

0 commit comments

Comments
 (0)