make select split work

This commit is contained in:
Daniella / Tove 2024-05-31 20:00:16 +02:00
parent 9ab7c76346
commit a77fbba876
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
5 changed files with 65 additions and 9 deletions

View file

@ -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;
}
}

View file

@ -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<Socket> clients = new ArrayList<>();
public ServerSocket server = null;
private int id = 0, maxID = 0;
private final Queue<RemoteEvent> toSend = new LinkedList<>();
private final Queue<RemoteEvent> 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;
}
}

View file

@ -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;
}
}

View file

@ -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

View file

@ -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