Minor changes to existing Flags

Splited TRAPDOOR from DOOR
Splited HURT_VILLAGERS from HURT_ANIMALS
Renamed HURT_MOBS to HURT_ANIMALS
Renamed MOB_SPAWN to ANIMAL_SPAWN
Made FIRE_SPREAD a SETTING type Flag
Fixed MobSpawnListener with mishandling of ANIMAL/MONSTER spawn
This commit is contained in:
Florian CUNY 2018-03-02 15:37:01 +01:00
parent f511e54429
commit e1f3da9449
5 changed files with 49 additions and 51 deletions

View File

@ -71,14 +71,16 @@ public class BlockInteractionListener extends AbstractFlagListener {
case DARK_OAK_DOOR:
case IRON_DOOR:
case IRON_DOOR_BLOCK:
case IRON_TRAPDOOR:
case JUNGLE_DOOR:
case SPRUCE_DOOR:
case TRAP_DOOR:
case WOODEN_DOOR:
case WOOD_DOOR:
checkIsland(e, e.getClickedBlock().getLocation(), Flags.DOOR);
break;
case TRAP_DOOR:
case IRON_TRAPDOOR:
checkIsland(e, e.getClickedBlock().getLocation(), Flags.TRAPDOOR);
break;
case ACACIA_FENCE_GATE:
case BIRCH_FENCE_GATE:
case DARK_OAK_FENCE_GATE:

View File

@ -7,17 +7,7 @@ import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Material;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Entity;
import org.bukkit.entity.IronGolem;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Snowman;
import org.bukkit.entity.Squid;
import org.bukkit.entity.Villager;
import org.bukkit.entity.*;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -32,6 +22,7 @@ import org.bukkit.potion.PotionEffect;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.lists.Flags;
/**
* Handles hurting of monsters and animals directly and indirectly
@ -42,7 +33,6 @@ public class HurtingListener extends AbstractFlagListener {
private HashMap<Integer, UUID> thrownPotions = new HashMap<>();
/**
* Handles mob and monster protection
*
@ -51,11 +41,12 @@ public class HurtingListener extends AbstractFlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onEntityDamage(final EntityDamageByEntityEvent e) {
// Mobs being hurt
if (e.getEntity() instanceof Animals || e.getEntity() instanceof IronGolem || e.getEntity() instanceof Snowman
|| e.getEntity() instanceof Villager) {
respond(e, e.getDamager(), us.tastybento.bskyblock.lists.Flags.HURT_MOBS);
if (e.getEntity() instanceof Animals || e.getEntity() instanceof IronGolem || e.getEntity() instanceof Snowman) {
respond(e, e.getDamager(), Flags.HURT_ANIMALS);
} else if (e.getEntity() instanceof Villager) {
respond(e, e.getDamager(), Flags.HURT_VILLAGERS);
} else if (e.getEntity() instanceof Monster || e.getEntity() instanceof Squid || e.getEntity() instanceof Slime) {
respond(e, e.getDamager(), us.tastybento.bskyblock.lists.Flags.HURT_MONSTERS);
respond(e, e.getDamager(), Flags.HURT_MONSTERS);
}
}
@ -92,29 +83,27 @@ public class HurtingListener extends AbstractFlagListener {
return;
}
if ((e.getCaught() instanceof Animals || e.getCaught() instanceof IronGolem || e.getCaught() instanceof Snowman
|| e.getCaught() instanceof Villager) && checkIsland(e, e.getCaught().getLocation(), us.tastybento.bskyblock.lists.Flags.HURT_MONSTERS)) {
if ((e.getCaught() instanceof Animals || e.getCaught() instanceof IronGolem || e.getCaught() instanceof Snowman)
&& checkIsland(e, e.getCaught().getLocation(), Flags.HURT_ANIMALS)) {
e.getHook().remove();
return;
}
if ((e.getCaught() instanceof Monster || e.getCaught() instanceof Squid || e.getCaught() instanceof Slime)
&& checkIsland(e, e.getCaught().getLocation(), us.tastybento.bskyblock.lists.Flags.HURT_MONSTERS)) {
} else if (e.getCaught() instanceof Villager && checkIsland(e, e.getCaught().getLocation(), Flags.HURT_VILLAGERS)) {
e.getHook().remove();
} else if ((e.getCaught() instanceof Monster || e.getCaught() instanceof Squid || e.getCaught() instanceof Slime)
&& checkIsland(e, e.getCaught().getLocation(), Flags.HURT_MONSTERS)) {
e.getHook().remove();
return;
}
}
/**
* Handles feeding cookies to animals, which may hurt them
* Handles feeding cookies to parrots, which may hurt them
* @param e - event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerHitEntity(PlayerInteractEntityEvent e) {
if (e.getRightClicked() instanceof Animals) {
public void onPlayerFeedParrots(PlayerInteractEntityEvent e) {
if (e.getRightClicked() instanceof Parrot) {
if ((e.getHand().equals(EquipmentSlot.HAND) && e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.COOKIE))
|| (e.getHand().equals(EquipmentSlot.OFF_HAND) && e.getPlayer().getInventory().getItemInOffHand().getType().equals(Material.COOKIE))) {
checkIsland(e, e.getRightClicked().getLocation(), us.tastybento.bskyblock.lists.Flags.HURT_MOBS);
checkIsland(e, e.getRightClicked().getLocation(), Flags.HURT_ANIMALS);
}
}
}
@ -137,7 +126,7 @@ public class HurtingListener extends AbstractFlagListener {
}
// Monsters being hurt
if (entity instanceof Monster || entity instanceof Slime || entity instanceof Squid) {
if (!setUser(User.getInstance(attacker)).checkIsland(e, entity.getLocation(), us.tastybento.bskyblock.lists.Flags.HURT_MONSTERS)) {
if (!setUser(User.getInstance(attacker)).checkIsland(e, entity.getLocation(), Flags.HURT_MONSTERS)) {
for (PotionEffect effect : e.getPotion().getEffects()) {
entity.removePotionEffect(effect.getType());
}
@ -145,14 +134,20 @@ public class HurtingListener extends AbstractFlagListener {
}
// Mobs being hurt
if (entity instanceof Animals || entity instanceof IronGolem || entity instanceof Snowman
|| entity instanceof Villager) {
if (!checkIsland(e, entity.getLocation(), us.tastybento.bskyblock.lists.Flags.HURT_MONSTERS)) {
if (entity instanceof Animals || entity instanceof IronGolem || entity instanceof Snowman) {
if (!checkIsland(e, entity.getLocation(), Flags.HURT_ANIMALS)) {
for (PotionEffect effect : e.getPotion().getEffects()) {
entity.removePotionEffect(effect.getType());
}
}
}
// Villagers being hurt
if (entity instanceof Villager && !checkIsland(e, entity.getLocation(), Flags.HURT_VILLAGERS)) {
for (PotionEffect effect : e.getPotion().getEffects()) {
entity.removePotionEffect(effect.getType());
}
}
}
}
}
@ -174,7 +169,7 @@ public class HurtingListener extends AbstractFlagListener {
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onLingeringPotionDamage(final EntityDamageByEntityEvent e) {
public void onLingeringPotionDamage(final EntityDamageByEntityEvent e) { //FIXME No need of #setCancelled() there?
if (e.getEntity() == null || e.getEntity().getUniqueId() == null) {
return;
}
@ -188,12 +183,15 @@ public class HurtingListener extends AbstractFlagListener {
Entity entity = e.getEntity();
// Monsters being hurt
if (entity instanceof Monster || entity instanceof Slime || entity instanceof Squid) {
checkIsland(e, entity.getLocation(), us.tastybento.bskyblock.lists.Flags.HURT_MONSTERS);
checkIsland(e, entity.getLocation(), Flags.HURT_MONSTERS);
}
// Mobs being hurt
if (entity instanceof Animals || entity instanceof IronGolem || entity instanceof Snowman
|| entity instanceof Villager) {
checkIsland(e, entity.getLocation(), us.tastybento.bskyblock.lists.Flags.HURT_MONSTERS);
if (entity instanceof Animals || entity instanceof IronGolem || entity instanceof Snowman) {
checkIsland(e, entity.getLocation(), Flags.HURT_ANIMALS);
}
// Villagers being hurt
if (entity instanceof Villager) {
checkIsland(e, entity.getLocation(), Flags.HURT_VILLAGERS);
}
}
}

View File

@ -46,11 +46,11 @@ public class MobSpawnListener extends AbstractFlagListener {
Optional<Island> island = getIslands().getIslandAt(e.getLocation());
// Cancel the event if these are true
if ((e.getEntity() instanceof Monster || e.getEntity() instanceof Slime)) {
boolean cancel = island.map(i -> !i.isAllowed(Flags.MOB_SPAWN)).orElse(!Flags.MOB_SPAWN.isDefaultSetting());
boolean cancel = island.map(i -> !i.isAllowed(Flags.MONSTER_SPAWN)).orElse(!Flags.MONSTER_SPAWN.isDefaultSetting());
e.setCancelled(cancel);
return cancel;
} else if (e.getEntity() instanceof Animals) {
boolean cancel = island.map(i -> !i.isAllowed(Flags.MONSTER_SPAWN)).orElse(!Flags.MONSTER_SPAWN.isDefaultSetting());
boolean cancel = island.map(i -> !i.isAllowed(Flags.ANIMAL_SPAWN)).orElse(!Flags.ANIMAL_SPAWN.isDefaultSetting());
e.setCancelled(cancel);
return cancel;
}

View File

@ -30,16 +30,12 @@ import us.tastybento.bskyblock.listeners.flags.TeleportationListener;
public class Flags {
// TODO: add HURT_VILLAGERS
// TODO: add TRAPDOOR
// TODO: add DYEING sheeps
// TODO: add ELYTRA
// TODO: add FISHING
// TODO: rename HURT_MOBS to HURT_ANIMALS
// TODO: add INTERACT_TAMED
// TODO: split LEVER_BUTTON into BUTTON and LEVER
// TODO: add KEEP_INVENTORY
// TODO: rename MOB_SPAWN to ANIMAL_SPAWN
// TODO: add KEEP_INVENTORY - is it needed?
public static final Flag BREAK_BLOCKS = new FlagBuilder().id("BREAK_BLOCKS").icon(Material.STONE).listener(new BreakBlocksListener()).build();
public static final Flag PLACE_BLOCKS = new FlagBuilder().id("PLACE_BLOCKS").icon(Material.BEDROCK).listener(new PlaceBlocksListener()).build();
@ -51,6 +47,7 @@ public class Flags {
public static final Flag BREWING = new FlagBuilder().id("BREWING").icon(Material.BREWING_STAND_ITEM).build();
public static final Flag CHEST = new FlagBuilder().id("CHEST").icon(Material.CHEST).build();
public static final Flag DOOR = new FlagBuilder().id("DOOR").allowedByDefault(true).icon(Material.WOOD_DOOR).build();
public static final Flag TRAPDOOR = new FlagBuilder().id("TRAPDOOR").allowedByDefault(true).icon(Material.TRAP_DOOR).build();
public static final Flag CRAFTING = new FlagBuilder().id("CRAFTING").allowedByDefault(true).icon(Material.WORKBENCH).build();
public static final Flag ENCHANTING = new FlagBuilder().id("ENCHANTING").allowedByDefault(true).icon(Material.ENCHANTMENT_TABLE).build();
public static final Flag FURNACE = new FlagBuilder().id("FURNACE").icon(Material.FURNACE).build();
@ -97,14 +94,14 @@ public class Flags {
*/
public static final Flag FIRE = new FlagBuilder().id("FIRE").icon(Material.FLINT_AND_STEEL).listener(new FireListener()).build();
public static final Flag FIRE_EXTINGUISH = new FlagBuilder().id("FIRE_EXTINGUISH").icon(Material.POTION).build();
public static final Flag FIRE_SPREAD = new FlagBuilder().id("FIRE_SPREAD").icon(Material.FIREWORK_CHARGE).build();
// Inventories
public static final Flag MOUNT_INVENTORY = new FlagBuilder().id("MOUNT_INVENTORY").icon(Material.IRON_BARDING).listener(new InventoryListener()).build();
// Hurting things
public static final Flag HURT_MOBS = new FlagBuilder().id("HURT_MOBS").icon(Material.STONE_SWORD).listener(new HurtingListener()).build();
public static final Flag HURT_ANIMALS = new FlagBuilder().id("HURT_ANIMALS").icon(Material.STONE_SWORD).listener(new HurtingListener()).build();
public static final Flag HURT_MONSTERS = new FlagBuilder().id("HURT_MONSTERS").icon(Material.WOOD_SWORD).build();
public static final Flag HURT_VILLAGERS = new FlagBuilder().id("HURT_VILLAGERS").icon(Material.GOLD_SWORD).build();
// Leashes
public static final Flag LEASH = new FlagBuilder().id("LEASH").icon(Material.LEASH).listener(new LeashListener()).build();
@ -128,8 +125,9 @@ public class Flags {
public static final Flag PVP_END = new FlagBuilder().id("PVP_END").icon(Material.END_CRYSTAL).type(Type.SETTING).build();
// Others
public static final Flag ENTER_EXIT_MESSAGES = new FlagBuilder().id("ENTER_EXIT_MESSAGES").icon(Material.DIRT).allowedByDefault(true).type(Type.SETTING).build();
public static final Flag MOB_SPAWN = new FlagBuilder().id("MOB_SPAWN").icon(Material.APPLE).allowedByDefault(true).type(Type.SETTING).build();
public static final Flag ANIMAL_SPAWN = new FlagBuilder().id("ANIMAL_SPAWN").icon(Material.APPLE).allowedByDefault(true).type(Type.SETTING).build();
public static final Flag MONSTER_SPAWN = new FlagBuilder().id("MONSTER_SPAWN").icon(Material.MOB_SPAWNER).allowedByDefault(true).type(Type.SETTING).build();
public static final Flag FIRE_SPREAD = new FlagBuilder().id("FIRE_SPREAD").icon(Material.FIREWORK_CHARGE).type(Type.SETTING).build();
/**
* @return List of all the flags in this class

View File

@ -221,7 +221,7 @@ public class MobSpawnListenerTest {
// Block mobs
Flags.MONSTER_SPAWN.setDefaultSetting(false);
Flags.MOB_SPAWN.setDefaultSetting(false);
Flags.ANIMAL_SPAWN.setDefaultSetting(false);
// Setup event
CreatureSpawnEvent e = mock(CreatureSpawnEvent.class);
when(e.getLocation()).thenReturn(location);
@ -249,7 +249,7 @@ public class MobSpawnListenerTest {
// Block mobs
Flags.MONSTER_SPAWN.setDefaultSetting(true);
Flags.MOB_SPAWN.setDefaultSetting(true);
Flags.ANIMAL_SPAWN.setDefaultSetting(true);
// Setup event
CreatureSpawnEvent e = mock(CreatureSpawnEvent.class);