GenisysPro  for Minecraft PE/Windows 10 v1.1.x
Feature-rich server software for Minecraft PE and Windows 10 Edition
Hopper Class Reference

Public Member Functions

 __construct (Level $level, CompoundTag $nbt)
 
 close ()
 
 activate ()
 
 deactivate ()
 
 canUpdate ()
 
 resetCooldownTicks ()
 
 onUpdate ()
 
 getInventory ()
 
 getSize ()
 
 getItem ($index)
 
 setItem ($index, Item $item)
 
 saveNBT ()
 
 getName ()
 
 hasName ()
 
 setName ($str)
 
 hasLock ()
 
 setLock (string $itemName="")
 
 checkLock (string $key)
 
 getSpawnCompound ()
 
- Public Member Functions inherited from Spawnable
 spawnTo (Player $player)
 
 __construct (Level $level, CompoundTag $nbt)
 
 spawnToAll ()
 
 getSpawnCompound ()
 
 updateCompoundTag (CompoundTag $nbt, Player $player)
 
- Public Member Functions inherited from Tile
 getSaveId ()
 
 __construct (Level $level, CompoundTag $nbt)
 
 getId ()
 
 saveNBT ()
 
 getBlock ()
 
 onUpdate ()
 
 scheduleUpdate ()
 
 __destruct ()
 
 close ()
 
 getName ()
 
- Public Member Functions inherited from Position
 __construct ($x=0, $y=0, $z=0, Level $level=null)
 
 asPosition ()
 
 add ($x, $y=0, $z=0)
 
 getLevel ()
 
 setLevel (Level $level=null)
 
 isValid ()
 
 getSide ($side, $step=1)
 
 __toString ()
 
 setComponents ($x, $y, $z)
 
 fromObjectAdd (Vector3 $pos, $x, $y, $z)
 
- Public Member Functions inherited from Vector3
 __construct ($x=0, $y=0, $z=0)
 
 getX ()
 
 getY ()
 
 getZ ()
 
 getFloorX ()
 
 getFloorY ()
 
 getFloorZ ()
 
 getRight ()
 
 getUp ()
 
 getForward ()
 
 getSouth ()
 
 getWest ()
 
 add ($x, $y=0, $z=0)
 
 subtract ($x=0, $y=0, $z=0)
 
 multiply ($number)
 
 divide ($number)
 
 ceil ()
 
 floor ()
 
 round ()
 
 abs ()
 
 getSide ($side, $step=1)
 
 asVector3 ()
 
 distance (Vector3 $pos)
 
 distanceSquared (Vector3 $pos)
 
 maxPlainDistance ($x=0, $z=0)
 
 length ()
 
 lengthSquared ()
 
 normalize ()
 
 dot (Vector3 $v)
 
 cross (Vector3 $v)
 
 equals (Vector3 $v)
 
 getIntermediateWithXValue (Vector3 $v, $x)
 
 getIntermediateWithYValue (Vector3 $v, $y)
 
 getIntermediateWithZValue (Vector3 $v, $z)
 
 setComponents ($x, $y, $z)
 
 fromObjectAdd (Vector3 $pos, $x, $y, $z)
 
 __toString ()
 

Protected Member Functions

 getSlotIndex ($index)
 
- Protected Member Functions inherited from Spawnable
 onChanged ()
 

Protected Attributes

 $inventory
 
 $isLocked = false
 
 $isPowered = false
 
- Protected Attributes inherited from Tile
 $lastUpdate
 
 $server
 
 $timings
 

Additional Inherited Members

- Static Public Member Functions inherited from Tile
static init ()
 
static createTile ($type, Level $level, CompoundTag $nbt,... $args)
 
static registerTile ($className)
 
- Static Public Member Functions inherited from Position
static fromObject (Vector3 $pos, Level $level=null)
 
- Static Public Member Functions inherited from Vector3
static getOppositeSide ($side)
 
static createRandomDirection (Random $random)
 
- Data Fields inherited from Tile
const BREWING_STAND = "BrewingStand"
 
const CHEST = "Chest"
 
const DL_DETECTOR = "DayLightDetector"
 
const ENCHANT_TABLE = "EnchantTable"
 
const FLOWER_POT = "FlowerPot"
 
const FURNACE = "Furnace"
 
const MOB_SPAWNER = "MobSpawner"
 
const SIGN = "Sign"
 
const SKULL = "Skull"
 
const ITEM_FRAME = "ItemFrame"
 
const DISPENSER = "Dispenser"
 
const DROPPER = "Dropper"
 
const CAULDRON = "Cauldron"
 
const HOPPER = "Hopper"
 
const BEACON = "Beacon"
 
const ENDER_CHEST = "EnderChest"
 
const BED = "Bed"
 
