-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
208 lines (152 loc) · 5.55 KB
/
server.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from typing import List, Any
import world
from world import World
import entity
import math
import os
import util
from entity import Entity
from player import Player
from util import BlockPos, ChunkPos
from nbt import nbt
from inventory import Stack, Slot
from dimension import Dimension
from dataclasses import dataclass
@dataclass
class Window:
playerId: int
pos: BlockPos
kind: str
class ServerState:
dimensions: List[Dimension]
players: List[Player]
preloadPos: ChunkPos
heldItems: dict[int, Stack]
craftSlots: dict[int, List[Slot]]
breakingBlock: float
breakingBlockPos: BlockPos
localPlayer: int
nextTeleportId: int
nextEntityId: int
nextWindowId: int
openWindows: dict[int, Window]
time: int
renderDistanceSq: int
chat: List[Any]
tickTimes: List[float]
tickTimeIdx: int
gravity: float
preloadProgress: int
saveName: str
def __init__(self):
self.nextTeleportId = 1
self.nextEntityId = 2
self.nextWindowId = 1
self.breakingBlock = 0.0
self.breakingBlockPos = BlockPos(0, 0, 0)
self.openWindows = {}
self.heldItems = {}
self.craftSlots = {}
self.tickTimes = [0.0] * 10
self.tickTimeIdx = 0
self.time = 0
self.gravity = 0.10
self.preloadProgress = 0
self.players = []
def getWindowId(self):
self.nextWindowId += 1
return self.nextWindowId
def getEntityId(self):
self.nextEntityId += 1
return self.nextEntityId
def getTeleportId(self):
self.nextTeleportId += 1
return self.nextTeleportId
def getLocalPlayer(self) -> Player:
for player in self.players:
if player.entityId == self.localPlayer:
return player
raise Exception("No local player")
def getLocalDimension(self) -> Dimension:
player = self.getLocalPlayer()
return self.getDimensionOf(player)
def getDimensionOf(self, player: Player) -> Dimension:
return self.getDimension(player.dimension)
def getDimension(self, name: str) -> Dimension:
if name == 'minecraft:overworld':
return self.dimensions[0]
elif name == 'minecraft:the_nether':
return self.dimensions[1]
else:
# TODO:
raise Exception()
def save(self):
self.saveWorld()
self.saveEntities()
self.savePlayers()
def saveWorld(self):
for dim in self.dimensions:
dim.world.save()
def saveEntities(self):
for dim in self.dimensions:
# FIXME:
path = self.saveFolderPath() + '/entities.dat'
nbtfile = nbt.NBTFile()
nbtfile.name = 'Entities'
# FIXME:
nbtfile.tags.append(entity.toNbt(dim.entities))
nbtfile.write_file(path)
def savePlayers(self):
os.makedirs(self.saveFolderPath() + '/playerdata', exist_ok=True)
for player in self.players:
# FIXME:
path = self.saveFolderPath() + '/playerdata/player.dat'
nbtfile = nbt.NBTFile()
nbtfile.name = 'Player'
nbtfile.tags = player.toNbt().tags
nbtfile.write_file(path)
def addPlayer(self, app):
path = self.saveFolderPath() + '/playerdata/player.dat'
try:
nbtfile = nbt.NBTFile(filename=path)
player = Player(app, tag=nbtfile)
except FileNotFoundError:
player = Player(app)
player.pos[1] = 75.0
player.entityId = self.getEntityId()
# TODO: Send `Join Game` packet
self.players.append(player)
self.localPlayer = player.entityId
def saveFolderPath(self):
return f'saves/{self.saveName}'
@classmethod
def open(cls, worldName, seed, importPath, app):
server = cls()
server.saveName = worldName
overworld = Dimension()
overworld.world = World(server.saveFolderPath() + '/region', world.OverworldGen(), seed, importPath=importPath)
overworld.world.dimTy = util.DIMENSION_CODEC.getDimension('minecraft:overworld').ty
try:
path = server.saveFolderPath() + '/entities.dat'
nbtfile = nbt.NBTFile(path)
overworld.entities = [entity.Entity(app, server.getEntityId(), nbt=tag) for tag in nbtfile["Entities"][1:]]
except FileNotFoundError:
overworld.entities = [entity.Entity(app, server.getEntityId(), 'fox', 5.0, 75.0, 3.0)]
nether = Dimension()
nether.world = World(server.saveFolderPath() + '/DIM-1/region', world.NetherGen(), seed, importPath=importPath)
nether.world.dimTy = util.DIMENSION_CODEC.getDimension('minecraft:the_nether').ty
try:
path = server.saveFolderPath() + '/DIM-1/entities.dat'
nbtfile = nbt.NBTFile(path)
nether.entities = [entity.Entity(app, server.getEntityId(), nbt=tag) for tag in nbtfile["Entities"][1:]]
except FileNotFoundError:
nether.entities = []
server.dimensions = [overworld, nether]
server.addPlayer(app)
preloadPos = server.players[0].pos
cx = math.floor(preloadPos[0] / 16)
cy = math.floor(preloadPos[1] / world.CHUNK_HEIGHT)
cz = math.floor(preloadPos[2] / 16)
server.preloadPos = ChunkPos(cx, cy, cz)
server.getLocalDimension().world.loadChunk((app.textures, app.cube, app.textureIndices), server.preloadPos)
return server