aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/CommandBlock.cpp
blob: c151172e0c5416d0de8c0b813d4f462deabbd3b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "stdafx.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.redstone.h"
#include "net.minecraft.world.level.tile.entity.h"
#include "CommandBlock.h"

CommandBlock::CommandBlock(int id) : BaseEntityTile(id, Material::metal, isSolidRender() )
{
}

shared_ptr<TileEntity> CommandBlock::newTileEntity(Level *level)
{
	return std::make_shared<CommandBlockEntity>();
}

void CommandBlock::neighborChanged(Level *level, int x, int y, int z, int type)
{
	if (!level->isClientSide)
	{

		bool signal = level->hasNeighborSignal(x, y, z);
		int data = level->getData(x, y, z);
		bool isTriggered = (data & TRIGGER_BIT) != 0;

		if (signal && !isTriggered)
		{
			level->setData(x, y, z, data | TRIGGER_BIT, Tile::UPDATE_NONE);
			level->addToTickNextTick(x, y, z, id, getTickDelay(level));
		}
		else if (!signal && isTriggered)
		{
			level->setData(x, y, z, data & ~TRIGGER_BIT, Tile::UPDATE_NONE);
		}
	}
}

void CommandBlock::tick(Level *level, int x, int y, int z, Random *random)
{
	shared_ptr<TileEntity> tileEntity = level->getTileEntity(x, y, z);

	if (tileEntity != nullptr && dynamic_pointer_cast<CommandBlockEntity>( tileEntity ) != nullptr)
	{
		shared_ptr<CommandBlockEntity> commandBlock = dynamic_pointer_cast<CommandBlockEntity>( tileEntity );
		commandBlock->setSuccessCount(commandBlock->performCommand(level));
		level->updateNeighbourForOutputSignal(x, y, z, id);
	}
}

int CommandBlock::getTickDelay(Level *level)
{
	return 1;
}

bool CommandBlock::use(Level *level, int x, int y, int z, shared_ptr<Player> player, int clickedFace, float clickX, float clickY, float clickZ, bool soundOnly)
{
	shared_ptr<CommandBlockEntity> amce = dynamic_pointer_cast<CommandBlockEntity>( level->getTileEntity(x, y, z) );

	if (amce != nullptr)
	{
		player->openTextEdit(amce);
	}

	return true;
}

bool CommandBlock::hasAnalogOutputSignal()
{
	return true;
}

int CommandBlock::getAnalogOutputSignal(Level *level, int x, int y, int z, int dir)
{
	shared_ptr<TileEntity> tileEntity = level->getTileEntity(x, y, z);

	if (tileEntity != nullptr && dynamic_pointer_cast<CommandBlockEntity>( tileEntity ) != nullptr)
	{
		return dynamic_pointer_cast<CommandBlockEntity>( tileEntity )->getSuccessCount();
	}

	return Redstone::SIGNAL_NONE;
}

void CommandBlock::setPlacedBy(Level *level, int x, int y, int z, shared_ptr<LivingEntity> by, shared_ptr<ItemInstance> itemInstance)
{
	shared_ptr<CommandBlockEntity> cblock = dynamic_pointer_cast<CommandBlockEntity>( level->getTileEntity(x, y, z) );

	if (itemInstance->hasCustomHoverName())
	{
		cblock->setName(itemInstance->getHoverName());
	}
}

int CommandBlock::getResourceCount(Random *random)
{
	return 0;
}