-
Notifications
You must be signed in to change notification settings - Fork 0
/
minecraft.py
130 lines (116 loc) · 5.62 KB
/
minecraft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
collarHost = "192.168.1.47" # Note that headset packets require a response from server, but collar packets are just sent out
collarPort = 4120
from time import sleep
import socket
from simpleLog import log
import subprocess
# logPath = '/home/jamesh/.local/share/PolyMC/instances/Other 1.20.4/.minecraft/logs/latest.log'
logPath = '/home/jamesh/.local/share/PolyMC/instances/1.20.4/.minecraft/logs/latest.log'
lastLine = ""
currentHealthP1 = 0 # TODO: A better solution than this
previousHealthP1 = 20 # Assume full health on load
currentHealthP2 = 0
previousHealthP2 = 20
lowHealth = False # Custom rules for maps that change the health the player has. Detects when the health goes from 20 to another arbitrary value.
lowHealthLevel = "20" # Level of punishment delivered when player death is detected in lowHealth mode
lowHealthTrigger = 14 # Triggers punishment when this much damage is taken, so if set to 14, 20 to 6 will trigger.
p1 = "timothyhay"
p2 = "snoopyhoff"
players = 2 # How many players to use. p1 uses channel 1 by default.
channel = "1"
mode = "4" # 1: LED, 2: BEEP, 3: VIBRATE, 4: ZAP
shockMultiplier = 5
block = False # If set to true, no commands will be sent. Used for lowHealth
if mode == "4":
log("WARNING -- You are running with shocks ENABLED! -- WARNING", "w")
log("WARNING -- With a multiplier of "+str(shockMultiplier)+", the max shock level is "+str(shockMultiplier*20)+"! -- WARNING", "w")
def getLastLine(filePath):
with open(filePath, 'r') as file:
lines = file.readlines()
lastLine = lines[-1].strip() # remove newline character
return lastLine
def sendCollar(host, port, command):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(command.encode(), (host, port))
def pairCollar(): # Call me before the loop to pair collars
while True:
chan = input("CHANNEL (q to quit): ")
if chan == "q":
exit()
elif chan == "1":
log("Sending channel 1")
sendCollar(collarHost, collarPort, "1350")
elif chan == "2":
log("Sending channel 2")
sendCollar(collarHost, collarPort, "2350")
else:
log("Sending input as raw!")
sendCollar(collarHost, collarPort, str(chan))
# pairCollar()
log("I will monitor the Minecraft logfile located at "+logPath+" and fire packets at "+collarHost+":"+str(collarPort))
while True:
# sleep(.1)
# currentLine = getLastLine(logPath)
currentLine = str((subprocess.check_output(['tail', '-1', logPath])).decode('utf-8')[0:-1])
# if lastLine != currentLine:
if "~" in currentLine:
line = currentLine.split("~")
# log(line[1]) # Player name
# log(line[3]) # Player health
if line[1] == p1:
# log("fsd")
# log(currentHealthP1)
# log(previousHealthP1)
currentHealthP1 = line[3]
if currentHealthP1 != previousHealthP1:
damage = (int(currentHealthP1) - int(previousHealthP1))*-1
log(line[1]+" health: "+currentHealthP1)
if damage > 0:
log(line[1]+" damage: "+str(damage))
level = str(int(damage*shockMultiplier))
if 0 < int(level) <= 100:
if damage == lowHealthTrigger and lowHealth:
log(line[1]+" gets a level "+lowHealthLevel+" shock on channel 1.", "w")
sendCollar(collarHost, collarPort, "1"+mode+lowHealthLevel)
block = True
else:
block = False
if not block and not lowHealth:
log(line[1]+" gets a level "+level+" shock on channel 1.", "w")
sendCollar(collarHost, collarPort, "1"+mode+level)
elif line[1] == p2:
currentHealthP2 = line[3]
if currentHealthP2 != previousHealthP2:
damage = (int(currentHealthP2) - int(previousHealthP2))*-1
log(line[1]+" health: "+currentHealthP2)
if damage > 0:
log(line[1]+" damage: "+str(damage))
level = str(int(damage*shockMultiplier))
if 0 < int(level) <= 100:
if damage == lowHealthTrigger and lowHealth:
print("fds")
log(line[1]+" gets a level "+lowHealthLevel+" shock on channel 2.", "w")
sendCollar(collarHost, collarPort, "2"+mode+lowHealthLevel)
block = True
else:
block = False
if not block and not lowHealth:
print("asfed")
log(line[1]+" gets a level "+level+" shock on channel 2.", "w")
sendCollar(collarHost, collarPort, "2"+mode+level)
# currentHealth = int(currentLine.split("~")[1])
# damage = (currentHealth - previousHealth)*-1 # Get actual damage
# if currentHealth != previousHealth:
# log("Health: "+str(currentHealth))
# if damage > 0:
# log("Damage: "+str(damage)+" damage.")
# level = str(int(damage*shockMultiplier))
# if 0 < int(level) <= 100:
# log("Player gets a level "+level+" shock.")
# sendCollar(collarHost, collarPort, channel+mode+level)
else:
# log(currentLine)
pass
lastLine = currentLine
previousHealthP1 = currentHealthP1
previousHealthP2 = currentHealthP2