From a38c1c109ca1e1f98e877fec71f4750d0a75248e Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 10 May 2025 02:25:29 +0300 Subject: [PATCH] some random stuff --- src/Player.php | 122 +++++++++++------- src/PocketMinecraftServer.php | 1 + src/material/block/solid/StonecutterBlock.php | 2 +- src/material/block/solid/WorkbenchBlock.php | 2 +- src/recipes/CraftingRecipes.php | 18 ++- 5 files changed, 88 insertions(+), 57 deletions(-) diff --git a/src/Player.php b/src/Player.php index 07db541a..c4558c1d 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2460,56 +2460,31 @@ class Player{ if($this->spawned === false or $this->blocked === true){ break; } - - if($this->lastCraft <= (microtime(true) - 1)){ - if(isset($this->toCraft[-1])){ - $this->toCraft = [-1 => $this->toCraft[-1]]; - }else{ - $this->toCraft = []; - } - $this->craftingItems = []; - } - - if($packet->windowid === 0){ - $craft = false; + + if($packet->windowid === 0){ //crafting + $slot = $this->getSlot($packet->slot); - - if($slot->count >= $packet->item->count && (($slot->getID() === $packet->item->getID() && $slot->getMetadata() === $packet->item->getMetadata()) || ($packet->item->getID() === AIR && $packet->item->count === 0)) && !isset($this->craftingItems[$packet->slot])){ //Crafting recipe - if(!isset($this->toCraft[-1])) $this->toCraft[-1] = 0; - $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count - $packet->item->count); - $this->craftingItems[$packet->slot] = $use; - $craft = true; - }elseif($slot->count <= $packet->item->count and ($slot->getID() === AIR or ($slot->getID() === $packet->item->getID() and $slot->getMetadata() === $packet->item->getMetadata()))){ //Crafting final - $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count - $slot->count); - if(count($this->toCraft) === 0){ - $this->toCraft[-1] = 0; - } - $this->toCraft[$packet->slot] = $craftItem; - $craft = true; - }elseif(((count($this->toCraft) === 1 and isset($this->toCraft[-1])) or count($this->toCraft) === 0) and $slot->count > 0 and $slot->getID() > AIR and ($slot->getID() !== $packet->item->getID() or $slot->getMetadata() !== $packet->item->getMetadata())){ //Crafting final - $craftItem = BlockAPI::getItem($packet->item->getID(), $packet->item->getMetadata(), $packet->item->count); - if(count($this->toCraft) === 0){ - $this->toCraft[-1] = 0; - } - $use = BlockAPI::getItem($slot->getID(), $slot->getMetadata(), $slot->count); - $this->craftingItems[$packet->slot] = $use; - $this->toCraft[$packet->slot] = $craftItem; - $craft = true; - } - - if($craft === true){ - $this->lastCraft = microtime(true); - } - - if($craft === true and count($this->craftingItems) > 0 and count($this->toCraft) > 0 and ($recipe = $this->craftItems($this->toCraft, $this->craftingItems, $this->toCraft[-1])) !== true){ - if($recipe === false){ - $this->sendInventory(); - $this->toCraft = []; + $citem = $packet->item; + if($slot->getID() == $citem->getID() && $slot->getMetadata() == $citem->getMetadata()){ + if($citem->count > $slot->count){ //item added, result + $this->addCraftingResult($slot->getID(), $slot->getMetadata(), $citem->count - $slot->count); + }else if($citem->count < $slot->count){ //item removed, ingridient + $this->addCraftingIngridient($slot->getID(), $slot->getMetadata(), $slot->count - $citem->count); }else{ - $this->toCraft = [-1 => $this->toCraft[-1]]; + //item synchronized } + }else if($citem->getID() == 0 && $slot->getID() > 0){ //client sent air, server has not air, ingridient + $this->addCraftingIngridient($slot->getID(), $slot->getMetadata(), $slot->count); + }else if($slot->getID() == 0 && $citem->getID() > 0){ //client sent non-air, server has air, result + $this->addCraftingResult($citem->getID(), $citem->getMetadata(), $citem->count); + }else{ + $this->sendInventory(); + $this->toCraft = []; $this->craftingItems = []; + ConsoleAPI::warn("{$this->username} tried modifying inventory! Server item: $slot({$slot->count}), Client item: $citem({$citem->count})"); + $this->sendChat("[NC] Haxxor(or bug?)."); } + }else{ $this->toCraft = []; $this->craftingItems = []; @@ -2697,6 +2672,63 @@ class Player{ } } + public $craftingType = 0; + public function tryCraft(){ + if(count($this->toCraft) <= 0 || count($this->craftingItems) <= 0) return; + + if(count($this->toCraft) > 1){ + ConsoleAPI::warn("Trying to craft more than 1 item, not supported for now."); + return false; + } + foreach($this->toCraft as $i => $e){ + $toCraft = [$i >> 8, $i & 0xff, $e]; + } + $ingridients = []; + foreach($this->craftingItems as $i => $e){ + $ingridients[$i >> 8] = [$i >> 8, $i & 0xff, $e]; + } + + + $cc = CraftingRecipes::canCraft($toCraft, $ingridients, $this->craftingType); + if(!is_array($cc) && $this->craftingType == CraftingRecipes::TYPE_CRAFTIGTABLE){ + $cc = CraftingRecipes::canCraft($toCraft, $ingridients, CraftingRecipes::TYPE_INVENTORY); + if(!is_array($cc)){ + return; //recipe not found + } + } + + var_dump($cc); + $this->addItem($toCraft[0], $toCraft[1], $toCraft[2], false); + foreach($ingridients as [$id, $meta, $count]){ + $this->removeItem($id, $meta, $count, false); + } + $this->sendInventory(); + $this->toCraft = []; + $this->craftingItems = []; + } + + public function addCraftingResult($id, $meta, $count){ + $index = ($id << 8) | $meta; + + if(!isset($this->toCraft[$index])) $this->toCraft[$index] = 0; + $this->toCraft[$index] += $count; + console("Result: $id, $meta, $count"); + if($this->tryCraft() === false){ + $this->sendInventory(); + } + } + + public function addCraftingIngridient($id, $meta, $count){ + $index = ($id << 8) | $meta; + + if(!isset($this->craftingItems[$index])) $this->craftingItems[$index] = 0; + $this->craftingItems[$index] += $count; + console("Ingridient: $id, $meta, $count"); + if($this->tryCraft() === false){ + $this->sendInventory(); + } + } + public function hasItem($type, $damage = false){ foreach($this->inventory as $s => $item){ if($item->getID() === $type and ($item->getMetadata() === $damage or $damage === false) and $item->count > 0){ diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 1b281cfc..dcdbb9da 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -12,6 +12,7 @@ class PocketMinecraftServer{ public $doTick, $levelData, $tiles, $entities, $schedule, $scheduleCnt, $whitelist, $spawn, $difficulty, $stop, $asyncThread; public static $FORCE_20_TPS = false, $KEEP_CHUNKS_LOADED = true, $PACKET_READING_LIMIT = 100; + public static $EXPERIMENTAL_INVENTORY = true; function __construct($name, $gamemode = SURVIVAL, $seed = false, $port = 19132, $serverip = "0.0.0.0"){ $this->port = (int) $port; $this->doTick = true; diff --git a/src/material/block/solid/StonecutterBlock.php b/src/material/block/solid/StonecutterBlock.php index b5fc3fe7..110747d1 100644 --- a/src/material/block/solid/StonecutterBlock.php +++ b/src/material/block/solid/StonecutterBlock.php @@ -8,7 +8,7 @@ class StonecutterBlock extends SolidBlock{ } public function onActivate(Item $item, Player $player){ - $player->toCraft[-1] = 2; + $player->craftingType = CraftingRecipes::TYPE_STONECUTTER; return true; } diff --git a/src/material/block/solid/WorkbenchBlock.php b/src/material/block/solid/WorkbenchBlock.php index db605a65..080ac13c 100644 --- a/src/material/block/solid/WorkbenchBlock.php +++ b/src/material/block/solid/WorkbenchBlock.php @@ -9,7 +9,7 @@ class WorkbenchBlock extends SolidBlock{ } public function onActivate(Item $item, Player $player){ - $player->toCraft[-1] = 1; + $player->craftingType = CraftingRecipes::TYPE_CRAFTIGTABLE; return true; } diff --git a/src/recipes/CraftingRecipes.php b/src/recipes/CraftingRecipes.php index 47064577..d9a838b3 100644 --- a/src/recipes/CraftingRecipes.php +++ b/src/recipes/CraftingRecipes.php @@ -1,7 +1,10 @@ CLAY_BLOCK:0x1", @@ -204,19 +207,19 @@ class CraftingRecipes{ $id = 1; foreach(CraftingRecipes::$small as $recipe){ $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = 0; //Type + $recipe[3] = self::TYPE_INVENTORY; //Type CraftingRecipes::$recipes[$id] = $recipe; ++$id; } foreach(CraftingRecipes::$big as $recipe){ $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = 1; + $recipe[3] = self::TYPE_CRAFTIGTABLE; CraftingRecipes::$recipes[$id] = $recipe; ++$id; } foreach(CraftingRecipes::$stone as $recipe){ $recipe = CraftingRecipes::parseRecipe($recipe); - $recipe[3] = 2; + $recipe[3] = self::TYPE_STONECUTTER; CraftingRecipes::$recipes[$id] = $recipe; ++$id; } @@ -268,9 +271,6 @@ class CraftingRecipes{ ksort($recipeItems); $recipeString = ""; foreach($recipeItems as $item){ - if($craftItem[0] === CAKE && $item[0] === BUCKET && $item[1] === 1){ //some dark magic with recipe happened in mcpe, pmmp restores it back to normal - $item[2] = 3; - } $recipeString .= $item[0] . "x" . $item[2] . ","; } $recipeString = substr($recipeString, 0, -1) . "=>" . $craftItem[0] . "x" . $craftItem[2]; @@ -287,9 +287,7 @@ class CraftingRecipes{ break; } $oitem = $recipeItems[$item[0]]; - if($craftItem[0] === CAKE && $oitem[0] === BUCKET && $item[1] === 1){ //some dark magic with recipe happened in mcpe, pmmp restores it back to normal x2 - $oitem[2] = 3; - } + if(($oitem[1] !== $item[1] and $item[1] !== false) or $oitem[2] !== $item[2]){ $continue = false; break;