From a77fbba87699b71a43c70dcd4e80b5ac23d675ef Mon Sep 17 00:00:00 2001 From: TudbuT Date: Fri, 31 May 2024 20:00:16 +0200 Subject: [PATCH] make select split work --- .../client/event/events/SelectEvent.java | 13 ++++++ .../event/remote/RemoteEventManager.java | 40 ++++++++++++++++--- .../event/remote/events/RemoteInitEvent.java | 13 ++++++ .../client/feature/client/Baritone.java | 6 ++- .../baseband/client/feature/world/Select.java | 2 +- 5 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 Client/src/main/java/com/baseband/client/event/remote/events/RemoteInitEvent.java diff --git a/Client/src/main/java/com/baseband/client/event/events/SelectEvent.java b/Client/src/main/java/com/baseband/client/event/events/SelectEvent.java index f3432d7..cfd3a47 100644 --- a/Client/src/main/java/com/baseband/client/event/events/SelectEvent.java +++ b/Client/src/main/java/com/baseband/client/event/events/SelectEvent.java @@ -1,6 +1,9 @@ package com.baseband.client.event.events; +import com.baseband.client.BaseBand; import com.baseband.client.event.remote.RemoteEvent; +import com.baseband.client.event.remote.RemoteEventManager; +import com.baseband.client.util.interact.BlockUtils; import com.baseband.client.util.type.Selection; import de.tudbut.obj.Save; @@ -11,4 +14,14 @@ public class SelectEvent extends RemoteEvent { public SelectEvent(Selection selection) { this.selection = selection; } + + public Selection getMySelection() { + if(BaseBand.remoteEventManager.isConnected()) { + RemoteEventManager manager = BaseBand.remoteEventManager; + Selection[] splitSelection = BlockUtils.splitSelection1D(selection, manager.getPeers()); + if(splitSelection.length > manager.getID()) + return splitSelection[manager.getID()]; + else return null; + } else return selection; + } } diff --git a/Client/src/main/java/com/baseband/client/event/remote/RemoteEventManager.java b/Client/src/main/java/com/baseband/client/event/remote/RemoteEventManager.java index 464f0eb..f41eb1b 100644 --- a/Client/src/main/java/com/baseband/client/event/remote/RemoteEventManager.java +++ b/Client/src/main/java/com/baseband/client/event/remote/RemoteEventManager.java @@ -2,6 +2,7 @@ package com.baseband.client.event.remote; import com.baseband.client.BaseBand; import com.baseband.client.Setup; +import com.baseband.client.event.remote.events.RemoteInitEvent; import com.baseband.client.feature.client.AltControl; import de.tudbut.io.TypedInputStream; import de.tudbut.io.TypedOutputStream; @@ -27,10 +28,12 @@ public class RemoteEventManager { public final ArrayList clients = new ArrayList<>(); public ServerSocket server = null; + private int id = 0, maxID = 0; + private final Queue toSend = new LinkedList<>(); private final Queue toProcess = new LinkedList<>(); - public boolean connect(@Nullable String ip) { + public void connect(@Nullable String ip) { try { end(); server = new ServerSocket(Setup.Port); @@ -40,10 +43,9 @@ public class RemoteEventManager { head = new Socket(ip, Setup.Port); initClient(); } catch (IOException ex) { - return false; + BaseBand.notify("[Remote] Failed to start."); } } - return true; } public void end() { @@ -56,6 +58,8 @@ public class RemoteEventManager { } } catch (IOException ignored) {} clients.clear(); + id = -1; + maxID = 0; server = null; head = null; BaseBand.ifFeatureEnabled(AltControl.class, f -> f.setEnabled(false)); @@ -73,6 +77,8 @@ public class RemoteEventManager { private void runServer() { try { + id = 0; + maxID = 0; server.setSoTimeout(1); while(server != null) { try { @@ -84,6 +90,7 @@ public class RemoteEventManager { s.getOutputStream().flush(); s.setSoTimeout(1); clients.add(s); + publish(new RemoteInitEvent(++maxID)); } else { s.close(); } @@ -165,13 +172,34 @@ public class RemoteEventManager { } public boolean isConnected() { - return head != null || server != null; + return id != -1 && (head != null || server != null); } public void onTick() { - while(!toProcess.isEmpty()) - BaseBand.eventManager.publish(toProcess.poll()); + while(!toProcess.isEmpty()) { + RemoteEvent event = toProcess.poll(); + if(event instanceof RemoteInitEvent) { + if(id == -1) { + id = ((RemoteInitEvent) event).id; + } + else { + maxID = ((RemoteInitEvent) event).id; + } + } + else { + BaseBand.eventManager.publish(event); + } + } if(!isConnected()) toSend.clear(); } + + public int getID() { + return id; + } + + public int getPeers() { + return maxID; + } + } diff --git a/Client/src/main/java/com/baseband/client/event/remote/events/RemoteInitEvent.java b/Client/src/main/java/com/baseband/client/event/remote/events/RemoteInitEvent.java new file mode 100644 index 0000000..2c961aa --- /dev/null +++ b/Client/src/main/java/com/baseband/client/event/remote/events/RemoteInitEvent.java @@ -0,0 +1,13 @@ +package com.baseband.client.event.remote.events; + +import com.baseband.client.event.remote.RemoteEvent; +import de.tudbut.obj.Save; + +public class RemoteInitEvent extends RemoteEvent { + @Save + public int id; + + public RemoteInitEvent(int id) { + this.id = id; + } +} diff --git a/Client/src/main/java/com/baseband/client/feature/client/Baritone.java b/Client/src/main/java/com/baseband/client/feature/client/Baritone.java index f4a8d5f..60dc35b 100644 --- a/Client/src/main/java/com/baseband/client/feature/client/Baritone.java +++ b/Client/src/main/java/com/baseband/client/feature/client/Baritone.java @@ -8,6 +8,7 @@ import com.baseband.client.feature.Feature; import com.baseband.client.feature.category.ClientCategory; import com.baseband.client.util.baritone.BaritoneManager; import com.baseband.client.util.baritone.BaritonePresenceManager; +import com.baseband.client.util.type.Selection; @ClientCategory public class Baritone extends Feature { @@ -37,8 +38,9 @@ public class Baritone extends Feature { public void onSelect(SelectEvent event) { ISelectionManager selectionManager = BaritoneManager.getBaritone().getSelectionManager(); selectionManager.removeAllSelections(); - if(event.selection != null) - selectionManager.addSelection(new BetterBlockPos(event.selection.pos1), new BetterBlockPos(event.selection.pos2)); + Selection selection = event.getMySelection(); + if(selection != null) + selectionManager.addSelection(new BetterBlockPos(selection.pos1), new BetterBlockPos(selection.pos2)); } @Override diff --git a/Client/src/main/java/com/baseband/client/feature/world/Select.java b/Client/src/main/java/com/baseband/client/feature/world/Select.java index a162b83..a5cbb79 100644 --- a/Client/src/main/java/com/baseband/client/feature/world/Select.java +++ b/Client/src/main/java/com/baseband/client/feature/world/Select.java @@ -32,7 +32,7 @@ public class Select extends Feature { @Priority(Integer.MAX_VALUE) public void onSelect(SelectEvent event) { - selection = event.selection; + selection = event.getMySelection(); } @Override