<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://bf2tech.uturista.pt/index.php?action=history&amp;feed=atom&amp;title=Cookbook%3AAdding_New_RCon_Commands</id>
	<title>Cookbook:Adding New RCon Commands - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://bf2tech.uturista.pt/index.php?action=history&amp;feed=atom&amp;title=Cookbook%3AAdding_New_RCon_Commands"/>
	<link rel="alternate" type="text/html" href="https://bf2tech.uturista.pt/index.php?title=Cookbook:Adding_New_RCon_Commands&amp;action=history"/>
	<updated>2026-08-06T00:32:20Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.40.1</generator>
	<entry>
		<id>https://bf2tech.uturista.pt/index.php?title=Cookbook:Adding_New_RCon_Commands&amp;diff=89&amp;oldid=prev</id>
		<title>Pireax: Created page with &quot;== Problem  ==  You want to create your own RCon command(s) to add administrative features to BF2. You really don&#039;t want to change the code for standard BF2 administration mod...&quot;</title>
		<link rel="alternate" type="text/html" href="https://bf2tech.uturista.pt/index.php?title=Cookbook:Adding_New_RCon_Commands&amp;diff=89&amp;oldid=prev"/>
		<updated>2018-06-21T19:28:41Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Problem  ==  You want to create your own RCon command(s) to add administrative features to BF2. You really don&amp;#039;t want to change the code for standard BF2 administration mod...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Problem  ==&lt;br /&gt;
&lt;br /&gt;
You want to create your own RCon command(s) to add administrative features to BF2. You really don&amp;#039;t want to change the code for standard BF2 administration module, though, because you know modifying the standard code is a Bad Idea™.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Solution  ==&lt;br /&gt;
&lt;br /&gt;
Use the dynamic object modification technique explained in the [[Cookbook:Changing_Objects_At_Runtime Changing Objects At Runtime]] recipie. Just import the following function from one of the main modules:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 import new, default&lt;br /&gt;
 &lt;br /&gt;
 def registerRConCommand(command, function):&lt;br /&gt;
     &amp;#039;&amp;#039;&amp;#039;Register a custom RCon command by dynamically adding a function that&lt;br /&gt;
      implements the new command as a new method of the default.AdminServer class.&lt;br /&gt;
      Parameters: command = a string that gives the name of the new RCon command&lt;br /&gt;
                  function = a function that implements the command.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
    &lt;br /&gt;
     # Put the function into a new instancemethod object bound to the AdminServer&lt;br /&gt;
     newMethod = new.instancemethod(function, default.server, default.AdminServer)&lt;br /&gt;
    &lt;br /&gt;
     # Create a new class attribute in AdminServer that&amp;#039;s wired to our function&lt;br /&gt;
     setattr(default.AdminServer, command, newMethod)&lt;br /&gt;
    &lt;br /&gt;
     # Add the new command into the rcon dispatch table, pointing to our function&lt;br /&gt;
     default.server.rcon_cmds[command] = newMethod&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;registerRConCommand&amp;lt;/code&amp;gt; takes care of everything you need to do to create a new RCon command; to use it, you just need to define the Python function you want your new RCon command to execute, and then call &amp;lt;code&amp;gt;registerRConCommand&amp;lt;/code&amp;gt; to register your new command and link it to your function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 # Create a function that executes our desired new RCon command; note that since&lt;br /&gt;
 # this is going to be a method within a class, the first argument to the&lt;br /&gt;
 # function MUST be &amp;quot;self&amp;quot;, even if we don&amp;#039;t use it. The admin module also&lt;br /&gt;
 # passes &amp;quot;ctx&amp;quot; (command context) and &amp;quot;cmd&amp;quot; (the rest of the RCon command line)&lt;br /&gt;
 # to our function.&lt;br /&gt;
 def rcmd_mycmd(self, ctx, cmd):&lt;br /&gt;
     # We can make our new command do anything we want here&lt;br /&gt;
     ctx.write(&amp;quot;We&amp;#039;re now executing mycmd.\n&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 # Now register the new RCon command&lt;br /&gt;
 registerRConCommand(&amp;#039;mycmd&amp;#039;, rcmd_mycmd)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After this code is imported, your new RCon command will be active; once you authenticate (&amp;lt;code&amp;gt;rcon login &amp;lt;password&amp;gt;&amp;lt;/code&amp;gt;) you can execute your command from the server console, or an in-game player console, by typing:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt; rcon mycmd &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to execute it from an RCon TCP connection, you just need to type &amp;lt;code&amp;gt;mycmd&amp;lt;/code&amp;gt; (without the &amp;quot;rcon&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Discussion  ==&lt;br /&gt;
&lt;br /&gt;
You can add as many commands as you want, each of which can implement any function you want--and they will behave exactly as though they were coded in the &amp;lt;code&amp;gt;Battlefield 2 Server/admin/default.py&amp;lt;/code&amp;gt; file--even though you never had to change that file!&lt;br /&gt;
&lt;br /&gt;
The reason this is so useful, and the reason why it&amp;#039;s such a bad idea to just hack around with the standard files, is that doing so makes it very difficult to upgrade to a new version of BF2 later, plus, if you have multiple mods or add-ons that want to add their own RCon commands, they will very quickly start tripping over and breaking each other if they&amp;#039;re all modifying the original code. This approach is much cleaner; in fact, you can have multiple scripts like this that are all developed separately by different people, but when they&amp;#039;re imported, they behave gracefully with each other, with no problem at all (unless two developers choose identical names for their new commands).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Submitted By  ==&lt;br /&gt;
&lt;br /&gt;
--Woody&lt;/div&gt;</summary>
		<author><name>Pireax</name></author>
	</entry>
</feed>