Protect Stackables from explosions

This commit is contained in:
Fabrizio La Rosa 2020-06-20 05:00:58 +02:00
parent 8be6bdfe7e
commit 9ae191c4c4

View File

@ -6,6 +6,7 @@ import com.songoda.skyblock.config.FileManager;
import com.songoda.skyblock.config.FileManager.Config;
import com.songoda.skyblock.island.*;
import com.songoda.skyblock.limit.impl.EntityLimitaton;
import com.songoda.skyblock.stackable.Stackable;
import com.songoda.skyblock.stackable.StackableManager;
import com.songoda.skyblock.upgrade.Upgrade;
import com.songoda.skyblock.utils.version.NMSUtil;
@ -14,6 +15,7 @@ import com.songoda.skyblock.world.WorldManager;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.*;
import org.bukkit.entity.Projectile;
@ -343,21 +345,8 @@ public class Entity implements Listener {
.getBoolean("Island.Block.Level.Enable"))
return;
CompatibleMaterial materials = CompatibleMaterial.getBlockMaterial(block.getType());
if (materials != null) {
IslandLevel level = island.getLevel();
if (level.hasMaterial(materials.name())) {
long materialAmount = level.getMaterialAmount(materials.name());
if (materialAmount - 1 <= 0) {
level.removeMaterial(materials.name());
} else {
level.setMaterialAmount(materials.name(), materialAmount - 1);
}
}
}
removeBlockFromLevel(island, block);
CompatibleMaterial materials;
if (event.getTo() != null && event.getTo() != Material.AIR) {
materials = CompatibleMaterial.getBlockMaterial(event.getTo());
@ -377,7 +366,7 @@ public class Entity implements Listener {
}
@EventHandler
@EventHandler(priority = EventPriority.LOW)
public void onEntityExplode(EntityExplodeEvent event) {
org.bukkit.entity.Entity entity = event.getEntity();
@ -387,47 +376,91 @@ public class Entity implements Listener {
if (skyblock.getWorldManager().isIslandWorld(entity.getWorld())) {
// Check permissions.
Island island = islandManager.getIslandAtLocation(entity.getLocation());
skyblock.getPermissionManager().processPermission(event, null, island);
if (!event.isCancelled()) {
if (skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration()
.getBoolean("Island.Block.Level.Enable")) {
for (org.bukkit.block.Block blockList : event.blockList()) {
@SuppressWarnings("deprecation")
CompatibleMaterial materials = CompatibleMaterial.getBlockMaterial(blockList.getType());
if (materials != null) {
IslandLevel level = island.getLevel();
StackableManager stackableManager = skyblock.getStackableManager();
if (level.hasMaterial(materials.name())) {
long materialAmount = level.getMaterialAmount(materials.name());
boolean removed;
Iterator<org.bukkit.block.Block> it = event.blockList().iterator();
while (it.hasNext()){
removed = false;
org.bukkit.block.Block block = it.next();
if (SkyBlock.getInstance().getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration()
.getBoolean("Island.Spawn.Protection")) {
IslandWorld world = worldManager.getIslandWorld(event.getEntity().getWorld());
if (LocationUtil.isLocationLocation(block.getLocation(),
island.getLocation(world, IslandEnvironment.Main).clone().subtract(0.0D, 1.0D, 0.0D))) {
it.remove();
removed = true;
}
}
if (materialAmount - 1 <= 0) {
level.removeMaterial(materials.name());
} else {
level.setMaterialAmount(materials.name(), materialAmount - 1);
}
Location blockLocation = block.getLocation();
if (stackableManager != null && stackableManager.isStacked(blockLocation)) {
Stackable stackable = stackableManager.getStack(block.getLocation(), CompatibleMaterial.getMaterial(block));
if (stackable != null) {
CompatibleMaterial material = CompatibleMaterial.getMaterial(block);
byte data = block.getData();
int removedAmount = (int) (Math.random() * Math.min(64, stackable.getSize()-1));
stackable.take(removedAmount);
Bukkit.getScheduler().runTask(skyblock, () -> {
block.getWorld().dropItemNaturally(blockLocation.clone().add(.5, 1, .5),
new ItemStack(material.getMaterial(), (int) (Math.random() * removedAmount), data));
});
if (stackable.getSize() <= 1) {
stackableManager.removeStack(stackable);
}
Config config = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml"));
FileConfiguration configLoad = config.getFileConfiguration();
if (configLoad.getBoolean("Island.Block.Level.Enable")) {
removeBlockFromLevel(island, block);
}
it.remove();
if(!removed){
removed = true;
}
}
}
}
if (SkyBlock.getInstance().getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration()
.getBoolean("Island.Spawn.Protection")) {
IslandWorld world = worldManager.getIslandWorld(event.getEntity().getWorld());
for (org.bukkit.block.Block block : event.blockList()) {
if (LocationUtil.isLocationLocation(block.getLocation(),
island.getLocation(world, IslandEnvironment.Main).clone().subtract(0.0D, 1.0D, 0.0D))) {
event.blockList().remove(block);
break;
if (skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")).getFileConfiguration()
.getBoolean("Island.Block.Level.Enable")) {
if(!removed){
removeBlockFromLevel(island, block);
}
}
}
}
}
}
private void removeBlockFromLevel(Island island, CompatibleMaterial material){
if (material != null) {
IslandLevel level = island.getLevel();
if (level.hasMaterial(material.name())) {
long materialAmount = level.getMaterialAmount(material.name());
if (materialAmount - 1 <= 0) {
level.removeMaterial(material.name());
} else {
level.setMaterialAmount(material.name(), materialAmount - 1);
}
}
}
}
private void removeBlockFromLevel(Island island, Block block) {
removeBlockFromLevel(island, CompatibleMaterial.getBlockMaterial(block.getType()));
}
@EventHandler(priority = EventPriority.MONITOR)
public void onEntityDeath(EntityDeathEvent event) {
LivingEntity livingEntity = event.getEntity();