Search in sources :

Example 41 with Region

use of br.net.fabiozumbi12.RedProtect.Sponge.Region in project RedProtect by FabioZumbi12.

the class RPSchematics method pasteSchematic.

public static void pasteSchematic(Player p) throws IOException {
    File file = new File(RedProtect.get().getDataFolder(), "schematics" + File.separator + RPConfig.getString("schematics.first-house-file"));
    FileInputStream stream = new FileInputStream(file);
    World world = p.getWorld();
    Location loc = p.getLocation();
    NBTInputStream nbtStream = new NBTInputStream(new GZIPInputStream(stream));
    NamedTag rootTag = nbtStream.readNamedTag();
    nbtStream.close();
    if (!rootTag.getName().equals("Schematic")) {
        throw new IllegalArgumentException("Tag \"Schematic\" does not exist or is not first");
    }
    CompoundTag schematicTag = (CompoundTag) rootTag.getTag();
    Map<String, Tag> schematic = schematicTag.getValue();
    if (!schematic.containsKey("Blocks")) {
        throw new IllegalArgumentException("Schematic file is missing a \"Blocks\" tag");
    }
    short width = getChildTag(schematic, "Width", ShortTag.class).getValue();
    short length = getChildTag(schematic, "Length", ShortTag.class).getValue();
    short height = getChildTag(schematic, "Height", ShortTag.class).getValue();
    String materials = getChildTag(schematic, "Materials", StringTag.class).getValue();
    if (!materials.equals("Alpha")) {
        throw new IllegalArgumentException("Schematic file is not an Alpha schematic");
    }
    // Get blocks
    byte[] blockId = getChildTag(schematic, "Blocks", ByteArrayTag.class).getValue();
    byte[] blockData = getChildTag(schematic, "Data", ByteArrayTag.class).getValue();
    byte[] addId = new byte[0];
    // Have to later combine IDs
    short[] blocks = new short[blockId.length];
    // the highest 4 bits are stored in a separate byte array.
    if (schematic.containsKey("AddBlocks")) {
        addId = getChildTag(schematic, "AddBlocks", ByteArrayTag.class).getValue();
    }
    // Combine the AddBlocks data with the first 8-bit block ID
    for (int index = 0; index < blockId.length; index++) {
        if ((index >> 1) >= addId.length) {
            // No corresponding AddBlocks index
            blocks[index] = (short) (blockId[index] & 0xFF);
        } else {
            if ((index & 1) == 0) {
                blocks[index] = (short) (((addId[index >> 1] & 0x0F) << 8) + (blockId[index] & 0xFF));
            } else {
                blocks[index] = (short) (((addId[index >> 1] & 0xF0) << 4) + (blockId[index] & 0xFF));
            }
        }
    }
    int offsetX = 0;
    int offsetY = 0;
    int offsetZ = 0;
    try {
        offsetX = getChildTag(schematic, "WEOffsetX", IntTag.class).getValue();
        offsetY = getChildTag(schematic, "WEOffsetY", IntTag.class).getValue();
        offsetZ = getChildTag(schematic, "WEOffsetZ", IntTag.class).getValue();
    } catch (Exception e) {
    // No offset data
    }
    Map<Integer, BlockState> blist = new HashMap<>();
    Location pos1 = loc;
    Location pos2 = loc;
    int order = 0;
    int base = 0;
    int validBlock = 0;
    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            for (int z = 0; z < length; ++z) {
                int index = y * width * length + z * width + x;
                Location locblock = new Location(world, x + loc.getX() + offsetX, y + loc.getY() + offsetY, z + loc.getZ() + offsetZ);
                Block block = locblock.getBlock();
                BlockState bstate = block.getState();
                blist.put(index, bstate);
                if (order == 0) {
                    pos1 = locblock;
                } else {
                    pos2 = locblock;
                }
                // get ground blocks
                if (block.getLocation().getBlockY() == pos1.getBlockY()) {
                    base++;
                    if (block.getType().isSolid()) {
                        validBlock++;
                    }
                }
                order++;
            }
        }
    }
    // check if can place the schematic
    if (validBlock < base / 2) {
        RPLang.sendMessage(p, "playerlistener.region.needground");
        return;
    }
    String regionName = RPUtil.regionNameConfiorm("", p);
    String pName = RPUtil.PlayerToUUID(p.getName());
    // check if player already have claims
    int claimused = RedProtect.get().rm.getPlayerRegions(p.getName(), p.getWorld());
    if (claimused > 0 && !p.hasPermission("RedProtect.get().bypass")) {
        RPLang.sendMessage(p, "playerlistener.region.claimlimit.start");
        return;
    }
    Region region = new Region(regionName, new ArrayList<>(), new ArrayList<>(), Collections.singletonList(pName), new int[] { pos1.getBlockX(), pos1.getBlockX(), pos2.getBlockX(), pos2.getBlockX() }, new int[] { pos1.getBlockZ(), pos1.getBlockZ(), pos2.getBlockZ(), pos2.getBlockZ() }, 0, p.getWorld().getMaxHeight(), 0, p.getWorld().getName(), RPUtil.DateNow(), RPConfig.getDefFlagsValues(), "", 0, null, false);
    List<String> othersName = new ArrayList<>();
    Region otherrg = null;
    // check if same area
    otherrg = RedProtect.get().rm.getTopRegion(region.getCenterLoc());
    if (otherrg != null && otherrg.get4Points(region.getCenterY()).equals(region.get4Points(region.getCenterY()))) {
        p.sendMessage(RPLang.get("regionbuilder.region.overlapping").replace("{location}", "x: " + otherrg.getCenterX() + ", z: " + otherrg.getCenterZ()).replace("{player}", otherrg.getLeadersDesc()));
        return;
    }
    // check regions inside region
    for (Region r : RedProtect.get().rm.getRegionsByWorld(p.getWorld())) {
        if (r.getMaxMbrX() <= region.getMaxMbrX() && r.getMaxY() <= region.getMaxY() && r.getMaxMbrZ() <= region.getMaxMbrZ() && r.getMinMbrX() >= region.getMinMbrX() && r.getMinY() >= region.getMinY() && r.getMinMbrZ() >= region.getMinMbrZ()) {
            if (!r.isLeader(p) && !p.hasPermission("RedProtect.get().bypass")) {
                p.sendMessage(RPLang.get("regionbuilder.region.overlapping").replace("{location}", "x: " + otherrg.getCenterX() + ", z: " + otherrg.getCenterZ()).replace("{player}", otherrg.getLeadersDesc()));
                return;
            }
            if (!othersName.contains(r.getName())) {
                othersName.add(r.getName());
            }
        }
    }
    // check borders for other regions
    List<Location> limitlocs = region.getLimitLocs(region.getMinY(), region.getMaxY(), true);
    for (Location locr : limitlocs) {
        /*
        	//check regions near
        	if (!RPUtil.canBuildNear(p, loc)){
            	return;    	
            }*/
        otherrg = RedProtect.get().rm.getTopRegion(locr);
        RedProtect.get().logger.debug("protection Block is: " + locr.getBlock().getType().name());
        if (otherrg != null) {
            if (!otherrg.isLeader(p) && !p.hasPermission("RedProtect.get().bypass")) {
                p.sendMessage(RPLang.get("regionbuilder.region.overlapping").replace("{location}", "x: " + otherrg.getCenterX() + ", z: " + otherrg.getCenterZ()).replace("{player}", otherrg.getLeadersDesc()));
                return;
            }
            if (!othersName.contains(otherrg.getName())) {
                othersName.add(otherrg.getName());
            }
        }
    }
    // check cost per block
    if (RPConfig.getEcoBool("claim-cost-per-block.enable") && RedProtect.get().Vault && !p.hasPermission("RedProtect.get().eco.bypass")) {
        Double peco = RedProtect.get().econ.getBalance(p);
        long reco = region.getArea() * RPConfig.getEcoInt("claim-cost-per-block.cost-per-block");
        if (!RPConfig.getEcoBool("claim-cost-per-block.y-is-free")) {
            reco = reco * Math.abs(region.getMaxY() - region.getMinY());
        }
        if (peco >= reco) {
            RedProtect.get().econ.withdrawPlayer(p, reco);
            p.sendMessage(RPLang.get("economy.region.claimed").replace("{price}", RPConfig.getEcoString("economy-symbol") + reco + " " + RPConfig.getEcoString("economy-name")));
        } else {
            RPLang.sendMessage(p, RPLang.get("regionbuilder.notenought.money").replace("{price}", RPConfig.getEcoString("economy-symbol") + reco));
            return;
        }
    }
    if (RPConfig.getBool("hooks.worldedit.use-for-schematics") && RedProtect.get().WE) {
        WEListener.pasteWithWE(p, file);
    } else {
        for (Integer key : blist.keySet()) {
            Block b = blist.get(key).getBlock();
            // paste schematic
            b.setTypeIdAndData(blocks[key], blockData[key], true);
        }
    }
    p.sendMessage(RPLang.get("general.color") + "------------------------------------");
    RPLang.sendMessage(p, "playerlistener.region.startdone");
    p.sendMessage(RPLang.get("general.color") + "------------------------------------");
    RPLang.sendMessage(p, "cmdmanager.region.firstwarning");
    p.sendMessage(RPLang.get("general.color") + "------------------------------------");
    RedProtect.get().logger.addLog("(World " + region.getWorld() + ") Player " + p.getName() + " CREATED(SCHEMATIC) region " + region.getName());
    RedProtect.get().rm.add(region, p.getWorld());
}
Also used : StringTag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.StringTag) World(org.bukkit.World) GZIPInputStream(java.util.zip.GZIPInputStream) NamedTag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.NamedTag) CompoundTag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.CompoundTag) FileInputStream(java.io.FileInputStream) ShortTag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.ShortTag) IOException(java.io.IOException) BlockState(org.bukkit.block.BlockState) NBTInputStream(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.NBTInputStream) Block(org.bukkit.block.Block) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region) CompoundTag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.CompoundTag) Tag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.Tag) StringTag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.StringTag) ShortTag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.ShortTag) IntTag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.IntTag) NamedTag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.NamedTag) ByteArrayTag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.ByteArrayTag) File(java.io.File) Location(org.bukkit.Location) ByteArrayTag(br.net.fabiozumbi12.RedProtect.Bukkit.schematics.org.jnbt.ByteArrayTag)

