Fading Door Fix for DarkRP

Does anyone know how I could go about editing the Fading Door tool to make it only work with a keypad or a button? There has got to be a workaround, I’ve seen it on other servers before. Here is the entire tool:



TOOL.Category = "Construction";
TOOL.Name = "#Fading Door";

TOOL.ClientConVar["key"] = "5"
TOOL.ClientConVar["toggle"] = "0"
TOOL.ClientConVar["reversed"] = "0"
TOOL.ClientConVar["noeffect"] = "0"

local function checkTrace(tr)
	return (tr.Entity and tr.Entity:IsValid() and not (tr.Entity:IsPlayer() or tr.Entity:IsNPC() or tr.Entity:IsVehicle() or tr.HitWorld));
end

if (CLIENT) then
	language.Add( "Tool.fading_door.name", "Fading Door" );
	language.Add( "Tool.fading_door.desc", "Makes an object fade away when activated by the player." );
	language.Add( "Tool.fading_door.0", "Click on an object to make it a fading door." );
	language.Add( "Undone_fading_door", "Undone Fading Door" );
	
	function TOOL:BuildCPanel()
		self:AddControl("Header",   {Text = "#Tool.fading_door.name", Description = "#Tool.fading_door.desc"});
		self:AddControl("CheckBox", {Label = "Start Faded", Command = "fading_door_reversed"});
		self:AddControl("CheckBox", {Label = "Toggle", Command = "fading_door_toggle"});
		self:AddControl("Numpad",   {Label = "Fade", ButtonSize = "22", Command = "fading_door_key"});
	end
	
	TOOL.LeftClick = checkTrace;
	
	return;
end

local function fadeActivate(self)
	self.fadeActive = true;
	self.fadeMaterial = self:GetMaterial();
	self:SetMaterial("sprites/heatwave")
	self:DrawShadow(false)
	self:SetNotSolid(true)
	--self:SetCollisionGroup(COLLISION_GROUP_WORLD)
	local phys = self:GetPhysicsObject();
	if (IsValid(phys)) then
		self.fadeMoveable = phys:IsMoveable();
		phys:EnableMotion(false);
	end
	if (WireLib) then
		Wire_TriggerOutput(self,  "FadeActive",  1);
	end
end

local function fadeDeactivate(self)
	self.fadeActive = false;
	self:SetMaterial(self.fadeMaterial or "");
	self:DrawShadow(true);
	self:SetNotSolid(false);
	--self:SetCollisionGroup(COLLISION_GROUP_NONE);
	local phys = self:GetPhysicsObject();
	if (IsValid(phys)) then
		phys:EnableMotion(self.fadeMoveable or false);
	end
	if (WireLib) then
		Wire_TriggerOutput(self,  "FadeActive",  0);
	end
end

local function fadeToggleActive(self)
	if (self.fadeActive) then
		self:fadeDeactivate();
	else
		self:fadeActivate();
	end
end

local function onUp(ply, ent)
	if (not (ent:IsValid() and ent.fadeToggleActive and not ent.fadeToggle)) then
		return;
	end
	ent:fadeToggleActive();
end
numpad.Register("Fading Door onUp", onUp);

local function onDown(ply, ent)
	if (not (ent:IsValid() and ent.fadeToggleActive)) then
		return;
	end
	ent:fadeToggleActive();
end
numpad.Register("Fading Door onDown", onDown);

-- I hate wire.
local function getWireInputs(ent)
	local inputs = ent.Inputs;
	local names, types, descs = {}, {}, {};
	if (inputs) then
		local num;
		for _, data in pairs(inputs) do
			num = data.Num;
			names[num] = data.Name;
			types[num] = data.Type;
			descs[num] = data.Desc;
		end
	end
	return names, types, descs;
end
local function doWireInputs(ent)
	local inputs = ent.Inputs;
	if (not inputs) then
		Wire_CreateInputs(ent, {"Fade"});
		return;
	end
	local names, types, descs = {}, {}, {};
	local num;
	for _, data in pairs(inputs) do
		num = data.Num;
		names[num] = data.Name;
		types[num] = data.Type;
		descs[num] = data.Desc;
	end
	table.insert(names, "Fade");
	WireLib.AdjustSpecialInputs(ent, names, types, descs);
end

local function doWireOutputs(ent)
	local outputs = ent.Outputs;
	if (not outputs) then
		Wire_CreateOutputs(ent, {"FadeActive"});
		return;
	end
	local names, types, descs = {}, {}, {};
	local num;
	for _, data in pairs(outputs) do
		num = data.Num;
		names[num] = data.Name;
		types[num] = data.Type;
		descs[num] = data.Desc;
	end
	table.insert(names, "FadeActive");
	WireLib.AdjustSpecialOutputs(ent, names, types, descs);
end

local function TriggerInput(self, name, value, ...)
	if (name == "Fade") then
		if (value == 0) then
			if (self.fadePrevWireOn) then
				self.fadePrevWireOn = false;
				if (not self.fadeToggle) then
					self:fadeToggleActive();
				end
			end
		else
			if (not self.fadePrevWireOn) then
				self.fadePrevWireOn = true;
				self:fadeToggleActive();
			end
		end
	elseif (self.fadeTriggerInput) then
		return self:fadeTriggerInput(name, value, ...);
	end
end

local function PreEntityCopy(self)
	local info = WireLib.BuildDupeInfo(self)
	if (info) then
		duplicator.StoreEntityModifier(self, "WireDupeInfo", info);
	end
	if (self.fadePreEntityCopy) then
		self:fadePreEntityCopy();
	end
end

