mirror of
https://github.com/BentoBoxWorld/Limits.git
synced 2024-09-27 14:12:57 +02:00
Prevent breeding using breeding event
This commit is contained in:
parent
3f1465d6ef
commit
d6f12c29dc
@ -2,7 +2,6 @@ package world.bentobox.limits.listeners;
|
|||||||
|
|
||||||
import java.util.AbstractMap;
|
import java.util.AbstractMap;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -17,6 +16,7 @@ import org.bukkit.Tag;
|
|||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockFace;
|
import org.bukkit.block.BlockFace;
|
||||||
|
import org.bukkit.entity.Animals;
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.entity.EntityType;
|
import org.bukkit.entity.EntityType;
|
||||||
import org.bukkit.entity.LivingEntity;
|
import org.bukkit.entity.LivingEntity;
|
||||||
@ -89,7 +89,13 @@ public class EntityLimitListener implements Listener {
|
|||||||
public void onBreed(final EntityBreedEvent e) {
|
public void onBreed(final EntityBreedEvent e) {
|
||||||
if (e.getBreeder() != null && e.getBreeder() instanceof Player p &&
|
if (e.getBreeder() != null && e.getBreeder() instanceof Player p &&
|
||||||
!(p.isOp() || p.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getEntity().getWorld()) + MOD_BYPASS))) {
|
!(p.isOp() || p.hasPermission(addon.getPlugin().getIWM().getPermissionPrefix(e.getEntity().getWorld()) + MOD_BYPASS))) {
|
||||||
checkLimit(e, e.getEntity(), SpawnReason.BREEDING, false);
|
if (!checkLimit(e, e.getEntity(), SpawnReason.BREEDING, false)) {
|
||||||
|
// Breeding not allowed so stop the love fest
|
||||||
|
if (e.getFather() instanceof Animals f && e.getMother() instanceof Animals m) {
|
||||||
|
f.setLoveModeTicks(0);
|
||||||
|
m.setLoveModeTicks(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,38 +158,41 @@ public class EntityLimitListener implements Listener {
|
|||||||
* Check if a creature is allowed to spawn or not
|
* Check if a creature is allowed to spawn or not
|
||||||
* @param e - CreatureSpawnEvent
|
* @param e - CreatureSpawnEvent
|
||||||
* @param async - true if check can be done async, false if not
|
* @param async - true if check can be done async, false if not
|
||||||
|
* @return true if allowed or asycn, false if not.
|
||||||
*/
|
*/
|
||||||
private void checkLimit(Cancellable c, LivingEntity e, SpawnReason reason, boolean async) {
|
private boolean checkLimit(Cancellable c, LivingEntity e, SpawnReason reason, boolean async) {
|
||||||
BentoBox.getInstance().logDebug("Event = " + c.toString() + " " + e + " " + reason + " ");
|
|
||||||
Location l = e.getLocation();
|
Location l = e.getLocation();
|
||||||
if (async) {
|
if (async) {
|
||||||
c.setCancelled(true);
|
c.setCancelled(true);
|
||||||
}
|
}
|
||||||
processIsland(c, e, l, reason, async);
|
return processIsland(c, e, l, reason, async);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processIsland(Cancellable c, LivingEntity e, Location l, SpawnReason reason, boolean async) {
|
private boolean processIsland(Cancellable c, LivingEntity e, Location l, SpawnReason reason, boolean async) {
|
||||||
addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> {
|
if (addon.getIslands().getIslandAt(e.getLocation()).isEmpty()) {
|
||||||
// Check if creature is allowed to spawn or not
|
return true;
|
||||||
AtLimitResult res = atLimit(island, e);
|
}
|
||||||
if (island.isSpawn() || !res.hit()) {
|
Island island = addon.getIslands().getIslandAt(e.getLocation()).get();
|
||||||
// Allowed
|
// Check if creature is allowed to spawn or not
|
||||||
if (async) {
|
AtLimitResult res = atLimit(island, e);
|
||||||
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> {
|
if (island.isSpawn() || !res.hit()) {
|
||||||
l.getWorld().spawn(l, e.getClass(), entity -> preSpawn(entity, reason, l));
|
// Allowed
|
||||||
});
|
if (async) {
|
||||||
} // else do nothing
|
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> {
|
||||||
|
l.getWorld().spawn(l, e.getClass(), entity -> preSpawn(entity, reason, l));
|
||||||
|
});
|
||||||
|
} // else do nothing
|
||||||
|
} else {
|
||||||
|
if (async) {
|
||||||
|
e.remove();
|
||||||
} else {
|
} else {
|
||||||
if (async) {
|
c.setCancelled(true);
|
||||||
e.remove();
|
|
||||||
} else {
|
|
||||||
c.setCancelled(true);
|
|
||||||
}
|
|
||||||
// If the reason is anything but because of a spawner then tell players within range
|
|
||||||
tellPlayers(l, e, reason, res);
|
|
||||||
}
|
}
|
||||||
|
// If the reason is anything but because of a spawner then tell players within range
|
||||||
});
|
tellPlayers(l, e, reason, res);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void preSpawn(Entity entity, SpawnReason reason, Location l) {
|
private void preSpawn(Entity entity, SpawnReason reason, Location l) {
|
||||||
|
@ -53,7 +53,6 @@ public class JoinListener implements Listener {
|
|||||||
*/
|
*/
|
||||||
public void checkPerms(Player player, String permissionPrefix, String islandId, String gameMode) {
|
public void checkPerms(Player player, String permissionPrefix, String islandId, String gameMode) {
|
||||||
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId);
|
IslandBlockCount ibc = addon.getBlockLimitListener().getIsland(islandId);
|
||||||
BentoBox.getInstance().logDebug("Check perms ibc == null? " + ibc == null);
|
|
||||||
// Check permissions
|
// Check permissions
|
||||||
if (ibc != null) {
|
if (ibc != null) {
|
||||||
// Clear permission limits
|
// Clear permission limits
|
||||||
@ -81,8 +80,6 @@ public class JoinListener implements Listener {
|
|||||||
}
|
}
|
||||||
// Make an ibc if required
|
// Make an ibc if required
|
||||||
if (ibc == null) {
|
if (ibc == null) {
|
||||||
|
|
||||||
BentoBox.getInstance().logDebug("Making new IBC");
|
|
||||||
ibc = new IslandBlockCount(islandId, gameMode);
|
ibc = new IslandBlockCount(islandId, gameMode);
|
||||||
}
|
}
|
||||||
// Get the value
|
// Get the value
|
||||||
|
Loading…
Reference in New Issue
Block a user