Guest User

MiniBlock (1.3.5)

a guest
Dec 28th, 2015
932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.20 KB | None | 0 0
  1. package libs;
  2.  
  3. import org.bukkit.*;
  4. import org.bukkit.entity.*;
  5. import org.bukkit.inventory.*;
  6. import org.bukkit.util.*;
  7.  
  8. public class MiniBlock {
  9.    
  10.     /**
  11.      * MiniBlock
  12.      *
  13.      * @version 1.3.5
  14.      * @author _Ug
  15.      *
  16.      * No credit is needed. ;)
  17.      */
  18.    
  19.     private Location location;
  20.    
  21.     private ItemStack item;
  22.    
  23.     private int size;
  24.        
  25.     private ArmorStand stand;
  26.    
  27.     private boolean isRemoved;
  28.    
  29.     /**
  30.      * Create a MiniBlock
  31.      *
  32.      * @param location
  33.      *            Location to spawn MiniBlock at
  34.      *            
  35.      * @param item
  36.      *            Item for the MiniBlock
  37.      *            
  38.      * @param size
  39.      *            Size of MiniBlock (1-3 for blocks, 1-2 for items)
  40.      */
  41.     public MiniBlock(Location location, ItemStack item, int size){
  42.                
  43.         this.location = location;
  44.        
  45.         this.item = item;
  46.        
  47.         if(!isSizeValid (item, size))size = 2;
  48.        
  49.         this.size = size;
  50.        
  51.         this.isRemoved = false;
  52.        
  53.         spawnMiniBlock(location, item, size);
  54.        
  55.     }
  56.    
  57.     /**
  58.      * Teleport MiniBlock
  59.      *
  60.      * @param location
  61.      *          Location to teleport to
  62.      */
  63.     public void teleport(Location location){
  64.        
  65.         this.location = location;
  66.                
  67.         if(!this.isRemoved){
  68.            
  69.             stand.remove();
  70.            
  71.             spawnMiniBlock(this.location, this.item, this.size);
  72.            
  73.         }
  74.        
  75.     }
  76.    
  77.     /**
  78.      * Set MiniBlock item
  79.      *
  80.      * @param item
  81.      *          Item to insert to MiniBlock
  82.      */
  83.     public void setItem(ItemStack item){
  84.        
  85.         this.item = item;
  86.        
  87.         stand.setItemInHand(item);
  88.        
  89.     }
  90.    
  91.     /**
  92.      * Set MiniBlock size
  93.      *
  94.      * @param size
  95.      *          Size of MiniBlock (1-3 for blocks, 1-2 for items)
  96.      */
  97.     public void setSize(int size){
  98.        
  99.         this.size = size;
  100.        
  101.         if(!this.isRemoved){
  102.            
  103.             stand.remove();
  104.            
  105.             spawnMiniBlock(this.location, this.item, this.size);
  106.            
  107.         }
  108.        
  109.     }
  110.    
  111.     /**
  112.      * Remove MiniBlock
  113.      */
  114.     public void remove(){
  115.                
  116.         if(!this.isRemoved){
  117.            
  118.             stand.remove();
  119.            
  120.             this.isRemoved = true;
  121.                    
  122.         }
  123.        
  124.     }
  125.    
  126.     /*
  127.      * Getters
  128.      */
  129.    
  130.     /**
  131.      * Get MiniBlock location
  132.      *
  133.      * @return MiniBlock location
  134.      *
  135.      */
  136.     public Location getLocation(){
  137.         return this.location;
  138.     }
  139.    
  140.     /**
  141.      * Get MiniBlock item
  142.      *
  143.      * @return MiniBlock item
  144.      *
  145.      */
  146.     public ItemStack getItem(){
  147.         return this.item;
  148.     }
  149.    
  150.     /**
  151.      * Get MiniBlock size
  152.      *
  153.      * @return MiniBlock size
  154.      *
  155.      */
  156.     public int getSize(){
  157.         return this.size;
  158.     }
  159.    
  160.     /**
  161.      * Get removed status
  162.      *
  163.      * @return true if MiniBlock is removed
  164.      *
  165.      */
  166.     public boolean isRemoved(){
  167.         return this.isRemoved;
  168.     }
  169.    
  170.     /*
  171.      * Private
  172.      */
  173.    
  174.     private void spawnMiniBlock(Location location, ItemStack item, int size){
  175.        
  176.         if(!isSizeValid(item, size))return;
  177.        
  178.         Location l = location;
  179.        
  180.         boolean isBlock = item.getType().isBlock();
  181.        
  182.         EulerAngle angle = new EulerAngle(0,0,0);
  183.        
  184.         boolean small = false;
  185.        
  186.         if(isBlock){
  187.             if(size == 1){
  188.                 l.add(.2, -.445, 0);
  189.                 l.setPitch(45);
  190.                 angle = new EulerAngle(-.6,0,0);
  191.                 small = true;
  192.             }else if(size == 2){
  193.                 l.add(.6, -.65, 0);
  194.                 l.setPitch(45);
  195.                 angle = new EulerAngle(-.25,0,0);
  196.             }else if(size == 3){
  197.                 l.add(0, -.7, 0);
  198.             }
  199.         }else{
  200.             if(size == 1){
  201.                 l.add(.2, -.5, -.1);
  202.                 angle = new EulerAngle(-.35,0,0);
  203.                 small = true;
  204.             }else if(size == 2){
  205.                 l.add(.7, -.8, -.1);
  206.                 angle = new EulerAngle(0,1,0);
  207.             }
  208.         }
  209.        
  210.         ArmorStand stand = (ArmorStand) l.getWorld().spawnEntity(l, EntityType.ARMOR_STAND);
  211.        
  212.         fixup(stand);
  213.        
  214.         stand.setSmall(small);
  215.        
  216.         stand.setRightArmPose(angle);
  217.        
  218.         stand.setItemInHand(item);
  219.        
  220.         this.stand = stand;
  221.  
  222.     }
  223.    
  224.     private boolean isSizeValid(ItemStack item, int size){
  225.         if(item.getType().isBlock()){
  226.             if(size == 1 || size == 2 || size == 3){
  227.                 return true;
  228.             }else{
  229.                 return false;
  230.             }
  231.         }else{
  232.             if(size == 1 || size == 2){
  233.                 return true;
  234.             }else{
  235.                 return false;
  236.             }
  237.         }
  238.     }
  239.    
  240.     private void fixup(ArmorStand stand){
  241.         stand.setVisible(false);
  242.         stand.setArms(true);
  243.         stand.setGravity(false);
  244.         stand.setCanPickupItems(false);
  245.     }
  246.        
  247. }
Advertisement
Add Comment
Please, Sign In to add comment