大家好,又见面了,我是你们的朋友全栈君。
诛仙管理员连接Gdeliveryd服务器发送邮件的Java实现
连接Gdeliveryd服务器,可以通过Socket建立邮件信息,向角色发送带有物品装备的邮件,Socket是一个和语言无关的协议,大多数语言比如C/C++/PHP/VB等都支持Socket,这里使用Java实现,适用于诛仙2和诛仙3
诛仙给角色发送物品装备邮件的代码,通过Socket连接Gdeliveryd服务器,建立邮件信息:
“`
package com.zhenzhigu.gm.zx;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class MailUtils {
/**
* 把0到255的整数转为十六进制
* 如果是1位则补到2位,如e补位之后是0e
*/
private static String intToHex2(int n){
String hex = Integer.toHexString(n);
if(hex.length()==1){
hex = “0”+hex;
}
return hex;
}
/**
* 把0-65535的整数转为十六进制,补位到4位,例如e补位之后是0e:00
*/
private static String intToHex4(int n){
String hex = Integer.toHexString(n);
if(hex.length()==1){
hex = “0”+hex+”:00″;
}else if(hex.length()==2){
hex += “:00”;
}else if(hex.length()==3){
String a = hex.substring(0, 1);
String b = hex.substring(1,3);
hex = “0”+a+”:”+b;
}else if(hex.length()==4){
String a = hex.substring(0, 2);
String b = hex.substring(2,4);
hex = a+”:”+b;
}else{
System.out.println(“err:”+hex+”:”+n);
}
return hex;
}
/**
* 标题与内容转为十六进制
* @param str
* @return
*/
private static String stringToHex(String str){
StringBuffer sb = new StringBuffer();
char[] crr = str.toCharArray();
for(int i=0;i
int n = crr[i];
String hex = Integer.toHexString(n);
if(hex.length()==2){
hex += “:00”;
}else{
String a = hex.substring(0, 2);
String b = hex.substring(2,4);
hex = b+”:”+a;
}
sb.append(hex);
if(i
sb.append(“:”);
}
}
return sb.toString();
}
private static String getHex(int receiver,int item,int number,String title,String text,int protect){
String pst = “00:00”;
switch (protect) {
case 0:pst=”00:00″;break;//[无保护类型=00:00]
case 1:pst=”00:40″;break;//[装备后绑定=00:40]
case 2:pst=”00:93″;break;//[已绑定状态=00:93]
case 3:pst=”02:53″;break;//[下线后消失=02:53]
default:break;
}
String str =
“90:76:xx:80:00:00:06:00:00:04:00:03:00:00:”
+ intToHex4(receiver) //收件人
+ “:” + intToHex2(title.toCharArray().length*2) //标题长度
+ “:” + stringToHex(title) //标题正文
+ “:” + intToHex2(text.toCharArray().length*2) //内容长度
+ “:” + stringToHex(text) //内容正文
+ “:00:00:”
+ intToHex4(item) //物品ID
+ “:00:00:00:00:00:00:”
+ intToHex4(number)
+ “:00:00:”
+ intToHex4(number)
+ “:00:00:00:”
+ pst
+ “:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00”;
str=str.replaceAll(“xx”, intToHex2(((str.length()+1)/3)-3));
return str;
}
/**
* 发送邮件
* @param receiver 收件人ID
* @param item 物品ID
* @param number 物品数量
* @param title 邮件标题
* @param text 邮件内容(不要超过100字)
* @param protect 保护类型:[0=无][1=装备后绑定][2=已绑定][3=下线后消失]
* @throws UnknownHostException
* @throws IOException
*/
public static void send(int receiver,int item,int number,String title,String text,int protect) throws UnknownHostException, IOException{
String hex = getHex(receiver, item, number, title, text,protect);
System.out.println(hex);
//连接Gdeliveryd服务器,建立邮件信息
Socket sc = new Socket(“192.168.200.100”, 29100);
OutputStream out = sc.getOutputStream();
byte[] bf = new byte[1024];
String[] srr = hex.split(“:”);
for(int i=0;i
bf[i] = (byte) Integer.parseInt(srr[i], 16);
}
out.write(bf, 0, srr.length);
out.close();
}
public static void main(String[] args) throws UnknownHostException, IOException {
/**
* 强化真仙散仙用补天符:4006
* 强化白装用炼器符:685
* 强化神魔装用龙魂符:21200
* 强化封神装用天机符: 56030
*
* 仙豆:18794
* 龙牙草:20481(修炼宠物斗气=2仙豆)
* 天龙草:20484(修炼宠物斗气=50龙牙草)
* 九转阴阳幡:19183 (更改宠物前世)
* 易筋断骨丹:16320 (更改宠物限界)
*/
int receiver = 1072; //角色ID
int item = 18794; //物品ID
int number = 99; //物品数量
String title = “鸿蒙快递,请查收”;
String text = “这是您在鸿蒙宝库购买的商品,请查收!”;
MailUtils.send(receiver,item,number,title,text,0);
//MailUtils.send(1024, 8079, 9999, “恭喜您获得奖品”, “这是GM发送给您的奖品,请注意查收附件!”, 3);
}
}
“`
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/161952.html原文链接:https://javaforall.cn