Skip to content

Commit

Permalink
wip: desti tags with proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Oct 26, 2024
1 parent 1b8c5a6 commit 353c4da
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.sekwah.advancedportals.core.network;

public interface Packet {
byte[] encode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.google.common.io.ByteStreams;
import com.sekwah.advancedportals.core.ProxyMessages;

public class ProxyCommandPacket {
public class ProxyCommandPacket implements Packet {

private final String command;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.sekwah.advancedportals.core.network;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.sekwah.advancedportals.core.ProxyMessages;

public class ProxyTransferDestiPacket implements Packet {

private final String serverName;
private final String destination;

public ProxyTransferDestiPacket(String serverName, String destination) {
this.serverName = serverName;
this.destination = destination;
}

public byte[] encode() {
ByteArrayDataOutput buffer = ByteStreams.newDataOutput();
buffer.writeUTF(ProxyMessages.PROXY_TRANSFER_DESTI);
buffer.writeUTF(this.serverName);
buffer.writeUTF(this.destination);
return buffer.toByteArray();

}

public static ProxyTransferDestiPacket decode(ByteArrayDataInput buffer) {
return new ProxyTransferDestiPacket(buffer.readUTF(), buffer.readUTF());
}

public String getServerName() {
return serverName;
}

public String getDestination() {
return destination;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.google.common.io.ByteStreams;
import com.sekwah.advancedportals.core.ProxyMessages;

public class ProxyTransferPacket {
public class ProxyTransferPacket implements Packet {

private final String serverName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public boolean preActivated(TagTarget target, PlayerContainer player,

activeData.setMetadata(TAG_NAME, selectedArg);

if(activeData.getMetadata(ProxyTag.TAG_NAME) != null) {
return true;
}

if(destinationServices.getDestination(selectedArg) == null) {
player.sendMessage(Lang.getNegativePrefix() + Lang.translateInsertVariables("desti.error.notfound", selectedArg));
return false;
Expand All @@ -92,6 +96,10 @@ public boolean preActivated(TagTarget target, PlayerContainer player,
@Override
public void postActivated(TagTarget target, PlayerContainer player,
ActivationData activationData, String[] argData) {
if(activationData.getMetadata(ProxyTag.TAG_NAME) != null) {
return;
}

var selectedArg = activationData.getMetadata(TAG_NAME);
Destination destination = destinationServices.getDestination(selectedArg);
if (destination != null) {
Expand All @@ -112,6 +120,10 @@ public void postActivated(TagTarget target, PlayerContainer player,
@Override
public boolean activated(TagTarget target, PlayerContainer player,
ActivationData activationData, String[] argData) {
if(activationData.getMetadata(ProxyTag.TAG_NAME) != null) {
return true;
}

var selectedArg = activationData.getMetadata(TAG_NAME);
Destination destination =
destinationServices.getDestination(selectedArg);
Expand Down Expand Up @@ -149,10 +161,4 @@ else if(this.configRepository.warpMessageInChat()) {
public List<String> autoComplete(String argData) {
return destinationServices.getDestinationNames();
}

@Nullable
@Override
public String splitString() {
return ",";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ public List<String> autoComplete(String argData) {
return null;
}

@Nullable
@Override
public String splitString() {
return null;
}

@Override
public boolean created(TagTarget target, PlayerContainer player,
String[] argData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ public List<String> autoComplete(String argData) {
return List.of("true", "false");
}

@Nullable
@Override
public String splitString() {
return "";
}

@Override
public Behaviour getDenyBehavior() {
return Behaviour.SILENT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import com.google.inject.Inject;
import com.sekwah.advancedportals.core.ProxyMessages;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.network.ProxyTransferDestiPacket;
import com.sekwah.advancedportals.core.network.ProxyTransferPacket;
import com.sekwah.advancedportals.core.registry.TagTarget;
import com.sekwah.advancedportals.core.repository.ConfigRepository;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.core.warphandler.ActivationData;
import com.sekwah.advancedportals.core.warphandler.Tag;

import javax.annotation.Nullable;
import java.util.Random;

public class ProxyTag implements Tag.Activation {
public class ProxyTag implements Tag.Activation, Tag.OrderPriority, Tag.Split {

@Inject
ConfigRepository configRepository;
Expand Down Expand Up @@ -48,6 +50,8 @@ public String description() {

@Override
public boolean preActivated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) {
String selectedArg = argData[random.nextInt(argData.length)];
activeData.setMetadata(TAG_NAME, selectedArg);
return true;
}

Expand All @@ -66,9 +70,16 @@ public boolean activated(TagTarget target, PlayerContainer player, ActivationDat

String selectedArg = argData[random.nextInt(argData.length)];

var packet = new ProxyTransferPacket(selectedArg);
var desti = activeData.getMetadata(DestiTag.TAG_NAME);

var packet = desti == null ? new ProxyTransferPacket(selectedArg) : new ProxyTransferDestiPacket(selectedArg, desti);
player.sendPacket(ProxyMessages.CHANNEL_NAME, packet.encode());
activeData.setWarpStatus(ActivationData.WarpedStatus.WARPED);
return true;
}

@Override
public Priority getPriority() {
return Priority.HIGHEST;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface Tag {
*/
enum TagType { PORTAL, DESTINATION }

enum Priority { LOWEST, LOW, NORMAL, HIGH, HIGHEST }
enum Priority { HIGHEST, HIGH, NORMAL, LOW, LOWEST }

/**
* Used to flag where the auto complete should show more or less info.
Expand Down Expand Up @@ -65,9 +65,6 @@ interface AutoComplete extends Tag {
*/
@Nullable
List<String> autoComplete(String argData);

@Nullable
String splitString();
}

interface Split extends Tag {
Expand All @@ -78,7 +75,9 @@ interface Split extends Tag {
* @return null if the tag does not support splitting
*/
@Nullable
String splitString();
default String splitString() {
return ",";
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@
import com.google.common.io.ByteStreams;
import com.sekwah.advancedportals.core.ProxyMessages;
import com.sekwah.advancedportals.core.network.ProxyCommandPacket;
import com.sekwah.advancedportals.core.network.ProxyTransferDestiPacket;
import com.sekwah.advancedportals.core.network.ProxyTransferPacket;
import com.sekwah.advancedportals.core.util.InfoLogger;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.proxycore.connector.container.ProxyContainer;
import com.sekwah.advancedportals.proxycore.connector.container.ProxyJoinData;
import com.sekwah.advancedportals.proxycore.connector.container.ProxyPlayerContainer;
import com.sekwah.advancedportals.proxycore.connector.container.ProxyServerContainer;

import java.util.HashMap;

public class AdvancedPortalsProxyCore {

private final InfoLogger logger;
private final ProxyContainer proxyContainer;

/**
* Keep a list of destinations players have been moved to
*
// TODO add a time to check against for this data to expire, and add a way to clean the data up every now and then
*/
public HashMap<String, ProxyJoinData> PlayerJoinMap = new HashMap<>();

public AdvancedPortalsProxyCore(InfoLogger logger, ProxyContainer proxyContainer) {
this.logger = logger;
this.proxyContainer = proxyContainer;
Expand Down Expand Up @@ -55,6 +66,10 @@ public void incomingMessage(ProxyPlayerContainer player, byte[] message) {
this.logger.info("Command request for " + player.getName() + " to run /" + commandPacket.getCommand());
this.proxyContainer.invokeCommand(player, commandPacket.getCommand());
break;
case ProxyMessages.PROXY_TRANSFER_DESTI:
var transferDestiPacket = ProxyTransferDestiPacket.decode(buffer);
this.logger.info("Transfer request for " + player.getName() + " to " + transferDestiPacket.getServerName() + " with destination " + transferDestiPacket.getDestination());
this.proxyContainer.transferPlayer(player, transferDestiPacket.getServerName());
default:
this.logger.info("Unknown message type: " + messageType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sekwah.advancedportals.proxycore.connector.container;

public class ProxyJoinData {

private final String destination;
private final String serverName;

public ProxyJoinData(String destination, String serverName) {
this.destination = destination;
this.serverName = serverName;
}
}

0 comments on commit 353c4da

Please sign in to comment.