turt2live

PR B855 Testing

Aug 6th, 2013
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. package com.turt2live.bukkit.testing.b855;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.Material;
  9. import org.bukkit.block.Block;
  10. import org.bukkit.event.EventHandler;
  11. import org.bukkit.event.Listener;
  12. import org.bukkit.event.player.PlayerInteractEvent;
  13. import org.bukkit.material.Step;
  14. import org.bukkit.plugin.java.JavaPlugin;
  15.  
  16. public class Main extends JavaPlugin implements Listener{
  17.  
  18.     // Borrow the list of applicable materials
  19.     private static final List<Material> textures = new ArrayList<Material>();
  20.     static {
  21.         textures.add(Material.STONE);
  22.         textures.add(Material.SANDSTONE);
  23.         textures.add(Material.WOOD);
  24.         textures.add(Material.COBBLESTONE);
  25.         textures.add(Material.BRICK);
  26.         textures.add(Material.SMOOTH_BRICK);
  27.         textures.add(Material.NETHER_BRICK); // From PR
  28.         textures.add(Material.QUARTZ_BLOCK); // From PR
  29.     }
  30.    
  31.     public void onEnable(){
  32.         this.getServer().getPluginManager().registerEvents(this, this);
  33.     }
  34.        
  35.     /*
  36.      * To activate this PR testing simply interact with a STEP or DOUBLE_STEP.
  37.      * This will go through the list of materials (known to work with steps) and
  38.      * set the STEP (or DOUBLE_STEP) to that material.
  39.      */
  40.     int index = 0;
  41.     @EventHandler
  42.     public void onInteract(PlayerInteractEvent event){
  43.         if(event.getClickedBlock()!=null){
  44.             Block block = event.getClickedBlock();
  45.             if(block.getType()==Material.STEP||block.getType()==Material.DOUBLE_STEP){
  46.                 // Clone the BlockState. This is required as without the clone it does not update
  47.                 // This happens without this PR as well.
  48.                 Step data = (Step) block.getState().getData().clone();
  49.                 data.setMaterial(textures.get(index)); // Get the applicable next texture
  50.                 block.setData(data.getData()); // Update the block's data to our changed (cloned) data.
  51.                 boolean updated = block.getState().update(true); // Force an update
  52.                 if(!updated){
  53.                     Bukkit.broadcastMessage(ChatColor.RED+"NOT UPDATED"); // Should never happen (in this test)
  54.                 }
  55.                 // <what we set it to> <what the block says it is> <what the block state says it is>
  56.                 Bukkit.broadcastMessage("TEXTURE: "+textures.get(index).name() +" REPORTED: "+data.getMaterial().name()+" || "+((Step)block.getState().getData()).getMaterial().name());
  57.                 index++;
  58.                 if(index==textures.size()){
  59.                     index=0;
  60.                     Bukkit.broadcastMessage("----"); // To show we iterated all of the possibilities
  61.                 }
  62.                 event.setCancelled(true); // To avoid creative left clicking and such
  63.             }
  64.         }
  65.     }
  66.    
  67. }
Advertisement
Add Comment
Please, Sign In to add comment