This plugin prevents players from using enderpearls (or other modes of teleportation) to trespass into other players' houses through iron doors, glass panes, fences, etc.
This will, hopefully, prevent theives from breaking into players' houses with enderpearls.
The plugin will create a .properties for the plugin if one doesn't exist. The properties file will contain one line: "message=No trespassing!" You can change this message or remove it if you don't want your players to get a message if their teleport has been aborted.
To install this plugin, copy the plugin JAR file to your server's plugins folder.
Code (Text):
package FredashaySpigotTeleportTrespassProtect;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.CommandExecutor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
public class MyPlugin extends JavaPlugin implements Listener, CommandExecutor {
private static Logger logger = null;
private static PluginDescriptionFile pdfFile = null;
private static Properties props = new Properties();
private static Player latestPlayer = null;
private static String message = "No trespassing!";
@Override
public void onEnable() {
logger = Logger.getLogger("Minecraft");
pdfFile = getDescription();
String propertiesFileName = pdfFile.getName() + ".properties";
getProperties(propertiesFileName);
getServer().getPluginManager().registerEvents(this,this);
}
private void getProperties(String fileName) {
InputStream input = null;
try {
input = new FileInputStream(fileName);
props.load(input);
}
catch (FileNotFoundException ok) {
saveProperties(fileName);
}
catch (IOException oops) {
logger.info("[" + pdfFile.getName() + "] Error reading properties file '" + fileName + "'. ");
oops.printStackTrace(System.err);
}
finally {
if (input != null) {
try {
input.close();
}
catch (IOException oops) {
oops.printStackTrace(System.err);
}
}
}
if (props.getProperty("MESSAGE") != null) {
message = props.getProperty("MESSAGE");
}
}
private void saveProperties(String fileName) {
OutputStream output = null;
try {
output = new FileOutputStream(fileName);
props.setProperty("MESSAGE", message);
props.store(output, null);
}
catch (IOException oops) {
logger.info("[" + pdfFile.getName() + "] Error writing properties file '" + fileName + "'. ");
oops.printStackTrace(System.err);
}
finally {
try {
output.close();
}
catch (IOException oops) {
oops.printStackTrace(System.err);
}
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
latestPlayer = event.getPlayer();
}
@EventHandler
public void onTeleport(PlayerTeleportEvent event) {
if (event.isCancelled() == false) {
int subX = 0;
int subY = 0;
int subZ = 0;
boolean nyet = false;
Location here = null;
Material thingy = null;
Player player = null;
if (event.getPlayer() instanceof Player) {
player = event.getPlayer();
Location location = event.getTo();
World world = getServer().getWorld(location.getWorld().getUID());
subX = location.getBlockX() - 2;
while ((subX <= location.getBlockX() + 2) && (nyet == false)) {
subY = location.getBlockY() - 2;
while ((subY <= location.getBlockY() + 2) && (nyet == false)) {
subZ = location.getBlockZ() - 2;
while ((subZ <= location.getBlockZ() + 2) && (nyet == false)) {
if (world.getBlockAt(subX, subY, subZ).getType() == Material.ACACIA_FENCE) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.BIRCH_FENCE) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.COBBLE_WALL) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.DARK_OAK_FENCE) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.FENCE) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.IRON_FENCE) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.IRON_TRAPDOOR) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.IRON_DOOR) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.IRON_DOOR_BLOCK) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.JUNGLE_FENCE) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.NETHER_FENCE) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.SPRUCE_FENCE) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.STAINED_GLASS_PANE) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType() == Material.THIN_GLASS) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType().name().toLowerCase().contains("door")) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType().name().toLowerCase().contains("fence")) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType().name().toLowerCase().contains("wall")) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType().name().toLowerCase().contains("thin")) {
nyet = true;
}
else if (world.getBlockAt(subX, subY, subZ).getType().name().toLowerCase().contains("pane")) {
nyet = true;
}
if (nyet) {
thingy = world.getBlockAt(subX, subY, subZ).getType();
here = location;
here.setX(subX);
here.setY(subY);
here.setZ(subZ);
}
subZ = subZ + 1;
}
subY = subY + 1;
}
subX = subX + 1;
}
if (nyet) {
if (player == latestPlayer) {
latestPlayer = null;
}
else {
ItemStack pearl = new ItemStack(Material.ENDER_PEARL);
world.dropItemNaturally(here, pearl);
player.sendMessage("<SERVER> " + message);
}
logger.info("[" + pdfFile.getName() + "] aborted player '" + player.getName() + "' from teleporting through a " + thingy.name() + " in " + location.getWorld().getName() + " at location (" + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ() + "). ");
event.setCancelled(true);
}
}
}
}
// private void doNothing() { }
}