Skip to content

Commit

Permalink
Release 1.3.0
Browse files Browse the repository at this point in the history
Now Item durablity appears well
Now item can be eat

Closes #9
Closes #5
Closes #6
  • Loading branch information
alvin0319 committed Jan 4, 2021
1 parent 0170ba4 commit d08a8f5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 12 deletions.
10 changes: 7 additions & 3 deletions example/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ items:
name: facebook
allow_off_hand: true
max_stack_size: 64
texture: ccc
durable: true
texture: facebook
durable: false
max_durability: 64
mining_speed: 10
mining_speed: 10
food: true
can_always_eat: true
nutrition: 10
saturation: 2
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: CustomItemLoader
author: alvin0319
main: alvin0319\CustomItemLoader\CustomItemLoader
version: 1.2.1
version: 1.3.0
api: 3.0.0

mcpe-protocol: 422
Expand Down
38 changes: 30 additions & 8 deletions src/alvin0319/CustomItemLoader/CustomItemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use alvin0319\CustomItemLoader\command\ResourcePackCreateCommand;
use alvin0319\CustomItemLoader\item\CustomDurableItem;
use alvin0319\CustomItemLoader\item\CustomFoodItem;
use alvin0319\CustomItemLoader\item\CustomItem;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;
Expand Down Expand Up @@ -129,17 +130,26 @@ public function parseTag(string $name, array $data) : CompoundTag{
$max_stack_size = (int) ($data["max_stack_size"] ?? 64);
$mining_speed = (float) ($data["mining_speed"] ?? 1);

$food = (int) ($data["food"] ?? false);
$can_always_eat = (int) ($data["can_always_eat"] ?? false);
$nutrition = (int) ($data["nutrition"] ?? 1);
$saturation = (float) ($data["saturation"] ?? 1);

$nbt = new CompoundTag("", [
new CompoundTag("components", [
new ByteTag("allow_off_hand", $allow_off_hand),
new ByteTag("can_destroy_in_creative", $can_destroy_in_creative),
new ByteTag("creative_category", $creative_category),
new ByteTag("hand_equipped", $hand_equipped),
new IntTag("max_stack_size", $max_stack_size),
new FloatTag("mining_speed", $mining_speed),
new ByteTag("animates_in_toolbar", 1),
new CompoundTag("minecraft:icon", [
new StringTag("texture", $data["texture"])
]),
new CompoundTag("item_properties", [
new IntTag("use_duration", 32),
new IntTag("use_animation", ($food === 1 ? 1 : 0)), // 2 is potion, but not now
new ByteTag("allow_off_hand", $allow_off_hand),
new ByteTag("can_destroy_in_creative", $can_destroy_in_creative),
new ByteTag("creative_category", $creative_category),
new ByteTag("hand_equipped", $hand_equipped),
new IntTag("max_stack_size", $max_stack_size),
new FloatTag("mining_speed", $mining_speed),
new ByteTag("animates_in_toolbar", 1),
])
]),
new ShortTag("minecraft:identifier", $runtimeId),
Expand All @@ -155,12 +165,22 @@ public function parseTag(string $name, array $data) : CompoundTag{
]);
$durable = false;
if(isset($data["durable"]) && (bool) ($data["durable"]) !== false){
$nbt->setTag(new CompoundTag("minecraft:durability", [
$nbt->getCompoundTag("components")->setTag(new CompoundTag("minecraft:durability", [
new ShortTag("damage_change", 1),
new ShortTag("max_durable", $data["max_durability"])
]));
$durable = true;
}
if($food === 1){
$nbt->getCompoundTag("components")->setTag(new CompoundTag("minecraft:food", [
new ByteTag("can_always_eat", $can_always_eat),
new FloatTag("nutrition", $nutrition),
new StringTag("saturation_modifier", "low")
]));
$nbt->getCompoundTag("components")->setTag(new CompoundTag("minecraft:use_duration", [
new IntTag("value", 1)
]));
}
$runtimeId = $id + ($id > 0 ? 5000 : -5000);
$this->coreToNetValues[$id] = $runtimeId;
$this->netToCoreValues[$runtimeId] = $id;
Expand All @@ -170,6 +190,8 @@ public function parseTag(string $name, array $data) : CompoundTag{
try{
if($durable){
ItemFactory::registerItem(new CustomDurableItem($id, $meta, $name, $data["max_stack_size"], $data["max_durability"], $mining_speed));
}elseif($food){
ItemFactory::registerItem(new CustomFoodItem($id, $meta, $name, $data["max_stack_size"], $nutrition, $can_always_eat === 1, $saturation));
}else{
ItemFactory::registerItem(new CustomItem($id, $meta, $name, $data["max_stack_size"], $mining_speed));
}
Expand Down
46 changes: 46 additions & 0 deletions src/alvin0319/CustomItemLoader/item/CustomFoodItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace alvin0319\CustomItemLoader\item;

use pocketmine\item\Food;

class CustomFoodItem extends Food{

protected $maxStackSize = 64;

protected $nutrition;

protected $canAlwaysEat = false;

protected $saturation;

public function __construct(int $id, int $meta = 0, string $name = "Unknown", int $maxStackSize = 64, int $nutrition = 1, bool $canAlwaysEat = false, float $saturation = 1){
parent::__construct($id, $meta, $name);
$this->maxStackSize = $maxStackSize;
$this->nutrition = $nutrition;
$this->canAlwaysEat = $canAlwaysEat;
$this->saturation = $saturation;
}

public function getMaxStackSize() : int{
return $this->maxStackSize;
}

public function getFoodRestore() : int{
return $this->nutrition;
}

public function requiresHunger() : bool{
return $this->canAlwaysEat;
}

public function getSaturationRestore() : float{
return $this->saturation;
}

public function getResidue(){
return parent::getResidue(); // TODO: Find out the components
}
}

0 comments on commit d08a8f5

Please sign in to comment.