mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 12:09:43 +00:00
rewritten how item durability/count is handled on serverside
This commit is contained in:
parent
4880be5452
commit
841e12b9ea
30 changed files with 128 additions and 152 deletions
|
|
@ -220,18 +220,30 @@ class Item{
|
|||
|
||||
}
|
||||
|
||||
public function useOn($object, $force = false){
|
||||
if($force){
|
||||
if(($object instanceof Entity) and !$this->isSword()){
|
||||
$this->meta += 2;
|
||||
}else{
|
||||
$this->meta++;
|
||||
/**
|
||||
* @param int $dmg - damage dealt to item
|
||||
* @param Player $player - player who used the item
|
||||
*/
|
||||
public function hurtAndBreak($dmg, Player $player){
|
||||
if(($player->gamemode & 1) !== 0) return; //dont break items in creative
|
||||
|
||||
if($this->getMaxDurability() !== false){
|
||||
$this->meta += $dmg;
|
||||
if($this->meta > $this->getMaxDurability()){
|
||||
$player->consumeSingleItem();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function hurtEnemy(Entity $target, Player $attacker){
|
||||
|
||||
}
|
||||
|
||||
public function mineBlock(Block $block, Player $player){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function isTool(){
|
||||
return false;
|
||||
}
|
||||
|
|
@ -334,4 +346,11 @@ class Item{
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Item damaging changed. Use hurtAndBreak to damage the item and update it in the client's inventory correctly.
|
||||
*/
|
||||
public function useOn($object, $force = false){
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,9 +10,7 @@ class TNTBlock extends SolidBlock{
|
|||
|
||||
public function onActivate(Item $item, Player $player){
|
||||
if($item->getID() === FLINT_STEEL){
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$item->useOn($this);
|
||||
}
|
||||
$item->hurtAndBreak(1, $player);
|
||||
$data = array(
|
||||
"x" => $this->x + 0.5,
|
||||
"y" => $this->y,
|
||||
|
|
|
|||
|
|
@ -32,9 +32,7 @@ class BeetrootBlock extends FlowableBlock{
|
|||
$this->meta = 7;
|
||||
}
|
||||
$this->level->setBlock($this, $this, true, false, true);
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$player->removeItem(DYE,0x0F,1);
|
||||
}
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -25,9 +25,7 @@ class CarrotBlock extends FlowableBlock{
|
|||
$this->meta = 7;
|
||||
}
|
||||
$this->level->setBlock($this, $this, true, false, true);
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$player->removeItem(DYE,0x0F,1);
|
||||
}
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -34,11 +34,9 @@ class CyanFlowerBlock extends FlowableBlock{
|
|||
|
||||
public function onActivate(Item $item, Player $player){
|
||||
if($item->getID() === DYE and $item->getMetadata() === 0x0F){
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$player->removeItem(DYE,0x0F,1);
|
||||
}
|
||||
$random = new Random();
|
||||
self::placeFlowers($this->level, new Vector3($this->x, $this->y, $this->z), $random, $random->nextRange(2, 5), 2);
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -29,11 +29,9 @@ class DandelionBlock extends FlowableBlock{
|
|||
|
||||
public function onActivate(Item $item, Player $player){
|
||||
if($item->getID() === DYE and $item->getMetadata() === 0x0F){
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$player->removeItem(DYE,0x0F,1);
|
||||
}
|
||||
$random = new Random();
|
||||
self::placeFlowers($this->level, new Vector3($this->x, $this->y, $this->z), $random, $random->nextRange(2, 5), 2);
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -58,9 +58,7 @@ class MelonStemBlock extends FlowableBlock{
|
|||
$this->meta = 7;
|
||||
}
|
||||
$this->level->setBlock($this, $this, true, false, true);
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$player->removeItem(DYE,0x0F,1);
|
||||
}
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -34,9 +34,7 @@ class PotatoBlock extends FlowableBlock{
|
|||
$this->meta = 7;
|
||||
}
|
||||
$this->level->setBlock($this, $this, true, false, true);
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$player->removeItem(DYE,0x0F,1);
|
||||
}
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -58,9 +58,7 @@ class PumpkinStemBlock extends FlowableBlock{
|
|||
$this->meta = 7;
|
||||
}
|
||||
$this->level->setBlock($this, $this, true, false, true);
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$player->removeItem(DYE,0x0F,1);
|
||||
}
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -36,9 +36,7 @@ class SaplingBlock extends FlowableBlock{
|
|||
public function onActivate(Item $item, Player $player){
|
||||
if($item->getID() === DYE and $item->getMetadata() === 0x0F){ //Bonemeal
|
||||
TreeObject::growTree($this->level, $this, new Random(), $this->meta & 0x03);
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$player->removeItem(DYE,0x0F,1);
|
||||
}
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -29,9 +29,7 @@ class SugarcaneBlock extends FlowableBlock{
|
|||
$this->level->fastSetBlockUpdateMeta($this->x, $this->y, $this->z, $this->meta, true);
|
||||
}
|
||||
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$player->removeItem(DYE,0x0F,1);
|
||||
}
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -25,9 +25,7 @@ class WheatBlock extends FlowableBlock{
|
|||
$this->meta = 7;
|
||||
}
|
||||
$this->level->setBlock($this, $this, true, false, true);
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$player->removeItem(DYE,0x0F,1);
|
||||
}
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -11,11 +11,7 @@ class DirtBlock extends SolidBlock{
|
|||
public function onActivate(Item $item, Player $player){
|
||||
if($item->isHoe()){
|
||||
if($this->getSide(1)->isTransparent === false) return false;
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$item->useOn($this);
|
||||
if($item->getMetadata() >= $item->getMaxDurability()) $player->setSlot($player->slot, new Item(AIR, 0, 0), false);
|
||||
else $player->setSlot($player->slot, $item, true);
|
||||
}
|
||||
$item->hurtAndBreak(1, $player); //TODO move to ItemHoe
|
||||
$this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, FARMLAND, 0, true);
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,18 +16,12 @@ class GrassBlock extends SolidBlock{
|
|||
|
||||
public function onActivate(Item $item, Player $player){
|
||||
if($item->getID() === DYE and $item->getMetadata() === 0x0F){
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$player->removeItem(DYE,0x0F,1);
|
||||
}
|
||||
$player->consumeSingleItem();
|
||||
TallGrassObject::growGrass($this->level, $this, new Random(), 8, 2);
|
||||
return true;
|
||||
}elseif($item->isHoe()){
|
||||
if($this->getSide(1)->isTransparent === false) return false;
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
$item->useOn($this);
|
||||
if($item->getMetadata() >= $item->getMaxDurability()) $player->setSlot($player->slot, new Item(AIR, 0, 0), false);
|
||||
else $player->setSlot($player->slot, $item, true);
|
||||
}
|
||||
$item->hurtAndBreak(1, $player); //TODO move to ItemHoe
|
||||
$this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, FARMLAND, 0, true);
|
||||
$this->seedsDrop();
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -5,15 +5,6 @@ abstract class ItemHoe extends ItemTool
|
|||
public function isHoe(){
|
||||
return true;
|
||||
}
|
||||
|
||||
public function useOn($object, $force = false){
|
||||
if(($object instanceof Block) and ($object->getID() === GRASS or $object->getID() === DIRT)){
|
||||
$this->meta++;
|
||||
return true;
|
||||
}else{
|
||||
return parent::useOn($object, $force);
|
||||
}
|
||||
}
|
||||
|
||||
public function getLevel(){
|
||||
return match ($this->id) {
|
||||
|
|
|
|||
|
|
@ -17,5 +17,15 @@ abstract class ItemSword extends ItemTool
|
|||
default => false,
|
||||
};
|
||||
}
|
||||
|
||||
public function hurtEnemy(Entity $target, Player $attacker){
|
||||
$this->hurtAndBreak(1, $attacker);
|
||||
}
|
||||
|
||||
|
||||
public function mineBlock(Block $block, Player $player){
|
||||
$this->hurtAndBreak(2, $player);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,16 +11,23 @@ abstract class ItemTool extends Item
|
|||
public function isTool(){
|
||||
return true;
|
||||
}
|
||||
public function useOn($object, $force = false){
|
||||
|
||||
if($this->isSword() && !($object instanceof Entity)){
|
||||
$this->meta += 2;
|
||||
}else if(($object instanceof Entity) && !$this->isSword()){
|
||||
$this->meta += 2;
|
||||
}else{
|
||||
$this->meta++;
|
||||
|
||||
public function hurtEnemy(Entity $target, Player $attacker){
|
||||
if($this->isPickaxe() || $this->isAxe() || $this->isShovel()){
|
||||
$this->hurtAndBreak(2, $attacker);
|
||||
return;
|
||||
}
|
||||
return true;
|
||||
|
||||
parent::hurtEnemy($target, $attacker);
|
||||
}
|
||||
|
||||
public function mineBlock(Block $block, Player $player){
|
||||
if($this->isPickaxe() || $this->isAxe() || $this->isShovel()){
|
||||
$this->hurtAndBreak(1, $player);
|
||||
return true;
|
||||
}
|
||||
|
||||
return parent::mineBlock($block, $player);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,8 +18,6 @@ class MinecartItem extends Item{
|
|||
];
|
||||
$e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_MINECART, $data);
|
||||
$server->api->entity->spawnToAll($e);
|
||||
if(($player->gamemode & 0x01) === 0x00){
|
||||
$player->removeItem($this->getID(), $this->getMetadata(), 1, false);
|
||||
}
|
||||
$player->consumeSingleItem();
|
||||
}
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ class PaintingItem extends Item{
|
|||
private static $right = array(4, 5, 3, 2);
|
||||
|
||||
public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
|
||||
if($target->isTransparent === false and $face > 1 and $block->isSolid === false){
|
||||
if($target->isTransparent === false && $face > 1 and $block->isSolid === false){
|
||||
$server = ServerAPI::request();
|
||||
|
||||
if($face < 2 || $face > 5) return;
|
||||
|
|
@ -65,9 +65,8 @@ class PaintingItem extends Item{
|
|||
$painting->eid = $server->api->entity->getNextEID();
|
||||
$server->api->entity->addRaw($painting);
|
||||
$server->api->entity->spawnToAll($painting);
|
||||
if(($player->gamemode & 0x01) === 0x00){
|
||||
$player->removeItem($this->getID(), $this->getMetadata(), 1, false);
|
||||
}
|
||||
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -17,9 +17,7 @@ class SpawnEggItem extends Item{
|
|||
);
|
||||
$e = ServerAPI::request()->api->entity->add($block->level, ENTITY_MOB, $this->meta, $data);
|
||||
ServerAPI::request()->api->entity->spawnToAll($e);
|
||||
if(($player->gamemode & 0x01) === 0){
|
||||
-- $this->count;
|
||||
}
|
||||
$player->consumeSingleItem();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -8,19 +8,11 @@ class FlintSteelItem extends ItemTool{
|
|||
}
|
||||
|
||||
public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
|
||||
if(($player->gamemode & 0x01) === 0 and $this->useOn($block) and $this->getMetadata() >= $this->getMaxDurability()){
|
||||
$player->setSlot($player->slot, new Item(AIR, 0, 0), false);
|
||||
}
|
||||
|
||||
$this->hurtAndBreak(1, $player);
|
||||
if($block->getID() === AIR && $target->isSolid){
|
||||
$level->fastSetBlockUpdate($block->x, $block->y, $block->z, FIRE, 0, true);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function useOn($object, $force = false){
|
||||
++$this->meta;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,11 +5,11 @@ class ShearsItem extends ItemTool{
|
|||
parent::__construct(SHEARS, $meta, $count, "Shears");
|
||||
}
|
||||
|
||||
public function useOn($object, $force = false){
|
||||
if(($object instanceof Sheep) and $this->id === SHEARS){
|
||||
$this->meta++;
|
||||
public function mineBlock(Block $block, Player $player){
|
||||
if($block->getID() === LEAVES || $block->getID() === COBWEB){
|
||||
$this->hurtAndBreak(1, $player);
|
||||
return true;
|
||||
}
|
||||
parent::useOn($object, $force);
|
||||
return parent::mineBlock($block, $player);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue