Advertisement
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.Config;
- 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.sound.Sounds;
- 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 boolean isDeleting;
- private Buildings(Location location, CustomBlock.Type type, int health, int blockDecayTime, boolean isDeleting) {
- this.location = location;
- this.type = type;
- this.health = health;
- this.decayTime = Config.BUILDINGS_DECAY_TIME.getInteger();
- this.blockDecayTime = blockDecayTime;
- this.isDeleting = isDeleting;
- buildingsMap.put(location, this);
- if (type.isDoubleBlock()) buildingsMap.put(location.clone().add(0, 1, 0), this);
- runDecay();
- runTimeDelete();
- }
- public static Buildings create(Location location, CustomBlock.Type type, int health, int blockDecayTime, boolean isDeleting) {
- return new Buildings(location, type, health, blockDecayTime, isDeleting);
- }
- public static Buildings fromLocation(Location location) {
- return buildingsMap.get(location);
- }
- public static int getBuildingsAmount() {
- return buildingsMap.size();
- }
- public static void save() {
- for (Buildings buildings : buildingsMap.values()) SQLManager.getManager().updateBuildingData(buildings);
- }
- public void openGUI(Player player) {
- Inventory inventory = Bukkit.createInventory(null, 27, Utils.stripColor("&2&lКонструкция"));
- int resourceAmount = Utils.getStacksAmount(player, type.getResourceStack());
- int repairCost = getRepairCost(player);
- inventory.setItem(13, type.isDecaying()
- ?
- ISBuilder.getBuilder(Material.RAW_CHICKEN, 0, 1, "&a&l» &e&lИнформация",
- "",
- "&7&l&nПрочность&7&l » &a&l" + health + "&7&l/&a&l" + type.getMaxHealth(),
- "",
- "&7- Если прочность будет равна &a0&7, то конструкция",
- "&aсломается",
- "",
- "&7&l&nЗадержка перед гниением&7&l » &a&l" + Utils.getTimeFormat(blockDecayTime),
- "",
- "&7- Задержка, в течение которого конструкция &aне &7гниет.",
- "&7Сбрасывается, если конструкция в &aзоне &7шкафа",
- "",
- "&7&l&nВремя гниения&7&l » &a&l" + Utils.getTimeFormat(decayTime),
- "",
- "&7- Время, по &aистечении &7которого конструкция потеряет",
- "&a1 &7единицу прочности"
- ).getStack()
- :
- ISBuilder.getBuilder(Material.RAW_CHICKEN, 0, 1, "&a&l» &e&lИнформация",
- "",
- "&7&l&nПрочность&7&l » &a&l" + health + "&7&l/&a&l" + type.getMaxHealth(),
- "",
- "&7- Если прочность будет равна &a0&7, то конструкция",
- "&aсломается"
- ).getStack()
- );
- inventory.setItem(11, health == type.getMaxHealth()
- ?
- ISBuilder.getBuilder(Material.COOKIE, 0, 1, "&a&l» &c&lКонструкция целая").getStack()
- :
- resourceAmount < repairCost
- ?
- ISBuilder.getBuilder(Material.COOKIE, 0, 1, "&a&l» &e&lОтремонтировать").addLore(
- "",
- "&7&l&nДля ремонта нужны ресурсы:",
- "",
- "&7- " + ChatColor.stripColor(type.getResourceStack().getItemMeta().getDisplayName().toLowerCase()) + " (" + (resourceAmount >= repairCost ? "&a" : "&c") + repairCost + "&7) - " + (resourceAmount < repairCost ? "не хватает - &c" + (repairCost - resourceAmount) : "у вас - &a" + resourceAmount)
- ).getStack()
- :
- ISBuilder.getBuilder(Material.COOKIE, 0, 1, "&a&l» &e&lОтремонтировать").addLore(
- "",
- "&7&l&nДля ремонта нужны ресурсы:",
- "",
- "&7- " + ChatColor.stripColor(type.getResourceStack().getItemMeta().getDisplayName().toLowerCase()) + " (" + (resourceAmount >= repairCost ? "&a" : "&c") + repairCost + "&7) - " + (resourceAmount < repairCost ? "не хватает - &c" + (repairCost - resourceAmount) : "у вас - &a" + resourceAmount)
- ).setTag("location", Utils.serialiseLocation(location)).setTag("repairCost", repairCost).getStack()
- );
- inventory.setItem(15, (type.getDrop() == null
- ?
- isDeleting
- ?
- ISBuilder.getBuilder(Material.RAW_BEEF, 0, 1, "&a&l» &c&lСломать")
- .addLore(
- "",
- "&7Вы уничтожите конструкцию и &cне &7получите &7ее назад"
- ).setTag("location", Utils.serialiseLocation(location))
- :
- ISBuilder.getBuilder(Material.RAW_BEEF, 0, 1, "&a&l» &c&lНельзя сломать",
- "",
- "&7Время удаления конструкции истекло"
- )
- :
- ISBuilder.getBuilder(Material.MELON, 0, 1, "&a&l» &e&lЗабрать")
- .addLore(
- "",
- "&7Вы уничтожите конструкцию и &aполучите &7ее назад"
- ).setTag("location", Utils.serialiseLocation(location))
- ).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 boolean isDeleting() {
- return isDeleting;
- }
- 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 setDeleting(boolean deleting) {
- isDeleting = deleting;
- }
- public void damage(int damage) {
- health = health - damage;
- if (health > 0) {
- SQLManager.getManager().updateBuildingData(this);
- return;
- }
- Bukkit.getPluginManager().callEvent(new RustBlockDecayEvent(location.getBlock(), type));
- if (type.isDoubleBlock()) {
- location.clone().add(0, 1, 0).getBlock().setType(Material.AIR, false);
- buildingsMap.remove(location.clone().add(0, 1, 0));
- }
- checkUppedBlocks(location.clone().add(0, 1, 0).getBlock().getLocation());
- breakBlock();
- }
- public void remove(Player player) {
- Bukkit.getPluginManager().callEvent(new RustBlockBreakEvent(player, type, new BlockBreakEvent(location.getBlock(), player)));
- if (type.isDoubleBlock()) {
- location.clone().add(0, 1, 0).getBlock().setType(Material.AIR, false);
- buildingsMap.remove(location.clone().add(0, 1, 0));
- }
- checkUppedBlocks(location.clone().add(0, 1, 0).getBlock().getLocation());
- breakBlock();
- }
- private void checkUppedBlocks(Location location) {
- CustomBlock upperBlock = CustomBlock.getBlock(location.getBlock());
- if (upperBlock == null) return;
- if (!upperBlock.getType().isGround()) return;
- Buildings building = fromLocation(location);
- if (building != null) {
- buildingsMap.remove(location);
- SQLManager.getManager().deleteBuildingData(building);
- }
- Bukkit.getPluginManager().callEvent(new RustPhysicBreakBlockEvent(location.getBlock(), upperBlock.getType()));
- checkUppedBlocks(location.clone().add(0, 1, 0));
- location.getBlock().setType(Material.AIR);
- 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);
- location.getWorld().playSound(location, Sounds.BREAK.getSoundName(), 1, 1);
- }
- private void breakBlock() {
- location.getBlock().setType(Material.AIR, false);
- buildingsMap.remove(location);
- SQLManager.getManager().deleteBuildingData(this);
- location.getWorld().playSound(location, Sounds.BREAK.getSoundName(), 1, 1);
- 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 runDecay() {
- new BukkitRunnable() {
- @Override
- public void run() {
- if (!buildingsMap.containsKey(location)) {
- cancel();
- return;
- }
- if (decayTime == -1) return;
- if (Cupboard.fromLocation(location) != null) {
- blockDecayTime = Config.BUILDINGS_BLOCK_DECAY_TIME.getInteger();
- return;
- }
- if (blockDecayTime != 0) {
- --blockDecayTime;
- return;
- }
- if (decayTime != 0) {
- --decayTime;
- return;
- }
- damage(1);
- decayTime = Config.BUILDINGS_DECAY_TIME.getInteger();
- }
- }.runTaskTimer(Main.getPlugin(), 1, 20);
- }
- public void runTimeDelete() {
- if (!type.isTimedDelete()) return;
- new BukkitRunnable() {
- @Override
- public void run() {
- if (!buildingsMap.containsKey(location)) {
- cancel();
- return;
- }
- if (!isDeleting) return;
- isDeleting = false;
- SQLManager.getManager().updateBuildingData(Buildings.this);
- }
- }.runTaskLater(Main.getPlugin(), Config.BUILDINGS_DELETE_TIME.getInteger() * 20);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement