Skip to content

Commit

Permalink
feat: bungee tags (#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Nov 18, 2024
1 parent 7e98127 commit a70e83b
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,17 @@ public void onEnable() {

this.registerCommands();
this.registerTags();
this.registerChannels();

this.portalServices.loadPortals();
this.destinationServices.loadDestinations();
this.infoLogger.info(Lang.translate("logger.pluginenable"));
}

private void registerChannels() {
this.serverContainer.registerOutgoingChannel(BungeeTag.PACKET_CHANNEL);
}

private void registerTags() {
this.tagRegistry.registerTag(new NameTag());
this.tagRegistry.registerTag(new DestiTag());
Expand All @@ -132,6 +137,7 @@ private void registerTags() {
this.tagRegistry.registerTag(new CommandTag());
this.tagRegistry.registerTag(new PortalEventTag());
this.tagRegistry.registerTag(new MessageTag());
this.tagRegistry.registerTag(new BungeeTag());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public void playerLeave(PlayerContainer player) {
this.playerDataServices.playerLeave(player);
}

public void incomingMessage(PlayerContainer player, String channel, byte[] message) {
// TODO implement proxy handling
}

public void tick() {
this.gameScheduler.tick();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public interface ServerContainer {

PlayerContainer[] getPlayers();

void registerOutgoingChannel(String channel);

void registerIncomingChannel(String channel);

void dispatchCommand(UUID uuid, String command,
CommandTag.CommandLevel commandLevel);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.sekwah.advancedportals.core.tags;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.google.inject.Inject;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.registry.TagTarget;
import com.sekwah.advancedportals.core.repository.ConfigRepository;
import com.sekwah.advancedportals.core.util.FriendlyDataOutput;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.core.warphandler.ActivationData;
import com.sekwah.advancedportals.core.warphandler.Tag;

import java.util.Random;

public class BungeeTag implements Tag.Activation {

public static final String PACKET_CHANNEL = "BungeeCord";
@Inject
ConfigRepository configRepository;

public static String TAG_NAME = "bungee";

private final TagType[] tagTypes =
new TagType[] {TagType.PORTAL};

private final Random random = new Random();

@Override
public TagType[] getTagTypes() {
return tagTypes;
}

@Override
public String getName() {
return TAG_NAME;
}

@Override
public String[] getAliases() {
return null;
}

@Override
public String description() {
return Lang.translate("tag.bungee.description");
}

@Override
public boolean preActivated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) {
return true;
}

@Override
public void postActivated(TagTarget target, PlayerContainer player, ActivationData activationData, String[] argData) {

}

@Override
public boolean activated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) {
String selectedArg = argData[random.nextInt(argData.length)];

ByteArrayDataOutput outForSend = ByteStreams.newDataOutput();
outForSend.writeUTF("Connect");
outForSend.writeUTF(selectedArg);
player.sendPacket(BungeeTag.PACKET_CHANNEL, outForSend.toByteArray());
activeData.setWarpStatus(ActivationData.WarpedStatus.WARPED);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@
import java.util.List;
import java.util.Random;

/**
* The name of the destination or portal.
*
* <p>Most of the implementation of this tag is external, this is just to allow
* for the tag to be used.
*
* <p>Most tags shouldn't be like this unless they are to be paired with
* another tag.
*/
public class MessageTag implements Tag.Activation {

@Inject
Expand Down Expand Up @@ -55,10 +46,6 @@ 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);
activeData.setWarpStatus(ActivationData.WarpedStatus.ACTIVATED);
return true;
}

Expand All @@ -77,7 +64,10 @@ public void postActivated(TagTarget target, PlayerContainer player, ActivationDa
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,20 @@ private int importPortals() {
if (triggerblock != null)
args.add(new DataTag("triggerblock", triggerblock.split(",")));
// It's called bungee as that's the implementation behind it
var bungee = config.getString(portalName + ".bungee");
if (bungee != null)
args.add(new DataTag("bungee", bungee.split(",")));

var destination = config.getString(portalName + ".destination");
if (destination != null)
args.add(new DataTag("destination", destination.split(",")));

var bungee = config.getString(portalName + ".bungee");
if (bungee != null) {
if(destination == null) {
args.add(new DataTag("bungee", bungee.split(",")));
} else {
args.add(new DataTag("proxy", bungee.split(",")));
}
}

ConfigurationSection portalConfigSection =
config.getConfigurationSection(portalName);
ConfigurationSection portalArgsConf =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package com.sekwah.advancedportals.spigot.connector.container;

import com.google.inject.Inject;
import com.sekwah.advancedportals.core.CoreListeners;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.connector.containers.ServerContainer;
import com.sekwah.advancedportals.core.connector.containers.WorldContainer;
import com.sekwah.advancedportals.core.tags.CommandTag;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import com.sekwah.advancedportals.spigot.AdvancedPortalsPlugin;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.plugin.messaging.PluginMessageListener;

public class SpigotServerContainer implements ServerContainer {
@Inject
private CoreListeners coreListeners;
private final Server server;
private final List<String> triggerBlockList =
Arrays.stream(Material.values())
Expand Down Expand Up @@ -67,6 +74,17 @@ public PlayerContainer[] getPlayers() {
.toArray(PlayerContainer[] ::new);
}

@Override
public void registerOutgoingChannel(String channel) {
server.getMessenger().registerOutgoingPluginChannel(AdvancedPortalsPlugin.getInstance(), channel);
}

@Override
public void registerIncomingChannel(String channel) {
server.getMessenger().registerIncomingPluginChannel(AdvancedPortalsPlugin.getInstance(), channel,
(s, player, bytes) -> coreListeners.incomingMessage(new SpigotPlayerContainer(player), s, bytes));
}

// Check if it's a material compatible with making portals
private boolean isAdvancedPortalBlock(Material material) {
return switch (material) {
Expand Down

0 comments on commit a70e83b

Please sign in to comment.