Skip to content

Commit

Permalink
Add entity protection rules; resolves #301
Browse files Browse the repository at this point in the history
  • Loading branch information
Sataniel98 committed Apr 25, 2018
1 parent 11ac5d5 commit ac78851
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Maven automatically fetches all dependencies and builds DungeonsXL; just run _bu
[Caliburn](https://github.com/DRE2N/CaliburnAPI) is an API to read custom items and mobs from config files. DungeonsXL contains Caliburn Beta 0.3.

### Java
Make sure that your server uses Java 7 or higher.
Make sure that your server uses Java 8 or higher.

### UUIDs
Supported.
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.github.dre2n.dungeonsxl.requirement.Requirement;
import io.github.dre2n.dungeonsxl.reward.Reward;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -27,6 +28,7 @@
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;

/**
Expand All @@ -51,6 +53,15 @@ public class GameRuleProvider {
DEFAULT_VALUES.breakBlocks = false;
DEFAULT_VALUES.breakPlacedBlocks = false;
DEFAULT_VALUES.breakWhitelist = null;
DEFAULT_VALUES.damageProtectedEntities = new HashSet<>(Arrays.asList(
EntityType.ARMOR_STAND,
EntityType.ITEM_FRAME,
EntityType.PAINTING
));
DEFAULT_VALUES.interactionProtectedEntities = new HashSet<>(Arrays.asList(
EntityType.ARMOR_STAND,
EntityType.ITEM_FRAME
));
DEFAULT_VALUES.placeBlocks = false;
DEFAULT_VALUES.placeWhitelist = null;
DEFAULT_VALUES.rain = null;
Expand Down Expand Up @@ -107,6 +118,8 @@ public class GameRuleProvider {
protected Boolean breakBlocks;
protected Boolean breakPlacedBlocks;
protected Map<Material, HashSet<Material>> breakWhitelist;
protected Set<EntityType> damageProtectedEntities;
protected Set<EntityType> interactionProtectedEntities;
protected Boolean placeBlocks;
protected Set<Material> placeWhitelist;
protected Boolean rain;
Expand Down Expand Up @@ -220,6 +233,20 @@ public Map<Material, HashSet<Material>> getBreakWhitelist() {
return breakWhitelist;
}

/**
* @return a Set of all entity types that cannot be damaged
*/
public Set<EntityType> getDamageProtectedEntities() {
return damageProtectedEntities;
}

/**
* @return a Set of all entity types that cannot be damaged
*/
public Set<EntityType> getInteractionProtectedEntities() {
return interactionProtectedEntities;
}

/**
* @return if blocks may be placed
*/
Expand Down Expand Up @@ -691,6 +718,26 @@ public void apply(GameRuleProvider defaultValues) {
breakWhitelist = defaultValues.breakWhitelist;
}

if (damageProtectedEntities == null) {
// If nothing is specialized for protected entites yet (=> damageProtectedEntites == null and DEFAULT_VALUES are used)
// and if blocks may be broken, it makes no sense to assume the user wants to have paintings etc. protected.
if (defaultValues == DEFAULT_VALUES && breakBlocks) {
damageProtectedEntities = new HashSet<>();
} else {
damageProtectedEntities = defaultValues.damageProtectedEntities;
}
}

if (interactionProtectedEntities == null) {
// If nothing is specialized for protected entites yet (=> interactionProtectedEntites == null and DEFAULT_VALUES are used)
// and if blocks may be broken, it makes no sense to assume the user wants to have paintings etc. protected.
if (defaultValues == DEFAULT_VALUES && breakBlocks) {
interactionProtectedEntities = new HashSet<>();
} else {
interactionProtectedEntities = defaultValues.interactionProtectedEntities;
}
}

if (placeBlocks == null) {
placeBlocks = defaultValues.placeBlocks;
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/github/dre2n/dungeonsxl/world/DGameWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.bukkit.block.Sign;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Hanging;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Spider;
Expand Down Expand Up @@ -580,6 +581,20 @@ public boolean onBreak(BlockBreakEvent event) {
return true;
}

// Cancel if a protected entity is attached
for (Entity entity : world.getNearbyEntities(block.getLocation(), 2, 2, 2)) {
if (!(entity instanceof Hanging)) {
continue;
}
if (entity.getLocation().getBlock().getRelative(((Hanging) entity).getAttachedFace()).equals(block)) {
Hanging hanging = (Hanging) entity;
if (rules.getDamageProtectedEntities().contains(hanging.getType())) {
event.setCancelled(true);
break;
}
}
}

Map<Material, HashSet<Material>> whitelist = rules.getBreakWhitelist();
Material material = block.getType();
Material breakTool = player.getItemInHand().getType();
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/io/github/dre2n/dungeonsxl/world/DWorldListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockSpreadEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.weather.WeatherChangeEvent;
import org.bukkit.event.world.ChunkUnloadEvent;

Expand Down Expand Up @@ -124,9 +125,25 @@ public void onEntityExplode(EntityExplodeEvent event) {
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onHangingBreakByEntity(HangingBreakByEntityEvent event) {
public void onEntityDamage(EntityDamageEvent event) {
DGameWorld gameWorld = DGameWorld.getByWorld(event.getEntity().getWorld());
if (gameWorld != null) {
if (gameWorld == null) {
return;
}
Game game = Game.getByGameWorld(gameWorld);
if (game.getRules().getDamageProtectedEntities().contains(event.getEntityType())) {
event.setCancelled(true);
}
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
DGameWorld gameWorld = DGameWorld.getByWorld(event.getPlayer().getWorld());
if (gameWorld == null) {
return;
}
Game game = Game.getByGameWorld(gameWorld);
if (game.getRules().getInteractionProtectedEntities().contains(event.getRightClicked().getType())) {
event.setCancelled(true);
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/github/dre2n/dungeonsxl/world/WorldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;

/**
Expand Down Expand Up @@ -171,6 +172,16 @@ public void load(ConfigurationSection configFile) {
}
}

if (configFile.contains("damageProtectedEntities")) {
damageProtectedEntities = new HashSet<>();
configFile.getStringList("damageProtectedEntities").forEach(e -> damageProtectedEntities.add(EnumUtil.getEnumIgnoreCase(EntityType.class, e)));
}

if (configFile.contains("interactionProtectedEntities")) {
interactionProtectedEntities = new HashSet<>();
configFile.getStringList("interactionProtectedEntities").forEach(e -> interactionProtectedEntities.add(EnumUtil.getEnumIgnoreCase(EntityType.class, e)));
}

if (configFile.contains("placeBlocks")) {
placeBlocks = configFile.getBoolean("placeBlocks");
}
Expand Down

0 comments on commit ac78851

Please sign in to comment.