slot based crafting

This commit is contained in:
GameHerobrine 2025-05-10 03:42:02 +03:00
parent a38c1c109c
commit af71be9f3c
3 changed files with 58 additions and 27 deletions

View file

@ -2467,22 +2467,19 @@ class Player{
$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);
$this->addCraftingResult($packet->slot, $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);
$this->addCraftingIngridient($packet->slot, $slot->getID(), $slot->getMetadata(), $slot->count - $citem->count);
}else{
//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);
$this->addCraftingIngridient($packet->slot, $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?).");
$this->addCraftingResult($packet->slot, $citem->getID(), $citem->getMetadata(), $citem->count);
}else{ //client sent result, server has ingridients
$this->addCraftingIngridient($packet->slot, $slot->getID(), $slot->getMetadata(), $slot->count);
$this->addCraftingResult($packet->slot, $citem->getID(), $citem->getMetadata(), $citem->count);
}
}else{
@ -2677,15 +2674,19 @@ class Player{
if(count($this->toCraft) <= 0 || count($this->craftingItems) <= 0) return;
if(count($this->toCraft) > 1){
$this->sendInventory();
$this->toCraft = [];
$this->craftingItems = [];
safe_var_dump($this->toCraft);
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];
foreach($this->toCraft as $i => $slotz){
$toCraft = [$i >> 8, $i & 0xff, array_sum($slotz)];
}
$ingridients = [];
foreach($this->craftingItems as $i => $e){
$ingridients[$i >> 8] = [$i >> 8, $i & 0xff, $e];
foreach($this->craftingItems as $i => $slotz){
$ingridients[$i >> 8] = [$i >> 8, $i & 0xff, array_sum($slotz)];
}
@ -2697,33 +2698,61 @@ class Player{
}
}
var_dump($cc);
$this->addItem($toCraft[0], $toCraft[1], $toCraft[2], false);
foreach($ingridients as [$id, $meta, $count]){
$this->removeItem($id, $meta, $count, false);
foreach($this->craftingItems as $i => $slotz){
$id = $i >> 8;
$meta = $i & 0xff;
foreach($slotz as $slot => $count){
$slt = $this->getSlot($slot);
if($slt->getID() == $id && $slt->getMetadata() == $meta){
$slt->count -= $count;
if($slt->count <= 0) $this->setSlot($slot, BlockAPI::getItem(0, 0, 0), false);
}else{
ConsoleAPI::warn("{$slt->getID()} != $id && {$slt->getMetadata()} != $meta!!!");
}
}
}
foreach($this->toCraft as $i => $slotz){
$id = $i >> 8;
$meta = $i & 0xff;
foreach($slotz as $slot => $count){
$slt = $this->getSlot($slot);
if($slt->getID() == $id && $slt->getMetadata() == $meta){
$slt->count += $count;
}else if($slt->getID() == 0){
$this->setSlot($slot, BlockAPI::getItem($id, $meta, $count), false);
}else{
ConsoleAPI::warn("{$slt->getID()} != $id || 0 !!!");
}
//TODO actually check id/meta
}
}
$this->sendInventory();
$this->toCraft = [];
$this->craftingItems = [];
}
public function addCraftingResult($id, $meta, $count){
public function addCraftingResult($slot, $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(!isset($this->toCraft[$index])) $this->toCraft[$index] = [];
if(!isset($this->toCraft[$index][$slot])) $this->toCraft[$index][$slot] = 0;
$this->toCraft[$index][$slot] += $count;
console("Result: $id, $meta, $count into $slot");
if($this->tryCraft() === false){
$this->sendInventory();
}
}
public function addCraftingIngridient($id, $meta, $count){
public function addCraftingIngridient($slot, $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(!isset($this->craftingItems[$index])) $this->craftingItems[$index] = [];
if(!isset($this->craftingItems[$index][$slot])) $this->craftingItems[$index][$slot] = 0;
$this->craftingItems[$index][$slot] += $count;
console("Ingridient: $id, $meta, $count into $slot");
if($this->tryCraft() === false){
$this->sendInventory();
}

View file

@ -12,7 +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;

View file

@ -56,6 +56,7 @@ class RakNetParser{
case RakNetInfo::DATA_PACKET_F:
$this->packet->seqNumber = $this->getLTriad();
$this->packet->data = [];
while(!$this->feof() and ($pk = $this->parseDataPacket()) instanceof RakNetDataPacket){
$this->packet->data[] = $pk;
}
@ -122,6 +123,7 @@ class RakNetParser{
$reliability = ($packetFlags & 0b11100000) >> 5;
$hasSplit = ($packetFlags & 0b00010000) > 0;
$length = (int) ceil($this->getShort() / 8);
if($reliability === 2
or $reliability === 3
or $reliability === 4