const DAY_LIGHT_DETECTOR = "DLDetector"
 
 $chunk
 
 $name
 
 $id
 
 $x
 
 $y
 
 $z
 
 $attach
 
 $metadata
 
 $closed = false
 
 $namedtag
 
 $tickTimer
 
- Data Fields inherited from Position
 $level = null
 
- Data Fields inherited from Vector3
const SIDE_DOWN = 0
 
const SIDE_UP = 1
 
const SIDE_NORTH = 2
 
const SIDE_SOUTH = 3
 
const SIDE_WEST = 4
 
const SIDE_EAST = 5
 
 $x
 
 $y
 
 $z
 
- Static Public Attributes inherited from Tile
static $tileCount = 1
 

Constructor & Destructor Documentation

◆ __construct()

__construct ( Level  $level,
CompoundTag  $nbt 
)

Hopper constructor.

Parameters
Level$level
CompoundTag$nbt
53  {
54  if(!isset($nbt->TransferCooldown) or !($nbt->TransferCooldown instanceof IntTag)){
55  $nbt->TransferCooldown = new IntTag("TransferCooldown", 0);
56  }
57 
58  parent::__construct($level, $nbt);
59 
60  $this->inventory = new HopperInventory($this);
61 
62  if(!isset($this->namedtag->Items) or !($this->namedtag->Items instanceof ListTag)){
63  $this->namedtag->Items = new ListTag("Items", []);
64  $this->namedtag->Items->setTagType(NBT::TAG_Compound);
65  }
66 
67  for($i = 0; $i < $this->getSize(); ++$i){
68  $this->inventory->setItem($i, $this->getItem($i));
69  }
70 
71  $this->scheduleUpdate();
72  }

Member Function Documentation

◆ activate()

activate ( )
83  {
84  $this->isPowered = true;
85  }

◆ canUpdate()

canUpdate ( )
Returns
bool
94  {
95  return $this->namedtag->TransferCooldown->getValue() === 0 and !$this->isPowered;
96  }

◆ checkLock()

checkLock ( string  $key)
Parameters
string$key
Returns
bool
318  {
319  return $this->namedtag->Lock->getValue() === $key;
320  }

◆ close()

close ( )
74  {
75  if($this->closed === false){
76  foreach($this->getInventory()->getViewers() as $player){
77  $player->removeWindow($this->getInventory());
78  }
79  parent::close();
80  }
81  }

◆ deactivate()

deactivate ( )
87  {
88  $this->isPowered = false;
89  }

◆ getInventory()

getInventory ( )
Returns
HopperInventory

Implements InventoryHolder.

190  {
191  return $this->inventory;
192  }

◆ getItem()

getItem (   $index)

This method should not be used by plugins, use the Inventory

Parameters
int$index
Returns
Item

Implements Container.

208  {
209  $i = $this->getSlotIndex($index);
210  if($i < 0){
211  return Item::get(Item::AIR, 0, 0);
212  }else{
213  return Item::nbtDeserialize($this->namedtag->Items[$i]);
214  }
215  }

◆ getName()

getName ( )
Returns
string

Implements Nameable.

272  : string{
273  return isset($this->namedtag->CustomName) ? $this->namedtag->CustomName->getValue() : "Hopper";
274  }

◆ getSize()

getSize ( )
Returns
int

Implements Container.

197  {
198  return 5;
199  }

◆ getSlotIndex()

getSlotIndex (   $index)
protected
Parameters
$index
Returns
int
251  {
252  foreach($this->namedtag->Items as $i => $slot){
253  if((int) $slot["Slot"] === (int) $index){
254  return (int) $i;
255  }
256  }
257 
258  return -1;
259  }

◆ getSpawnCompound()

getSpawnCompound ( )
Returns
CompoundTag
325  {
326  $c = new CompoundTag("", [
327  new StringTag("id", Tile::HOPPER),
328  new IntTag("x", (int) $this->x),
329  new IntTag("y", (int) $this->y),
330  new IntTag("z", (int) $this->z)
331  ]);
332 
333  if($this->hasName()){
334  $c->CustomName = $this->namedtag->CustomName;
335  }
336  if($this->hasLock()){
337  $c->Lock = $this->namedtag->Lock;
338  }
339 
340  return $c;
341  }

◆ hasLock()

hasLock ( )
Returns
bool
298  {
299  return isset($this->namedtag->Lock);
300  }

◆ hasName()

hasName ( )
Returns
bool

Implements Nameable.

279  {
280  return isset($this->namedtag->CustomName);
281  }

◆ onUpdate()

