Advertisement
Maxlego08

Untitled

Jan 31st, 2021
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. package fbbh.maxlego08.fr;
  2.  
  3. import java.util.UUID;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Location;
  7. import org.bukkit.World;
  8. import org.bukkit.block.Block;
  9. import org.bukkit.block.data.BlockData;
  10. import org.primesoft.blockshub.IBlocksHubApi;
  11. import org.primesoft.blockshub.api.IBlockData;
  12. import org.primesoft.blockshub.api.platform.BukkitBlockData;
  13.  
  14. import com.sk89q.worldedit.EditSession.Stage;
  15. import com.sk89q.worldedit.WorldEdit;
  16. import com.sk89q.worldedit.WorldEditException;
  17. import com.sk89q.worldedit.event.extent.EditSessionEvent;
  18. import com.sk89q.worldedit.extent.AbstractDelegateExtent;
  19. import com.sk89q.worldedit.math.BlockVector3;
  20. import com.sk89q.worldedit.util.eventbus.EventBus;
  21. import com.sk89q.worldedit.util.eventbus.Subscribe;
  22. import com.sk89q.worldedit.world.block.BlockStateHolder;
  23.  
  24. public class FastAsyncWorldEditHandler implements Handler {
  25.  
  26.     private final EventBus eventBus;
  27.     private final FbbhPlugin plugin;
  28.  
  29.     public FastAsyncWorldEditHandler(FbbhPlugin plugin) {
  30.         super();
  31.         this.eventBus = WorldEdit.getInstance().getEventBus();
  32.         this.plugin = plugin;
  33.     }
  34.  
  35.     @Subscribe
  36.     public void onEditSession(EditSessionEvent event) {
  37.  
  38.         Stage stage = event.getStage();
  39.         if (stage.equals(Stage.BEFORE_CHANGE)) {
  40.  
  41.             final IBlocksHubApi api = plugin.getIBlock();
  42.             UUID uuid = event.getActor().getUniqueId();
  43.             World world = Bukkit.getWorld(event.getWorld().getName());
  44.  
  45.             event.setExtent(new AbstractDelegateExtent(event.getExtent()) {
  46.                 @SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
  47.                 @Override
  48.                 public boolean setBlock(BlockVector3 pos, BlockStateHolder blockStateHolder) throws WorldEditException {
  49.  
  50.                     final double x = pos.getBlockX();
  51.                     final double y = pos.getBlockY();
  52.                     final double z = pos.getBlockZ();
  53.  
  54.                     final Location location = new Location(world, x, y, z);
  55.                     final Block blockBefore = location.getBlock();
  56.                     final IBlockData blockDataBefore = convert(blockBefore.getBlockData().clone());
  57.  
  58.                     Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
  59.  
  60.                         final Block blockAfter = location.getBlock();
  61.                         final IBlockData blockDataAfter = convert(blockAfter.getBlockData().clone());
  62.  
  63.                         System.out.println(String.format("Je vais remplacer le block %s en %s %s %s par le block %s",
  64.                                 blockDataBefore.getData(BlockData.class), x, y, z,
  65.                                 blockDataAfter.getData(BlockData.class)));
  66.                         api.logBlock(uuid, world.getUID(), x, y, z, blockDataBefore, blockDataAfter);
  67.  
  68.                     }, 7);
  69.  
  70.                     return getExtent().setBlock(pos, blockStateHolder);
  71.                 }
  72.  
  73.             });
  74.  
  75.         }
  76.  
  77.     }
  78.  
  79.     public void enable() {
  80.         this.eventBus.register(this);
  81.     }
  82.  
  83.     public void disable() {
  84.         this.eventBus.unregister(this);
  85.     }
  86.  
  87.     private IBlockData convert(BlockData blockData) {
  88.         return blockData == null ? null : new BukkitBlockData(blockData);
  89.     }
  90.  
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement