Skip to content

Commit 5dbeecb

Browse files
committed
- Remove transferred entities #3
- Static server Initialization #15
1 parent 6b35cfd commit 5dbeecb

6 files changed

Lines changed: 60 additions & 61 deletions

File tree

src/main/java/com/manwe/dsl/arbiter/ConnectionInfo.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,31 @@ public static ConnectionInfo read(JsonObject entry){
1414
int id = entry.get("id").getAsInt();
1515
return new ConnectionInfo(ip,port,id);
1616
}
17+
18+
public static ConnectionInfo read(String entry){
19+
int lastColon = entry.lastIndexOf(':');
20+
int secondColon = entry.lastIndexOf(':', lastColon - 1);
21+
22+
if (secondColon < 0 || lastColon < 0) throw new IllegalArgumentException("Missing fields in " + entry);
23+
24+
String host = entry.substring(0, secondColon);
25+
String portStr = entry.substring(secondColon + 1, lastColon);
26+
String idStr = entry.substring(lastColon + 1);
27+
28+
if (host.startsWith("[") && host.endsWith("]"))
29+
host = host.substring(1, host.length() - 1);
30+
31+
int port;
32+
int id;
33+
try {
34+
port = Integer.parseInt(portStr);
35+
id = Integer.parseInt(idStr);
36+
} catch (NumberFormatException e) {
37+
throw new IllegalArgumentException("None numeric values " + entry);
38+
}
39+
if (port < 0 || port > 65_535)
40+
throw new IllegalArgumentException("Port out of range (0-65535): " + port);
41+
42+
return new ConnectionInfo(host, port, id);
43+
}
1744
}
Lines changed: 18 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.manwe.dsl.config;
22

3+
import com.manwe.dsl.DistributedServerLevels;
4+
import com.manwe.dsl.arbiter.ConnectionInfo;
35
import net.neoforged.neoforge.common.ModConfigSpec;
46

57
import java.net.InetAddress;
8+
import java.net.UnknownHostException;
69
import java.util.List;
10+
import java.util.Objects;
711

