mirror of
https://github.com/DRE2N/DungeonsXL.git
synced 2025-01-24 09:01:52 +01:00
Add entity protection rules; resolves #301
This commit is contained in:
parent
11ac5d5071
commit
ac788515db
@ -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.
|
||||
|
4
pom.xml
4
pom.xml
@ -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>
|
||||
|
@ -19,6 +19,7 @@ package io.github.dre2n.dungeonsxl.game;
|
||||
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;
|
||||
@ -27,6 +28,7 @@ import java.util.Set;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
/**
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -220,6 +233,20 @@ public class GameRuleProvider {
|
||||
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
|
||||
*/
|
||||
@ -691,6 +718,26 @@ public class GameRuleProvider {
|
||||
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;
|
||||
}
|
||||
|
@ -61,6 +61,7 @@ import org.bukkit.block.Block;
|
||||
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;
|
||||
@ -580,6 +581,20 @@ public class DGameWorld extends DInstanceWorld {
|
||||
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();
|
||||
|
@ -27,9 +27,10 @@ import org.bukkit.event.block.BlockBreakEvent;
|
||||
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;
|
||||
|
||||
@ -124,9 +125,25 @@ public class DWorldListener implements Listener {
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
@ -42,6 +42,7 @@ import org.bukkit.World.Environment;
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -171,6 +172,16 @@ public class WorldConfig extends GameRuleProvider {
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user