BingoRufus

Artifact Forger

Mar 20th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.12 KB | None | 0 0
  1. package me.BingoRufus.ArtifactForge;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.UUID;
  8.  
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.Location;
  12. import org.bukkit.Material;
  13. import org.bukkit.Particle;
  14. import org.bukkit.Sound;
  15. import org.bukkit.block.Block;
  16. import org.bukkit.entity.Item;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.event.Listener;
  19. import org.bukkit.event.block.Action;
  20. import org.bukkit.event.entity.EntityPickupItemEvent;
  21. import org.bukkit.event.inventory.InventoryClickEvent;
  22. import org.bukkit.event.inventory.InventoryCloseEvent;
  23. import org.bukkit.event.player.PlayerInteractEvent;
  24. import org.bukkit.inventory.Inventory;
  25. import org.bukkit.inventory.ItemStack;
  26. import org.bukkit.inventory.meta.ItemMeta;
  27. import org.bukkit.event.EventHandler;
  28. import org.bukkit.plugin.Plugin;
  29. import org.bukkit.plugin.java.JavaPlugin;
  30. import org.bukkit.util.Vector;
  31.  
  32. //Item Slots are 10,11,12,19,20,21,28,29,30. With Output slot of  24,
  33. public class Main extends JavaPlugin implements Listener {
  34.     ItemStack JustAStick;
  35.     ItemStack InvalidRecipe;
  36.     int PickupMsgDelay = 5000;
  37.     List<HashMap<Integer, ItemStack>> Recipes = new ArrayList<HashMap<Integer, ItemStack>>();
  38.     Map<Player, Long> PickupSpammer = new HashMap<Player, Long>();
  39.     Map<UUID, Player> ItemOwner = new HashMap<UUID, Player>();
  40.     HashMap<Integer, ItemStack> CurrentRecipe = new HashMap<Integer, ItemStack>();
  41.     HashMap<Player, Boolean> InForgeMenu = new HashMap<Player, Boolean>();
  42.     HashMap<Player, Integer> id = new HashMap<Player, Integer>();
  43.     List<Integer> NotBlackTiles = new ArrayList<Integer>();
  44.     List<Integer> BlackTiles = new ArrayList<Integer>();
  45.     ItemStack item;
  46.     ItemMeta meta;
  47.     List<String> lore = new ArrayList<String>();
  48.     int TaskId;
  49.     public Inventory Forge;
  50.     Runnable CircleParticles;
  51.     public Plugin plugin;
  52.  
  53.     @Override
  54.     public void onEnable() {
  55.         plugin = this;
  56.         createStuff();
  57.         Bukkit.getPluginManager().registerEvents(this, this);
  58.  
  59.     }
  60.  
  61.     @Override
  62.     public void onDisable() {
  63.  
  64.     }
  65.  
  66.     @EventHandler()
  67.     public void Interact(PlayerInteractEvent e) {
  68.         Player p = e.getPlayer();
  69.         Block b = e.getClickedBlock();
  70.  
  71.         if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) {
  72.             if (e.getItem() != null) {
  73.                 ItemStack I = e.getItem();
  74.                 if (I.equals(JustAStick)) {
  75.                     p.sendMessage(ChatColor.YELLOW + "Well, what'd you think it'd do?");
  76.                 }
  77.                 if (I.equals(InvalidRecipe)) {
  78.                     e.setCancelled(true);
  79.                     p.sendMessage("Yoink");
  80.                     if (p.getInventory().getItemInOffHand() != null) {
  81.                         if (p.getInventory().getItemInOffHand().equals(InvalidRecipe)) {
  82.                             p.getInventory().setItemInOffHand(null);
  83.                             return;
  84.                         }
  85.                     }
  86.                     if (p.getInventory().getItemInMainHand() != null) {
  87.                         if (p.getInventory().getItemInMainHand().equals(InvalidRecipe)) {
  88.                             p.getInventory().setItemInMainHand(null);
  89.                             return;
  90.                         }
  91.                     }
  92.                 }
  93.             }
  94.  
  95.         }
  96.  
  97.         // Open Artifact ForgerVVV
  98.         if (b.getType() == Material.ANVIL) {
  99.             if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
  100.                 if (b.getLocation().subtract(0, 1, 0).getBlock().getType() == Material.MAGMA_BLOCK) {
  101.                     if (id.get(p) != null) {
  102.                         Bukkit.getScheduler().cancelTask(id.get(p));
  103.                         id.remove(p);
  104.                     }
  105.                     e.setCancelled(true);
  106.                     InForgeMenu.put(p, true);
  107.                     TaskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
  108.                         double a;
  109.  
  110.                         public void run() {
  111.                             if (id.get(p) == null) {
  112.                                 id.put(p, TaskId);
  113.                             }
  114.                             if (InForgeMenu.get(p) == null) {
  115.                                 Bukkit.getScheduler().cancelTask(id.get(p));
  116.                                 id.remove(p);
  117.                                 return;
  118.                             }
  119.                             a += Math.PI / 24;
  120.                             Location loc = e.getClickedBlock().getLocation().add(0.5, 0, 0.5);
  121.                             Location first = loc.clone()
  122.                                     .add(new Vector(Math.cos(a), 1.25, Math.sin(a)).divide(new Vector(1.5, 1, 1.5)));
  123.                             Location second = loc.clone()
  124.                                     .add(new Vector(Math.cos(a + Math.PI), 1.25, Math.sin(a + Math.PI))
  125.                                             .divide(new Vector(1.5, 1, 1.5)));
  126.                             p.getWorld().spawnParticle(Particle.FLAME, first, 0, 0, 0, 0);
  127.                             p.getWorld().spawnParticle(Particle.FLAME, second, 0, 0, 0, 0);
  128.                             p.getWorld().spawnParticle(Particle.ENCHANTMENT_TABLE, loc.clone().add(0, 2, 0), 1);
  129.  
  130.                         }
  131.  
  132.                     }, 0, 1);
  133.                     Forge = Bukkit.createInventory(null, 45,
  134.                             ChatColor.translateAlternateColorCodes('&', "&b&lArtifact Forge"));
  135.                     for (int i = 0; i < 45; i++) {
  136.                         item = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);
  137.                         meta = item.getItemMeta();
  138.                         meta.setDisplayName(" ");
  139.                         item.setItemMeta(meta);
  140.                         if (!NotBlackTiles.contains(i)) {
  141.                             BlackTiles.add(i);
  142.                             Forge.setItem(i, item);
  143.                         }
  144.                     }
  145.                     Forge.setItem(24, InvalidRecipe);
  146.                     p.openInventory(Forge);
  147.                     return;
  148.                 }
  149.             }
  150.         }
  151.  
  152.     }
  153.  
  154.     @EventHandler()
  155.     public void onClose(InventoryCloseEvent e) {
  156.         Inventory inv = e.getInventory();
  157.         Player p = (Player) e.getPlayer();
  158.         if (e.getView().getTitle().equals(ChatColor.AQUA + "" + ChatColor.BOLD + "Artifact Forge")) {
  159.             InForgeMenu.remove(p);
  160.             for (int i : NotBlackTiles) {
  161.                 if (i != 24) {
  162.                     giveItem(inv.getItem(i), p);
  163.                 }
  164.                 continue;
  165.             }
  166.             return;
  167.  
  168.         }
  169.  
  170.     }
  171.  
  172.     public void giveItem(ItemStack item, Player p) {
  173.         if (item != null) {
  174.             if (p.getInventory().firstEmpty() == -1) {
  175.                 Item DroppedItem = p.getWorld().dropItem(p.getLocation(), item);
  176.                 if (item.getItemMeta().hasDisplayName()) {
  177.                     p.sendMessage(
  178.                             ChatColor.DARK_RED + "" + ChatColor.BOLD + "Your " + item.getItemMeta().getDisplayName()
  179.                                     + " has been dropped on the ground because your inventory is full!");
  180.                     ItemOwner.put(DroppedItem.getUniqueId(), p);
  181.                     return;
  182.                 }
  183.  
  184.                 p.sendMessage(ChatColor.DARK_RED + "" + ChatColor.BOLD + "Your " + item.getType().name()
  185.                         + " has been dropped on the ground because your inventory is full!");
  186.                 ItemOwner.put(DroppedItem.getUniqueId(), p);
  187.                 return;
  188.             }
  189.             p.getInventory().addItem(item);
  190.             return;
  191.         }
  192.     }
  193.  
  194.     @EventHandler()
  195.     public void onItemPickup(EntityPickupItemEvent e) {
  196.         if (!(e.getEntity() instanceof Player)) {
  197.             e.setCancelled(true);
  198.             return;
  199.         }
  200.         Player p = (Player) e.getEntity();
  201.         if (ItemOwner.get(e.getItem().getUniqueId()) != null) {
  202.             Player Owner = (Player) ItemOwner.get(e.getItem().getUniqueId());
  203.             if (Owner.equals(p))
  204.                 return;
  205.             if (p.hasPermission("ArtifactForger.pickupoverride")) {
  206.                 p.sendMessage(ChatColor.GREEN + "You picked up " + Owner.getDisplayName() + ChatColor.GREEN + "'s "
  207.                         + e.getItem().getName());
  208.                 return;
  209.             }
  210.             if (PickupSpammer.get(p) == null || System.currentTimeMillis() - PickupSpammer.get(p) >= PickupMsgDelay) {
  211.                 p.sendMessage(ChatColor.RED + "Only " + Owner.getName() + " can pick this item up");
  212.                 PickupSpammer.put(p, System.currentTimeMillis());
  213.             }
  214.             e.setCancelled(true);
  215.             return;
  216.  
  217.         }
  218.         return;
  219.     }
  220.  
  221.     @EventHandler()
  222.     public void onClick(InventoryClickEvent e) {
  223.         // Item Slots are 10,11,12,19,20,21,28,29,30. With Output slot of 24
  224.         Player p = (Player) e.getWhoClicked();
  225.         if (e.getInventory().equals(Forge)) {
  226.             Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
  227.                 public void run() {
  228.                     ItemStack Slot1 = e.getInventory().getItem(10);
  229.                     ItemStack Slot2 = e.getInventory().getItem(11);
  230.                     ItemStack Slot3 = e.getInventory().getItem(12);
  231.                     ItemStack Slot4 = e.getInventory().getItem(19);
  232.                     ItemStack Slot5 = e.getInventory().getItem(20);
  233.                     ItemStack Slot6 = e.getInventory().getItem(21);
  234.                     ItemStack Slot7 = e.getInventory().getItem(28);
  235.                     ItemStack Slot8 = e.getInventory().getItem(29);
  236.                     ItemStack Slot9 = e.getInventory().getItem(30);
  237.                     CurrentRecipe.put(1, Slot1);
  238.                     CurrentRecipe.put(2, Slot2);
  239.                     CurrentRecipe.put(3, Slot3);
  240.                     CurrentRecipe.put(4, Slot4);
  241.                     CurrentRecipe.put(5, Slot5);
  242.                     CurrentRecipe.put(6, Slot6);
  243.                     CurrentRecipe.put(7, Slot7);
  244.                     CurrentRecipe.put(8, Slot8);
  245.                     CurrentRecipe.put(9, Slot9);
  246.                 }
  247.             }, 1L);
  248.             if (e.getInventory().getItem(24) != InvalidRecipe) {
  249.                 e.getInventory().setItem(24, InvalidRecipe);
  250.             }
  251.             Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
  252.                 public void run() {
  253.  
  254.                     for (int i = 0; i < Recipes.size(); i++) {
  255.                         HashMap<Integer, ItemStack> Recipe = (HashMap<Integer, ItemStack>) Recipes.get(i);
  256.                         int matches = 0;
  257.                         for (i = 1; i <= 9; i++) {
  258.                             if (Recipe.get(i) == null) {
  259.                                 if (CurrentRecipe.get(i) == null) {
  260.                                     matches++;
  261.                                     continue;
  262.                                 }
  263.                                 break;
  264.                             }
  265.                             if (CurrentRecipe.get(i) == null)
  266.                                 break;
  267.  
  268.                             if (!(Recipe.get(i).equals(CurrentRecipe.get(i))))
  269.                                 break;
  270.                             matches++;
  271.  
  272.                         }
  273.                         if (matches == 9) {
  274.                             e.getInventory().setItem(24, Recipe.get(10));
  275.                             e.setCancelled(false);
  276.                             break;
  277.                         }
  278.                     }
  279.  
  280.                     e.setCancelled(false);
  281.  
  282.                 }
  283.             }, 2L);
  284.             p.updateInventory();
  285.         }
  286.         if (e.getClickedInventory().equals(Forge)) {
  287.  
  288.             if (BlackTiles.contains(e.getSlot())) {
  289.                 e.setCancelled(true);
  290.                 return;
  291.             }
  292.             if (e.getSlot() == 24) {
  293.                 if (e.getCurrentItem().equals(InvalidRecipe)) {
  294.                     e.setCancelled(true);
  295.                     return;
  296.                 }
  297.  
  298.                 for (int i : NotBlackTiles) {
  299.                     if (i != 24) {
  300.                         e.getInventory().setItem(i, null);
  301.                     }
  302.                 }
  303.                 p.getWorld().playSound(p.getLocation(), Sound.BLOCK_ENCHANTMENT_TABLE_USE, 10, (float) 0.5);
  304.  
  305.                 return;
  306.             }
  307.  
  308.             return;
  309.         }
  310.  
  311.     }
  312.  
  313.     @SuppressWarnings("serial")
  314.     public void createStick() {
  315.         JustAStick = new ItemStack(Material.STICK);
  316.         meta = JustAStick.getItemMeta();
  317.         meta.setDisplayName(ChatColor.GREEN + "Just a Stick");
  318.         lore.clear();
  319.         lore.add(ChatColor.YELLOW + "Does nothing");
  320.         meta.setLore(lore);
  321.         JustAStick.setItemMeta(meta);
  322.  
  323.         HashMap<Integer, ItemStack> StickRecipe1 = new HashMap<Integer, ItemStack>() {
  324.  
  325.             {
  326.                 item = new ItemStack(Material.STICK);
  327.                 item.setItemMeta(null);
  328.                 put(1, item);
  329.                 put(2, null);
  330.                 put(3, null);
  331.                 put(4, null);
  332.                 put(5, null);
  333.                 put(6, null);
  334.                 put(7, null);
  335.                 put(8, null);
  336.                 put(9, null);
  337.  
  338.                 put(10, JustAStick);
  339.             }
  340.         };
  341.         Recipes.add(StickRecipe1);
  342.         HashMap<Integer, ItemStack> StickRecipe2 = new HashMap<Integer, ItemStack>() {
  343.  
  344.             {
  345.                 item = new ItemStack(Material.STICK);
  346.                 item.setItemMeta(null);
  347.                 put(2, item);
  348.                 put(1, null);
  349.                 put(3, null);
  350.                 put(4, null);
  351.                 put(5, null);
  352.                 put(6, null);
  353.                 put(7, null);
  354.                 put(8, null);
  355.                 put(9, null);
  356.  
  357.                 put(10, JustAStick);
  358.             }
  359.         };
  360.         Recipes.add(StickRecipe2);
  361.         HashMap<Integer, ItemStack> StickRecipe3 = new HashMap<Integer, ItemStack>() {
  362.  
  363.             {
  364.                 item = new ItemStack(Material.STICK);
  365.                 item.setItemMeta(null);
  366.                 put(3, item);
  367.                 put(2, null);
  368.                 put(1, null);
  369.                 put(4, null);
  370.                 put(5, null);
  371.                 put(6, null);
  372.                 put(7, null);
  373.                 put(8, null);
  374.                 put(9, null);
  375.  
  376.                 put(10, JustAStick);
  377.             }
  378.         };
  379.         Recipes.add(StickRecipe3);
  380.         HashMap<Integer, ItemStack> StickRecipe4 = new HashMap<Integer, ItemStack>() {
  381.             private static final long serialVersionUID = -8987080176816314175L;
  382.  
  383.             {
  384.                 item = new ItemStack(Material.STICK);
  385.                 item.setItemMeta(null);
  386.                 put(4, item);
  387.                 put(2, null);
  388.                 put(3, null);
  389.                 put(1, null);
  390.                 put(5, null);
  391.                 put(6, null);
  392.                 put(7, null);
  393.                 put(8, null);
  394.                 put(9, null);
  395.  
  396.                 put(10, JustAStick);
  397.             }
  398.         };
  399.         Recipes.add(StickRecipe4);
  400.         HashMap<Integer, ItemStack> StickRecipe5 = new HashMap<Integer, ItemStack>() {
  401.             private static final long serialVersionUID = -8987080176816314175L;
  402.  
  403.             {
  404.                 item = new ItemStack(Material.STICK);
  405.                 item.setItemMeta(null);
  406.                 put(5, item);
  407.                 put(2, null);
  408.                 put(3, null);
  409.                 put(1, null);
  410.                 put(4, null);
  411.                 put(6, null);
  412.                 put(7, null);
  413.                 put(8, null);
  414.                 put(9, null);
  415.  
  416.                 put(10, JustAStick);
  417.             }
  418.         };
  419.         Recipes.add(StickRecipe5);
  420.         HashMap<Integer, ItemStack> StickRecipe6 = new HashMap<Integer, ItemStack>() {
  421.  
  422.             {
  423.                 item = new ItemStack(Material.STICK);
  424.                 item.setItemMeta(null);
  425.                 put(6, item);
  426.                 put(2, null);
  427.                 put(3, null);
  428.                 put(4, null);
  429.                 put(5, null);
  430.                 put(1, null);
  431.                 put(7, null);
  432.                 put(8, null);
  433.                 put(9, null);
  434.  
  435.                 put(10, JustAStick);
  436.             }
  437.         };
  438.         Recipes.add(StickRecipe6);
  439.  
  440.         HashMap<Integer, ItemStack> StickRecipe7 = new HashMap<Integer, ItemStack>() {
  441.  
  442.             {
  443.                 item = new ItemStack(Material.STICK);
  444.                 item.setItemMeta(null);
  445.                 put(7, item);
  446.                 put(2, null);
  447.                 put(3, null);
  448.                 put(4, null);
  449.                 put(5, null);
  450.                 put(6, null);
  451.                 put(1, null);
  452.                 put(8, null);
  453.                 put(9, null);
  454.  
  455.                 put(10, JustAStick);
  456.             }
  457.         };
  458.         Recipes.add(StickRecipe7);
  459.         HashMap<Integer, ItemStack> StickRecipe8 = new HashMap<Integer, ItemStack>() {
  460.  
  461.             {
  462.                 item = new ItemStack(Material.STICK);
  463.                 item.setItemMeta(null);
  464.                 put(8, item);
  465.                 put(2, null);
  466.                 put(3, null);
  467.                 put(4, null);
  468.                 put(5, null);
  469.                 put(6, null);
  470.                 put(7, null);
  471.                 put(1, null);
  472.                 put(9, null);
  473.  
  474.                 put(10, JustAStick);
  475.             }
  476.         };
  477.         Recipes.add(StickRecipe8);
  478.         HashMap<Integer, ItemStack> StickRecipe9 = new HashMap<Integer, ItemStack>() {
  479.  
  480.             {
  481.                 item = new ItemStack(Material.STICK);
  482.                 item.setItemMeta(null);
  483.                 put(1, item);
  484.                 put(2, null);
  485.                 put(3, null);
  486.                 put(4, null);
  487.                 put(5, null);
  488.                 put(6, null);
  489.                 put(7, null);
  490.                 put(8, null);
  491.                 put(9, null);
  492.  
  493.                 put(10, JustAStick);
  494.             }
  495.         };
  496.         Recipes.add(StickRecipe9);
  497.  
  498.     }
  499.  
  500.     public void createInvalidRecipe() {
  501.         InvalidRecipe = new ItemStack(Material.BARRIER);
  502.         meta = InvalidRecipe.getItemMeta();
  503.         meta.setDisplayName(ChatColor.RED + "Invalid Recipe");
  504.         InvalidRecipe.setItemMeta(meta);
  505.     }
  506.  
  507.     public void createStuff() {
  508.         createStick();
  509.         createInvalidRecipe();
  510.         NotBlackTiles.add(10);
  511.         NotBlackTiles.add(11);
  512.         NotBlackTiles.add(12);
  513.         NotBlackTiles.add(19);
  514.         NotBlackTiles.add(20);
  515.         NotBlackTiles.add(21);
  516.         NotBlackTiles.add(24);
  517.         NotBlackTiles.add(28);
  518.         NotBlackTiles.add(29);
  519.         NotBlackTiles.add(30);
  520.     }
  521. }
Advertisement
Add Comment
Please, Sign In to add comment