Last active
October 26, 2015 15:36
-
-
Save SirEdvin/3a6fb93311eb26d126ae to your computer and use it in GitHub Desktop.
Custom Event usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package siredvin.github.io; | |
import com.google.common.collect.Lists; | |
import com.sk89q.worldguard.bukkit.RegionQuery; | |
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; | |
import com.sk89q.worldguard.domains.Association; | |
import com.sk89q.worldguard.protection.ApplicableRegionSet; | |
import com.sk89q.worldguard.protection.association.RegionOverlapAssociation; | |
import com.sk89q.worldguard.protection.regions.ProtectedRegion; | |
import io.github.siredvin.events.BlockBlockInteractionEvent; | |
import io.github.siredvin.events.PlayerBlockInteractionEvent; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Location; | |
import org.bukkit.World; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import java.util.OptionalInt; | |
import java.util.Set; | |
import java.util.logging.Logger; | |
/** | |
* Created by siredvin on 24.10.15. | |
* | |
* @author siredvin | |
*/ | |
public class InteractionEventListener implements Listener { | |
private WorldGuardPlugin plugin; | |
private Logger logger = Logger.getLogger("Magic Fixes"); | |
public InteractionEventListener(){ | |
plugin = (WorldGuardPlugin) Bukkit.getPluginManager().getPlugin("WorldGuard"); | |
} | |
@EventHandler | |
public void playerBlockInteraction(PlayerBlockInteractionEvent event){ | |
Player player =Bukkit.getPlayer(event.player); | |
event.setCancelled(plugin.canBuild(player,new Location(player.getWorld(),event.x,event.y,event.z))); | |
} | |
@EventHandler | |
public void blockBlockInteraction(BlockBlockInteractionEvent event){ | |
RegionQuery query = plugin.getRegionContainer().createQuery(); | |
World world = Bukkit.getWorld(event.world); | |
Set<ProtectedRegion> sourceBlockSet = query.getApplicableRegions(new Location(world, event.blockX, event.blockY, event.blockZ)).getRegions(); | |
Set<ProtectedRegion> blockSet = query.getApplicableRegions(new Location(world,event.x,event.y,event.z)).getRegions(); | |
if (blockSet.size()!=0){//Блок, на который действуйет другой блок защищен | |
if (sourceBlockSet.size()==0){//Блок, который пытается действовать не в привате | |
event.setCancelled(true); | |
} else if (!sourceBlockSet.equals(blockSet)){//Убираем самый частый случай - два блока в одном привате | |
//Плохая штука, не учитывает приоритетность регионов | |
//event.setCancelled(new RegionOverlapAssociation(sourceBlockSet).getAssociation(Lists.newArrayList(blockSet)) == Association.NON_MEMBER); | |
//Ничего плохого не произойдет, обе коллекции уже проверены на пустоту | |
//Так же учитывая разположение приватов. | |
int blockMaxPriority= blockSet.stream().mapToInt(ProtectedRegion::getPriority).max().getAsInt(); | |
int sourceBlockMaxPriority = sourceBlockSet.stream().filter(blockSet::contains).mapToInt(ProtectedRegion::getPriority).max().getAsInt(); | |
event.setCancelled(blockMaxPriority>sourceBlockMaxPriority); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment