Skip to content

Commit 0cef92f

Browse files
committed
Add ProtectItemFrame
1 parent c98ae33 commit 0cef92f

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

ProtectItemFrame.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/**
4+
* ProtectItemFrame plugin for PocketMine-MP
5+
* Copyright (C) 2018 PresentKim <https://github.com/PMMPPlugin>
6+
*
7+
* The AGPL license differs from the other GNU licenses in that it was built for network software.
8+
* You can distribute modified versions if you keep track of the changes and the date you made them.
9+
* As per usual with GNU licenses, you must license derivatives under AGPL.
10+
* It provides the same restrictions and freedoms as the GPLv3 but with an additional clause which makes it so that source code must be distributed along with web publication.
11+
* Since web sites and services are never distributed in the traditional sense, the AGPL is the GPL of the web.
12+
*
13+
* @name ProtectItemFrame
14+
* @main presentkim\singleton\ProtectItemFrame
15+
* @version 1.0.0
16+
* @api 3.0.0-ALPHA10
17+
* @description Protect item frame by stick
18+
* @author PresentKim
19+
*/
20+
21+
22+
namespace presentkim\singleton {
23+
24+
use pocketmine\event\{
25+
Listener, player\PlayerInteractEvent
26+
};
27+
use pocketmine\{
28+
item\Item, nbt\tag\ByteTag, permission\Permission, plugin\PluginBase, tile\ItemFrame, utils\TextFormat
29+
};
30+
31+
class ProtectItemFrame extends PluginBase implements Listener{
32+
33+
public const PROTECTED_NONE = 0;
34+
35+
public const PROTECTED_DROP = 1;
36+
37+
public const PROTECTED_ROTATE = 2;
38+
39+
public function onEnable(){
40+
$this->getServer()->getPluginManager()->registerEvents($this, $this);
41+
try{
42+
Permission::loadPermission('itemframe.protect', [
43+
'description' => 'Itemframe protect',
44+
'children' => [
45+
'itemframe.protect.make' => ['description' => 'Make itemframe protect'],
46+
'itemframe.protect.drop' => ['description' => 'Drop itemframe item'],
47+
'itemframe.protect.rotate' => ['description' => 'Rotate itemframe item'],
48+
],
49+
]);
50+
} catch (\Exception $e){
51+
}
52+
}
53+
54+
/**
55+
* @priority HIGHEST
56+
*
57+
* @param PlayerInteractEvent $event
58+
*/
59+
public function onPlayerInteractEvent(PlayerInteractEvent $event){
60+
if (!$event->isCancelled()) {
61+
$block = $event->getBlock();
62+
$tile = $block->getLevel()->getTile($block);
63+
if ($tile instanceof ItemFrame && $tile->hasItem()) {
64+
$protected = $tile->namedtag->getTagValue('itemframe-protect', ByteTag::class, 0, true);
65+
66+
$player = $event->getPlayer();
67+
68+
if ($player->getInventory()->getItemInHand()->getId() === Item::STICK) {
69+
if ($player->hasPermission('itemframe.protect.make')) {
70+
$tile->namedtag->setTagValue('itemframe-protect', ByteTag::class, $protected = ++$protected % 3, true);
71+
if ($protected === self::PROTECTED_DROP) {
72+
$player->sendMessage(TextFormat::GREEN . '[ProtectItemFrame] protected drop');
73+
} elseif ($protected === self::PROTECTED_ROTATE) {
74+
$player->sendMessage(TextFormat::GREEN . '[ProtectItemFrame] protected drop & rotate');
75+
} else {
76+
$player->sendMessage(TextFormat::DARK_GREEN . '[ProtectItemFrame] unprotected');
77+
}
78+
} else {
79+
$player->sendMessage(TextFormat::RED . '[ProtectItemFrame] You don\'t have permission');
80+
}
81+
$event->setCancelled(true);
82+
} elseif ($protected === self::PROTECTED_DROP && !$player->hasPermission('itemframe.protect.drop') || $protected === self::PROTECTED_ROTATE && !$player->hasPermission('itemframe.protect.rotate')) {
83+
$event->setCancelled(true);
84+
}
85+
}
86+
}
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)