This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
master39's profile picture

Player block break and scoreboards

Started by master39, 18 November 2015 - 02:55 PM
master39 #1
Posted 18 November 2015 - 03:55 PM
I've added Computercraft to my server to replace the vanilla command blocks. Command computers are powerful, useful and, although some essentials commands aren't working, I'm using it to do a lot of work (6 or 7 less plugins with computercraft installed).
My problem is the vanilla interaction with scoreboard, as far as I found, is practically impossible to have a simply and direct access to them. In the minigame I'm creating the players have to mine in a cube of ores generated with a command computer. My problem is that I need, after and during the match, to count the block breaking events of every player to calculate their score. With scoreboard I can successfully count the block breaking events with a "stat.mineBlock " objective but I can't transfer it to computercraft.
Any help would be appreciated, also a different method of counting the player block break.
Bomb Bloke #2
Posted 19 November 2015 - 06:05 AM
If a command works with a command block, then it should work with a command computer. I haven't found any exceptions thus far, but do remember that you're playing with Minecraft 1.7.10. There's a lot more functionality in later builds.

The command system is a strange beast. Pretty much the only information you can get out of it is either "true" or "false" (which, with a real command block, you'd detect as a redstone signal or lack thereof).

Some commands do return more information, which isn't intended to be "useful" so much as it's a debug log of sorts. Becomes pretty handy for command computer usage, though! For example, the xp command can be used to find out who's online, and the tp command can be used to find out where they are - see here for some examples of how to extract this information.

I'm not aware of any scoreboard-related commands that give this sort of extra info, however. So to determine exact scores for players, I would play the "guess the number between one and a hundred" game - the scoreboard can at least tell you if the number is higher or lower. Let's say you already know the names of your players (either because they typed them into the computer before playing, or because you used the xp/tp techniques above). I'll demonstrate using a stat called "Kills" (which I'm choosing because the first guide I found on stats happened to use it) set up like so:

/scoreboard objectives add Kills totalKillCount
/scoreboard objectives setDisplay sidebar Kills

This counter should go up for each player as they kill stuff (becoming visible to them once they start doing so). Let's say you wanted to get the exact value for a given player, and happen to know that the maximum value it could be is, say, 100. A monitoring script might go like this:

local players = {"master39","BombBloke"}  -- Fill this table however you like.

commands.scoreboard("objectives add Kills totalKillCount")  -- Sample objective.
commands.scoreboard("objectives setDisplay sidebar Kills")  -- Makes the counter visible to players (once they start).

local maxVal = 100  -- Highest value you expect a player to reach.

local function getKillCount(playerName)
	if not commands.xp("0 "..playerName) then return 0 end  -- In case the player is not online.
	
	local minGuess, maxGuess, curGuess = 0, maxVal, math.floor(maxVal / 2)
	
	while curGuess >= minGuess and curGuess < maxGuess do
		if commands.testfor("@a[name="..playerName..",score_Kills="..tostring(curGuess).."]") then
			-- Score is equal to or less than curGuess.
			maxGuess = curGuess
			curGuess = minGuess + math.floor((maxGuess - minGuess) / 2)
		else
			-- Score is greater than curGuess.
			minGuess = curGuess
			curGuess = math.max(minGuess + 1, minGuess + math.floor((maxGuess - minGuess) / 2))
		end
	end
	
	return curGuess
end

-- A primitive score display loop:
while true do
	term.clear()
	term.setCursorPos(1,1)
	for i = 1, #players do print(players[i]..": "..getKillCount(players[i])) end
	sleep(30)  -- Or however many seconds you want between updates.
end

With any luck you should be able to adapt this for your purposes.

In the case of the above example, if you wanted to clear the stats you'd do:

/scoreboard objectives setDisplay sidebar
/scoreboard objectives remove Kills
Edited on 19 November 2015 - 05:06 AM
master39 #3
Posted 19 November 2015 - 08:20 AM
Thanks for the fast answer, for players detecting and position I use the immibis's peripheral “adventure map interface” that tells me position and other in formations about players. Sadly I know that 1.7.10 has a lot less functionality than newer versions, but command computers are way more powerful than a command block systems.
This afternoon I'll try your solution and I'll report the results here.
I have another question, apart from Immibis's peripheral that I already have installed, there aren't any other peripheral/addon that improves command computer control over players? If it'is possible I'd really like to do this whole system without scoreboards, but i can't figure out how to do this.
Bomb Bloke #4
Posted 19 November 2015 - 10:53 AM
I'm not aware of any method that'd allow you to detect which players broke blocks - outside of the scoreboard, that is.

It sounds like the sort of thing bukkit plugin authors would write code for, but they do that more for logging server vandalism than for your sorts of purposes. It wouldn't surprise me if a peripheral author around these parts might be interested in whipping something up for you, if you were to be specific about the functionality you required.

(Heck, simply being able to input/output data into the scoreboard would be a very useful ability indeed.)
master39 #5
Posted 19 November 2015 - 12:27 PM
It would be awesome to have an "advanced scoreboard interface" peripheral, but my programming skills are limited to Lua and a bit of PHP (learned two weeks ago only for CC purposes), there is a forum section for peripheral requests / suggestions?