Skip to content

Commit

Permalink
Custom skin support
Browse files Browse the repository at this point in the history
  • Loading branch information
ddf8196 committed Jan 8, 2022
1 parent 0b13f56 commit 5cf3474
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.ddf.fakeplayer'
version '0.5.0-alpha'
version '0.5.1-alpha'

repositories {
maven {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ddf/fakeplayer/main/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class Config {
private boolean webSocketEnabled = false;
private int webSocketPort = 54321;
private String language = "zh-cn";
private String theme = "darcula";
private String theme = "dark";
private List<PlayerData> players = new ArrayList<>();
private Map<String, CustomSkinData> customSkins = new LinkedHashMap<>();
private transient KeyPair serverKeyPair;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ddf/fakeplayer/main/gui/GUIMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void initMenuBar() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter(I18N.get("menu.file.configFileDescription"), "yaml"));
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter(I18N.get("fileFilter.description.configFile"), "yaml"));
int result = fileChooser.showSaveDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
String path = fileChooser.getSelectedFile().getAbsolutePath();
Expand All @@ -140,7 +140,7 @@ private void initMenuBar() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter(I18N.get("menu.file.configFileDescription"), "yaml"));
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter(I18N.get("fileFilter.description.configFile"), "yaml"));
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

import com.ddf.fakeplayer.client.Client;
import com.ddf.fakeplayer.main.I18N;
import com.ddf.fakeplayer.main.config.CustomSkinData;
import com.ddf.fakeplayer.main.config.PlayerData;
import com.ddf.fakeplayer.main.gui.GUIMain;
import com.ddf.fakeplayer.util.Image;
import com.ddf.fakeplayer.util.SkinUtil;
import net.miginfocom.swing.MigLayout;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Objects;

public class PlayerInfoDialog extends JDialog {
public static final int TYPE_ADD = 0;
Expand All @@ -26,6 +35,8 @@ public class PlayerInfoDialog extends JDialog {
private JButton ok;
private JButton cancel;

private SkinComboBoxModel skinComboBoxModel;

public PlayerInfoDialog(GUIMain main) {
this(main, TYPE_ADD, null);
}
Expand All @@ -45,6 +56,7 @@ public PlayerInfoDialog(GUIMain main, int type, String playerName) {
this.playerSkin = null;
}
initLayout();
initListener();
initData();
}

Expand All @@ -54,10 +66,7 @@ private void initLayout() {
JLabel nameLabel = new JLabel(I18N.get("label.playerName"));
name = new JTextField();
JLabel skinLabel = new JLabel(I18N.get("label.playerSkin"));
skin = new JComboBox<>(new String[]{
"steve",
"alex"
});
skin = new JComboBox<>();
allowChatMessageControl = new JCheckBox(I18N.get("checkBox.allowChatMessageControl"));
JPanel buttonBar = new JPanel();
ok = new JButton(I18N.get("button.ok"));
Expand All @@ -84,16 +93,54 @@ private void initLayout() {
setLocationRelativeTo(getOwner());
}

private void initListener() {
skin.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
JFileChooser fileChooser = new JFileChooser();

fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter(I18N.get("fileFilter.description.skinFile"), "png"));
int result = fileChooser.showOpenDialog(PlayerInfoDialog.this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
Image image = new Image();
image.load(file);
// String name = JOptionPane.showInputDialog(PlayerInfoDialog.this, I18N.get(""));
String name = file.getName();
CustomSkinData skinData = new CustomSkinData();
skinData.setImageWidth(image.getWidth());
skinData.setImageHeight(image.getHeight());
skinData.setImageData(SkinUtil.encodeSkinToBase64(image));
main.getConfig().addCustomSkin(name, skinData);
skinComboBoxModel.add(name);
skin.setSelectedItem(name);
}
}
}
});
}

private void initData() {
skinComboBoxModel = new SkinComboBoxModel();
skin.setModel(skinComboBoxModel);
pack();
switch (type) {
case TYPE_ADD:
setTitle(I18N.get("title.addFakePlayer"));
skin.setSelectedItem("steve");
break;
case TYPE_EDIT:
setTitle(I18N.get("title.editFakePlayer"));
PlayerData data = main.getConfig().getPlayerData(playerName);
name.setText(data.getName());
skin.setSelectedItem(data.getSkin());
String skinType = data.getSkin();
if ("steve".equals(skinType) || "alex".equals(skinType))
skin.setSelectedItem(skinType);
else
skin.setSelectedItem(data.getCustomSkin());
allowChatMessageControl.setSelected(data.isAllowChatMessageControl());
break;
}
Expand All @@ -104,7 +151,7 @@ private void initData() {

private void addOrEdit() {
String playerName = name.getText();
String skin = this.skin.getSelectedItem().toString();
String skin = Objects.toString(this.skin.getSelectedItem());
boolean allowChatMessageControl = this.allowChatMessageControl.isSelected();
switch (type) {
case TYPE_ADD: {
Expand All @@ -117,6 +164,10 @@ private void addOrEdit() {
return;
}
PlayerData playerData = new PlayerData(playerName, skin, allowChatMessageControl);
if (!"steve".equals(skin) && !"alex".equals(skin)) {
playerData.setSkin("custom");
playerData.setCustomSkin(skin);
}
main.addPlayer(playerData);
dispose();
break;
Expand All @@ -135,6 +186,10 @@ private void addOrEdit() {
client.setAllowChatMessageControl(allowChatMessageControl);
} else {
PlayerData playerData = new PlayerData(playerName, skin, allowChatMessageControl);
if (!"steve".equals(skin) && !"alex".equals(skin)) {
playerData.setSkin("custom");
playerData.setCustomSkin(skin);
}
main.removePlayer(this.playerName);
main.addPlayer(playerData);
}
Expand All @@ -143,4 +198,40 @@ private void addOrEdit() {
}
dispose();
}

private class SkinComboBoxModel extends AbstractListModel<String> implements ComboBoxModel<String> {
private String selectedItem;
private ArrayList<String> strings = new ArrayList<>();

SkinComboBoxModel() {
strings.add("steve");
strings.add("alex");
strings.addAll(main.getConfig().getCustomSkins().keySet());
}

public void add(String name) {
strings.add(name);
fireIntervalAdded(this, strings.size() - 2, strings.size() - 1);
}

@Override
public void setSelectedItem(Object anItem) {
selectedItem = Objects.toString(anItem);
}

@Override
public String getSelectedItem() {
return selectedItem;
}

@Override
public int getSize() {
return strings.size();
}

@Override
public String getElementAt(int index) {
return strings.get(index);
}
}
}
10 changes: 6 additions & 4 deletions src/main/resources/i18n/Messages_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ log.saveConfigSucceed=配置文件保存成功
log.saveConfigFail=配置文件保存失败

title.about=About
title.addFakePlayer=添加假人
title.editFakePlayer=编辑假人
title.serverPublicKey=服务器公钥
title.addFakePlayer=Add
title.editFakePlayer=Edit
title.serverPublicKey=Public Key
title.settings=Settings

menu.file=File
menu.file.exportConfig=Export Config
menu.file.configFileDescription=Config File(*.yaml)
menu.file.importConfig=Import Config
menu.file.saveConfig=Save Config
menu.file.exit=Exit
Expand All @@ -33,6 +32,9 @@ popupMenu.playersTable.connectSelected=连接选中
popupMenu.playersTable.disconnectSelected=断开选中
popupMenu.playersTable.removeSelected=移除选中

fileFilter.description.configFile=Config File(*.yaml)
fileFilter.description.skinFile=Skin File(*.png)

label.serverAddress=Server Address
label.serverPort=Server Port
label.publicKey=Public Key
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/i18n/Messages_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ title.settings=设置

menu.file=文件
menu.file.exportConfig=导出配置
menu.file.configFileDescription=配置文件(*.yaml)
menu.file.importConfig=导入配置
menu.file.saveConfig=保存配置
menu.file.exit=退出
Expand All @@ -33,6 +32,9 @@ popupMenu.playersTable.connectSelected=连接选中
popupMenu.playersTable.disconnectSelected=断开选中
popupMenu.playersTable.removeSelected=移除选中

fileFilter.description.configFile=配置文件(*.yaml)
fileFilter.description.skinFile=皮肤(*.png)

label.serverAddress=服务器地址
label.serverPort=服务器端口
label.publicKey=服务器公钥
Expand Down

0 comments on commit 5cf3474

Please sign in to comment.