Skip to content

Commit

Permalink
Temoporaly here
Browse files Browse the repository at this point in the history
  • Loading branch information
Рауль Вахитов authored Jul 12, 2016
1 parent c2edffa commit 25c3da3
Showing 1 changed file with 108 additions and 72 deletions.
180 changes: 108 additions & 72 deletions src/shoghicp/FastTransfer/FastTransfer.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

/*
* FastTransfer plugin for PocketMine-MP
* Copyright (C) 2015 Shoghi Cervantes <https://github.com/shoghicp/FastTransfer>
* SynapseTransfer plugin for Genisys
* Copyright (C) 2016 Raul Vakhitov <https://github.com/MrGenga/SynapseTransfer>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -15,26 +15,87 @@
* GNU General Public License for more details.
*/

namespace shoghicp\FastTransfer;
namespace MrGenga\SynapseTransfer;

use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\event\TranslationContainer;
use pocketmine\network\Network;
use pocketmine\network\RakLibInterface;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\TextFormat;

class FastTransfer extends PluginBase{
use synapse\Player as SynapsePlayer;

private $lookup = [];
class SynapseTransfer extends PluginBase{

protected $config;

public function onEnable(){
@mkdir($this->getDataFolder());
$this->config = new Config($this->getDataFolder() . "config.yml", Config::YAML, [
"list" => [
"hub" => "hub"
]
]);
$this->conf = $this->config->getAll();
$this->list = $this->conf["list"];
if(!$this->getServer()->isSynapseEnabled()){
$this->getLogger()->error("Synapse Client service has been disabled, this plugin won't work!");
$this->setEnabled(false);
return;
}
$this->getLogger()->info("§aSynapseTransfer has been enabled.");
}

public function onDisable(){
$this->conf["list"] = $this->list;
$this->config->setAll($this->conf);
$this->config->save();
$this->getLogger()->info("§cSynapseTransfer has been disabled.");
}

/**
* @param string $ld
* @return null|string
*/
public function getDescriptionByListData(string $ld){
if(isset($this->list[$ld])){
return $this->list[$ld];
}
return null;
}

/**
* @param $des
* @return array|null
*/
public function getClientDataByDescription(string $des){
foreach($this->getServer()->getSynapse()->getClientData() as $cdata){
if($cdata["description"] == $des){
return $cdata;
}
}
return null;
}

/**
* @param string $des
* @return null|string
*/
public function getClientHashByDescription(string $des){
foreach($this->getServer()->getSynapse()->getClientData() as $hash => $cdata){
if($cdata["description"] == $des){
return $hash;
}
}
return null;
}

/**
* Will transfer a connected player to another server.
* This will trigger PlayerTransferEvent
*
* Player transfer might not be instant if you use a DNS address instead of an IP address
* Player transfer works only if he has connected from Synapse
*
* @param Player $player
* @param string $address
Expand All @@ -43,54 +104,43 @@ class FastTransfer extends PluginBase{
*
* @return bool
*/
public function transferPlayer(Player $player, $address, $port = 19132, $message = "You are being transferred"){
$ev = new PlayerTransferEvent($player, $address, $port, $message);
public function transferPlayer(Player $player, $server, $message = "You are being transferred"){
if(!$player instanceof SynapsePlayer){
$this->getLogger()->warn("Only SynapsePlayer can transfer!");
return false;
}
$ev = new PlayerTransferEvent($player, $server, $message);
$this->getServer()->getPluginManager()->callEvent($ev);
if($ev->isCancelled()){
return false;
}

$ip = $this->lookupAddress($ev->getAddress());

if($ip === null){
return false;
}

if($message !== null and $message !== ""){
$player->sendMessage($message);
}

$packet = new StrangePacket();
$packet->address = $ip;
$packet->port = $ev->getPort();
$player->dataPacket($packet->setChannel(Network::CHANNEL_PRIORITY));
$rakLib = null;
foreach($this->getServer ->getNetwork()-> getInterfaces() as $interface) {
if($interface instanceof RakLibInterface) {
$raklib = $interface;
break;
}
$des = $this->getDescriptionByListData($server);
if($des == null){
$this->getLogger()->warn(TextFormat::RED . "Undefined SynapseClient $server");
return false;
}
if($rakLib == null){
return;
if(($hash = $this->getClientHashByDescription($des)) != null){
if($des == $sender->getServer()->getSynapse()->getDescription()){
$this->getLogger()->warn(TextFormat::RED . "Cannot transfer to the current server");
return false;
}
$player->transfer($hash);
}else{
$this->getLogger()->warn(TextFormat::RED . "$server is not a SynapseClient");
}
$identifier = $player ->getAddress () . ":" . $player->getPort();
$rakLib->closeSession($identifier, "transfer");

return true;
}

/**
* Clear the DNS lookup cache.
*/
public function cleanLookupCache(){
$this->lookup = [];
}


public function onCommand(CommandSender $sender, Command $command, $label, array $args){
if($label === "transfer"){
if(count($args) < 2 or count($args) > 3 or (count($args) === 2 and !($sender instanceof Player))){
if(count($args) < 1 or count($args) > 2 or (count($args) === 1 and !($sender instanceof Player))){
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$command->getUsage()]));

return true;
Expand All @@ -99,22 +149,35 @@ public function onCommand(CommandSender $sender, Command $command, $label, array
/** @var Player $target */
$target = $sender;

if(count($args) === 3){
if(count($args) === 2){
$target = $sender->getServer()->getPlayer($args[0]);
$address = $args[1];
$port = (int) $args[2];
$server = $args[1];
}else{
$address = $args[0];
$port = (int) $args[1];
$server = $args[0];
}

if($target === null){
$sender->sendMessage(new TranslationContainer(TextFormat::RED . "%commands.generic.player.notFound"));
return true;
}

$sender->sendMessage("Transferring player " . $target->getDisplayName() . " to $address:$port");
if(!$this->transferPlayer($target, $address, $port)){
$des = $this->getDescriptionByListData($server);
if($des == null){
$sender->sendMessage(TextFormat::RED . "Undefined SynapseClient $server");
return true;
}
if($target instanseof SynapsePlayer && ($hash = $this->getClientHashByDescription($des)) != null){
if($des == $sender->getServer()->getSynapse()->getDescription()){
$sender->sendMessage(TextFormat::RED . "Cannot transfer to the current server");
return true;
}
}else{
$sender->sendMessage(TextFormat::RED . $target->getName() + " is not a SynapsePlayer or $server is not a SynapseClient");
return true;
}

$sender->sendMessage("Transferring player " . $target->getDisplayName() . " to $server");
if(!$this->transferPlayer($target, $server, "")){
$sender->sendMessage(TextFormat::RED . "An error occurred during the transfer");
}

Expand All @@ -124,31 +187,4 @@ public function onCommand(CommandSender $sender, Command $command, $label, array
return false;
}

/**
* @param $address
*
* @return null|string
*/
private function lookupAddress($address){
//IP address
if(preg_match("/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/", $address) > 0){
return $address;
}

$address = strtolower($address);

if(isset($this->lookup[$address])){
return $this->lookup[$address];
}

$host = gethostbyname($address);
$host = file_get_contents("http://mrgenga.tk/host2ip.php?host=".$host);

if($host === "fail"){
return null;
}

$this->lookup[$address] = $host;
return $host;
}
}

0 comments on commit 25c3da3

Please sign in to comment.