Skip to content
This repository was archived by the owner on Oct 28, 2025. It is now read-only.
This repository was archived by the owner on Oct 28, 2025. It is now read-only.

IBlockPlacerListener jobAdded doesn't seem to work #37

Closed

Description

@DeKleineKobini

Hi,

I have a plugin where I need to do things when starting and ending the pasting.
I have the ending part working but the starting part doesn't seem to work.

To test this I did:

bp.addListener(new IBlockPlacerListener(){

            @Override
            public void jobAdded(IJobEntry paramIJobEntry) {
                Bukkit.broadcastMessage("AWE: Job added");

            }

            @Override
            public void jobRemoved(IJobEntry paramIJobEntry) {
                Bukkit.broadcastMessage("AWE: Job removed");

            }

        });

The removed worked but the added didn't.

Activity

  1. SBPrime commented on Jan 26, 2016

    @SBPrime
    Owner

    I tested this and job added is called. There are multiple cases why the job did not add to say more I would need to see your code that initializes the job.

  2. DeKleineKobini commented on Jan 26, 2016

    @DeKleineKobini
    Author
    package be.kobini.funzcraft.escapethehunters.game.handler.map;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.logging.Level;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.plugin.Plugin;
    import org.primesoft.asyncworldedit.api.IAsyncWorldEdit;
    import org.primesoft.asyncworldedit.api.blockPlacer.IBlockPlacer;
    import org.primesoft.asyncworldedit.api.blockPlacer.IBlockPlacerListener;
    import org.primesoft.asyncworldedit.api.blockPlacer.entries.IJobEntry;
    import org.primesoft.asyncworldedit.api.playerManager.IPlayerEntry;
    import org.primesoft.asyncworldedit.api.playerManager.IPlayerManager;
    import org.primesoft.asyncworldedit.api.utils.IFuncParamEx;
    import org.primesoft.asyncworldedit.api.worldedit.IAsyncEditSessionFactory;
    import org.primesoft.asyncworldedit.api.worldedit.ICancelabeEditSession;
    import org.primesoft.asyncworldedit.api.worldedit.IThreadSafeEditSession;
    
    import com.sk89q.worldedit.EditSession;
    import com.sk89q.worldedit.MaxChangedBlocksException;
    import com.sk89q.worldedit.Vector;
    import com.sk89q.worldedit.WorldEdit;
    import com.sk89q.worldedit.bukkit.BukkitWorld;
    import com.sk89q.worldedit.bukkit.WorldEditPlugin;
    import com.sk89q.worldedit.extent.clipboard.Clipboard;
    import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
    import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
    import com.sk89q.worldedit.function.operation.Operation;
    import com.sk89q.worldedit.function.operation.Operations;
    import com.sk89q.worldedit.session.ClipboardHolder;
    import com.sk89q.worldedit.world.registry.WorldData;
    
    import be.kobini.funzcraft.escapethehunters.ETH;
    import be.kobini.funzcraft.escapethehunters.game.handler.GameHandler;
    import be.kobini.funzcraft.escapethehunters.game.handler.GameHandler.GameState;
    
    public class ASchematic{
    
        private ETH plugin;
        private GameHandler gameHandler;
    
        public ASchematic(ETH plugin){
            this.plugin = plugin;
    
            this.gameHandler = plugin.getGameHandler();
        }
    
        private static IAsyncWorldEdit getAWE() {
            return (IAsyncWorldEdit) Bukkit.getPluginManager().getPlugin("AsyncWorldEdit");
        }
    
        public void pasteSchematicAsync(File schematic, Location pasteLocation) {
            int maxBlocks = 999999999;
    
            IAsyncWorldEdit awe = getAWE();
            IPlayerManager pm = awe.getPlayerManager();
            final IPlayerEntry playerEntry = pm.getUnknownPlayer();
    
            BukkitWorld bukkitWorld = new BukkitWorld(pasteLocation.getWorld());
            IThreadSafeEditSession tsSession = (IThreadSafeEditSession) createEditSession(bukkitWorld, maxBlocks, playerEntry);
            IFuncParamEx<Integer, ICancelabeEditSession, MaxChangedBlocksException> action = new PasteAction(bukkitWorld, pasteLocation, schematic);
            IBlockPlacer bp = awe.getBlockPlacer();
    
            bp.performAsAsyncJob(tsSession, playerEntry, "ETH paste map", action);
    
            gameHandler.setState(GameHandler.GameState.MAP_RESETTING);
    
            bp.addListener(new IBlockPlacerListener(){
    
                @Override
                public void jobRemoved(IJobEntry job) {
                    if(!job.getName().equalsIgnoreCase("ETH paste map")) return;
                    if(gameHandler.getState() != GameState.MAP_RESETTING) return;
    
                    gameHandler.setState(GameHandler.GameState.MAP_INLOBBY);
                }
    
                @Override
                public void jobAdded(IJobEntry job) {
                    if(!job.getName().equalsIgnoreCase("ETH paste map")) return;
    
                    gameHandler.setState(GameState.MAP_RESETTING);
                }
            });
        }
    
        private class PasteAction implements IFuncParamEx<Integer, ICancelabeEditSession, MaxChangedBlocksException>{
            private final BukkitWorld bukkitWorld;
            private final Location origin;
            private final File file;
    
            public PasteAction(BukkitWorld bukkitWorld, Location origin, File file){
                this.bukkitWorld = bukkitWorld;
                this.origin = origin;
                this.file = file;
            }
    
            @Override
            public Integer execute(ICancelabeEditSession editSession) throws MaxChangedBlocksException {
                try {
                    ClipboardReader reader = ClipboardFormat.SCHEMATIC.getReader(new FileInputStream(file));
                    WorldData worldData = bukkitWorld.getWorldData();
                    Clipboard clipboard = reader.read(worldData);
                    ClipboardHolder holder = new ClipboardHolder(clipboard, worldData);
                    editSession.enableQueue();
                    editSession.setFastMode(true);
                    Vector to = new Vector(origin.getBlockX(), origin.getBlockY(), origin.getBlockZ());
                    final Operation operation = holder.createPaste(editSession, worldData).to(to).ignoreAirBlocks(true).build();
                    Operations.completeBlindly(operation);
                    editSession.flushQueue();
                } catch (IOException e) {
                    plugin.getLogger().log(Level.WARNING, "ETH: Error trying to paste " + file.getName() + " async", e);
                }
                return 32768;
            }
        }
    
        public EditSession createEditSession(BukkitWorld bukkitWorld, int maxBlocks, IPlayerEntry player) {
            WorldEdit worldEdit = getWorldEditPlugin().getWorldEdit();
            IAsyncEditSessionFactory sessionFactory = (IAsyncEditSessionFactory) worldEdit.getEditSessionFactory();
    
            return (EditSession) sessionFactory.getThreadSafeEditSession(bukkitWorld, maxBlocks, null, player);
        }
    
        public WorldEditPlugin getWorldEditPlugin() {
            Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
            if(plugin == null || !(plugin instanceof WorldEditPlugin)) return null;
            return (WorldEditPlugin) plugin;
        }
    
    }

    That is the code I use

  3. SBPrime commented on Jan 26, 2016

    @SBPrime
    Owner

    Move:

     bp.addListener(new IBlockPlacerListener(){
    

    Before:

     bp.performAsAsyncJob(tsSession, playerEntry, "ETH paste map", action);
    

    This:

    return 32768;
    

    indicates the number of blocks placed by the job. I would also create a custom player entry instead of using the unknown version. Rest looks ok.

  4. DeKleineKobini commented on Jan 26, 2016

    @DeKleineKobini
    Author

    Thanks, I have another problem.

    I'm pasting a schematic, is works but it doesn't replace blocks where air should be.

  5. SBPrime commented on Jan 26, 2016

    @SBPrime
    Owner

    What do you mean by not replacing blocks where air should be?

  6. DeKleineKobini commented on Jan 26, 2016

    @DeKleineKobini
    Author

    Like when I place a block, I paste the schematic and the loction where I put the block is air in the schematic then the blocks doesn't get removed

  7. SBPrime commented on Jan 26, 2016

    @SBPrime
    Owner

    No comment:

    final Operation operation = holder.createPaste(editSession, worldData).to(to).ignoreAirBlocks(true).build();
    
  8. DeKleineKobini commented on Jan 26, 2016

    @DeKleineKobini
    Author

    Didn't remeber I put it there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions