Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.49 KB | None | 0 0
  1. package me.torrent.Teleporters;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Random;
  8. import java.util.logging.Logger;
  9.  
  10. import org.bukkit.Effect;
  11. import org.bukkit.Location;
  12. import org.bukkit.Material;
  13. import org.bukkit.World;
  14. import org.bukkit.block.Block;
  15. import org.bukkit.block.BlockState;
  16. import org.bukkit.block.Chest;
  17. import org.bukkit.block.Furnace;
  18. import org.bukkit.entity.Player;
  19. import org.bukkit.event.block.Action;
  20. import org.bukkit.event.player.PlayerInteractEvent;
  21. import org.bukkit.event.player.PlayerListener;
  22. import org.bukkit.inventory.Inventory;
  23. import org.bukkit.inventory.ItemStack;
  24.  
  25. public class TeleportersPlayerListenerExtended extends PlayerListener
  26. {
  27.     /* Initialize logger */
  28.     public final Logger logger = Logger.getLogger( "Minecraft" );
  29.    
  30.     /* Create HasMap for teleporters */
  31.     public final HashMap<Block, Teleporter> Teleporters = new HashMap<Block, Teleporter>();
  32.    
  33.     /* Obvious */
  34.     public static Teleporters plugin;
  35.    
  36.     /* Start the class */
  37.     public TeleportersPlayerListenerExtended( Teleporters instance )
  38.     {
  39.         plugin = instance;
  40.     }
  41.    
  42.     /* When player tries to interact with blocks, this event is fired, this is good event for handling teleporter activities */
  43.     public void onPlayerInteract( PlayerInteractEvent event )
  44.     {
  45.         if( event.getAction() == Action.RIGHT_CLICK_BLOCK )
  46.         {
  47.             Player player = event.getPlayer();
  48.             Block block = event.getClickedBlock();
  49.             ItemStack itemInHand = player.getItemInHand();
  50.            
  51.             if( block.getType() == Material.OBSIDIAN )
  52.             {
  53.                 if( isTeleporter( block ) )
  54.                 {          
  55.                     tryTeleport( block, player );
  56.                 }
  57.                 else
  58.                 {
  59.                     /*
  60.                         If player is holding redstone ore, he can "debug" the teleporter
  61.                         It's good because then your chat won't be spammed with text each
  62.                         time you mess with obsidian blocks.
  63.                     */
  64.                     boolean validTeleporter = false;
  65.                    
  66.                     boolean debug = false;
  67.                     if( itemInHand.getType() != Material.REDSTONE_ORE )
  68.                         debug = true;
  69.                     else
  70.                         debug = false;
  71.                    
  72.                     List<String> isValidReturn = isValidTeleporter( block );
  73.                    
  74.                     if( isValidReturn.get( 0 ) != "isValid" )
  75.                     {
  76.                         validTeleporter = false;
  77.                         if( debug )
  78.                         {
  79.                             for( int i = 0; i < isValidReturn.size(); i++ )
  80.                             {
  81.                                 player.sendMessage( String.valueOf( i + 1 ) + " " + isValidReturn.get( i ) );
  82.                             }
  83.                         }
  84.                     }
  85.                     else
  86.                         validTeleporter = true;
  87.                    
  88.                     if( validTeleporter )
  89.                     {
  90.                         Teleporter teleporterInfo = new Teleporter();
  91.                        
  92.                         teleporterInfo.creator = player;
  93.                         teleporterInfo.chest = GetChest( block );
  94.                         teleporterInfo.furnace = GetFurnace( block );
  95.                         teleporterInfo.date = new Date();
  96.                        
  97.                         Teleporters.put( block, teleporterInfo );
  98.                        
  99.                         player.sendMessage( "Teleporter is now functional!" );
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.     }
  105.    
  106.     /* Custom classes and functions */
  107.    
  108.     /*
  109.         tryTeleport
  110.     */
  111.     public void tryTeleport( Block block, Player player )
  112.     {
  113.         boolean validTeleporter = true;
  114.         boolean doTeleport = false;
  115.        
  116.         World world = block.getWorld();
  117.        
  118.         List<String> isValidReturn = isValidTeleporter( block );
  119.        
  120.         if( isValidReturn.get( 0 ) != "isValid" )
  121.         {
  122.             validTeleporter = false;
  123.            
  124.             for( int i = 0; i < isValidReturn.size(); i++ )
  125.             {
  126.                 player.sendMessage( String.valueOf( i + 1 ) + " " + isValidReturn.get( i ) );
  127.             }
  128.            
  129.             Teleporters.remove( block );
  130.         }
  131.        
  132.         Furnace thisFurnace = GetFurnace( block );
  133.        
  134.         Inventory thisInventory = thisFurnace.getInventory();
  135.        
  136.         if( thisInventory.contains( Material.COAL, 10 ) )
  137.         {
  138.             ItemStack removeItem = new ItemStack( Material.COAL, 10 );
  139.             thisInventory.removeItem( removeItem );
  140.             doTeleport = true;
  141.         }
  142.         else
  143.         {
  144.             player.sendMessage( "Teleporters furnace doesn't have enough coal!" );
  145.             doTeleport = false;
  146.         }
  147.        
  148.         List<Block> teleportLocations = new ArrayList<Block>();
  149.         String teleporterCode = GetTeleporterCode( block );
  150.    
  151.         if( validTeleporter )
  152.         {
  153.             for (Block loopBlock : Teleporters.keySet())
  154.             {
  155.                 String loopBlockCode = GetTeleporterCode( loopBlock );
  156.                 if( teleporterCode.contains( loopBlockCode ) || loopBlockCode.contains( teleporterCode ) )
  157.                 {
  158.                     if( !String.valueOf( loopBlock.getLocation() ).contains( String.valueOf( block.getLocation() ) ) ||
  159.                             !String.valueOf( block.getLocation() ).contains( String.valueOf( loopBlock.getLocation() ) ) )
  160.                     {
  161.                         teleportLocations.add( loopBlock );
  162.                     }
  163.                 }
  164.             }
  165.         }
  166.        
  167.         if( doTeleport )
  168.         {
  169.             Block teleportBlock = null;
  170.             if( teleportLocations.size() <= 0 )
  171.             {
  172.                 player.sendMessage( "There is nowhere to teleport!" );
  173.             }
  174.             else if( teleportLocations.size() == 1 )
  175.             {
  176.                 teleportBlock = teleportLocations.get( 0 );
  177.             }
  178.             else
  179.             {
  180.                 Random randomIntGen = new Random();
  181.                
  182.                 int RandomInt = randomIntGen.nextInt( teleportLocations.size() );
  183.                
  184.                 player.sendMessage( String.valueOf( RandomInt) );
  185.                
  186.                 teleportBlock = teleportLocations.get( RandomInt );
  187.             }
  188.            
  189.             Location teleportCoordinates = GetClearTeleport( teleportBlock, player );
  190.            
  191.             if( teleportCoordinates != null )
  192.             {
  193.                 world.playEffect( thisFurnace.getBlock().getLocation(), Effect.CLICK2, 10);
  194.                 player.teleport( teleportCoordinates );
  195.                
  196.                 byte note = (byte) 0xFA;
  197.                 player.playNote( player.getLocation(), note, (byte) 10);
  198.             }
  199.             else
  200.             {
  201.                 player.sendMessage( "Connection failed, something is blocking teleport in the other side!" );
  202.             }
  203.         }
  204.     }
  205.    
  206.     Location GetClearTeleport( Block block, Player player )
  207.     {
  208.         World world = block.getWorld();
  209.         int bX = block.getLocation().getBlockX();
  210.         int bY = block.getLocation().getBlockY();
  211.         int bZ = block.getLocation().getBlockZ();
  212.        
  213.         boolean teleportNotFound = true;
  214.        
  215.         Location returnLocation = null;
  216.        
  217.         int i = 0;
  218.         while( teleportNotFound )
  219.         {
  220.             if( i >= 10 )
  221.             {
  222.                 break;
  223.             }
  224.            
  225.             Block FirstBlock = world.getBlockAt( bX, bY + 1 * i, bZ );
  226.             Block SecondBlock = world.getBlockAt( bX, bY + 2 * i, bZ );
  227.            
  228.             if( FirstBlock.getType() == Material.AIR && SecondBlock.getType() == Material.AIR )
  229.             {
  230.                 teleportNotFound = false;
  231.                 returnLocation = SecondBlock.getLocation();
  232.             }
  233.             else
  234.             {
  235.                 i++;   
  236.             }
  237.         }
  238.        
  239.         return (Location) returnLocation;
  240.        
  241.        
  242.     }
  243.    
  244.     /*
  245.         TELEPORTER
  246.             Each teleporter will be added to the Teleporters HasMap, key will be the obisian block thats the teleporter, and
  247.             value will be that class, so each teleporter will have its own chest, where players can initialize "password" to connect
  248.             to other telepoters, furnace where players put coal, so they can't teleport all the time for free, because that would make
  249.             game boring, just like /home /sethome plugin did.
  250.             And ofcourse date so i can limit the time between each teleport.
  251.     */
  252.     class Teleporter
  253.     {
  254.         public Chest chest;
  255.         public Furnace furnace;
  256.         public Player creator;
  257.         public Date date;
  258.     }
  259.  
  260.     /*
  261.         isTeleporter
  262.             This function will see if the obsidian block is registered teleporter, i think i have to redo that
  263.     */ 
  264.     public boolean isTeleporter( Block block )
  265.     {
  266.         if( Teleporters.containsKey( block ) )
  267.         {
  268.             return true;
  269.         }
  270.         else
  271.         {
  272.             return false;
  273.         }
  274.     }
  275.    
  276.     /*
  277.         isValidTeleporter
  278.             This function will see if block has everything to be an teleporter
  279.     */
  280.    
  281.     public List<String> isValidTeleporter( Block block )
  282.     {
  283.         List<String> returnList = new ArrayList<String>();
  284.        
  285.         Furnace validFurnace = null;
  286.         if( GetFurnace( block ) != null)
  287.             validFurnace = GetFurnace( block );
  288.        
  289.         if( validFurnace == null )
  290.         {
  291.             returnList.add( "Teleporter is missing furnace or has too many of them!" );
  292.         }
  293.        
  294.         Chest validChest = null;
  295.         if( GetChest( block ) != null )
  296.             validChest = GetChest( block );
  297.        
  298.         if( validChest == null )
  299.         {
  300.             returnList.add( "Teleporter is missing chest or has too many of them!" );
  301.         }
  302.         else
  303.         {
  304.             Inventory validInventory = validChest.getInventory();
  305.            
  306.             ItemStack codeOne = validInventory.getItem( 0 );
  307.             ItemStack codeTwo = validInventory.getItem( 1 );
  308.             ItemStack codeThree = validInventory.getItem( 2 );
  309.             ItemStack codeFour = validInventory.getItem( 3 );
  310.            
  311.             if(     codeOne.getType() == Material.AIR
  312.                     || codeTwo.getType() == Material.AIR
  313.                     || codeThree.getType() == Material.AIR
  314.                     || codeFour.getType() == Material.AIR
  315.             )
  316.             {
  317.                 returnList.add( "Code for the teleporter contains empty slots!" );
  318.             }
  319.         }
  320.        
  321.         if( returnList.size() <= 0 )
  322.         {
  323.             returnList.add( "isValid" );
  324.         }
  325.        
  326.         return returnList;
  327.     }
  328.    
  329.     /*
  330.         GetFurnace
  331.             This function will get furnace around the obisidan block
  332.     */
  333.     public Furnace GetFurnace( Block block )
  334.     {
  335.         World world = block.getWorld();
  336.        
  337.         int bX = block.getLocation().getBlockX();
  338.         int bY = block.getLocation().getBlockY();
  339.         int bZ = block.getLocation().getBlockZ();
  340.        
  341.         Block newBlock = null;
  342.         BlockState theFurnace;
  343.        
  344.         newBlock = world.getBlockAt( bX, bY, bZ + 1 );
  345.         if( newBlock.getType() == Material.FURNACE )
  346.         {
  347.             theFurnace = newBlock.getState();
  348.         }
  349.         else
  350.         {
  351.             newBlock = world.getBlockAt( bX, bY, bZ - 1 );
  352.             if( newBlock.getType() == Material.FURNACE )
  353.             {
  354.                 theFurnace = newBlock.getState();
  355.             }      
  356.             else
  357.             {
  358.                 newBlock = world.getBlockAt( bX + 1, bY, bZ );
  359.                 if( newBlock.getType() == Material.FURNACE )
  360.                 {
  361.                     theFurnace = newBlock.getState();
  362.                 }  
  363.                 else
  364.                 {
  365.                     newBlock = world.getBlockAt( bX - 1, bY, bZ );
  366.                     if( newBlock.getType() == Material.FURNACE )
  367.                     {
  368.                         theFurnace = newBlock.getState();
  369.                     }
  370.                     else
  371.                     {
  372.                         return (Furnace) null;
  373.                     }
  374.                 }
  375.                    
  376.             }
  377.         }
  378.        
  379.         return (Furnace) theFurnace;
  380.     }
  381.    
  382.     /*
  383.         GetChest
  384.             This function will get chest around the obisidan block
  385.     */
  386.     public Chest GetChest( Block block )
  387.     {
  388.         World world = block.getWorld();
  389.        
  390.         int bX = block.getLocation().getBlockX();
  391.         int bY = block.getLocation().getBlockY();
  392.         int bZ = block.getLocation().getBlockZ();
  393.        
  394.         Block newBlock = null;
  395.         BlockState theChest;
  396.        
  397.         newBlock = world.getBlockAt( bX, bY, bZ + 1 );
  398.         if( newBlock.getType() == Material.CHEST )
  399.         {
  400.             theChest = newBlock.getState();
  401.         }
  402.         else
  403.         {
  404.             newBlock = world.getBlockAt( bX, bY, bZ - 1 );
  405.             if( newBlock.getType() == Material.CHEST )
  406.             {
  407.                 theChest = newBlock.getState();
  408.             }      
  409.             else
  410.             {
  411.                 newBlock = world.getBlockAt( bX + 1, bY, bZ );
  412.                 if( newBlock.getType() == Material.CHEST )
  413.                 {
  414.                     theChest = newBlock.getState();
  415.                 }  
  416.                 else
  417.                 {
  418.                     newBlock = world.getBlockAt( bX - 1, bY, bZ );
  419.                     if( newBlock.getType() == Material.CHEST )
  420.                     {
  421.                         theChest = newBlock.getState();
  422.                     }
  423.                     else
  424.                     {
  425.                         return (Chest) null;
  426.                     }
  427.                 }
  428.                    
  429.             }
  430.         }
  431.        
  432.         return (Chest) theChest;
  433.     }
  434.    
  435.     public String GetTeleporterCode( Block block )
  436.     {
  437.         Chest codeChest = null;
  438.         if( GetChest( block ) != null )
  439.             codeChest = GetChest( block ); 
  440.        
  441.         Inventory codeInventory = codeChest.getInventory();
  442.        
  443.         ItemStack codeOne = codeInventory.getItem( 0 );
  444.         ItemStack codeTwo = codeInventory.getItem( 1 );
  445.         ItemStack codeThree = codeInventory.getItem( 2 );
  446.         ItemStack codeFour = codeInventory.getItem( 3 );
  447.        
  448.         String codeString = "";
  449.        
  450.         codeString += String.valueOf( codeOne.getTypeId() );
  451.         codeString += String.valueOf( codeTwo.getTypeId() );
  452.         codeString += String.valueOf( codeThree.getTypeId() );
  453.         codeString += String.valueOf( codeFour.getTypeId() );
  454.        
  455.         return codeString;
  456.     }
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement