Skip to content
This repository has been archived by the owner on Jul 7, 2020. It is now read-only.

Commit

Permalink
v1.0.0 (#3)
Browse files Browse the repository at this point in the history
* v1.0.0 - Initial commits

- Added plugin.yml
- Added icon

* Create Main.php

- Added Main.php

* Kit Model and Player events

- Added Kit.php
- Added PlayerEvents.php

* Create Window.php

- Added Window.php

* Create Handler.php

- Added Window Handler

* Create KitMainMenu.php

- Added KitMainMenu.php

* Create KitError.php and KitInfo.php

- Added KitError and KitInfo windows

* Create CoolDownTask.php

- Added Cooldown task to process time

* Create LangManager.php

- Added lang config

* Create config.yml

- Added config

* Create Kits.yml

- Added config for kits

* Update README.md

- Added info

* Added about
  • Loading branch information
Infernus101 committed Oct 23, 2017
1 parent 2f9fde6 commit c57fdaa
Show file tree
Hide file tree
Showing 15 changed files with 751 additions and 3 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# [WIP] KitUI
# KitUI
Select kits from a form! Just do /kit and get kits info and select it if available! For MCBE! PMMP support only!
(No Plugin library or Virion required! Easy as pie!)

## Holdup
Star, watch this repo if you're interested and want me to work on this!
## Usage
- /kit - List of kits fetched from kits.yml on a form!

## Features
- Easy config
- Enchantments
- Effects
- Commands
- Cooldown
- Custom Names for items
- Economy
- Unlimited Kits

## About
- Made by @Infernus101
- Github - http://github.com/Infernus101
- Twitter - http://twitter.com/Infernus101
- Email - [email protected]
- Server - Fallentech.tk 19132 | Countertech.tk 19132
Binary file added kit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: KitUI
author: Infernus101
api: [3.0.0-ALPHA1, 3.0.0-ALPHA2, 3.0.0-ALPHA3, 3.0.0-ALPHA4, 3.0.0-ALPHA5, 3.0.0-ALPHA6, 3.0.0-ALPHA7, 3.0.0-ALPHA8, 3.0.0-ALPHA9]
main: Infernus101\KitUI\Main
version: 1.0.0
permission: # kit permissions - kit.<kit name>
kit.command:
description: kit command!
default: true
commands:
kit:
description: Kit UI!
6 changes: 6 additions & 0 deletions resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
# Players can't get another kit till they die
one-kit-per-life: true
# Players can claim another kit if they relog(only if one-kit-per-life is true)
reset-on-logout: true
...
41 changes: 41 additions & 0 deletions resources/kits.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
testkit:
# Format: "id:damage:count:name:ench_name:ench_level"

# If you want only custom name (no enchantments): "id:damage:count:name"

# If you don't want enchantments or custom name: "id:damage:count"

# If you want only enchantments (no custom name): "id:damage:count:DEFAULT:ench_name:ench_level" -- you have to put DEFAULT in the name field

# If you want more than one enchantment just do: "id:damage:count:name:ench1_name:ench1_level:ench2_name:ench2_level"
# or "id:damage:count:DEFAULT:ench1_name:ench1_level:ench2_name:ench2_level" if you don't want a custom item name

# Please note: You have to write numeric IDs
items:
- "260:0:10"
- "267:0:1:Sword Name:sharpness:3:knockback:1"
helmet: "302:0:1"
chestplate: "303:0:1:DEFAULT:protection:1"
leggings: "304:0:1:Leggings Name"
boots: "305:0:1"

commands: # {player} will get replaced by player's name
- "tell {player} Kit claimed!"

cooldown:
hours: 24
minutes: 30

# Format: "name:time:amplifier"
# Time is in seconds
effects:
- "speed:120:2"

# Add a cost for the kit. Compatible with EconomyAPI only
# Put 0 if you want the kit to be free
money: 50

# Info to show when selected a Kit in UI form
info: "This kit is too op XD Cost: 50$"
...
159 changes: 159 additions & 0 deletions src/Infernus101/KitUI/Kit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace Infernus101\KitUI;

use pocketmine\command\ConsoleCommandSender;
use pocketmine\entity\Effect;
use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\Item;
use pocketmine\utils\TextFormat;
use pocketmine\Player;

class Kit{

public $pl;
public $data;
public $name;
public $cost = 0;
public $timer;
public $timers = [];

public function __construct(Main $pl, array $data, string $name){
$this->pl = $pl;
$this->data = $data;
$this->name = $name;
$this->timer = $this->getTimerMinutes();
if(file_exists($this->pl->getDataFolder()."timer/".strtolower($this->name).".sl")){
$this->timers = unserialize(file_get_contents($this->pl->getDataFolder()."timer/".strtolower($this->name).".sl"));
}
if(isset($this->data["money"]) and $this->data["money"] != 0){
$this->cost = (int) $this->data["money"];
}
}

public function getName() : string{
return $this->name;
}

public function isInventoryFull(Player $player){
$full = true;
for($i = 0; $i < $player->getInventory()->getSize(); $i++){
if($player->getInventory()->getItem($i)->getId() === 0){
$full = false;
}
}
return $full;
}

public function add(Player $player){
$inv = $player->getInventory();
$flag = false;

isset($this->data["helmet"]) and $inv->setHelmet($this->loadItem(...explode(":", $this->data["helmet"])));
isset($this->data["chestplate"]) and $inv->setChestplate($this->loadItem(...explode(":", $this->data["chestplate"])));
isset($this->data["leggings"]) and $inv->setLeggings($this->loadItem(...explode(":", $this->data["leggings"])));
isset($this->data["boots"]) and $inv->setBoots($this->loadItem(...explode(":", $this->data["boots"])));

foreach($this->data["items"] as $itemString){
if(!$this->isInventoryFull($player)){
$inv->setItem($inv->firstEmpty(), $i = $this->loadItem(...explode(":", $itemString)));
}
else{
$flag = true;
}
}

if($flag == true){
$player->sendMessage($this->pl->language->getTranslation("inv-full"));
}

if(isset($this->data["effects"])){
foreach($this->data["effects"] as $effectString){
$e = $this->loadEffect(...explode(":", $effectString));
if($e !== null){
$player->addEffect($e);
}
}
}

if(isset($this->data["commands"]) and is_array($this->data["commands"])){
foreach($this->data["commands"] as $cmd){
$this->pl->getServer()->dispatchCommand(new ConsoleCommandSender(), str_replace("{player}", $player->getName(), $cmd));
}
}

if($this->timer){
$this->timers[strtolower($player->getName())] = $this->timer;
}

$this->pl->hasKit[strtolower($player->getName())] = $this;
$player->sendMessage($this->pl->language->getTranslation("sel-kit", ucfirst($this->name)));

}

public function loadItem(int $id = 0, int $damage = 0, int $count = 1, string $name = "default", ...$enchantments) : Item{
$item = Item::get($id, $damage, $count);
if(strtolower($name) !== "default"){
$item->setCustomName($name);
}
foreach($enchantments as $key => $name_level){
if($key % 2 === 0){ //Name expected
$ench = Enchantment::getEnchantmentByName($name_level);
}else{ //Level expected
if(isset($ench) and $ench !== null){
$item->addEnchantment($ench->setLevel($name_level));
}
}
}
return $item;
}

public function loadEffect(string $name = "INVALID", int $seconds = 60, int $amplifier = 1){
$e = Effect::getEffectByName($name);
if($e !== null){
return $e->setDuration($seconds * 20)->setAmbient($amplifier);
}
return null;
}

public function getTimerMinutes() : int{
$min = 0;
if(isset($this->data["cooldown"]["minutes"])){
$min += (int) $this->data["cooldown"]["minutes"];
}
if(isset($this->data["cooldown"]["hours"])){
$min += (int) $this->data["cooldown"]["hours"] * 60;
}
return $min;
}

public function getTimerLeft(Player $player) : string{
if(($minutes = $this->timers[strtolower($player->getName())]) < 60){
return $this->pl->language->getTranslation("timer-format1", $minutes);
}
if(($modulo = $minutes % 60) !== 0){
return $this->pl->language->getTranslation("timer-format2", floor($minutes / 60), $modulo);
}
return $this->pl->language->getTranslation("timer-format3", $minutes / 60);
}

public function processTimer(){
foreach($this->timers as $player => $min){
$this->timers[$player] -= 1;
if($this->timers[$player] <= 0){
unset($this->timers[$player]);
}
}
}

public function testPermission(Player $player) : bool{
return $player->hasPermission("kit.".strtolower($this->name));
}

public function save(){
if(count($this->timers) > 0){
file_put_contents($this->pl->getDataFolder()."timer/".strtolower($this->name).".sl", serialize($this->timers));
}
}

}
111 changes: 111 additions & 0 deletions src/Infernus101/KitUI/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Infernus101\KitUI;

use Infernus101\KitUI\UI\Handler;
use Infernus101\KitUI\lang\LangManager;
use Infernus101\KitUI\tasks\CoolDownTask;
use pocketmine\Player;
use pocketmine\Server;
use pocketmine\command\CommandSender;
use pocketmine\command\ConsoleCommandSender;
use pocketmine\command\Command;
use pocketmine\event\Listener;
use pocketmine\plugin\PluginBase;
use pocketmine\network\mcpe\protocol\ModalFormRequestPacket;
use pocketmine\utils\Config;
use pocketmine\utils\TextFormat;

class Main extends PluginBase implements Listener {

public $kits = [];
public $kitused = [];
public $language;

public function onEnable(){
@mkdir($this->getDataFolder()."timer/");
$this->getServer()->getLogger()->notice("[KitUI] Enabled! - By Infernus101");
$this->configFixer();
$files = array("kits.yml","config.yml");
foreach($files as $file){
if(!file_exists($this->getDataFolder() . $file)){
@mkdir($this->getDataFolder());
file_put_contents($this->getDataFolder() . $file, $this->getResource($file));
}
}
$this->kit = new Config($this->getDataFolder() . "kit.yml", Config::YAML);
$this->config = new Config($this->getDataFolder() . "config.yml", Config::YAML);
$this->language = new LangManager($this);
$this->getServer()->getPluginManager()->registerEvents(new PlayerEvents($this), $this);
$this->getServer()->getScheduler()->scheduleDelayedRepeatingTask(new CoolDownTask($this), 1200, 1200);
$allKits = yaml_parse_file($this->getDataFolder()."kits.yml");
foreach($allKits as $name => $data){
$this->kits[$name] = new Kit($this, $data, $name);
}
}

public function onDisable(){
foreach($this->kits as $kit){
$kit->save();
}
$this->getServer()->getLogger()->notice("[KitUI] Disabled! - By Infernus101");
}

public function onCommand(CommandSender $sender, Command $cmd, String $label, array $args): bool{
if(!$sender instanceof Player){
$sender->sendMessage(TextFormat::RED."> Command must be run ingame!");
return true;
}
switch(strtolower($cmd->getName())){
case "kit":
if(isset($args[0])){
$sender->sendMessage(TextFormat::GREEN."About:\nKit UI by Infernus101! github.com/Infernus101/KitUI\n".TextFormat::AQUA."Servers - FallenTech.tk | CounterTech.tk 19132");
return false;
}
$handler = new Handler();
$packet = new ModalFormRequestPacket();
$packet->formId = $handler->getWindowIdFor(Handler::KIT_MAIN_MENU);
$packet->formData = $handler->getWindowJson(Handler::KIT_MAIN_MENU, $this, $sender);
$sender->dataPacket($packet);
break;
}
return true;
}

private function configFixer(){
$this->saveResource("kits.yml");
$allKits = yaml_parse_file($this->getDataFolder()."kits.yml");
$this->fixConfig($allKits);
foreach($allKits as $name => $data){
$this->kits[$name] = new Kit($this, $data, $name);
}
}

private function fixConfig(&$config){
foreach($config as $name => $kit){
if(isset($kit["users"])){
$users = array_map("strtolower", $kit["users"]);
$config[$name]["users"] = $users;
}
if(isset($kit["worlds"])){
$worlds = array_map("strtolower", $kit["worlds"]);
$config[$name]["worlds"] = $worlds;
}
}
}

public function getPlayerKit($player, $obj = false){
if($player instanceof Player){
$player = $player->getName();
}
return isset($this->kitused[strtolower($player)]) ? ($obj ? $this->kitused[strtolower($player)] : $this->kitused[strtolower($player)]->getName()) : null;
}

public function getKit(string $kit){
$lower = array_change_key_case($this->kits, CASE_LOWER);
if(isset($lower[strtolower($kit)])){
return $lower[strtolower($kit)];
}
return null;
}
}
Loading

0 comments on commit c57fdaa

Please sign in to comment.