Fixed blocklevel issue.

Added EpicSpawners support.
This commit is contained in:
Brianna O'Keefe 2019-01-29 22:05:00 -05:00
parent 2ce9e1d187
commit 8e929906df
4 changed files with 193 additions and 173 deletions

View File

@ -49,6 +49,11 @@
<artifactId>leaderheads</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.songoda</groupId>
<artifactId>epicspawners</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>

View File

@ -1,21 +1,5 @@
package me.goodandevil.skyblock.levelling;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import me.goodandevil.skyblock.SkyBlock;
import me.goodandevil.skyblock.api.event.island.IslandLevelChangeEvent;
import me.goodandevil.skyblock.config.FileManager.Config;
@ -26,197 +10,225 @@ import me.goodandevil.skyblock.utils.version.Materials;
import me.goodandevil.skyblock.utils.version.NMSUtil;
import me.goodandevil.skyblock.utils.version.Sounds;
import me.goodandevil.skyblock.world.WorldManager;
import org.bukkit.Bukkit;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
public class LevellingManager {
private final SkyBlock skyblock;
private final SkyBlock skyblock;
private List<Material> materialStorage = new ArrayList<>();
private List<Material> materialStorage = new ArrayList<>();
public LevellingManager(SkyBlock skyblock) {
this.skyblock = skyblock;
public LevellingManager(SkyBlock skyblock) {
this.skyblock = skyblock;
registerMaterials();
}
registerMaterials();
}
public void calculatePoints(Player player, Island island) {
WorldManager worldManager = skyblock.getWorldManager();
public void calculatePoints(Player player, Island island) {
WorldManager worldManager = skyblock.getWorldManager();
Chunk chunk = new Chunk(skyblock, island);
chunk.prepare();
Chunk chunk = new Chunk(skyblock, island);
chunk.prepare();
int NMSVersion = NMSUtil.getVersionNumber();
int NMSVersion = NMSUtil.getVersionNumber();
new BukkitRunnable() {
@SuppressWarnings("deprecation")
@Override
public void run() {
if (chunk.isComplete()) {
cancel();
new BukkitRunnable() {
@SuppressWarnings("deprecation")
@Override
public void run() {
if (!chunk.isComplete()) return;
cancel();
Map<String, Integer> materials = new HashMap<>();
Map<String, Integer> materials = new HashMap<>();
Method getBlockTypeMethod = null;
Method getBlockTypeIdMethod = null;
Method getBlockTypeDataMethod = null;
Method getMaterialMethod = null;
Method getBlockTypeMethod = null;
Method getBlockTypeIdMethod = null;
Method getBlockTypeDataMethod = null;
Method getMaterialMethod = null;
int worldMaxHeight = 0;
int worldMaxHeight = 0;
for (IslandWorld worldList : IslandWorld.values()) {
org.bukkit.World world = worldManager.getWorld(worldList);
for (IslandWorld worldList : IslandWorld.values()) {
org.bukkit.World world = worldManager.getWorld(worldList);
if (worldMaxHeight == 0 || worldMaxHeight > world.getMaxHeight()) {
worldMaxHeight = world.getMaxHeight();
}
}
if (worldMaxHeight == 0 || worldMaxHeight > world.getMaxHeight()) {
worldMaxHeight = world.getMaxHeight();
}
}
for (ChunkSnapshot chunkSnapshotList : chunk.getChunkSnapshots()) {
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = 0; y < worldMaxHeight; y++) {
try {
org.bukkit.Material blockMaterial = org.bukkit.Material.AIR;
int blockData = 0;
for (ChunkSnapshot chunkSnapshotList : chunk.getChunkSnapshots()) {
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = 0; y < worldMaxHeight; y++) {
try {
org.bukkit.Material blockMaterial = org.bukkit.Material.AIR;
int blockData = 0;
if (NMSVersion > 12) {
if (getBlockTypeMethod == null) {
getBlockTypeMethod = chunkSnapshotList.getClass()
.getMethod("getBlockType", int.class, int.class, int.class);
}
if (NMSVersion > 12) {
if (getBlockTypeMethod == null) {
getBlockTypeMethod = chunkSnapshotList.getClass()
.getMethod("getBlockType", int.class, int.class, int.class);
}
blockMaterial = (org.bukkit.Material) getBlockTypeMethod
.invoke(chunkSnapshotList, x, y, z);
} else {
if (getBlockTypeIdMethod == null) {
getBlockTypeIdMethod = chunkSnapshotList.getClass()
.getMethod("getBlockTypeId", int.class, int.class, int.class);
}
blockMaterial = (org.bukkit.Material) getBlockTypeMethod
.invoke(chunkSnapshotList, x, y, z);
} else {
if (getBlockTypeIdMethod == null) {
getBlockTypeIdMethod = chunkSnapshotList.getClass()
.getMethod("getBlockTypeId", int.class, int.class, int.class);
}
if (getBlockTypeDataMethod == null) {
getBlockTypeDataMethod = chunkSnapshotList.getClass()
.getMethod("getBlockData", int.class, int.class, int.class);
}
if (getBlockTypeDataMethod == null) {
getBlockTypeDataMethod = chunkSnapshotList.getClass()
.getMethod("getBlockData", int.class, int.class, int.class);
}
if (getMaterialMethod == null) {
getMaterialMethod = blockMaterial.getClass().getMethod("getMaterial",
int.class);
}
if (getMaterialMethod == null) {
getMaterialMethod = blockMaterial.getClass().getMethod("getMaterial",
int.class);
}
blockMaterial = (org.bukkit.Material) getMaterialMethod.invoke(
blockMaterial,
(int) getBlockTypeIdMethod.invoke(chunkSnapshotList, x, y, z));
blockData = (int) getBlockTypeDataMethod.invoke(chunkSnapshotList, x, y, z);
}
blockMaterial = (org.bukkit.Material) getMaterialMethod.invoke(
blockMaterial,
(int) getBlockTypeIdMethod.invoke(chunkSnapshotList, x, y, z));
blockData = (int) getBlockTypeDataMethod.invoke(chunkSnapshotList, x, y, z);
}
if (blockMaterial != org.bukkit.Material.AIR) {
for (Material materialList : materialStorage) {
if (materialList == null) continue;
ItemStack is = materialList.getItemStack();
if (blockMaterial == org.bukkit.Material.AIR) continue;
if (blockMaterial == materialList.getItemStack().getType()) {
if (NMSVersion < 13) {
if (!(blockData == is.getDurability())) {
continue;
}
}
for (Material materialList : materialStorage) {
if (materialList == null) continue;
ItemStack is = materialList.getItemStack();
if (materials.containsKey(materialList.getMaterials().name())) {
materials.put(materialList.getMaterials().name(),
materials.get(materialList.getMaterials().name()) + 1);
} else {
materials.put(materialList.getMaterials().name(), 1);
}
}
}
}
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}
}
}
}
if (blockMaterial != materialList.getItemStack().getType()) continue;
if (NMSVersion < 13) {
if (!(blockData == is.getDurability())) {
continue;
}
}
if (materials.size() == 0) {
if (player != null) {
skyblock.getMessageManager().sendMessage(player, skyblock.getFileManager()
.getConfig(new File(skyblock.getDataFolder(), "language.yml"))
.getFileConfiguration().getString("Command.Island.Level.Materials.Message"));
skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F);
}
} else {
IslandLevel level = island.getLevel();
level.setLastCalculatedPoints(level.getPoints());
level.setLastCalculatedLevel(level.getLevel());
level.setMaterials(materials);
int amount = 1;
if (blockMaterial == Materials.SPAWNER.parseMaterial()
&& Bukkit.getPluginManager().isPluginEnabled("EpicSpawners")) {
World world = Bukkit.getWorld(chunkSnapshotList.getWorldName());
com.songoda.epicspawners.api.EpicSpawners epicSpawners =
com.songoda.epicspawners.api.EpicSpawnersAPI.getImplementation();
Location location = new Location(world, chunkSnapshotList.getX() * 16 + x, y, chunkSnapshotList.getZ() * 16 + z);
if (epicSpawners.getSpawnerManager().isSpawner(location)) {
amount = epicSpawners.getSpawnerManager()
.getSpawnerFromWorld(location).getSpawnerDataCount();
}
}
Bukkit.getServer().getPluginManager().callEvent(
new IslandLevelChangeEvent(island.getAPIWrapper(), island.getAPIWrapper().getLevel()));
if (materials.containsKey(materialList.getMaterials().name())) {
materials.put(materialList.getMaterials().name(),
materials.get(materialList.getMaterials().name()) + amount);
} else {
materials.put(materialList.getMaterials().name(), amount);
}
}
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}
}
}
}
if (player != null) {
me.goodandevil.skyblock.menus.Levelling.getInstance().open(player);
}
}
}
}
}.runTaskTimerAsynchronously(skyblock, 0L, 1L);
}
if (materials.size() == 0) {
if (player != null) {
skyblock.getMessageManager().sendMessage(player, skyblock.getFileManager()
.getConfig(new File(skyblock.getDataFolder(), "language.yml"))
.getFileConfiguration().getString("Command.Island.Level.Materials.Message"));
skyblock.getSoundManager().playSound(player, Sounds.VILLAGER_NO.bukkitSound(), 1.0F, 1.0F);
}
} else {
IslandLevel level = island.getLevel();
level.setLastCalculatedPoints(level.getPoints());
level.setLastCalculatedLevel(level.getLevel());
level.setMaterials(materials);
public void registerMaterials() {
Config config = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "levelling.yml"));
FileConfiguration configLoad = config.getFileConfiguration();
Bukkit.getServer().getPluginManager().callEvent(
new IslandLevelChangeEvent(island.getAPIWrapper(), island.getAPIWrapper().getLevel()));
if (configLoad.getString("Materials") != null) {
for (String materialList : configLoad.getConfigurationSection("Materials").getKeys(false)) {
try {
Materials materials = Materials.fromString(materialList);
if (player != null) {
me.goodandevil.skyblock.menus.Levelling.getInstance().open(player);
}
}
}
}.runTaskTimerAsynchronously(skyblock, 0L, 1L);
}
if (!containsMaterials(materials)) {
addMaterial(materials, configLoad.getInt("Materials." + materialList + ".Points"));
}
} catch (Exception e) {
Bukkit.getServer().getLogger().log(Level.WARNING, "SkyBlock | Error: The material '" + materialList
+ "' is not a Material type. Make sure the material name is a 1.13 material name. Please correct this in the 'levelling.yml' file.");
}
}
}
}
public void registerMaterials() {
Config config = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "levelling.yml"));
FileConfiguration configLoad = config.getFileConfiguration();
public void unregisterMaterials() {
materialStorage.clear();
}
if (configLoad.getString("Materials") != null) {
for (String materialList : configLoad.getConfigurationSection("Materials").getKeys(false)) {
try {
Materials materials = Materials.fromString(materialList);
public void addMaterial(Materials materials, int points) {
materialStorage.add(new Material(materials, points));
}
if (!containsMaterials(materials)) {
addMaterial(materials, configLoad.getInt("Materials." + materialList + ".Points"));
}
} catch (Exception e) {
Bukkit.getServer().getLogger().log(Level.WARNING, "SkyBlock | Error: The material '" + materialList
+ "' is not a Material type. Make sure the material name is a 1.13 material name. Please correct this in the 'levelling.yml' file.");
}
}
}
}
public void removeMaterial(Material material) {
materialStorage.remove(material);
}
public void unregisterMaterials() {
materialStorage.clear();
}
public boolean containsMaterials(Materials materials) {
for (Material materialList : materialStorage) {
if (materialList.getMaterials().name().equals(materials.name())) {
return true;
}
}
public void addMaterial(Materials materials, int points) {
materialStorage.add(new Material(materials, points));
}
return false;
}
public void removeMaterial(Material material) {
materialStorage.remove(material);
}
public Material getMaterial(Materials materials) {
for (Material materialList : materialStorage) {
if (materialList.getMaterials().name().equals(materials.name())) {
return materialList;
}
}
public boolean containsMaterials(Materials materials) {
for (Material materialList : materialStorage) {
if (materialList.getMaterials().name().equals(materials.name())) {
return true;
}
}
return null;
}
return false;
}
public List<Material> getMaterials() {
return materialStorage;
}
public Material getMaterial(Materials materials) {
for (Material materialList : materialStorage) {
if (materialList.getMaterials().name().equals(materials.name())) {
return materialList;
}
}
return null;
}
public List<Material> getMaterials() {
return materialStorage;
}
}

View File

@ -105,6 +105,8 @@ public class Block implements Listener {
@SuppressWarnings("deprecation")
Materials materials = Materials.getMaterials(block.getType(), block.getData());
Bukkit.broadcastMessage("testt");
if (materials == null) return;
IslandLevel level = island.getLevel();
@ -169,7 +171,8 @@ public class Block implements Listener {
}
}
if (event.isCancelled() || configLoad.getBoolean("Island.Block.Level.Enable")) return;
if (event.isCancelled() || !configLoad.getBoolean("Island.Block.Level.Enable")) return;
@SuppressWarnings("deprecation")
Materials materials = Materials.getMaterials(block.getType(), block.getData());

View File

@ -198,7 +198,7 @@ Island:
Level:
# When enabled, any changes to a block will be made to the levelling materials
# calculated for an island.
Enable: false
Enable: true
Piston:
# Prevent Piston blocks being retracted or extended when connected to a circuit
# [!] Prevents Pistons extending out of Island border bug