Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

built-in transfer command #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 126 additions & 124 deletions src/main/java/org/itxtech/synapseapi/SynapseAPI.java
Original file line number Diff line number Diff line change
@@ -1,124 +1,126 @@
package org.itxtech.synapseapi;

import cn.nukkit.Server;
import cn.nukkit.network.RakNetInterface;
import cn.nukkit.network.SourceInterface;
import cn.nukkit.network.protocol.DataPacket;
import cn.nukkit.network.protocol.ProtocolInfo;
import cn.nukkit.plugin.PluginBase;
import cn.nukkit.utils.ConfigSection;
import org.itxtech.synapseapi.messaging.Messenger;
import org.itxtech.synapseapi.messaging.StandardMessenger;
import org.itxtech.synapseapi.network.protocol.mcpe.SetHealthPacket;

import java.util.*;

/**
* @author boybook
*/
public class SynapseAPI extends PluginBase {

public static boolean enable = true;
private static SynapseAPI instance;
private boolean autoConnect = true;
private Map<String, SynapseEntry> synapseEntries = new HashMap<>();
private Messenger messenger;

public static SynapseAPI getInstance() {
return instance;
}

public boolean isAutoConnect() {
return autoConnect;
}

@Override
public void onLoad() {
instance = this;
}

@Override
public void onEnable() {
this.getServer().getNetwork().registerPacket(ProtocolInfo.SET_HEALTH_PACKET, SetHealthPacket.class);
this.messenger = new StandardMessenger();
loadEntries();
}

public Map<String, SynapseEntry> getSynapseEntries() {
return synapseEntries;
}

public void addSynapseAPI(SynapseEntry entry) {
this.synapseEntries.put(entry.getHash(), entry);
}

public SynapseEntry getSynapseEntry(String hash) {
return this.synapseEntries.get(hash);
}

public void shutdownAll() {
for (SynapseEntry entry : new ArrayList<>(this.synapseEntries.values())) {
entry.shutdown();
}
}

@Override
public void onDisable() {
this.shutdownAll();
}

public DataPacket getPacket(byte[] buffer) {
byte pid = buffer[0] == (byte) 0xfe ? (byte) 0xff : buffer[0];

byte start = 1;
DataPacket data;
data = this.getServer().getNetwork().getPacket(pid);

if (data == null) {
Server.getInstance().getLogger().notice("C => S 未找到匹配数据包");
return null;
}
data.setBuffer(buffer, start);
return data;
}

private void loadEntries() {
this.saveDefaultConfig();
enable = this.getConfig().getBoolean("enable", true);

if (!enable) {
this.getLogger().warning("The SynapseAPI is not be enabled!");
} else {
if (this.getConfig().getBoolean("disable-rak")) {
for (SourceInterface sourceInterface : this.getServer().getNetwork().getInterfaces()) {
if (sourceInterface instanceof RakNetInterface) {
sourceInterface.shutdown();
}
}
}

List entries = this.getConfig().getList("entries");

for (Object entry : entries) {
@SuppressWarnings("unchecked")
ConfigSection section = new ConfigSection((LinkedHashMap) entry);
String serverIp = section.getString("server-ip", "127.0.0.1");
int port = section.getInt("server-port", 10305);
boolean isMainServer = section.getBoolean("isMainServer");
boolean isLobbyServer = section.getBoolean("isLobbyServer");
boolean transfer = section.getBoolean("transferOnShutdown", true);
String password = section.getString("password");
String serverDescription = section.getString("description");
this.autoConnect = section.getBoolean("autoConnect", true);
if (this.autoConnect) {
this.addSynapseAPI(new SynapseEntry(this, serverIp, port, isMainServer, isLobbyServer, transfer, password, serverDescription));
}
}

}
}

public Messenger getMessenger() {
return messenger;
}
}
package org.itxtech.synapseapi;

import cn.nukkit.Server;
import cn.nukkit.network.RakNetInterface;
import cn.nukkit.network.SourceInterface;
import cn.nukkit.network.protocol.DataPacket;
import cn.nukkit.network.protocol.ProtocolInfo;
import cn.nukkit.plugin.PluginBase;
import cn.nukkit.utils.ConfigSection;
import org.itxtech.synapseapi.command.TransferCommand;
import org.itxtech.synapseapi.messaging.Messenger;
import org.itxtech.synapseapi.messaging.StandardMessenger;
import org.itxtech.synapseapi.network.protocol.mcpe.SetHealthPacket;