onUpdate ( )
Returns
bool
105  {
106  if(!($this->getBlock() instanceof HopperBlock)){
107  return false;
108  }
109  //Pickup dropped items
110  //This can happen at any time regardless of cooldown
111  $area = clone $this->getBlock()->getBoundingBox(); //Area above hopper to draw items from
112  $area->maxY = ceil($area->maxY) + 1; //Account for full block above, not just 1 + 5/8
113  foreach($this->getLevel()->getChunkEntities($this->getBlock()->x >> 4, $this->getBlock()->z >> 4) as $entity){
114  if(!($entity instanceof DroppedItem) or !$entity->isAlive()){
115  continue;
116  }
117  if(!$entity->boundingBox->intersectsWith($area)){
118  continue;
119  }
120 
121  $item = $entity->getItem();
122  if(!$item instanceof Item){
123  continue;
124  }
125  if($item->getCount() < 1){
126  $entity->kill();
127  continue;
128  }
129 
130  if($this->inventory->canAddItem($item)){
131  $this->inventory->addItem($item);
132  $entity->kill();
133  }
134  }
135 
136  if(!$this->canUpdate()){ //Hoppers only update CONTENTS every 8th tick
137  $this->namedtag->TransferCooldown->setValue($this->namedtag->TransferCooldown->getValue() - 1);
138  return true;
139  }
140 
141  //Suck items from above tile inventories
142  $source = $this->getLevel()->getTile($this->getBlock()->getSide(Vector3::SIDE_UP));
143  if($source instanceof Tile and $source instanceof InventoryHolder){
144  $inventory = $source->getInventory();
145  $item = clone $inventory->getItem($inventory->firstOccupied());
146  $item->setCount(1);
147  if($this->inventory->canAddItem($item)){
148  $this->inventory->addItem($item);
149  $inventory->removeItem($item);
150  $this->resetCooldownTicks();
151  if($source instanceof Hopper){
152  $source->resetCooldownTicks();
153  }
154  }
155  }
156 
157  //Feed item into target inventory
158  //Do not do this if there's a hopper underneath this hopper, to follow vanilla behaviour
159  if(!($this->getLevel()->getTile($this->getBlock()->getSide(Vector3::SIDE_DOWN)) instanceof Hopper)){
160  $target = $this->getLevel()->getTile($this->getBlock()->getSide($this->getBlock()->getDamage()));
161  if($target instanceof Tile and $target instanceof InventoryHolder){
162  $inv = $target->getInventory();
163  foreach($this->inventory->getContents() as $item){
164  if($item->getId() === Item::AIR or $item->getCount() < 1){
165  continue;
166  }
167  $targetItem = clone $item;
168  $targetItem->setCount(1);
169 
170  if($inv->canAddItem($targetItem)){
171  $inv->addItem($targetItem);
172  $this->inventory->removeItem($targetItem);
173  $this->resetCooldownTicks();
174  if($target instanceof Hopper){
175  $target->resetCooldownTicks();
176  }
177  break;
178  }
179 
180  }
181  }
182  }
183 
184  return true;
185  }

◆ resetCooldownTicks()

resetCooldownTicks ( )
98  {
99  $this->namedtag->TransferCooldown->setValue(8);
100  }

◆ saveNBT()

saveNBT ( )
261  {
262  $this->namedtag->Items = new ListTag("Items", []);
263  $this->namedtag->Items->setTagType(NBT::TAG_Compound);
264  for($index = 0; $index < $this->getSize(); ++$index){
265  $this->setItem($index, $this->inventory->getItem($index));
266  }
267  }

◆ setItem()

setItem (   $index,
Item  $item 
)

This method should not be used by plugins, use the Inventory

Parameters
int$index
Item$item
Returns
bool

Implements Container.

225  {
226  $i = $this->getSlotIndex($index);
227 
228  if($item->getId() === Item::AIR or $item->getCount() <= 0){
229  if($i >= 0){
230  unset($this->namedtag->Items[$i]);
231  }
232  }elseif($i < 0){
233  for($i = 0; $i <= $this->getSize(); ++$i){
234  if(!isset($this->namedtag->Items[$i])){
235  break;
236  }
237  }
238  $this->namedtag->Items[$i] = $item->nbtSerialize($index);
239  }else{
240  $this->namedtag->Items[$i] = $item->nbtSerialize($index);
241  }
242 
243  return true;
244  }

◆ setLock()

setLock ( string  $itemName = "")
Parameters
string$itemName
305  {
306  if($itemName === ""){
307  unset($this->namedtag->Lock);
308  return;
309  }
310  $this->namedtag->Lock = new StringTag("Lock", $itemName);
311  }

◆ setName()

setName (   $str)
Parameters
void$str

Implements Nameable.

286  {
287  if($str === ""){
288  unset($this->namedtag->CustomName);
289  return;
290  }
291  $this->namedtag->CustomName = new StringTag("CustomName", $str);
292  }

Field Documentation

◆ $inventory

$inventory
protected

◆ $isLocked

$isLocked = false
protected

◆ $isPowered

$isPowered = false
protected

The documentation for this class was generated from the following file: