Skip to content

Commit

Permalink
2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Dituon committed Jun 1, 2022
1 parent acda467 commit 9b283ed
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 48 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'xmmt.dituon'
version = '2.1'
version = '2.2'

repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
Expand Down
6 changes: 4 additions & 2 deletions config/xmmt.dituon.petpet/PetPetAutoSaveConfig.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
content:
version: 2.0
version: 2.2
command: pet
probability: 30
antialias: false
disabled: []
disabled: []
keyCommand: false
respondImage: false
62 changes: 46 additions & 16 deletions src/main/java/xmmt/dituon/plugin/Petpet.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import net.mamoe.mirai.event.events.BotOnlineEvent;
import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.event.events.NudgeEvent;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.Message;
import net.mamoe.mirai.message.data.PlainText;
import net.mamoe.mirai.message.data.QuoteReply;
import net.mamoe.mirai.message.data.*;

import java.util.ArrayList;

Expand All @@ -25,7 +22,7 @@ public final class Petpet extends JavaPlugin {
PluginPetService pluginPetService;

private Petpet() {
super(new JvmPluginDescriptionBuilder("xmmt.dituon.petpet", "2.1")
super(new JvmPluginDescriptionBuilder("xmmt.dituon.petpet", "2.2")
.name("PetPet")
.author("Dituon")
.build());
Expand All @@ -38,6 +35,7 @@ public void onEnable() {

pluginPetService.readConfigByPluginAutoSave();
pluginPetService.readData(getDataFolder());

GlobalEventChannel.INSTANCE.subscribeOnce(BotOnlineEvent.class, e -> {
if (bot == null) {
bot = e.getBot();
Expand Down Expand Up @@ -71,24 +69,56 @@ private void onGroupMessage(GroupMessageEvent e) {
return;
}

if (!isDisabled(e.getGroup()) && e.getMessage().contains(At.Key)
&& e.getMessage().contentToString().startsWith(pluginPetService.command)) {
At at = null;
Member to = e.getSender();
if (pluginPetService.keyCommand && e.getMessage().contains(At.Key)) {
String key = null;
for (Message m : e.getMessage()) {
if (m instanceof At) { // 遍历消息取出At的对象
at = (At) m;
to = e.getGroup().get(at.getTarget());
if (m instanceof PlainText &&
pluginPetService.dataMap.containsKey(m.contentToString().replace(" ", ""))) {
key = m.contentToString().replace(" ", "");
continue;
}
if (m instanceof PlainText && at != null && !m.contentToString().endsWith(" ")) {
pluginPetService.sendImage(e.getGroup(), e.getSender(), to, m.contentToString().replace(" ", ""));
if (m instanceof At && key != null) {
At at = (At) m;
pluginPetService.sendImage(e.getGroup(), e.getSender(), e.getGroup().get(at.getTarget()), key);
return;
}
}
pluginPetService.sendImage(e.getGroup(), e.getSender(), to);
}
}

if (!isDisabled(e.getGroup()) && e.getMessage().contentToString().startsWith(pluginPetService.command)) {
if (pluginPetService.respondImage && e.getMessage().contains(Image.Key)) {
getLogger().info("img");
String toURL = null;
for (Message m : e.getMessage()) {
if (m instanceof Image) {
toURL = Image.queryUrl((Image) m);
getLogger().info(toURL);
continue;
}
if (m instanceof PlainText && toURL != null && !m.contentToString().endsWith(" ")) {
pluginPetService.sendImage(e.getGroup(), e.getSender(), e.getSender().getAvatarUrl(), toURL, m.contentToString().replace(" ", ""));
return;
}
}
pluginPetService.sendImage(e.getGroup(), e.getSender(), e.getSender().getAvatarUrl(), toURL);
return;
}
At at = null;
Member to = e.getSender();
for (Message m : e.getMessage()) {
if (m instanceof At) { // 遍历消息取出At的对象
at = (At) m;
to = e.getGroup().get(at.getTarget());
continue;
}
if (m instanceof PlainText && at != null && !m.contentToString().endsWith(" ")) {
pluginPetService.sendImage(e.getGroup(), e.getSender(), to, m.contentToString().replace(" ", ""));
return;
}
}
pluginPetService.sendImage(e.getGroup(), e.getSender(), to);
}
}

private boolean isDisabled(Group group) {
if (disabledGroup != null && !disabledGroup.isEmpty()) {
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/xmmt/dituon/plugin/PluginPetService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package xmmt.dituon.plugin;

import kotlinx.serialization.json.JsonArray;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.contact.Member;
import net.mamoe.mirai.message.data.Image;
Expand All @@ -15,7 +14,7 @@ public class PluginPetService extends BasePetService {

public void readConfigByPluginAutoSave() {
ConfigDTO config = PetPetAutoSaveConfig.INSTANCE.content.get();
System.out.println("从AutoSaveConfig中读出:" + ConfigDTOKt.encode(config));
// System.out.println("从AutoSaveConfig中读出:" + ConfigDTOKt.encode(config));
readConfig(config);
}

Expand All @@ -24,6 +23,10 @@ public void sendImage(Group group, Member from, Member to) {
sendImage(group, from, to, keyList.get(new Random().nextInt(keyList.size())));
}

public void sendImage(Group group, Member m, String fromURL, String toURL) {
sendImage(group, m, fromURL, toURL, keyList.get(new Random().nextInt(keyList.size())));
}

public void sendImage(Group group, Member from, Member to, boolean random) {
if (!random) {
sendImage(group, from, to);
Expand All @@ -37,15 +40,20 @@ public void sendImage(Group group, Member from, Member to, boolean random) {
}

public void sendImage(Group group, Member from, Member to, String key) {
BufferedImage fromAvatarImage = ImageSynthesis.getAvatarImage(from.getAvatarUrl());
BufferedImage toAvatarImage = ImageSynthesis.getAvatarImage(to.getAvatarUrl());
sendImage(group, from, from.getAvatarUrl(), to.getAvatarUrl(), key);
}

public void sendImage(Group group, Member m, String fromURL, String toURL, String key) {
BufferedImage fromAvatarImage = ImageSynthesis.getAvatarImage(fromURL);
BufferedImage toAvatarImage = ImageSynthesis.getAvatarImage(toURL);

InputStream generatedImage = generateImage(fromAvatarImage, toAvatarImage, key);

try {
if (generatedImage != null) {
ExternalResource resource = ExternalResource.create(generatedImage);
Image image = to.uploadImage(resource);
Image image = m.uploadImage(resource);
resource.close();
group.sendMessage(image);
} else {
System.out.println("生成图片失败");
Expand All @@ -55,13 +63,4 @@ public void sendImage(Group group, Member from, Member to, String key) {
ex.printStackTrace();
}
}

private int[] JsonArrayToIntArray(JsonArray ja) {
return new int[]{
Integer.parseInt(ja.get(0).toString()),
Integer.parseInt(ja.get(1).toString()),
Integer.parseInt(ja.get(2).toString()),
Integer.parseInt(ja.get(3).toString())
};
}
}
21 changes: 10 additions & 11 deletions src/main/java/xmmt/dituon/share/BasePetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
Expand All @@ -16,6 +15,8 @@ public class BasePetService {
public boolean antialias = false;
public String command = "pet";
public int randomMax = 40;
public boolean keyCommand = false;
public boolean respondImage = false;
public File dataRoot;
public ArrayList<String> disabledKey = new ArrayList<>();
public ArrayList<String> keyList = new ArrayList<>();
Expand All @@ -28,7 +29,7 @@ public BasePetService() {
this.imageMaker = new BaseImageMaker();
this.gifMaker = new BaseGifMaker();
}

public void readData(File dir) {
this.dataRoot = dir;
String[] children = dir.list();
Expand All @@ -43,7 +44,9 @@ public void readData(File dir) {
File dataFile = new File(dir.getAbsolutePath() + File.separator + path + "/data.json");
try {
DataJSON data = ConfigDTOKt.getData(getFileStr(dataFile));
if (!disabledKey.contains(path)) {
if (!disabledKey.contains(path)
&& !disabledKey.contains("Type." + data.getType())
&& !disabledKey.contains("Avatar." + data.getAvatar())) {
keyList.add(path);
}
dataMap.put(path, data);
Expand All @@ -57,14 +60,15 @@ public void readData(File dir) {
}



public void readConfig(ConfigDTO config) {
command = config.getCommand();
antialias = config.getAntialias();
randomMax = config.getProbability();
keyCommand = config.getKeyCommand();
respondImage = config.getRespondImage();

for (String path : config.getDisabled()) {
disabledKey.add(path.toString().replace("\"", ""));
disabledKey.add(path.replace("\"", ""));
}

System.out.println("Petpet 初始化成功,使用 " + command + " 以生成GIF。");
Expand All @@ -81,11 +85,6 @@ public String getFileStr(File file) throws IOException {
return sb.toString();
}






public InputStream generateImage(BufferedImage fromAvatarImage, BufferedImage toAvatarImage) {
return generateImage(fromAvatarImage, toAvatarImage, keyList.get(new Random().nextInt(keyList.size())));
}
Expand Down Expand Up @@ -145,7 +144,7 @@ public InputStream generateImage(BufferedImage fromAvatarImage, BufferedImage to
}
}

if (data.getType() == Type.IMG){
if (data.getType() == Type.IMG) {
if (data.getAvatar() == Avatar.SINGLE) {
int[] pos = JsonArrayToIntArray(data.getPos());

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/xmmt/dituon/share/ConfigDTO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@ import kotlinx.serialization.json.JsonArray

@Serializable
data class ConfigDTO(
val version: Float = 2.0F,
val version: Float = 2.2F,
val command: String = "pet",
val probability: Int = 30,
val antialias: Boolean = false,
val disabled: List<String> = emptyList()
val disabled: List<String> = emptyList(),
val keyCommand: Boolean = false,
val respondImage: Boolean = false
)

fun decode(str: String): ConfigDTO {
return Json.decodeFromString(str)
}

fun encode(config: ConfigDTO): String {
return Json{encodeDefaults = true}.encodeToString(config)
return Json { encodeDefaults = true }.encodeToString(config)
}

enum class Type {
GIF, IMG
}

enum class Avatar {
SINGLE , DOUBLE
SINGLE, DOUBLE
}

@Serializable
Expand Down

0 comments on commit 9b283ed

Please sign in to comment.