Skip to content
This repository has been archived by the owner on Oct 21, 2018. It is now read-only.

Commit

Permalink
Update Main.php
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeDarling committed Nov 12, 2014
1 parent 28a242a commit 8205b08
Showing 1 changed file with 115 additions and 15 deletions.
130 changes: 115 additions & 15 deletions src/LDX/iProtector/Main.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace LDX\iProtector;

use pocketmine\math\Vector3;
use pocketmine\command\Command;
use pocketmine\command\CommandExecutor;
Expand All @@ -9,21 +11,37 @@
use pocketmine\plugin\PluginBase;
use pocketmine\utils\TextFormat as Color;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\event\block\BlockPlaceEvent;
use pocketmine\event\block\BlockBreakEvent;

class Main extends PluginBase implements Listener {

public function onEnable() {
$this->getServer()->getPluginManager()->registerEvents($this,$this);
@mkdir($this->getDataFolder());
if(!file_exists($this->getDataFolder() . "areas.dat")) {
@mkdir($this->getDataFolder());
file_put_contents($this->getDataFolder() . "areas.dat",yaml_emit(array()));
}
if(!file_exists($this->getDataFolder() . "config.yml")) {
@mkdir($this->getDataFolder());
file_put_contents($this->getDataFolder() . "config.yml",$this->getResource("config.yml"));
}
$this->areas = array();
$this->areadata = yaml_parse(file_get_contents($this->getDataFolder() . "areas.dat"));
foreach($this->areadata as $data) {
if(isset($data["flags"]["chest"])) {
$data["flags"]["touch"] = $data["flags"]["chest"];
unset($data["flags"]["chest"]);
}
$area = new Area($data,$this);
}
$c = yaml_parse(file_get_contents($this->getDataFolder() . "config.yml"));
$this->god = $c["Default"]["God"];
$this->edit = $c["Default"]["Edit"];
$this->touch = $c["Default"]["Touch"];
}

public function onCommand(CommandSender $p,Command $cmd,$label,array $args) {
if(!($p instanceof Player)) {
$p->sendMessage(Color::RED . "Command must be used in-game.");
Expand All @@ -37,16 +55,24 @@ public function onCommand(CommandSender $p,Command $cmd,$label,array $args) {
switch($action) {
case "pos1":
if($p->hasPermission("iprotector") || $p->hasPermission("iprotector.command") || $p->hasPermission("iprotector.command.area") || $p->hasPermission("iprotector.command.area.pos1")) {
$this->pos1[$n] = new Vector3(round($p->getX() + 0.5),round($p->getY()),round($p->getZ() + 0.5));
$o = "Position 1 set to: (" . $this->pos1[$n]->getX() . "," . $this->pos1[$n]->getY() . "," . $this->pos1[$n]->getZ() . ")";
if(isset($this->sel1[$n]) || isset($this->sel2[$n])) {
$o = "You're already selecting a position!";
} else {
$this->sel1[$n] = true;
$o = "Please place or break the first position.";
}
} else {
$o = "You do not have permission to use this subcommand.";
}
break;
case "pos2":
if($p->hasPermission("iprotector") || $p->hasPermission("iprotector.command") || $p->hasPermission("iprotector.command.area") || $p->hasPermission("iprotector.command.area.pos2")) {
$this->pos2[$n] = new Vector3(round($p->getX() + 0.5),round($p->getY()),round($p->getZ() + 0.5));
$o = "Position 2 set to: (" . $this->pos2[$n]->getX() . "," . $this->pos2[$n]->getY() . "," . $this->pos2[$n]->getZ() . ")";
if(isset($this->sel1[$n]) || isset($this->sel2[$n])) {
$o = "You're already selecting a position!";
} else {
$this->sel2[$n] = true;
$o = "Please place or break the second position.";
}
} else {
$o = "You do not have permission to use this subcommand.";
}
Expand All @@ -56,7 +82,7 @@ public function onCommand(CommandSender $p,Command $cmd,$label,array $args) {
if(isset($args[1])) {
if(isset($this->pos1[$n]) && isset($this->pos2[$n])) {
if(!isset($this->areas[strtolower($args[1])])) {
$area = new Area(array("name" => strtolower($args[1]),"flags" => array("edit" => true,"god" => false,"chest" => false),"pos1" => array($this->pos1[$n]->getX(),$this->pos1[$n]->getY(),$this->pos1[$n]->getZ()),"pos2" => array($this->pos2[$n]->getX(),$this->pos2[$n]->getY(),$this->pos2[$n]->getZ())),$this);
$area = new Area(array("name" => strtolower($args[1]),"flags" => array("edit" => true,"god" => false,"touch" => false),"pos1" => array($this->pos1[$n]->getX(),$this->pos1[$n]->getY(),$this->pos1[$n]->getZ()),"pos2" => array($this->pos2[$n]->getX(),$this->pos2[$n]->getY(),$this->pos2[$n]->getZ())),$this);
$this->saveAreas();
unset($this->pos1[$n]);
unset($this->pos2[$n]);
Expand Down Expand Up @@ -90,7 +116,7 @@ public function onCommand(CommandSender $p,Command $cmd,$label,array $args) {
if(isset($args[2])) {
if(isset($area->flags[strtolower($args[2])])) {
$flag = strtolower($args[2]);
if(isset($args[3]) && strtolower($args[3]) == ("on" || "off" || "true" || "false")) {
if(isset($args[3])) {
$mode = strtolower($args[3]);
if($mode == ("true" || "on")) {
$mode = true;
Expand All @@ -111,7 +137,7 @@ public function onCommand(CommandSender $p,Command $cmd,$label,array $args) {
$o = "Flag not found. (Flags: edit, god)";
}
} else {
$o = "Please specify a flag. (Flags: edit, god)";
$o = "Please specify a flag. (Flags: god, edit, touch)";
}
} else {
$o = "Area doesn't exist.";
Expand Down Expand Up @@ -147,51 +173,125 @@ public function onCommand(CommandSender $p,Command $cmd,$label,array $args) {
$p->sendMessage($o);
return true;
}

public function onHurt(EntityDamageEvent $event) {
if($event->getEntity() instanceof Player) {
$p = $event->getEntity();
$cancel = false;
$damage = false;
$pos = new Vector3($p->getX(),$p->getY(),$p->getZ());
foreach($this->areas as $area) {
if($area->contains($pos) && $area->getFlag("god")) {
$cancel = true;
} else if($area->contains($pos) && !$area->getFlag("god")) {
$damage = true;
}
}
if($cancel) {
$event->setCancelled();
} else {
if($this->god && !$damage) {
$event->setCancelled();
}
}
}
}

public function onBlockBreak(BlockBreakEvent $event) {
$b = $event->getBlock();
$p = $event->getPlayer();
$n = strtolower($p->getName());
$cancel = false;
$pos = new Vector3($b->x,$b->y,$b->z);
foreach($this->areas as $area) {
if($area->contains($pos) && $area->getFlag("edit") && !($p->hasPermission("iprotector") || $p->hasPermission("iprotector.access"))) {
$cancel = true;
$allow = false;
if(isset($this->sel1[$n])) {
unset($this->sel1[$n]);
$this->pos1[$n] = new Vector3(round($b->getX()),round($b->getY()),round($b->getZ()));
$p->sendMessage("Position 1 set to: (" . $this->pos1[$n]->getX() . ", " . $this->pos1[$n]->getY() . ", " . $this->pos1[$n]->getZ() . ")");
$event->setCancelled();
} else if(isset($this->sel2[$n])) {
unset($this->sel2[$n]);
$this->pos2[$n] = new Vector3(round($b->getX()),round($b->getY()),round($b->getZ()));
$p->sendMessage("Position 2 set to: (" . $this->pos2[$n]->getX() . ", " . $this->pos2[$n]->getY() . ", " . $this->pos2[$n]->getZ() . ")");
$event->setCancelled();
} else {
$pos = new Vector3($b->x,$b->y,$b->z);
foreach($this->areas as $area) {
if($area->contains($pos) && $area->getFlag("edit") && !($p->hasPermission("iprotector") || $p->hasPermission("iprotector.access"))) {
$cancel = true;
} else if($area->contains($pos) && $area->getFlag("edit") == false) {
$allow = true;
}
}
if($cancel) {
$event->setCancelled();
} else {
if($this->edit && !($p->hasPermission("iprotector") || $p->hasPermission("iprotector.access")) && !$allow) {
$event->setCancelled();
}
}
}
if($cancel) {
}

public function onBlockPlace(BlockPlaceEvent $event) {
$b = $event->getBlock();
$p = $event->getPlayer();
$n = strtolower($p->getName());
$cancel = false;
$allow = false;
if(isset($this->sel1[$n])) {
unset($this->sel1[$n]);
$this->pos1[$n] = new Vector3(round($b->getX()),round($b->getY()),round($b->getZ()));
$p->sendMessage("Position 1 set to: (" . $this->pos1[$n]->getX() . ", " . $this->pos1[$n]->getY() . ", " . $this->pos1[$n]->getZ() . ")");
$event->setCancelled();
} else if(isset($this->sel2[$n])) {
unset($this->sel2[$n]);
$this->pos2[$n] = new Vector3(round($b->getX()),round($b->getY()),round($b->getZ()));
$p->sendMessage("Position 2 set to: (" . $this->pos2[$n]->getX() . ", " . $this->pos2[$n]->getY() . ", " . $this->pos2[$n]->getZ() . ")");
$event->setCancelled();
} else {
$pos = new Vector3($b->x,$b->y,$b->z);
foreach($this->areas as $area) {
if($area->contains($pos) && $area->getFlag("edit") && !($p->hasPermission("iprotector") || $p->hasPermission("iprotector.access"))) {
$cancel = true;
} else if($area->contains($pos) && $area->getFlag("edit") == false) {
$allow = true;
}
}
if($cancel) {
$event->setCancelled();
} else {
if($this->edit && !($p->hasPermission("iprotector") || $p->hasPermission("iprotector.access")) && !$allow) {
$event->setCancelled();
}
}
}
}
public function onBlockPlace(BlockPlaceEvent $event) {

public function onBlockTouch(PlayerInteractEvent $event) {
$b = $event->getBlock();
$p = $event->getPlayer();
$cancel = false;
$allow = false;
$pos = new Vector3($b->x,$b->y,$b->z);
foreach($this->areas as $area) {
if($area->contains($pos) && $area->getFlag("edit") && !($p->hasPermission("iprotector") || $p->hasPermission("iprotector.access"))) {
if($area->contains($pos) && $area->getFlag("touch") && !($p->hasPermission("iprotector") || $p->hasPermission("iprotector.access"))) {
$cancel = true;
} else if($area->contains($pos) && $area->getFlag("touch") == false) {
$allow = true;
}
}
if($cancel) {
$event->setCancelled();
} else {
if($this->touch && !($p->hasPermission("iprotector") || $p->hasPermission("iprotector.access")) && !$allow) {
$event->setCancelled();
}
}
}

public function saveAreas() {
file_put_contents($this->getDataFolder() . "areas.dat",yaml_emit($this->areadata));
}

}
?>

0 comments on commit 8205b08

Please sign in to comment.