Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Inject All Virions into your plugin using a single command.
<?php
declare(strict_types = 1);
/**
* @name InjectAllVirions
* @main JackMD\InjectAllVirions\Main
* @version 1.0.0
* @api 3.0.0
* @description A plugin script which allows injecting all virions in a plugin using a single command.
* @author JackMD
* @softDepend VirionTools
*/
namespace JackMD\InjectAllVirions
{
use JackMD\InjectAllVirions\Command\InjectAllCommand;
use JackMD\VirionTools\VirionTools;
use pocketmine\plugin\PluginBase;
class Main extends PluginBase{
public function onEnable(){
$virionTools = $this->getServer()->getPluginManager()->getPlugin("VirionTools");
if($virionTools instanceof VirionTools){
$this->getServer()->getCommandMap()->register("viriontools", new InjectAllCommand($virionTools));
$this->getLogger()->info("§aInjectAllVirions Script enabled.");
}else{
$this->getLogger()->error("Plugin VirionTools not found. Disabling.");
$this->getServer()->getPluginManager()->disablePlugin($this);
}
}
}
}
namespace JackMD\InjectAllVirions\Command
{
use JackMD\VirionTools\utils\VirionInjectScript;
use JackMD\VirionTools\VirionTools;
use Phar;
use pocketmine\command\CommandSender;
use pocketmine\command\PluginCommand;
class InjectAllCommand extends PluginCommand{
/** @var VirionTools */
private $plugin;
/**
* InjectDependenciesCommand constructor.
*
* @param VirionTools $plugin
*/
public function __construct(VirionTools $plugin){
parent::__construct("injectall", $plugin);
$this->setDescription("Inject all virions in the plugin using a single command.");
$this->setUsage("/injectall [string:plugin]");
$this->setPermission("vt.cmd.id");
$this->setAliases(
[
"id",
"ia"
]
);
$this->plugin = $plugin;
}
/**
* @param CommandSender $sender
* @param string $commandLabel
* @param array $args
*/
public function execute(CommandSender $sender, string $commandLabel, array $args): void{
if(!$this->testPermission($sender)){
return;
}
if(!isset($args[0])){
$sender->sendMessage(VirionTools::PREFIX . "§cUsage: §7/ia [string:plugin]");
return;
}
$plugin = (string) $args[0];
if(strpos($plugin, ".phar") === false){
$plugin = $plugin . ".phar";
}
if(!$this->plugin->pluginPharExists($plugin)){
$sender->sendMessage(VirionTools::PREFIX . "§cPhar plugin with the name §d" . $plugin . " §cwas not found.");
$sender->sendMessage(VirionTools::PREFIX . "§aMake sure that the phared plugin, to which the virion is to be injected in, is located in §2plugin_data\VirionTools\plugins.");
return;
}
$pluginDirectory = $this->plugin->getDataFolder() . "plugins" . DIRECTORY_SEPARATOR;
$virionDirectory = $this->plugin->getDataFolder() . "builds" . DIRECTORY_SEPARATOR;
$hostPath = $pluginDirectory . $plugin;
$host = new Phar($hostPath);
$host->startBuffering();
$hostPath = "phar://" . str_replace(DIRECTORY_SEPARATOR, "/", $host->getPath()) . "/";
if(!isset($host["plugin.yml"])){
$sender->sendMessage(VirionTools::PREFIX . "§4plugin.yml §cnot found in plugin §6{$plugin}§c. Aborting");
return;
}
$pluginYml = yaml_parse(file_get_contents($hostPath . "plugin.yml"));
if(!is_array($pluginYml)){
$sender->sendMessage(VirionTools::PREFIX . "§cCorrupted plugin.yml found in plugin §6$plugin");
return;
}
if(!isset($pluginYml["virions"])){
$sender->sendMessage(VirionTools::PREFIX . "§4virions §ckey not found in plugin.yml of §6{$plugin}§c. Aborting.");
return;
}
$start = microtime(true);
$count = 0;
$sender->sendMessage(VirionTools::PREFIX . "§aInitiating virion injection process");
$virions = $pluginYml["virions"];
foreach($virions as $virion){
if(strpos($virion, ".phar") === false){
$virion = $virion . ".phar";
}
if(!$this->plugin->virionPharExists($virion)){
$sender->sendMessage(VirionTools::PREFIX . "§cVirion with the name §d" . $virion . " §cwas not found.");
$sender->sendMessage(VirionTools::PREFIX . "§aMake sure that the virion you want to inject is located in §2plugin_data\VirionTools\builds.");
continue;
}
$virus = new Phar($virionDirectory . $virion);
if(VirionInjectScript::virion_infect($sender, $virion, $virus, $plugin, $host)){
$sender->sendMessage(VirionTools::PREFIX . "§aVirion §d$virion §asuccessfully injected in plugin §6$plugin");
$count++;
}
}
$host->stopBuffering();
$sender->sendMessage(VirionTools::PREFIX . "§aDone in §e" . round(microtime(true) - $start, 3) . "s");
$sender->sendMessage(VirionTools::PREFIX . "§aPlugin §6$plugin §ainfected with §d$count virion(s) §asuccessfully.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.