Example 42 with Region

use of br.net.fabiozumbi12.RedProtect.Sponge.Region in project RedProtect by FabioZumbi12.

the class MPListener method onEntityDamageByPet.

@EventHandler
public void onEntityDamageByPet(EntityDamageEvent e) {
    if (e.isCancelled()) {
        return;
    }
    Entity ent = e.getEntity();
    if (ent instanceof MyPetBukkitEntity) {
        Region r = RedProtect.get().rm.getTopRegion(ent.getLocation());
        if (r != null && r.flagExists("invincible")) {
            if (r.getFlagBool("invincible")) {
                e.setCancelled(true);
                ((MyPetBukkitEntity) ent).forgetTarget();
            }
        }
    }
    if (e instanceof EntityDamageByEntityEvent) {
        EntityDamageByEntityEvent de = (EntityDamageByEntityEvent) e;
        Entity e1 = de.getEntity();
        Entity e2 = de.getDamager();
        Location loc = e1.getLocation();
        Region r1 = RedProtect.get().rm.getTopRegion(loc);
        if (r1 == null) {
            return;
        }
        if (e1 instanceof Animals || e1 instanceof Villager || e1 instanceof Golem) {
            Entity damager = e2;
            if (damager instanceof CraftMyPetProjectile) {
                damager = ((CraftMyPetProjectile) damager).getMyPetProjectile().getShooter().getBukkitEntity();
            }
            if (damager instanceof MyPetBukkitEntity) {
                MyPetBukkitEntity mp2 = (MyPetBukkitEntity) damager;
                Player p2 = mp2.getOwner().getPlayer();
                LivingEntity liv = (LivingEntity) e1;
                if (!r1.canBuild(p2) || !r1.canInteractPassives(p2)) {
                    e.setCancelled(true);
                    mp2.getMyPet().removePet();
                    for (PotionEffect ef : liv.getActivePotionEffects()) {
                        liv.removePotionEffect(ef.getType());
                    }
                    p2.sendMessage(RPLang.get("mplistener.cantattack.passives"));
                    return;
                }
            }
        }
        if (e1 instanceof Player) {
            Entity damager = e2;
            if (damager instanceof CraftMyPetProjectile) {
                damager = ((CraftMyPetProjectile) damager).getMyPetProjectile().getShooter().getBukkitEntity();
            }
            if (damager instanceof MyPetBukkitEntity) {
                MyPetBukkitEntity mp2 = (MyPetBukkitEntity) damager;
                Player p2 = mp2.getOwner().getPlayer();
                if (!r1.canPVP((Player) e1, p2)) {
                    e.setCancelled(true);
                    mp2.getMyPet().removePet();
                    for (PotionEffect ef : p2.getActivePotionEffects()) {
                        p2.removePotionEffect(ef.getType());
                    }
                    p2.sendMessage(RPLang.get("mplistener.cantattack.players"));
                }
            }
        }
    }
}
Also used : MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) PotionEffect(org.bukkit.potion.PotionEffect) CraftMyPetProjectile(de.Keyle.MyPet.api.skill.skills.ranged.CraftMyPetProjectile) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 43 with Region

