mc-firewall/README.md
2025-06-13 12:23:41 -07:00

6.1 KiB

Minecraft Windows Network Configuration

This guide provides PowerShell commands to configure Windows Firewall for Minecraft networking.

Prerequisites

  • Windows 10 or later
  • PowerShell running as Administrator
  • Java Runtime Environment (JRE)

Check Installed Java Versions

First, let's check which Java versions are installed on your system:

# List all Java installations
Get-Command java -ErrorAction SilentlyContinue | Select-Object Source
Get-ChildItem "C:\Program Files\Java" -ErrorAction SilentlyContinue
Get-ChildItem "C:\Program Files (x86)\Java" -ErrorAction SilentlyContinue
Get-ChildItem "$env:LOCALAPPDATA\Programs\Java" -ErrorAction SilentlyContinue
Get-ChildItem "$env:APPDATA\ATLauncher\jre" -ErrorAction SilentlyContinue

Firewall Configuration

Open PowerShell as Administrator and run the following commands:

1. Allow Java through Windows Firewall (Dynamic Selection)

# Function to create firewall rules for a specific Java path
function Add-MinecraftJavaFirewallRules {
    param (
        [string]$JavaPath
    )
    
    if (Test-Path $JavaPath) {
        Write-Host "Adding firewall rules for Java at: $JavaPath"
        New-NetFirewallRule -DisplayName "Minecraft Java" -Direction Inbound -Program $JavaPath -Action Allow
        New-NetFirewallRule -DisplayName "Minecraft Java" -Direction Outbound -Program $JavaPath -Action Allow
    } else {
        Write-Host "Java path not found: $JavaPath"
    }
}

# Get all possible Java paths
$javaPaths = @(
    "C:\Users\rob\AppData\Roaming\ATLauncher\jre\bin\java.exe",
    "C:\Program Files\Java\*\bin\java.exe",
    "C:\Program Files (x86)\Java\*\bin\java.exe",
    "$env:LOCALAPPDATA\Programs\Java\*\bin\java.exe"
)

# Find all existing Java installations
$foundJavaPaths = $javaPaths | ForEach-Object { Get-ChildItem $_ -ErrorAction SilentlyContinue } | Select-Object -ExpandProperty FullName

if ($foundJavaPaths.Count -gt 1) {
    Write-Host "Multiple Java installations found:"
    for ($i = 0; $i -lt $foundJavaPaths.Count; $i++) {
        Write-Host "[$i] $($foundJavaPaths[$i])"
    }
    
    $selection = Read-Host "Enter the number of the Java installation to use (or 'all' for all installations)"
    
    if ($selection -eq 'all') {
        foreach ($path in $foundJavaPaths) {
            Add-MinecraftJavaFirewallRules -JavaPath $path
        }
    } else {
        $index = [int]$selection
        if ($index -ge 0 -and $index -lt $foundJavaPaths.Count) {
            Add-MinecraftJavaFirewallRules -JavaPath $foundJavaPaths[$index]
        } else {
            Write-Host "Invalid selection"
        }
    }
} else {
    # If only one Java installation is found, use it automatically
    if ($foundJavaPaths) {
        Add-MinecraftJavaFirewallRules -JavaPath $foundJavaPaths[0]
    } else {
        Write-Host "No Java installations found"
    }
}

2. Open Required Minecraft Ports

# Allow Minecraft default port (25565)
New-NetFirewallRule -DisplayName "Minecraft TCP" -Direction Inbound -LocalPort 25565 -Protocol TCP -Action Allow
New-NetFirewallRule -DisplayName "Minecraft UDP" -Direction Inbound -LocalPort 25565 -Protocol UDP -Action Allow

3. Verify Rules

# List all Minecraft-related firewall rules
Get-NetFirewallRule | Where-Object DisplayName -like "*Minecraft*" | Format-Table DisplayName, Enabled, Direction, Action

LAN Connection Configuration

1. Network Discovery and File Sharing

# Enable Network Discovery
Set-NetFirewallRule -DisplayGroup "Network Discovery" -Enabled True

# Enable File and Printer Sharing
Set-NetFirewallRule -DisplayGroup "File and Printer Sharing" -Enabled True

2. Check Network Profile

# Get current network profile
Get-NetConnectionProfile | Select-Object Name, NetworkCategory

# Set network to Private (if needed)
Set-NetConnectionProfile -NetworkCategory Private

3. Verify LAN Connectivity

# Get local IP address
Get-NetIPAddress | Where-Object {$_.AddressFamily -eq "IPv4" -and $_.PrefixOrigin -eq "Dhcp"} | Select-Object IPAddress, InterfaceAlias

# Test connection to other players
Test-NetConnection -ComputerName <other_player_ip> -Port 25565

Removing Rules

If you need to remove the firewall rules, use these commands:

# Remove Minecraft firewall rules
Remove-NetFirewallRule -DisplayName "Minecraft Java"
Remove-NetFirewallRule -DisplayName "Minecraft TCP"
Remove-NetFirewallRule -DisplayName "Minecraft UDP"

Notes

  • Make sure to run PowerShell as Administrator
  • The script will automatically detect Java installations
  • Default Minecraft port is 25565, adjust if using a different port
  • These rules allow both incoming and outgoing connections for Minecraft
  • For LAN games, ensure all players are on the same network
  • Some antivirus software may block Minecraft connections

Troubleshooting

If you experience connection issues:

  1. Verify the rules are enabled:
Get-NetFirewallRule -DisplayName "Minecraft*" | Select-Object DisplayName, Enabled
  1. Check if the ports are actually open:
Test-NetConnection -ComputerName localhost -Port 25565
  1. Ensure Java is running with proper permissions:
Get-Process java | Select-Object Id, ProcessName, Path
  1. Check Java version:
java -version
  1. LAN Connection Issues:
# Check if Windows Defender Firewall is blocking Minecraft
Get-NetFirewallApplicationFilter -Program "C:\Users\rob\AppData\Roaming\ATLauncher\jre\bin\java.exe"

# Verify network sharing settings
Get-NetFirewallRule -DisplayGroup "Network Discovery" | Select-Object DisplayName, Enabled
Get-NetFirewallRule -DisplayGroup "File and Printer Sharing" | Select-Object DisplayName, Enabled

# Check if port is in use
netstat -ano | findstr :25565
  1. Common LAN Issues:
    • Ensure all players are on the same network
    • Check if Windows Defender Firewall is blocking the connection
    • Verify that Network Discovery is enabled
    • Make sure the host's computer is not in sleep mode
    • Check if any antivirus software is blocking the connection
    • Ensure the host's Minecraft version matches other players' versions