import java.util.*;

/**
* @author boybook
*/
public class SynapseAPI extends PluginBase {

public static boolean enable = true;
private static SynapseAPI instance;
private boolean autoConnect = true;
private Map<String, SynapseEntry> synapseEntries = new HashMap<>();
private Messenger messenger;

public static SynapseAPI getInstance() {
return instance;
}

public boolean isAutoConnect() {
return autoConnect;
}

@Override
public void onLoad() {
instance = this;
}

@Override
public void onEnable() {
this.getServer().getNetwork().registerPacket(ProtocolInfo.SET_HEALTH_PACKET, SetHealthPacket.class);
this.getServer().getCommandMap().register("transfer", new TransferCommand("transfer"));
this.messenger = new StandardMessenger();
loadEntries();
}

public Map<String, SynapseEntry> getSynapseEntries() {
return synapseEntries;
}

public void addSynapseAPI(SynapseEntry entry) {
this.synapseEntries.put(entry.getHash(), entry);
}

public SynapseEntry getSynapseEntry(String hash) {
return this.synapseEntries.get(hash);
}

public void shutdownAll() {
for (SynapseEntry entry : new ArrayList<>(this.synapseEntries.values())) {
entry.shutdown();
}
}

@Override
public void onDisable() {
this.shutdownAll();
}

public DataPacket getPacket(byte[] buffer) {
byte pid = buffer[0] == (byte) 0xfe ? (byte) 0xff : buffer[0];

byte start = 1;
DataPacket data;
data = this.getServer().getNetwork().getPacket(pid);

if (data == null) {
Server.getInstance().getLogger().notice("C => S 未找到匹配数据包");
return null;
}
data.setBuffer(buffer, start);
return data;
}

private void loadEntries() {
this.saveDefaultConfig();
enable = this.getConfig().getBoolean("enable", true);

if (!enable) {
this.getLogger().warning("The SynapseAPI is not be enabled!");
} else {
if (this.getConfig().getBoolean("disable-rak")) {
for (SourceInterface sourceInterface : this.getServer().getNetwork().getInterfaces()) {
if (sourceInterface instanceof RakNetInterface) {
sourceInterface.shutdown();
}
}
}

List entries = this.getConfig().getList("entries");

for (Object entry : entries) {
@SuppressWarnings("unchecked")
ConfigSection section = new ConfigSection((LinkedHashMap) entry);
String serverIp = section.getString("server-ip", "127.0.0.1");
int port = section.getInt("server-port", 10305);
boolean isMainServer = section.getBoolean("isMainServer");
boolean isLobbyServer = section.getBoolean("isLobbyServer");
boolean transfer = section.getBoolean("transferOnShutdown", true);
String password = section.getString("password");
String serverDescription = section.getString("description");
this.autoConnect = section.getBoolean("autoConnect", true);
if (this.autoConnect) {
this.addSynapseAPI(new SynapseEntry(this, serverIp, port, isMainServer, isLobbyServer, transfer, password, serverDescription));
}
}

}
}

public Messenger getMessenger() {
return messenger;
}
}
31 changes: 31 additions & 0 deletions src/main/java/org/itxtech/synapseapi/command/TransferCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.itxtech.synapseapi.command;

import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.utils.TextFormat;
import org.itxtech.synapseapi.SynapsePlayer;

public class TransferCommand extends Command {

public TransferCommand(String name) {
super(name);
this.setDescription("Transfer server to another server");
this.setUsage("/transfer <description>");
this.setPermission("synapseapi.command.transfer");
this.setAliases(new String[] {"transfer", "stransfer", "synapsetransfer", "proxy"});
}

@Override
public boolean execute(CommandSender sender, String s, String[] args) {
if (sender instanceof SynapsePlayer){
if (args.length > 0){
((SynapsePlayer) sender).transferByDescription(args[0]);
return true;
}
sender.sendMessage(TextFormat.RED + this.getUsage());
return false;
}
sender.sendMessage(TextFormat.RED + "[SynapseAPI] This command only available for player!");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why only players?

make it for console too. it's just checks :D

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wkwkwk

return false;
}
}