use of br.net.fabiozumbi12.RedProtect.Sponge.Region in project RedProtect by FabioZumbi12.

the class McMMoListener method onSecondaryAbilityEvent.

@EventHandler
public void onSecondaryAbilityEvent(SecondaryAbilityEvent e) {
    RedProtect.get().logger.debug("Mcmmo SecondaryAbilityEvent event.");
    Player p = e.getPlayer();
    Region r = RedProtect.get().rm.getTopRegion(e.getPlayer().getLocation());
    if (r == null) {
        return;
    }
    if (!r.canSkill(p)) {
        e.setCancelled(true);
    }
    if (!r.canPVP(p, null) && (e.getSkill().equals(SkillType.SWORDS) || e.getSkill().equals(SkillType.UNARMED) || e.getSkill().equals(SkillType.AXES))) {
        e.setCancelled(true);
    }
}
Also used : Player(org.bukkit.entity.Player) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region) EventHandler(org.bukkit.event.EventHandler)

Example 44 with Region

use of br.net.fabiozumbi12.RedProtect.Sponge.Region in project RedProtect by FabioZumbi12.

the class McMMoListener method onFakeEntityDamageByEntityEvent.

@EventHandler
public void onFakeEntityDamageByEntityEvent(FakeEntityDamageByEntityEvent e) {
    RedProtect.get().logger.debug("Mcmmo FakeEntityDamageByEntityEvent event.");
    if (e.getDamager() instanceof Player) {
        Player p = (Player) e.getDamager();
        Region r = RedProtect.get().rm.getTopRegion(e.getEntity().getLocation());
        if (e.getEntity() instanceof Animals) {
            if (r != null && !r.canInteractPassives(p)) {
                RPLang.sendMessage(p, "entitylistener.region.cantpassive");
                e.setCancelled(true);
            }
        }
        if (e.getEntity() instanceof Player) {
            if (r != null && !r.canPVP(p, (Player) e.getEntity())) {
                RPLang.sendMessage(p, "entitylistener.region.cantpvp");
                e.setCancelled(true);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) Animals(org.bukkit.entity.Animals) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region) EventHandler(org.bukkit.event.EventHandler)

Example 45 with Region

use of br.net.fabiozumbi12.RedProtect.Sponge.Region in project RedProtect by FabioZumbi12.

the class McMMoListener method onPlayerActivateAbillity.

@EventHandler
public void onPlayerActivateAbillity(McMMOPlayerAbilityActivateEvent e) {
    if (e.isCancelled()) {
        return;
    }
    RedProtect.get().logger.debug("Mcmmo McMMOPlayerAbilityActivateEvent event.");
    Player p = e.getPlayer();
    // try to fix invisibility on bersek
    if (RPConfig.getBool("hooks.mcmmo.fix-berserk-invisibility") && e.getAbility().equals(AbilityType.BERSERK)) {
        p.damage(0);
        for (Entity ent : p.getNearbyEntities(10, 10, 10)) {
            if (ent instanceof LivingEntity) {
                ((LivingEntity) ent).damage(0);
            }
        }
    }
    Region r = RedProtect.get().rm.getTopRegion(p.getLocation());
    if (r == null) {
        return;
    }
    if (!r.canSkill(p)) {
        p.sendMessage(RPLang.get("mcmmolistener.notallowed"));
        e.setCancelled(true);
    }
    if (!r.canPVP(p, null) && (e.getSkill().equals(SkillType.SWORDS) || e.getSkill().equals(SkillType.UNARMED))) {
        e.setCancelled(true);
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Region (br.net.fabiozumbi12.RedProtect.Bukkit.Region)113 EventHandler (org.bukkit.event.EventHandler)92 Player (org.bukkit.entity.Player)69 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)47 Listener (org.spongepowered.api.event.Listener)46 Location (org.bukkit.Location)43 World (org.spongepowered.api.world.World)30 Block (org.bukkit.block.Block)26 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)20 PvPlayer (me.NoChance.PvPManager.PvPlayer)20 Entity (org.bukkit.entity.Entity)20 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)19 Entity (org.spongepowered.api.entity.Entity)19 LivingEntity (org.bukkit.entity.LivingEntity)16 Player (org.spongepowered.api.entity.living.player.Player)16 World (org.bukkit.World)10 Monster (org.bukkit.entity.Monster)10 Material (org.bukkit.Material)9 ClanPlayer (net.sacredlabyrinth.phaed.simpleclans.ClanPlayer)8 OfflinePlayer (org.bukkit.OfflinePlayer)8