812
public class DSLServerConfigs {
913

@@ -17,7 +21,8 @@ public class DSLServerConfigs {
1721
public static final ModConfigSpec.ConfigValue<Integer> WORKER_SIZE;
1822

1923
public static final ModConfigSpec.ConfigValue<Boolean> USE_ARBITER;
20-
//public static final ModConfigSpec.ConfigValue<List<InetAddress>> CONNECTIONS;
24+
public static final ModConfigSpec.ConfigValue<List<? extends String>> CONNECTIONS;
25+
public static final ModConfigSpec.ConfigValue<Integer> PORT;
2126

2227
static {
2328
BUILDER.push("Server");
@@ -32,62 +37,22 @@ public class DSLServerConfigs {
3237

3338
BUILDER.push("Auto start").comment("Address assignment for workers");
3439
USE_ARBITER = BUILDER.comment("Use the arbiter").define("use_arbiter",true);
40+
BUILDER.comment("use_arbiter = TRUE ->");
3541
ARBITER_ADDR = BUILDER.comment("Complete address of the arbiter, This is ignored if use_arbiter = false").define("arbiter_addr","http://localhost:8080");
36-
//BUILDER.push("If proxy").comment("Connections to the workers");
37-
//CONNECTIONS = BUILDER.comment("").define("arbiter_addr","http://localhost:8080");
38-
//BUILDER.pop();
39-
BUILDER.pop();
42+
BUILDER.comment("use_arbiter = FALSE ->");
43+
PORT = BUILDER.define("this_port",25565);
44+
CONNECTIONS = BUILDER.comment("If this is proxy and not using arbiter. Define worker address. ip:port:worker_id").defineListAllowEmpty("connections",List.of("127.0.0.1:25565:1","127.0.0.1:25566:2"),object -> {
45+
if (object instanceof String s) return true;
46+
return false;
47+
});
4048

41-
/*
42-
BUILDER.push("Into Pattern").comment(
43-
"""
44-
This is the ID pattern, form center to edge clockwise
45-
+-----------------------------------+
46-
| | |
47-
| 8 | 2 |
48-
| +--------+--------+ |
49-
| | | | |
50-
| | 7 | 1 | |
51-
|--------+--------+--------+--------|
52-
| | 5 | 3 | |
53-
| | | | |
54-
| +--------+--------+ |
55-
| 6 | 4 |
56-
| | |
57-
+-----------------------------------+
58-
59-
+-----------------------------------+
60-
| | |
61-
| | |
62-
| (-x+z) | (+x+z) |
63-
| | |
64-
| 4 | 1 |
65-
|-----------------+-----------------|
66-
| 3 | 2 |
67-
| | |
68-
| (-x-z) | (+x-z) |
69-
| | |
70-
| | |
71-
+-----------------------------------+
72-
73-
+-----------------------------------+
74-
| | |
75-
| | |
76-
| | |
77-
| | |
78-
| | |
79-
| 2 | 1 |
80-
| | |
81-
| | |
82-
| | |
83-
| | |
84-
| | |
85-
+-----------------------------------+
86-
"""
87-
);
8849
BUILDER.pop();
89-
*/
50+
9051
}
9152

9253
public static final ModConfigSpec SPEC = BUILDER.build();
54+
55+
public static List<ConnectionInfo> getConnectionAddresses() {
56+
return CONNECTIONS.get().stream().map(ConnectionInfo::read).toList();
57+
}
9358
}

src/main/java/com/manwe/dsl/dedicatedServer/proxy/ProxyDedicatedServer.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,19 @@ public ProxyDedicatedServer(Thread pServerThread, LevelStorageSource.LevelStorag
5656

5757
@Override
5858
public boolean initServer() throws IOException {
59-
try {
60-
topology = arbiterClient.fetch();
61-
System.out.println("Port from Arbiter: "+topology.port);
62-
} catch (Exception ex) {
63-
DistributedServerLevels.LOGGER.error("Unexpected Error",ex);
64-
throw new RuntimeException("Arbiter unavailable cannot fetch port and role");
59+
if(DSLServerConfigs.USE_ARBITER.get()){
60+
try {
61+
topology = arbiterClient.fetch();
62+
System.out.println("Port from Arbiter: "+topology.port);
63+
} catch (Exception ex) {
64+
DistributedServerLevels.LOGGER.error("Unexpected Error",ex);
65+
throw new RuntimeException("Arbiter unavailable cannot fetch port and role");
66+
}
67+
}else {
68+
topology = new ArbiterClient.ArbiterRes(DSLServerConfigs.PORT.get(),DSLServerConfigs.getConnectionAddresses());
6569
}
6670

71+
6772
if(!isProxy){
6873
EntityAccessor.getEntityCounter().set((workerId - 1) * SHARD_MAX_ENTITIES); //Set exclusive entity ids for each worker
6974
}

src/main/java/com/manwe/dsl/dedicatedServer/worker/packets/transfer/WorkerBoundPlayerTransferPacket.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ public DisconnectionDetails getDefaultDisconnectionDetails(){
9090

9191
public ServerPlayer rebuildServerPlayer(MinecraftServer server) {
9292
ServerPlayer clone = new ServerPlayer(server, server.overworld(), this.gameProfile, this.clientInformation);
93-
clone.setId(this.entityId); //Clone the id assigned by its source worker, entity id do not collide between workers each worker has its own id range
94-
clone.setChunkTrackingView(new ChunkTrackingView.Positioned(clone.chunkPosition(),viewDistance)); //Set this view distance to avoid chunk resends
9593
clone.load(this.playerNbt);
94+
clone.setChunkTrackingView(new ChunkTrackingView.Positioned(clone.chunkPosition(),viewDistance)); //Set this view distance to avoid chunk resends
95+
clone.setId(this.entityId); //Clone the id assigned by its source worker, entity id do not collide between workers each worker has its own id range
9696
return clone;
9797
}
9898

src/main/java/com/manwe/dsl/dedicatedServer/worker/packets/transfer/WorkerBoundaryListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public void onEnteringSection(EntityEvent.EnteringSection event){
4646
}else {
4747
System.out.println("Transfer Entity NeoForge");
4848
workerListener.send(new ProxyBoundEntityTransferPacket(entity,id));
49+
entity.remove(Entity.RemovalReason.DISCARDED); //DELETE
4950
}
5051
}
5152
} catch (NoSuchElementException exception){

src/main/java/com/manwe/dsl/mixinExtension/ServerLevelExtension.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
public interface ServerLevelExtension {
66

77
void distributedServerLevels$addEntityWithoutEvent(Entity entity);
8+
89
}

0 commit comments

Comments
 (0)