From 8205b08df309fd032cde6743a9e0f812a1fccae9 Mon Sep 17 00:00:00 2001 From: Luke Darling Date: Wed, 12 Nov 2014 16:53:28 -0500 Subject: [PATCH] Update Main.php --- src/LDX/iProtector/Main.php | 130 +++++++++++++++++++++++++++++++----- 1 file changed, 115 insertions(+), 15 deletions(-) diff --git a/src/LDX/iProtector/Main.php b/src/LDX/iProtector/Main.php index ecfaed0..23aa19c 100644 --- a/src/LDX/iProtector/Main.php +++ b/src/LDX/iProtector/Main.php @@ -1,5 +1,7 @@ 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."); @@ -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."; } @@ -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]); @@ -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; @@ -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."; @@ -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)); } + } ?>