Pangamma icon

SurvivalWorldEditMask.java

Pangamma | PRO | 04/04/19 04:13:07 AM UTC | 0 ⭐ | 395 👁️ | Never ⏰ | []
Java |

4.68 KB

|

None

|

0 👍

/

0 👎

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lumengaming.swe;
 
import com.sk89q.minecraft.util.commands.CommandException;
import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitPlayer;
import com.sk89q.worldedit.function.mask.AbstractExtentMask;
import com.sk89q.worldedit.function.mask.Mask2D;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockType;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
 
/**
 *
 * @author prota
 */
public class SurvivalWorldEditMain extends JavaPlugin implements Listener{
 
    private final Set<BlockType> blocks = new HashSet<>();
    
    @Override
    public void onEnable(){
        getServer().getPluginManager().registerEvents(this, this);
        this.blocks.add(BlockType.REGISTRY.get("minecraft:air"));
        this.blocks.add(BlockType.REGISTRY.get("minecraft:cave_air"));
        this.blocks.add(BlockType.REGISTRY.get("minecraft:void_air"));
        this.blocks.add(BlockType.REGISTRY.get("minecraft:water"));
    }
    
    @Override
    public void onDisable(){
        HandlerList.unregisterAll((JavaPlugin) this);
    }
    
    @EventHandler
    public void onPrettyMuchAllTheTime(PlayerCommandPreprocessEvent e) throws CommandException{
        if (!e.getMessage().startsWith("//")){ return; }
        BukkitPlayer p = BukkitAdapter.adapt(e.getPlayer());
        WorldEdit WE = WorldEdit.getInstance();
        LocalSession session = WE.getSessionManager().get(p);
        
        session.setMask(null);
        session.setMask(new AbstractExtentMask(p.getExtent()){
 
            @Override
            public boolean test(BlockVector3 bv) {
                if (!blocks.contains(getExtent().getBlock(bv).getBlockType())) {
                    return false;
                }
                Block blockAt = e.getPlayer().getWorld().getBlockAt(bv.getBlockX(), bv.getBlockY(), bv.getBlockZ());
                return canPlaceBlockByEvent(e.getPlayer(), blockAt);
                
            }
 
            @Override
            public Mask2D toMask2D() {
                return null;
            }
        });
    }
 
    public boolean canPlaceBlockByEvent(Player p, Block b){
        b.setMetadata("swe:isFakePlace", new FixedMetadataValue(this, true));
        BlockPlaceEvent evt = new BlockPlaceEvent(b, b.getState(),
            b.getRelative(BlockFace.DOWN), new ItemStack(Material.RED_STAINED_GLASS) /* p.getInventory().getItemInMainHand() */,
            p, true , EquipmentSlot.HAND);
        
        synchronized(Bukkit.getPluginManager()){
            Bukkit.getPluginManager().callEvent(evt);
        }
        b = evt.getBlock();
        
        List<MetadataValue> canBuild = b.getMetadata("swe:canBuild");
        
        boolean rtVal = true;
        for(MetadataValue val : canBuild){
            if (this.equals(val.getOwningPlugin())){
                if (!val.asBoolean()){ rtVal = false; }
            }
        }
        
        b.removeMetadata("swe:canBuild", this);
        b.removeMetadata("swe:isFakePlace", this);
        return rtVal;
    }
 
    
    @EventHandler(ignoreCancelled = false, priority = EventPriority.HIGHEST)
    public void onFakeBlockPlace(BlockPlaceEvent e){
        Block b = e.getBlock();
        if (e.getItemInHand().getType() != Material.RED_STAINED_GLASS) return;
        List<MetadataValue> isFake = b.getMetadata("swe:isFakePlace");
        boolean isNormal = true;
        for(MetadataValue val : isFake){
            if (val.asBoolean()){
                isNormal = false;
            }
        }
        
        if (isNormal) return;
        
        b.setMetadata("swe:canBuild", new FixedMetadataValue(this, !e.isCancelled()));
        e.setCancelled(true);
    }
    
}

Comments