Skip to content

Commit 5e7b5a0

Browse files
main.go: Various fixes.
1 parent 1f35f3b commit 5e7b5a0

File tree

2 files changed

+42
-70
lines changed

2 files changed

+42
-70
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,3 @@ worldrenderer will automatically run and render the chunks in cache in real-time
2929
- `v1` (post-v1.2.13, only a single layer)
3030
- `v8/v9` (post-v1.2.13, up to 256 layers) (persistent and runtime)
3131
- `v9 (sub-chunk request system)` (post-v1.18.0, up to 256 layers) (persistent and runtime)
32-
33-
## credits
34-
35-
special thanks to [T 14Raptor](https://github.com/T14Raptor) for his help with the original worldcompute project, and
36-
[Sandertv](https://github.com/Sandertv) for his work on gophertunnel and dragonfly.

main.go

Lines changed: 42 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ import (
2626
)
2727

2828
var (
29-
chunkMu sync.Mutex
30-
chunks = make(map[world.ChunkPos]*chunk.Chunk)
31-
renderer *worldrenderer.Renderer
32-
saveInProgress bool
29+
mu sync.Mutex
30+
chunks = make(map[world.ChunkPos]*chunk.Chunk)
31+
renderer *worldrenderer.Renderer
3332
)
3433

3534
// main starts the renderer and proxy.
@@ -69,7 +68,7 @@ func main() {
6968
}
7069
}()
7170

72-
renderer = worldrenderer.NewRendererDirect(4, 6.5, mgl64.Vec2{}, &chunkMu, chunks)
71+
renderer = worldrenderer.NewRendererDirect(4, 6.5, mgl64.Vec2{}, &mu, chunks)
7372

7473
ebiten.SetWindowSize(1718, 1360)
7574
ebiten.SetWindowResizable(true)
@@ -98,9 +97,6 @@ func handleConn(log *logrus.Logger, conn *minecraft.Conn, listener *minecraft.Li
9897

9998
airRID, _ := chunk.StateToRuntimeID("minecraft:air", nil)
10099
oldFormat := data.BaseGameVersion == "1.17.40"
101-
if oldFormat {
102-
log.Debugf("old format detected, using old biomes")
103-
}
104100

105101
pos := data.PlayerPosition
106102
dimension := world.Dimension(world.Overworld)
@@ -137,8 +133,6 @@ func handleConn(log *logrus.Logger, conn *minecraft.Conn, listener *minecraft.Li
137133
g.Wait()
138134

139135
log.Printf("successfully spawned in to %s", config.Connection.RemoteAddress)
140-
141-
// TODO: Clean up this shithole lmao
142136
go func() {
143137
defer listener.Disconnect(conn, "connection lost")
144138
defer serverConn.Close()
@@ -165,63 +159,48 @@ func handleConn(log *logrus.Logger, conn *minecraft.Conn, listener *minecraft.Li
165159
if len(line) == 0 {
166160
continue
167161
}
168-
169-
processed := true
170162
switch line[0] {
163+
case "/cancel":
164+
_ = conn.WritePacket(&packet.Text{Message: text.Colourf("<red><bold><italic>Terminated save.</italic></bold></red>")})
165+
continue
171166
case "/reset":
172-
chunkMu.Lock()
167+
mu.Lock()
173168
for chunkPos := range chunks {
174169
delete(chunks, chunkPos)
175170
}
176-
chunkMu.Unlock()
171+
mu.Unlock()
177172

178173
renderer.Rerender()
179-
case "/save":
180-
_ = conn.WritePacket(&packet.Text{Message: text.Colourf("<yellow><bold><italic>What would you like to save the file as?</italic></bold></yellow>")})
181-
saveInProgress = true
182-
case "/cancel":
183-
_ = conn.WritePacket(&packet.Text{Message: text.Colourf("<red><bold><italic>Terminated save.</italic></bold></red>")})
184-
continue
185-
default:
186-
processed = false
187-
}
188-
if processed {
189-
// Processed a command, don't send it to the server
190174
continue
191-
}
192-
case *packet.Text:
193-
if !saveInProgress {
194-
break
195-
}
196-
saveInProgress = false
197-
198-
fileName := pk.Message
199-
_ = conn.WritePacket(&packet.Text{Message: text.Colourf("<aqua><bold><italic>Processing chunks to be saved...</italic></bold></aqua>")})
200-
go func() {
201-
prov, err := mcdb.New(fileName, dimension)
202-
if err != nil {
203-
panic(err)
204-
}
205-
for pos, c := range chunks {
206-
c.Compact()
207-
err = prov.SaveChunk(pos, c)
175+
case "/save":
176+
saveName := strings.Join(line[1:], " ")
177+
_ = conn.WritePacket(&packet.Text{Message: text.Colourf("<aqua><bold><italic>Processing chunks to be saved...</italic></bold></aqua>")})
178+
go func() {
179+
prov, err := mcdb.New(saveName, dimension)
180+
if err != nil {
181+
panic(err)
182+
}
183+
for pos, c := range chunks {
184+
c.Compact()
185+
err = prov.SaveChunk(pos, c)
186+
if err != nil {
187+
panic(err)
188+
}
189+
}
190+
prov.SaveSettings(&world.Settings{
191+
Name: data.WorldName,
192+
Spawn: [3]int{int(pos.X()), int(pos.Y()), int(pos.Z())},
193+
Time: data.Time,
194+
})
195+
err = prov.Close()
208196
if err != nil {
209197
panic(err)
210198
}
211-
}
212-
prov.SaveSettings(&world.Settings{
213-
Name: data.WorldName,
214-
Spawn: [3]int{int(pos.X()), int(pos.Y()), int(pos.Z())},
215-
Time: data.Time,
216-
})
217-
err = prov.Close()
218-
if err != nil {
219-
panic(err)
220-
}
221199

222-
_ = conn.WritePacket(&packet.Text{Message: text.Colourf("<green><bold><italic>Saved all chunks received to the \"%v\" folder!</italic></bold></green>", fileName)})
223-
}()
224-
continue
200+
_ = conn.WritePacket(&packet.Text{Message: text.Colourf("<green><bold><italic>Saved all chunks received to the \"%v\" folder!</italic></bold></green>", saveName)})
201+
}()
202+
continue
203+
}
225204
}
226205
if err := serverConn.WritePacket(pk); err != nil {
227206
if disconnect, ok := errors.Unwrap(err).(minecraft.DisconnectError); ok {
@@ -276,32 +255,32 @@ func handleConn(log *logrus.Logger, conn *minecraft.Conn, listener *minecraft.Li
276255
pk.Position.Z() + int32(entry.Offset[2]),
277256
}
278257

279-
chunkMu.Lock()
258+
mu.Lock()
280259
c, ok := chunks[offsetPos]
281260
if !ok {
282261
c = chunk.New(airRID, dimension.Range())
283262
chunks[offsetPos] = c
284263
}
285-
chunkMu.Unlock()
264+
mu.Unlock()
286265

287266
var ind byte
288267
newSub, err := chunk.DecodeSubChunk(bytes.NewBuffer(entry.RawPayload), c, &ind, chunk.NetworkEncoding)
289268
if err == nil {
290-
chunkMu.Lock()
269+
mu.Lock()
291270
c.Sub()[ind] = newSub
292-
chunkMu.Unlock()
271+
mu.Unlock()
293272
}
294273

295274
renderer.RerenderChunk(offsetPos)
296275
}
297276
}
298277
}()
299278
case *packet.ChangeDimension:
300-
chunkMu.Lock()
279+
mu.Lock()
301280
for chunkPos := range chunks {
302281
delete(chunks, chunkPos)
303282
}
304-
chunkMu.Unlock()
283+
mu.Unlock()
305284

306285
dimension = world.Dimension(world.Overworld)
307286
switch pk.Dimension {
@@ -319,9 +298,9 @@ func handleConn(log *logrus.Logger, conn *minecraft.Conn, listener *minecraft.Li
319298
chunkPos := world.ChunkPos{pk.Position.X(), pk.Position.Z()}
320299
c, err := chunk.NetworkDecode(airRID, pk.RawPayload, int(pk.SubChunkCount), oldFormat, dimension.Range())
321300
if err == nil {
322-
chunkMu.Lock()
301+
mu.Lock()
323302
chunks[chunkPos] = c
324-
chunkMu.Unlock()
303+
mu.Unlock()
325304

326305
renderer.RerenderChunk(chunkPos)
327306
}
@@ -389,8 +368,6 @@ func tokenSource() oauth2.TokenSource {
389368
src := auth.RefreshTokenSource(token)
390369
_, err = src.Token()
391370
if err != nil {
392-
// The cached refresh token expired and can no longer be used to obtain a new token. We require the
393-
// user to log in again and use that token instead.
394371
token, err = auth.RequestLiveToken()
395372
check(err)
396373
src = auth.RefreshTokenSource(token)

0 commit comments

Comments
 (0)