Skip to content

Commit

Permalink
Make dragon fight crystals explodey again.
Browse files Browse the repository at this point in the history
  • Loading branch information
totemo committed Jan 5, 2019
1 parent 1fdce97 commit 83072a4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>nu.nerd</groupId>
<name>SafeCrystals</name>
<artifactId>${project.name}</artifactId>
<version>1.0.2</version>
<version>1.0.3</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>
Expand Down
39 changes: 31 additions & 8 deletions src/nu/nerd/safecrystals/SafeCrystals.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package nu.nerd.safecrystals;

import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
Expand All @@ -22,6 +19,11 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;

// ----------------------------------------------------------------------------
/**
* Prevent Ender Crystals from exploding and breaking blocks or damaging
Expand Down Expand Up @@ -60,14 +62,13 @@ public void onEnable() {

// ------------------------------------------------------------------------
/**
* Prevent Ender Crystals from exploding.
*
* Remove and drop as item when appropriate.
* Prevent Ender Crystals from exploding, except in the case of those on
* bedrock in the end.
*/
@EventHandler(ignoreCancelled = true)
public void onEntityExplode(EntityExplodeEvent event) {
Entity entity = event.getEntity();
if (entity.getType() == EntityType.ENDER_CRYSTAL) {
if (entity.getType() == EntityType.ENDER_CRYSTAL && !isDragonFightCrystal(entity.getLocation())) {
event.setCancelled(true);
}
}
Expand All @@ -83,6 +84,11 @@ public void onEntityExplode(EntityExplodeEvent event) {
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
Entity entity = event.getEntity();
if (entity.getType() == EntityType.ENDER_CRYSTAL) {
if (isDragonFightCrystal(entity.getLocation())) {
// Vanilla handlilng.
return;
}

event.setCancelled(true);

if (event.getDamager() instanceof Player) {
Expand Down Expand Up @@ -178,6 +184,23 @@ private boolean canBuild(Player player, Location location) {
return WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery().testBuild(wrappedLocation, localPlayer);
}

// ------------------------------------------------------------------------
/**
* Return true if the crystal is associated with the dragon fight.
*
* For this purpose, any crystal on bedrock in the end is assumed to be part
* of the dragon fight. These crystals are not protected (they will behave
* as in vanilla).
*
* @param loc the location of the end crystal.
* @return true if the crystal is associated with the dragon fight.
*/
private static boolean isDragonFightCrystal(Location loc) {
Block blockUnder = loc.getBlock().getRelative(0, -1, 0);
return loc.getWorld().getEnvironment() == Environment.THE_END &&
blockUnder != null && blockUnder.getType() == Material.BEDROCK;
}

// ------------------------------------------------------------------------
/**
* Reference to WorldGuard.
Expand Down

0 comments on commit 83072a4

Please sign in to comment.