local function PostEntityPaste(self, ply, ent, ents)
	if (self.EntityMods and self.EntityMods.WireDupeInfo) then
		WireLib.ApplyDupeInfo(ply, self, self.EntityMods.WireDupeInfo, function(id) return ents[id]; end);
	end
	if (self.fadePostEntityPaste) then
		self:fadePostEntityPaste(ply, ent, ents);
	end
end
	

local function onRemove(self)
	numpad.Remove(self.fadeUpNum);
	numpad.Remove(self.fadeDownNum);
end

-- Handling Duplications
local function dooEet(ply, ent, stuff)
	if (ent.isFadingDoor) then
		ent:fadeDeactivate();
		onRemove(ent)
	else
		ent.isFadingDoor = true;
		ent.fadeActivate = fadeActivate;
		ent.fadeDeactivate = fadeDeactivate;
		ent.fadeToggleActive = fadeToggleActive;
		ent:CallOnRemove("Fading Door", onRemove);
		if (WireLib) then
			doWireInputs(ent);
			doWireOutputs(ent);
			ent.fadeTriggerInput = ent.fadeTriggerInput or ent.TriggerInput;
			ent.TriggerInput = TriggerInput;
			if (not ent.IsWire) then -- Dupe Support
				ent.fadePreEntityCopy = ent.PreEntityCopy;
				ent.PreEntityCopy = PreEntityCopy;
				ent.fadePostEntityPaste = ent.PostEntityPaste;
				ent.PostEntityPaste = PostEntityPaste;
			end				
		end
	end
	ent.fadeUpNum = numpad.OnUp(ply, stuff.key, "Fading Door onUp", ent);
	ent.fadeDownNum = numpad.OnDown(ply, stuff.key, "Fading Door onDown", ent);
	ent.fadeToggle = stuff.toggle;
	if (stuff.reversed) then
		ent:fadeActivate();
	end
	duplicator.StoreEntityModifier(ent, "Fading Door", stuff);
	return true;
end

duplicator.RegisterEntityModifier("Fading Door", dooEet);

if (not FadingDoor) then
	local function legacy(ply, ent, data)
		return dooEet(ply, ent, {
			key      = data.Key;
			toggle   = data.Toggle;
			reversed = data.Inverse;
			noeffect = data.NoEffect;
		});
	end
	duplicator.RegisterEntityModifier("FadingDoor", legacy);
end

local function doUndo(undoData, ent)
	if (IsValid(ent)) then
		onRemove(ent);
		ent:fadeDeactivate();
		ent.isFadingDoor = false;
		if (WireLib) then
			ent.TriggerInput = ent.fadeTriggerInput;
			if (ent.Inputs) then
				Wire_Link_Clear(ent, "Fade");
				ent.Inputs['Fade'] = nil;
				WireLib._SetInputs(ent);
			end if (ent.Outputs) then
				local port = ent.Outputs['FadeActive']
				if (port) then
					for i,inp in ipairs(port.Connected) do
						if (inp.Entity:IsValid()) then
							Wire_Link_Clear(inp.Entity, inp.Name)
						end
					end
				end
				ent.Outputs['FadeActive'] = nil;
				WireLib._SetOutputs(ent);
			end
		end
	end
end

function TOOL:LeftClick(tr)
	if (not checkTrace(tr)) then
		return false;
	end
	local ent = tr.Entity;
	local ply = self:GetOwner();
	dooEet(ply, ent, {
		key      = self:GetClientNumber("key");
		toggle   = self:GetClientNumber("toggle") == 1;
		reversed = self:GetClientNumber("reversed") == 1;
	});
	undo.Create("fading_door");
		undo.AddFunction(doUndo, ent);
		undo.SetPlayer(ply);
	undo.Finish();
	return true
end


Just a guess that you can remove numpad.OnUp numpad.OnDown and numpad.Register and they will just stop working. Also the numpad.Remove so it doesnt error.

Wouldn’t this disable the ability to open the door with REGULAR keypads? Wire buttons and keypads will still work, but I’m quite sure that the regular keypads and buttons break if you remove those lines. Could be wrong though.

The non-wire keypad simulates the player pressing the button. Your best option, that isn’t so great, is to enforce people to use wiremod.
Although it is possible.

It may be possible through some horrid code, which will wreck shit (autorun/server):
[lua]
-snip code to not be reproduced (read below replies)-
[/lua]

That’ll break every tool that uses the numpad lib from not using keypads for input too :v: So sort of useless
(Smart clients might get around this)

Yeah, you’re probably right. Iv’e never messed with any of that code. Makes sense that the “Buttons” are just simulating keypresses.

Actually while just reading through the wiki (and Ctrl + numpad), I found this: numpad.FromButton( )

Basically, for every place you check for numpad.OnUp() and numpad.OnDown(), add a check if numpad.FromButton() is also true, and you’re done!

I’ve never noticed this, sweet.

[editline]x[/editline]

The base gamemode doesn’t pass the parameter neccesary to make this work

I started Lua not even a week ago :V

well, to be fair, I made some E2 functions a month or two ago and modified some DarkRP shit, other than that, just started on my first gamemode less than a week ago.

Ok I had a look in to that method, and it seems to actually be for buttons created by the button STOOL, not keyboard presses.

In this case I’ve decided to update the keypad to also set this variable, so you should be able to use numpad.FromButton() to check if input was supplied by a button/keypad. (I’ll release this update a little later tonight).

[editline]25th June 2014[/editline]

Updated

@OP, The Fading Door addon has been updated to be compatibe with my keypad tool’s new update, adding a “fading_door_nokeyboard” convar.

Just chuck “fading_door_nokeyboard 1” in to your server.cfg assuming you are using both of the following addons:

[editline]x[/editline]

Goodbye merge

Thanks for the support guys, I’ll add the setting now.