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

Adding a Blacklist to existing sensor code

Started by BioFusionz, 22 May 2014 - 09:32 PM
BioFusionz #1
Posted 22 May 2014 - 11:32 PM
Hey, I'm wanting to edit a piece of code I received from a player on my server, but I'm not sure where to start.
The existing code is for the sensor from OpenPeripherals and opens the door when a specified player goes near it. What I want to do, is modify the code so it emits redstone for any player when they go near it, but the names specified.


local sensor = peripheral.wrap('top') -- where the sensor is
local output = 'left' -- Where to output the signal
local initial = true -- What to output when no one's around
-- How close you need to be to the sensor in order to use it.
local minX = -2 local maxX = 2
local minY = 0  local maxY = 2
local minZ = -2 local maxZ = 2
-- Initial output
redstone.setOutput(output, initial)
while true do
players = sensor.getPlayerNames()
-- Don't do checks if there are no players.
if #players == 0 then
  redstone.setOutput(output, initial)
  sleep(0.7)
else -- There are players
  for _, name in pairs(players) do
   if name == 'player1' or name == 'player2' or name == 'player3' then
	info = sensor.getPlayerData(name)
	pos = info.position
  
	if pos.x >= minX and pos.x <= maxX
	and pos.y >= minY and pos.y <= maxY
	and pos.z >= minZ and pos.z <= maxZ then
	 redstone.setOutput(output, not initial)
	 break
	end
   end
  end
  -- There are players around, we need to be more responsive
  sleep(0.5)
end
end

A friend of mine, who doesn't know much lua but writes mods in Java added this to see if it would work:

						if name == 'player1' or name == 'player2' or name == 'player3' then
								end
						else
								info = sensor.getPlayerData(name)

I couldn't get that to work so now I'm pretty much lost as to where to go with this. Any help would be greatly appreciated.
Thanks, Bio
CometWolf #2
Posted 23 May 2014 - 01:04 AM
Should be simple enough. You just gotta use a variable to determine wether to set the signal or not, and potentially break the loop if a blacklisted player is found

local tBlacklist = { --blacklist table
  CometWolf,
  SomeOtherPersonWeDon'tLike,
  AndSoForth
}
while true do
  local enable = true --wether to enable the redstone or not
  local tPlayers = sensor.getPlayerNames()
  for _k,pName in pairs(tPlayers) do
    for _k,bName in pairs(tBlacklist) do
	  if pName == bName then --a blacklisted player was detected
	    enable = false --signal disabled
	    break --break the loop
	  end
    end
    if not enable then
	  break --need to break this one as well
    end
  end
  rs.setOutput(side,enable)
  sleep(0.5)
end