Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package coolboy.turret;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.List;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.GameMode;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.Particle;
- import org.bukkit.Sound;
- import org.bukkit.block.Block;
- import org.bukkit.entity.ArmorStand;
- import org.bukkit.entity.Arrow;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.EntityType;
- import org.bukkit.entity.Player;
- import org.bukkit.entity.SpectralArrow;
- import org.bukkit.entity.TippedArrow;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.Action;
- import org.bukkit.event.inventory.InventoryClickEvent;
- import org.bukkit.event.player.PlayerArmorStandManipulateEvent;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemFlag;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.inventory.meta.PotionMeta;
- import org.bukkit.metadata.FixedMetadataValue;
- import org.bukkit.plugin.Plugin;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.potion.PotionData;
- import org.bukkit.potion.PotionEffect;
- import org.bukkit.potion.PotionEffectType;
- import org.bukkit.potion.PotionType;
- import org.bukkit.scheduler.BukkitRunnable;
- import org.bukkit.util.EulerAngle;
- import org.bukkit.util.Vector;
- public class Main extends JavaPlugin implements Listener {
- String preffix = ChatColor.GRAY + "[" + ChatColor.GREEN + "Turret" + ChatColor.GRAY + "]" + ChatColor.DARK_GREEN + " ";
- String team = "none";
- boolean teams = false;
- List<Entity> stands = new ArrayList<>();
- Plugin plugin = this;
- int liveTime = 20*60*5;
- @Override
- public void onEnable() {
- Bukkit.getPluginManager().registerEvents(this, this);
- Bukkit.getLogger().info(preffix + "Enabled.");
- }
- @Override
- public void onDisable() {
- for (Entity standy : stands) {
- standy.remove();
- }
- Bukkit.getLogger().info(preffix + "Disabled.");
- }
- @EventHandler
- public void interactEvent(PlayerInteractEvent event) {
- Player player = event.getPlayer();
- int nearEntity = 0;
- if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)
- && event.getClickedBlock().getType().equals(Material.STRUCTURE_BLOCK)
- && event.hasItem()
- && event.getItem().getType().equals(Material.IRON_BARDING)
- && event.getItem().getItemMeta().getLore().contains("Створює Турель")) {
- Block block = event.getClickedBlock();
- Location location = block.getLocation();
- Collection<Entity> near = block.getLocation().add(0, 1, 0).getWorld().getNearbyEntities(block.getLocation(), 2, 2, 2);
- for (Entity st : near) {
- if (st instanceof ArmorStand && st.getName().startsWith("Турель")) {
- nearEntity = 1;
- }
- }
- if (nearEntity == 0) {
- ArmorStand turret = (ArmorStand) location.getWorld().spawnEntity(location.add(0.5, 1, 0.5), EntityType.ARMOR_STAND);
- turret.getEquipment().setHelmet(new ItemStack(Material.DISPENSER, 1));
- turret.setSmall(true);
- turret.setInvulnerable(true);
- turret.setCustomName("Турель");
- turret.setGravity(false);
- turret.setCustomNameVisible(true);
- turret.setMetadata("turret", new FixedMetadataValue(this, true));
- turret.setMetadata("lived", new FixedMetadataValue(this, 0));
- turret.setMetadata("type", new FixedMetadataValue(this, "normal"));
- if (player.getScoreboard().getTeam("white").hasEntry(player.getName())) {
- player.getScoreboard().getTeam("white").addEntry(turret.getUniqueId().toString());
- team = "white";
- }
- if (player.getScoreboard().getTeam("black").hasEntry(player.getName())) {
- player.getScoreboard().getTeam("black").addEntry(turret.getUniqueId().toString());
- team = "black";
- }
- if ("none".equals(team)) {
- teams = true;
- }
- runTurret(turret, team, player);
- stands.add(turret);
- }
- }
- }
- public void runTurret(ArmorStand turret, String team, Player player) {
- new BukkitRunnable() {
- @Override
- public void run() {
- if (turret.isDead()) {
- stands.remove(turret);
- this.cancel();
- }
- Location rotate = turret.getLocation();
- rotate.setYaw(0);
- List<Entity> list = turret.getNearbyEntities(7, 5, 7);
- for (Entity mob : list) {
- if (mob instanceof Player) {
- Player playery = (Player) mob;
- if ( !mob.getName().equals(player.getName())
- && !playery.getGameMode().equals(GameMode.SPECTATOR)
- && !playery.getGameMode().equals(GameMode.CREATIVE)
- && !playery.getScoreboard().getTeam(team).hasEntry(playery.getName())
- ) {
- Vector vector = mob.getLocation().toVector().subtract(turret.getLocation().toVector());
- switch (turret.getMetadata("type").get(0).asString()) {
- case "fire":
- launchArrow(turret, vector).setFireTicks(5000);;
- break;
- case "slow":
- launchTippedArrow(turret, vector, new PotionEffect(PotionEffectType.SLOW, 200, 0));
- break;
- case "poison":
- launchTippedArrow(turret, vector, new PotionEffect(PotionEffectType.POISON, 20, 0));
- break;
- case "glow":
- turret.launchProjectile(SpectralArrow.class).setVelocity(vector);
- break;
- default:
- launchArrow(turret, vector);
- break;
- }
- double dX = turret.getLocation().getX() - mob.getLocation().getX();
- double dZ = turret.getLocation().getZ() - mob.getLocation().getZ();
- double yaw = Math.atan2(dZ, dX) + Math.PI / 2;
- turret.setHeadPose(new EulerAngle(0, yaw, 0));
- /*
- turret.teleport(new Location(turret.getLocation().getWorld(),
- turret.getLocation().getX(),
- turret.getLocation().getY(),
- turret.getLocation().getZ(),
- yaw, 0));*/
- break;
- }
- }
- }
- turret.setMetadata("lived", new FixedMetadataValue(plugin, turret.getMetadata("lived").get(0).asInt() + 40));
- turret.setCustomName("Турель "+Integer.toString((liveTime-turret.getMetadata("lived").get(0).asInt())/20)+"с");
- if (turret.getMetadata("lived").get(0).asInt() > liveTime) {
- turret.getLocation().getWorld().spawnParticle(Particle.EXPLOSION_NORMAL, turret.getLocation(), 300);
- turret.getLocation().getWorld().playSound(turret.getLocation(), Sound.ENTITY_BLAZE_HURT, 50, 0);
- turret.remove();
- stands.remove(turret);
- this.cancel();
- }
- }
- }.runTaskTimer(this, 0, 40);
- }
- @EventHandler
- public void stand(PlayerArmorStandManipulateEvent event) {
- Player player = event.getPlayer();
- if (event.getRightClicked().getName().equals("Турель")
- && event.getPlayer().getScoreboard().getTeam(team).hasEntry(player.getName())
- && event.getRightClicked().getMetadata("type").get(0).asString().equals("normal")){
- Inventory inv = Bukkit.createInventory(player, 9, ChatColor.RED + "Турель");
- event.getPlayer().setMetadata("turretEdit", new FixedMetadataValue(this, event.getRightClicked()));
- ItemStack fire = new ItemStack(Material.BLAZE_POWDER, 1);
- ItemMeta firem = fire.getItemMeta();
- firem.setDisplayName("Вогонь");
- firem.setLore(Arrays.asList("Створює запалені стріли", "Вартість: 10 рівнів"));
- fire.setItemMeta(firem);
- ItemStack slow = new ItemStack(Material.PACKED_ICE, 1);
- ItemMeta slowm = slow.getItemMeta();
- slowm.setDisplayName("Заповільнення");
- slowm.setLore(Arrays.asList("Створює заповільнюючі стріли", "Вартість: 10 рівнів"));
- slow.setItemMeta(slowm);
- ItemStack glow = new ItemStack(Material.SPECTRAL_ARROW, 1);
- ItemMeta glowm = glow.getItemMeta();
- glowm.setDisplayName("Підсвічування");
- glowm.setLore(Arrays.asList("Створює спектральні стріли", "Вартість: 20 рівнів"));
- glow.setItemMeta(glowm);
- ItemStack poison = new ItemStack(Material.POTION, 1);
- PotionMeta meta = (PotionMeta) poison.getItemMeta();
- meta.setBasePotionData(new PotionData(PotionType.POISON, false, false));
- meta.setDisplayName("Отрута");
- meta.setLore(Arrays.asList("Створює отруєні стріли", "Вартість: 15 рівнів"));
- meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
- poison.setItemMeta(meta);
- ItemStack exit = new ItemStack(Material.BARRIER, 1);
- ItemMeta exitm = exit.getItemMeta();
- exitm.setDisplayName("Вихід");
- exit.setItemMeta(exitm);
- inv.setItem(0, fire);
- inv.setItem(1, slow);
- inv.setItem(2, poison);
- inv.setItem(3, glow);
- inv.setItem(8, exit);
- player.openInventory(inv);
- }
- event.setCancelled(true);
- }
- @EventHandler
- public void upgrade(InventoryClickEvent event) {
- String message = ChatColor.RED + "У вас недостатньо досвіду";
- if (!event.getInventory().getName().equalsIgnoreCase(ChatColor.WHITE + "Турель")) return;
- event.setCancelled(true);
- Player player = (Player) event.getWhoClicked();
- if (event.getCurrentItem() == null || event.getCurrentItem().getType() == Material.BARRIER || !event.getCurrentItem().hasItemMeta()) {
- player.closeInventory();
- }
- ArmorStand stand = (ArmorStand) event.getWhoClicked().getMetadata("turretEdit").get(0).value();
- if (null != event.getCurrentItem()) {
- switch (event.getCurrentItem().getItemMeta().getDisplayName()) {
- case "Вогонь":
- if (player.getLevel() > 10) {
- player.setLevel(player.getLevel() - 10);
- stand.setMetadata("type", new FixedMetadataValue(this, "fire"));
- stand.setMetadata("lived", new FixedMetadataValue(this, 0));
- player.closeInventory();
- } else {
- player.sendMessage(message);
- }
- break;
- case "Отрута":
- if (player.getLevel() > 15) {
- player.setLevel(player.getLevel() - 15);
- stand.setMetadata("type", new FixedMetadataValue(this, "poison"));
- stand.setMetadata("lived", new FixedMetadataValue(this, 0));
- player.closeInventory();
- } else {
- player.sendMessage(message);
- }
- break;
- case "Заповільнення":
- if (player.getLevel() > 10) {
- player.setLevel(player.getLevel() - 15);
- stand.setMetadata("type", new FixedMetadataValue(this, "slow"));
- stand.setMetadata("lived", new FixedMetadataValue(this, 0));
- player.closeInventory();
- } else {
- player.sendMessage(message);
- }
- break;
- case "Підсвічування":
- if (player.getLevel() > 20) {
- player.setLevel(player.getLevel() - 15);
- stand.setMetadata("type", new FixedMetadataValue(this, "glow"));
- stand.setMetadata("lived", new FixedMetadataValue(this, 0));
- player.closeInventory();
- } else {
- player.sendMessage(message);
- }
- break;
- case "Вихід":
- player.closeInventory();
- break;
- }
- }
- }
- public Arrow launchArrow(ArmorStand stand, Vector vector) {
- Arrow arrow = stand.launchProjectile(Arrow.class);
- arrow.setVelocity(vector.multiply(0.4));
- arrow.setCritical(false);
- arrow.spigot().setDamage(2);
- return arrow;
- }
- public TippedArrow launchTippedArrow(ArmorStand stand, Vector vector, PotionEffect effect) {
- TippedArrow arrow = stand.launchProjectile(TippedArrow.class);
- arrow.setVelocity(vector.multiply(0.4));
- arrow.setCritical(false);
- arrow.spigot().setDamage(2);
- arrow.addCustomEffect(effect, true);
- return arrow;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement