Generators will now work correctly with automation.

This commit is contained in:
Brianna O'Keefe 2019-01-29 23:07:44 -05:00
parent 8e929906df
commit babac7adc6
2 changed files with 42 additions and 104 deletions

View File

@ -11,7 +11,6 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.Levelled;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.io.File;
@ -231,59 +230,33 @@ public class GeneratorManager {
}
@SuppressWarnings("deprecation")
public void generateBlock(Player player, Block block) {
public void generateBlock(Generator generator, Block block) {
block.setType(Material.AIR);
Bukkit.getServer().getScheduler().runTaskAsynchronously(skyblock, new Runnable() {
@Override
public void run() {
for (int i = generatorStorage.size() - 1; i >= 0; i--) {
Generator generator = generatorStorage.get(i);
Bukkit.getServer().getScheduler().runTaskAsynchronously(skyblock, () -> {
if (generator.isPermission()) {
if (!player.hasPermission(generator.getPermission())
&& !player.hasPermission("fabledskyblock.generator.*")
&& !player.hasPermission("fabledskyblock.*")) {
continue;
}
Materials materials = getRandomMaterials(generator);
if (materials == null) return;
Bukkit.getScheduler().runTask(skyblock, () -> {
skyblock.getSoundManager().playSound(block.getLocation(), Sounds.FIZZ.bukkitSound(),
1.0F, 10.0F);
if (NMSUtil.getVersionNumber() > 12) {
block.setType(materials.parseMaterial());
} else {
ItemStack is = materials.parseItem();
block.setType(is.getType());
try {
block.getClass().getMethod("setData", byte.class).invoke(block,
(byte) is.getDurability());
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
Materials materials = getRandomMaterials(generator);
if (materials != null) {
Bukkit.getScheduler().runTask(skyblock, new Runnable() {
public void run() {
skyblock.getSoundManager().playSound(block.getLocation(), Sounds.FIZZ.bukkitSound(),
1.0F, 10.0F);
if (NMSUtil.getVersionNumber() > 12) {
block.setType(materials.parseMaterial());
} else {
ItemStack is = materials.parseItem();
block.setType(is.getType());
try {
block.getClass().getMethod("setData", byte.class).invoke(block,
(byte) is.getDurability());
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}
}
});
}
return;
}
Bukkit.getServer().getScheduler().runTask(skyblock, new Runnable() {
@Override
public void run() {
block.setType(Material.COBBLESTONE);
}
});
}
});
});
}
@ -342,6 +315,10 @@ public class GeneratorManager {
return false;
}
public List<Generator> getGeneratorStorage() {
return generatorStorage;
}
public List<Generator> getGenerators() {
return generatorStorage;
}

View File

@ -2,6 +2,7 @@ package me.goodandevil.skyblock.listeners;
import me.goodandevil.skyblock.SkyBlock;
import me.goodandevil.skyblock.config.FileManager.Config;
import me.goodandevil.skyblock.generator.Generator;
import me.goodandevil.skyblock.generator.GeneratorLocation;
import me.goodandevil.skyblock.generator.GeneratorManager;
import me.goodandevil.skyblock.island.*;
@ -251,6 +252,7 @@ public class Block implements Listener {
public void onBlockForm(BlockFormEvent event) {
org.bukkit.block.Block block = event.getBlock();
IslandManager islandManager = skyblock.getIslandManager();
PlayerDataManager playerDataManager = skyblock.getPlayerDataManager();
GeneratorManager generatorManager = skyblock.getGeneratorManager();
WorldManager worldManager = skyblock.getWorldManager();
@ -266,67 +268,26 @@ public class Block implements Listener {
}
if (generatorManager != null && generatorManager.getGenerators().size() > 0) {
org.bukkit.Location location = event.getBlock().getLocation();
Island island = islandManager.getIslandAtLocation(event.getBlock().getLocation());
IslandWorld world = worldManager.getIslandWorld(event.getBlock().getWorld());
for (Player all : Bukkit.getOnlinePlayers()) {
if (!playerDataManager.hasPlayerData(all)) continue;
PlayerData playerData = playerDataManager.getPlayerData(all);
if (!LocationUtil.isLocationAtLocationRadius(all.getLocation(),
island.getLocation(world, IslandEnvironment.Island), island.getRadius())) continue;
if (playerData.getGenerator() == null) continue;
GeneratorLocation generatorLocation = playerData.getGenerator();
int i = generatorManager.getGeneratorStorage().size() - 1;
Generator generator = generatorManager.getGeneratorStorage().get(i);
if (generatorLocation.getWorld() != worldManager.getIslandWorld(block.getWorld())) continue;
if (location.getBlockX() == generatorLocation.getBlockX()
&& location.getBlockY() == generatorLocation.getBlockY()
&& location.getBlockZ() == generatorLocation.getBlockZ()) {
event.setCancelled(true);
generatorManager.generateBlock(all, block);
playerData.setGenerator(null);
return;
}
}
}
}
@EventHandler
public void onBlockFromTo(BlockFromToEvent event) {
org.bukkit.block.Block block = event.getBlock();
PlayerDataManager playerDataManager = skyblock.getPlayerDataManager();
GeneratorManager generatorManager = skyblock.getGeneratorManager();
WorldManager worldManager = skyblock.getWorldManager();
if (NMSUtil.getVersionNumber() < 13) {
if (worldManager.isIslandWorld(block.getWorld())) {
if (generatorManager != null && generatorManager.getGenerators().size() > 0) {
org.bukkit.Location location = block.getLocation();
for (Player all : Bukkit.getOnlinePlayers()) {
if (playerDataManager.hasPlayerData(all)) {
PlayerData playerData = playerDataManager.getPlayerData(all);
if (playerData.getGenerator() != null) {
GeneratorLocation generatorLocation = playerData.getGenerator();
if (generatorLocation.getWorld() == worldManager.getIslandWorld(block.getWorld())) {
if (location.getBlockX() == generatorLocation.getLiquidX()
&& location.getBlockY() == generatorLocation.getLiquidY()
&& location.getBlockZ() == generatorLocation.getLiquidZ()) {
event.setCancelled(true);
generatorManager.generateBlock(all,
new org.bukkit.Location(location.getWorld(),
generatorLocation.getBlockX(), generatorLocation.getBlockY(),
generatorLocation.getBlockZ()).getBlock());
playerData.setGenerator(null);
return;
}
}
}
}
if (generator.isPermission()) {
if (!all.hasPermission(generator.getPermission())
&& !all.hasPermission("fabledskyblock.generator.*")
&& !all.hasPermission("fabledskyblock.*")) {
continue;
}
}
generatorManager.generateBlock(generator, block);
return;
}
}
}