Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.34 KB | None | 0 0
  1. package me.knayder.StoneG;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.Location;
  5. import org.bukkit.Material;
  6. import org.bukkit.NamespacedKey;
  7. import org.bukkit.block.Block;
  8. import org.bukkit.block.Dispenser;
  9. import org.bukkit.block.data.Directional;
  10. import org.bukkit.enchantments.Enchantment;
  11. import org.bukkit.event.EventHandler;
  12. import org.bukkit.event.Listener;
  13. import org.bukkit.event.block.BlockDispenseEvent;
  14. import org.bukkit.inventory.ItemFlag;
  15. import org.bukkit.inventory.ItemStack;
  16. import org.bukkit.inventory.ShapedRecipe;
  17. import org.bukkit.inventory.meta.ItemMeta;
  18. import org.bukkit.plugin.Plugin;
  19.  
  20. import java.lang.reflect.Array;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25.  
  26. public class CustomGenerator implements Listener {
  27.  
  28.     @EventHandler
  29.     public void onBlockDispenseEvent(BlockDispenseEvent event) {
  30.         Block block = event.getBlock();
  31.         if ( ! block.getType().equals(Material.DISPENSER))
  32.             return;
  33.  
  34.         ItemStack item = event.getItem();
  35.         if ( ! item.getType().equals(this.material))
  36.             return;
  37.  
  38.         if ( ! item.getItemMeta().getDisplayName().equals( this.name ) )
  39.             return;
  40.  
  41.         Dispenser dispenser = (Dispenser) event.getBlock().getState();
  42.  
  43.         Bukkit.getScheduler().runTaskLater(StoneG.get(), new Runnable() {
  44.             @Override
  45.             public void run() {
  46.                 for (ItemStack it : dispenser.getInventory().getContents()) {
  47.                     if(it == null)
  48.                         continue;
  49.                     if ( ! it.equals(item))
  50.                         continue;
  51.                     Directional directional = (Directional) dispenser.getBlockData();
  52.                     Location loc = block.getLocation();
  53.                     loc.add(directional.getFacing().getDirection());
  54.  
  55.                     placeBlock(it, loc);
  56.  
  57.                     /*
  58.                     ItemMeta itemMeta = it.getItemMeta();
  59.                     int delta = 1337;
  60.                     long currentTime = System.currentTimeMillis();
  61.                     if (itemMeta.hasCustomModelData()) {
  62.                         delta = (int) (currentTime - itemMeta.getCustomModelData());
  63.                     }
  64.                     if (delta >= 1000) {
  65.                         if (loc.getBlock().getType().equals(Material.AIR)) {
  66.                             loc.getBlock().setType(Material.COBBLESTONE);
  67.                             itemMeta.setCustomModelData((int) currentTime);
  68.                             it.setItemMeta(itemMeta);
  69.                         }
  70.                     }
  71.                     */
  72.                     break;
  73.                 }
  74.             }
  75.         }, 1L);
  76.         event.setCancelled(true);
  77.     }
  78.  
  79.     private static String removeLetters(String s) {
  80.         s = s.replaceAll("[^\\d.]", "");
  81.         return s;
  82.     }
  83.  
  84.     private void placeBlock(ItemStack item, Location loc) {
  85.         ItemMeta meta = item.getItemMeta();
  86.  
  87.         if( ! meta.hasLore())
  88.             return;
  89.  
  90.         List<String> lore = meta.getLore();
  91.  
  92.         if( ! (lore.size() > LAST_INDEX))
  93.             return;
  94.  
  95.         String s = removeLetters(lore.get(USES_INDEX));
  96.         int uses = s.equals("") ? -1 : Integer.parseInt(s);
  97.  
  98.         if( uses == 0 )
  99.             return;
  100.  
  101.         int delay = Integer.parseInt(removeLetters(lore.get(DELAY_INDEX)));
  102.         int last = Integer.parseInt(removeLetters(lore.get(LAST_INDEX)));
  103.         int current = (int) System.currentTimeMillis();
  104.         if (current - last >= delay) {
  105.             if (loc.getBlock().getType().equals(Material.AIR)) {
  106.                 loc.getBlock().setType(Material.COBBLESTONE);
  107.  
  108.                 lore.set(LAST_INDEX, "Last: " + String.valueOf(current));
  109.                 if (uses > 0)
  110.                     lore.set(USES_INDEX, "Ilosc uzyc: " + String.valueOf(uses - 1));
  111.                 meta.setLore(lore);
  112.             }
  113.         }
  114.         item.setItemMeta(meta);
  115.  
  116.     }
  117.  
  118.  
  119.  
  120.  
  121.     /*
  122.     ////////////////////////////
  123.     RECIPE PART BELOW
  124.     ////////////////////////////
  125.      */
  126.     private Plugin plugin;
  127.  
  128.     private Map<Character, Material> ingredients = new HashMap<>();
  129.     private String a=null, b=null, c=null;
  130.     private String name = null;
  131.     private NamespacedKey namespacedKey = null;
  132.     private Material material = null;
  133.  
  134.     private Boolean generated = false;
  135.  
  136.     private int uses = -1;
  137.     private int delay = 0;
  138.  
  139.     private static int USES_INDEX = 0;
  140.     private static int DELAY_INDEX = 1;
  141.     private static int LAST_INDEX = 2;
  142.  
  143.     private Boolean warning() {
  144.         if (generated) {
  145.             System.out.println("Cannot change generated ");
  146.             return true;
  147.         }
  148.         return false;
  149.     }
  150.  
  151.     public static CustomGenerator create(Plugin plugin) {
  152.         CustomGenerator customGenerator = new CustomGenerator();
  153.         customGenerator.plugin = plugin;
  154.         return customGenerator;
  155.     }
  156.  
  157.     public CustomGenerator setNamespacedKey(String key) {
  158.         if( warning() ) return this;
  159.  
  160.         namespacedKey = new NamespacedKey(plugin, key);
  161.  
  162.         return this;
  163.     }
  164.  
  165.     public CustomGenerator setItemName(String name) {
  166.         if( warning() ) return this;
  167.  
  168.         this.name = name;
  169.  
  170.         return this;
  171.     }
  172.  
  173.     public CustomGenerator setRecipeShape(String a, String b, String c) {
  174.         if( warning() ) return this;
  175.  
  176.         this.a = a;
  177.         this.b = b;
  178.         this.c = c;
  179.  
  180.         return this;
  181.     }
  182.  
  183.     public CustomGenerator setIngredient(char key, Material material) {
  184.         if( warning() ) return this;
  185.  
  186.         ingredients.put(key, material);
  187.  
  188.         return this;
  189.     }
  190.  
  191.     public CustomGenerator setItem(Material type) {
  192.         if( warning() ) return this;
  193.  
  194.         material = type;
  195.  
  196.         return this;
  197.     }
  198.  
  199.     public CustomGenerator setUses(int uses) {
  200.         if( warning() ) return this;
  201.  
  202.         this.uses = uses;
  203.  
  204.         return this;
  205.     }
  206.  
  207.     public CustomGenerator setDelay(int delay) {
  208.         if( warning() ) return this;
  209.  
  210.         this.delay = delay;
  211.  
  212.         return this;
  213.     }
  214.  
  215.     public CustomGenerator generate() {
  216.         generated = true;
  217.         if(name != null && namespacedKey != null && material != null && a != null && b != null && c != null) {
  218.             ItemStack itemStack = new ItemStack(material, 1);
  219.  
  220.             ItemMeta meta = itemStack.getItemMeta();
  221.             meta.setDisplayName(name);
  222.             meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
  223.             meta.addEnchant(Enchantment.DURABILITY, 1, true);
  224.  
  225.             ArrayList<String> lore = new ArrayList<String>();
  226.             lore.add("Ilosc uzyc: " + ((uses >= 0)?String.valueOf(uses):"inf") );
  227.             lore.add("Czas regeneracji: " + String.valueOf(delay) + "ms");
  228.             lore.add("Last: 0");
  229.             meta.setLore(lore);
  230.  
  231.  
  232.             itemStack.setItemMeta(meta);
  233.  
  234.             ShapedRecipe recipe = new ShapedRecipe(namespacedKey, itemStack);
  235.             recipe.shape(a, b, c);
  236.             for (Map.Entry<Character, Material> entry : ingredients.entrySet()) {
  237.                 recipe.setIngredient(entry.getKey(), entry.getValue());
  238.             }
  239.             plugin.getServer().addRecipe(recipe);
  240.         }
  241.         plugin.getServer().getPluginManager().registerEvents(this, plugin);
  242.         return this;
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement