Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.lisenochek.mcrust.objects.blockMechanic;
- import com.comphenix.protocol.wrappers.EnumWrappers;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.entity.Player;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.scheduler.BukkitRunnable;
- import ru.lisenochek.mcrust.Main;
- import ru.lisenochek.mcrust.events.block.RustBlockBreakEvent;
- import ru.lisenochek.mcrust.events.block.RustBlockDecayEvent;
- import ru.lisenochek.mcrust.events.block.RustPhysicBreakBlockEvent;
- import ru.lisenochek.mcrust.objects.misc.CustomBlock;
- import ru.lisenochek.mcrust.sql.SQLManager;
- import ru.lisenochek.mcrust.utils.ISBuilder;
- import ru.lisenochek.mcrust.utils.Utils;
- import java.util.HashMap;
- public class Buildings {
- private static HashMap<Location, Buildings> buildingsMap = new HashMap<>();
- private Location location;
- private CustomBlock.Type type;
- private int health;
- private int decayTime;
- private int blockDecayTime;
- private Buildings(Location location, CustomBlock.Type type, int health, int blockDecayTime) {
- this.location = location;
- this.type = type;
- this.health = health;
- this.decayTime = type.getDecayTime();
- this.blockDecayTime = blockDecayTime;
- buildingsMap.put(location, this);
- if (type.isDoubleBlock()) buildingsMap.put(location.clone().add(0, 1, 0), this);
- runTimer();
- }
- public static Buildings create(Location location, CustomBlock.Type type, int health, int blockDecayTime) {
- return new Buildings(location, type, health, blockDecayTime);
- }
- public static void save() {
- for (Buildings buildings : buildingsMap.values()) SQLManager.getManager().updateBuildingData(buildings);
- }
- public static Buildings fromLocation(Location location) {
- return buildingsMap.get(location);
- }
- public static void openGUI(Player player, Buildings buildings) {
- Inventory inventory = Bukkit.createInventory(null, 27, Utils.stripColor("&2&lКонструкция"));
- int resourceAmount = Utils.getStacksAmount(player, buildings.getType().getResourceStack()), repairCost = buildings.getRepairCost(player);
- ISBuilder repairBuilder = ISBuilder.getBuilder(Material.WOOD_SPADE, 0, 1, "&a&l» &e&lОтремонтировать",
- "",
- "&7&l&nДля ремонта нужны ресурсы:",
- "",
- "&7- " + ChatColor.stripColor(buildings.getType().getResourceStack().getItemMeta().getDisplayName().toLowerCase()) + " (" + (resourceAmount >= repairCost ? "&a" : "&c") + repairCost + "&7) - " + (resourceAmount < repairCost ? "не хватает - &c" + (repairCost - resourceAmount) : "у вас - &a" + resourceAmount)
- ).hideFlags();
- ISBuilder removeBuilder = ISBuilder.getBuilder(Material.BARRIER, 0, 1, "&a&l» &c&lСломать");
- inventory.setItem(13, buildings.getDecayTime() == -1
- ?
- ISBuilder.getBuilder(Material.BOOK, 0, 1, "&a&l» &e&lИнформация",
- "",
- "&7&l&nПрочность&7&l » &a&l" + buildings.getHealth() + "&7&l/&a&l" + buildings.getType().getMaxHealth(),
- "",
- "&7- Если прочность будет равна &a0&7, то конструкция",
- "&aсломается"
- ).getStack()
- :
- ISBuilder.getBuilder(Material.BOOK, 0, 1, "&a&l» &e&lИнформация",
- "",
- "&7&l&nПрочность&7&l » &a&l" + buildings.getHealth() + "&7&l/&a&l" + buildings.getType().getMaxHealth(),
- "",
- "&7- Если прочность будет равна &a0&7, то конструкция",
- "&aсломается",
- "",
- "&7&l&nЗадержка перед гниением&7&l » &a&l" + Utils.getTimeFormat(buildings.getBlockDecayTime()),
- "",
- "&7- Задержка, в течение которого конструкция &aне &7гниет.",
- "&7Сбрасывается, если конструкция в &aзоне &7шкафа",
- "",
- "&7&l&nВремя гниения&7&l » &a&l" + Utils.getTimeFormat(buildings.getDecayTime()),
- "",
- "&7- Время, по &aистечении &7которого конструкция потеряет",
- "&a1 &7единицу прочности"
- ).getStack()
- );
- inventory.setItem(11, (buildings.getHealth() == buildings.getType().getMaxHealth()
- ?
- ISBuilder.getBuilder(Material.WOOD_SPADE, 0, 1, "&a&l» &c&lКонструкция целая").hideFlags()
- :
- resourceAmount < repairCost
- ?
- repairBuilder.addLore(
- "",
- "&cНедостаточно &7ресурсов для починки"
- )
- :
- repairBuilder.addLore(
- "",
- "&aНажмите&7, чтобы починить"
- ).setTag("location", Utils.serialiseLocation(buildings.getLocation())).setTag("repairCost", repairCost)).getStack()
- );
- inventory.setItem(15, (buildings.getType().getDrop() == null
- ?
- removeBuilder.addLore(
- "",
- "&7Вы уничтожите конструкцию и &cне &7получите &7ее назад"
- )
- :
- removeBuilder.addLore(
- "",
- "&7Вы уничтожите конструкцию и &aполучите &7ее назад"
- )).setTag("location", Utils.serialiseLocation(buildings.getLocation())).getStack()
- );
- for (int freeSlot = 0; freeSlot < inventory.getSize(); freeSlot++) if (inventory.getItem(freeSlot) == null) inventory.setItem(freeSlot, ISBuilder.getBuilder(Material.STAINED_GLASS_PANE, 0, 1, " ").getStack());
- player.openInventory(inventory);
- }
- public Location getLocation() {
- return location;
- }
- public CustomBlock.Type getType() {
- return type;
- }
- public int getHealth() {
- return health;
- }
- public int getDecayTime() {
- return decayTime;
- }
- public int getBlockDecayTime() {
- return blockDecayTime;
- }
- public void setType(CustomBlock.Type type) {
- this.type = type;
- }
- public void setHealth(int health) {
- this.health = health;
- }
- public void setDecayTime(int decayTime) {
- this.decayTime = decayTime;
- }
- public void setBlockDecayTime(int blockDecayTime) {
- this.blockDecayTime = blockDecayTime;
- }
- public void damage(int damage) {
- health = health - damage;
- if (health > 0) {
- SQLManager.getManager().updateBuildingData(this);
- return;
- }
- if (type.isDoubleBlock()) {
- location.clone().add(0, 1, 0).getBlock().setType(Material.AIR, false);
- buildingsMap.remove(location.clone().add(0, 1, 0));
- }
- breakUpBlocks(location.clone().add(0, 1, 0).getBlock().getLocation());
- Bukkit.getPluginManager().callEvent(new RustBlockDecayEvent(location.getBlock(), CustomBlock.getBlock(location.getBlock())));
- breakBlock(location);
- }
- public void remove(Player player) {
- if (type.isDoubleBlock()) {
- location.clone().add(0, 1, 0).getBlock().setType(Material.AIR, false);
- buildingsMap.remove(location.clone().add(0, 1, 0));
- }
- breakUpBlocks(location.clone().add(0, 1, 0).getBlock().getLocation());
- Bukkit.getPluginManager().callEvent(new RustBlockBreakEvent(player, CustomBlock.getBlock(location.getBlock()), new BlockBreakEvent(location.getBlock(), player)));
- breakBlock(location);
- }
- private void breakUpBlocks(Location location) {
- CustomBlock upperBlock = CustomBlock.getBlock(location.getBlock());
- if (upperBlock == null) return;
- if (!upperBlock.getType().isGround()) return;
- Buildings buildings = buildingsMap.get(location);
- breakUpBlocks(location.clone().add(0, 1, 0));
- location.getBlock().setType(Material.AIR);
- buildingsMap.remove(location);
- SQLManager.getManager().deleteBuildingData(buildings);
- Utils.playEffect(location.clone().add(0.5D, 0, 0.5D), false, false, 0.5f, 0.5f, 0.5f, 0, EnumWrappers.Particle.CLOUD, Material.AIR, 25);
- Bukkit.getPluginManager().callEvent(new RustPhysicBreakBlockEvent(location.getBlock(), upperBlock));
- }
- private void breakBlock(Location location) {
- location.getBlock().setType(Material.AIR, false);
- buildingsMap.remove(location);
- SQLManager.getManager().deleteBuildingData(this);
- Utils.playEffect(location.clone().add(0.5D, 0, 0.5D), false, false, 0.5f, 0.5f, 0.5f, 0, EnumWrappers.Particle.CLOUD, Material.AIR, 25);
- }
- public int getRepairCost(Player player) {
- int coef = ((type.getMaxHealth() - health) / 10);
- return coef == 0 ? 1 : coef;
- }
- public void runTimer() {
- new BukkitRunnable() {
- @Override
- public void run() {
- if (!buildingsMap.containsKey(location)) {
- cancel();
- return;
- }
- if (decayTime == -1) return;
- if (Cupboard.fromLocation(location) != null) {
- blockDecayTime = type.getBlockDecayTime();
- return;
- }
- if (blockDecayTime != 0) {
- --blockDecayTime;
- return;
- }
- if (decayTime != 0) {
- --decayTime;
- return;
- }
- damage(1);
- decayTime = type.getDecayTime();
- }
- }.runTaskTimer(Main.plugin, 1, 20);
- }
- }
Add Comment
Please, Sign In to add comment