Skip to content

Commit

Permalink
Initial version.
Browse files Browse the repository at this point in the history
  • Loading branch information
totemo committed Apr 21, 2016
1 parent 69456db commit a43c722
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# Eclipse
.classpath
.project
.settings
classes
bin

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# SafeCrystals
SafeCrystals
------------
Prevent Ender Crystals from exploding and breaking blocks or damaging entities.

Players who can build in a region can break the crystals causing them to drop
as an item. That event is recorded in the server log.
14 changes: 14 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: ${project.name}
version: ${project.version}
author: totemo
authors: [totemo]
description: ${project.description}
website: ${project.url}
main: nu.nerd.safecrystals.SafeCrystals

depend: [WorldGuard]

permissions: {}

commands: {}

79 changes: 79 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>nu.nerd</groupId>
<name>SafeCrystals</name>
<artifactId>${project.name}</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>
<description>Prevent Ender Crystals from exploding and breaking blocks or damaging entities.</description>
<url>https://github.com/totemo/${project.name}</url>
<scm>
<connection>scm:git:git://github.com/NerdNu/${project.name}.git</connection>
<url>https://github.com/NerdNu/${project.name}</url>
<developerConnection>scm:git:git://github.com/NerdNu/${project.name}.git</developerConnection>
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository>
<repository>
<id>sk89q-repo</id>
<url>http://maven.sk89q.com/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.9-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sk89q</groupId>
<artifactId>worldguard</artifactId>
<version>6.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<defaultGoal>clean package</defaultGoal>
<sourceDirectory>${basedir}/src</sourceDirectory>
<resources>
<resource>
<targetPath>.</targetPath>
<filtering>true</filtering>
<directory>${basedir}</directory>
<includes>
<include>plugin.yml</include>
</includes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.1</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<finalName>${project.artifactId}-${project.version}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
125 changes: 125 additions & 0 deletions src/nu/nerd/safecrystals/SafeCrystals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package nu.nerd.safecrystals;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import com.sk89q.worldguard.bukkit.WorldGuardPlugin;

// ----------------------------------------------------------------------------
/**
* Prevent Ender Crystals from exploding and breaking blocks or damaging
* entities.
*
* Players who have permission to build in a WorldGuard region can break the
* crystals into dropped items.
*
* WorldGuard build permission is checked before allowing a player to place an
* Ender Crystal in a region.
*/
public class SafeCrystals extends JavaPlugin implements Listener {

// ------------------------------------------------------------------------
/**
* @see org.bukkit.plugin.java.JavaPlugin#onEnable()
*/
@Override
public void onEnable() {
_worldGuard = (WorldGuardPlugin) getServer().getPluginManager().getPlugin("WorldGuard");
Bukkit.getPluginManager().registerEvents(this, this);
}

// ------------------------------------------------------------------------
/**
* Prevent Ender Crystals from exploding.
*
* Remove and drop as item when appropriate.
*/
@EventHandler(ignoreCancelled = true)
public void onEntityExplode(EntityExplodeEvent event) {
Entity entity = event.getEntity();
if (entity.getType() == EntityType.ENDER_CRYSTAL) {
event.setCancelled(true);
}
}

// ------------------------------------------------------------------------
/**
* Prevent Ender Crystals from being damaged by other entities.
*
* If the damager is a player who can build, drop the crystal as an item.
* Projectiles are handled the same as the player who shot them.
*/
@EventHandler()
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
Entity entity = event.getEntity();
if (entity.getType() == EntityType.ENDER_CRYSTAL) {
event.setCancelled(true);

if (event.getDamager() instanceof Player) {
tryBreakEnderCrystal(entity, (Player) event.getDamager());
} else if (event.getDamager() instanceof Projectile) {
Projectile projectile = (Projectile) event.getDamager();
if (projectile.getShooter() instanceof Player) {
tryBreakEnderCrystal(entity, (Player) projectile.getShooter());
}
}
}
}

// ------------------------------------------------------------------------
/**
* Check that a player can build before placing an Ender Crystal.
*
* Minecraft will place an Ender Crystal on top of obsidian or bedrock even
* when the player clicks the sides or underside of the block. Therefore, we
* always check build permissions <i>above</i> the clicked block.
*/
@EventHandler()
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getMaterial() == Material.END_CRYSTAL && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
Block destination = event.getClickedBlock().getRelative(BlockFace.UP);
if (!_worldGuard.canBuild(event.getPlayer(), destination.getLocation())) {
event.setCancelled(true);
}
}
}

// ------------------------------------------------------------------------
/**
* Handle a specific player's attempt to can break an Ender Crystal.
*
* @param crystal the crystal.
* @param player the player.
*/
protected void tryBreakEnderCrystal(Entity crystal, Player player) {
Location loc = crystal.getLocation();
if (_worldGuard.canBuild(player, loc)) {
crystal.remove();
loc.getWorld().dropItemNaturally(loc, new ItemStack(Material.END_CRYSTAL));
getLogger().info(player.getName() + " broke an Ender Crystal at " +
loc.getWorld().getName() + ", " +
loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ());
}
}

// ------------------------------------------------------------------------
/**
* Reference to WorldGuard.
*/
protected WorldGuardPlugin _worldGuard;
} // class SafeCrystals

0 comments on commit a43c722

Please sign in to comment.