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.Location;
- import org.bukkit.Material;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.HumanEntity;
- import org.bukkit.entity.LivingEntity;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.scheduler.BukkitRunnable;
- import ru.lisenochek.mcrust.Items;
- import ru.lisenochek.mcrust.Main;
- import ru.lisenochek.mcrust.objects.world.entity.CustomEntity;
- import ru.lisenochek.mcrust.sql.SQLManager;
- import ru.lisenochek.mcrust.utils.ISBuilder;
- import ru.lisenochek.mcrust.utils.Utils;
- import java.util.*;
- public class Campfire {
- private static HashMap<Location, Campfire> campfireMap = new HashMap<>();
- private static List<Integer> campfireSlots = Arrays.asList(10, 11, 12, 13, 14);
- private Location location;
- private boolean isBuring;
- private int taskID;
- private Inventory inventory;
- public Campfire(Location location) {
- this.location = location;
- this.inventory = Bukkit.createInventory(null, 27, Utils.stripColor("&2&lКостер"));
- Campfire.campfireMap.put(location, this);
- }
- public Campfire(Location location, String data) {
- this.location = location;
- this.inventory = Bukkit.createInventory(null, 27, Utils.stripColor("&2&lКостер"));
- for (String s : data.split(" ")) {
- String[] s1 = s.split("-");
- inventory.setItem(Integer.parseInt(s1[0]), ISBuilder.deserialiseStack(s1[1]));
- }
- Campfire.campfireMap.put(location, this);
- }
- public static Campfire create(Location location) {
- return new Campfire(location);
- }
- public static Campfire create(Location location, String data) {
- return new Campfire(location, data);
- }
- public static void save() {
- for (Campfire campfire : Campfire.campfireMap.values()) {
- campfire.stopBuring();
- SQLManager.getManager().updateCampfireData(campfire);
- }
- }
- public static List<Integer> getCampfireSlots() {
- return campfireSlots;
- }
- public static Campfire fromLocation(Location location) {
- return campfireMap.get(location);
- }
- public Location getLocation() {
- return location;
- }
- public boolean isBuring() {
- return isBuring;
- }
- public List<ItemStack> getContents() {
- List<ItemStack> stacks = new ArrayList<>();
- for (int slot : campfireSlots) {
- ItemStack stack = inventory.getItem(slot);
- if (stack != null) stacks.add(stack);
- }
- return stacks;
- }
- public void remove() {
- for (ItemStack stack : getContents()) location.getWorld().dropItemNaturally(location.clone().add(0.5D, 0, 0.5D), stack);
- stopBuring();
- Campfire.campfireMap.remove(location);
- SQLManager.getManager().deleteCampfireData(this);
- }
- public void openGUI(Player player) {
- inventory.setItem(16, (isBuring
- ?
- ISBuilder.getBuilder(Material.BOOK, 0, 1, "&a&l» &e&lКостер зажжен",
- "",
- "&7Нажмите, чтобы &aпотушить",
- "&7костер"
- )
- :
- ISBuilder.getBuilder(Material.BOOK, 0, 1, "&a&l» &e&lКостер потушен",
- "",
- "&7Нажмите, чтобы &aзажжечь",
- "&7костер"
- )).setTag("location", Utils.serialiseLocation(location)).getStack()
- );
- inventory.setItem(18, ISBuilder.getBuilder(Material.BOOK, 0, 1, "&a&l» &e&lРабота костра",
- "",
- "&7&l&nЕда для костра&7&l:",
- "",
- "&aПоложите &7в любые &a5 &7слотов &aсырую еду &7и &aдревесину &7в качестве",
- "&aтоплива&7. Еда готовится &aодновременно &7и появляется в &aсвободных",
- "&7слотах",
- "",
- "&7&l&nРабота костра&7&l:",
- "",
- "&7Нажмите по иконке &aсправа&7, чтобы &aзажжечь &7костер. Костер",
- "&aпотухнет&7, если в нем &aзакончится &7древесина или он будет &aпереполнен",
- "&7едой"
- ).getStack());
- for (int freeSlot = 0; freeSlot < inventory.getSize(); freeSlot++) if (!campfireSlots.contains(freeSlot) && inventory.getItem(freeSlot) == null) inventory.setItem(freeSlot, ISBuilder.getBuilder(Material.STAINED_GLASS_PANE, 0, 1, " ").getStack());
- player.openInventory(inventory);
- }
- public void startBuring() {
- if (isFull() || isEmpty()) return;
- isBuring = true;
- for (HumanEntity entity : new ArrayList<>(inventory.getViewers())) openGUI((Player) entity);
- BukkitRunnable runnable = new BukkitRunnable() {
- int milsec = 0;
- @Override
- public void run() {
- if (!campfireMap.containsKey(location)) {
- cancel();
- return;
- }
- if (isFull() || isEmpty()) {
- stopBuring();
- return;
- }
- milsec++;
- Utils.playEffect(location.clone().add(0.5D, 0.1, 0.5D), false, false, 0.1F, 0.1F, 0.1F, 0, EnumWrappers.Particle.FLAME, Material.AIR, 3);
- Utils.playEffect(location.clone().add(0.5D, 0.1, 0.5D), false, false, 0.1F, 0.1F, 0.1F, 0, EnumWrappers.Particle.SMOKE_NORMAL, Material.AIR, 1);
- if (milsec % 20 == 0) {
- for (Entity entity : location.getWorld().getNearbyEntities(location.clone().add(0.5D, 0, 0.5D), 0.5, 1, 0.5)) {
- CustomEntity customEntity = CustomEntity.getEntity(entity.getType());
- if (customEntity == null) continue;
- if (!customEntity.getType().isLiving()) continue;
- ((LivingEntity) entity).damage(2);
- }
- }
- if (milsec % (20 * 5) != 0) return;
- for (int slot : campfireSlots) {
- ItemStack item = inventory.getItem(slot);
- if (item == null) continue;
- if (Utils.checkItem(item, Items.WOOD.getStack())) {
- inventory.setItem(slot, ISBuilder.getBuilder(item).setAmount(item.getAmount() - 1).getStack());
- continue;
- }
- for (Food food : Food.values()) {
- if (!Utils.checkItem(item, food.getStack())) continue;
- inventory.setItem(slot, ISBuilder.getBuilder(item).setAmount(item.getAmount() - 1).getStack());
- Collection<ItemStack> dropList = inventory.addItem(food.getResult()).values();
- if (dropList.size() != 0) stopBuring();
- for (ItemStack drops : dropList) location.getWorld().dropItemNaturally(location.clone().add(0.5D, 1, 0.5D), drops);
- }
- }
- }
- };
- runnable.runTaskTimer(Main.plugin, 1, 1);
- taskID = runnable.getTaskId();
- }
- public void stopBuring() {
- isBuring = false;
- for (HumanEntity entity : new ArrayList<>(inventory.getViewers())) openGUI((Player) entity);
- Bukkit.getScheduler().cancelTask(taskID);
- }
- private boolean isFull() {
- boolean isFull = true;
- for (int slot : campfireSlots) {
- ItemStack stack = inventory.getItem(slot);
- if (stack == null) {
- isFull = false;
- break;
- }
- for (Food food : Food.values()) {
- if (!Utils.checkItem(food.getResult(), stack)) continue;
- if (stack.getAmount() == stack.getMaxStackSize()) continue;
- isFull = false;
- break;
- }
- }
- return isFull;
- }
- private boolean isEmpty() {
- boolean isEmpty = true;
- for (int slot : campfireSlots) {
- ItemStack stack = inventory.getItem(slot);
- if (stack == null) continue;
- if (!Utils.checkItem(Items.WOOD.getStack(), stack)) continue;
- isEmpty = false;
- break;
- }
- return isEmpty;
- }
- public String serialize() {
- if (getContents().size() == 0) return null;
- StringBuilder builder = new StringBuilder();
- for (int slot : campfireSlots) {
- ItemStack stack = inventory.getItem(slot);
- if (stack == null) continue;
- builder.append(slot).append("-").append(ISBuilder.serialise(stack)).append(" ");
- }
- return builder.toString();
- }
- public enum Food {
- RAW_MEAT(Items.RAW_MEAT.getStack(), Items.COOKED_MEAT.getStack()),
- RAW_FISH(Items.RAW_FISH.getStack(), Items.COOKED_FISH.getStack());
- private ItemStack stack;
- private ItemStack result;
- Food(ItemStack stack, ItemStack result) {
- this.stack = stack;
- this.result = result;
- }
- public ItemStack getStack() {
- return stack;
- }
- public ItemStack getResult() {
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement