Advertisement
Guest User

Untitled

a guest
Apr 25th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. import net.minecraft.client.Minecraft;
  2. import net.minecraft.client.main.Main;
  3.  
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.net.HttpURLConnection;
  11. import java.net.URL;
  12. import java.security.MessageDigest;
  13.  
  14. public class InfinityOriginLogin extends JFrame {
  15.  
  16. public static boolean successful = false;
  17. private final JTextField usernameField;
  18. private final JPasswordField passwordField;
  19. private final JButton loginButton;
  20. private final String verifyAddress = "https://127.0.0.1:8000";
  21.  
  22. public InfinityOriginLogin() {
  23. setTitle("InfinityOrigin Login");
  24. setSize(400, 300);
  25. setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  26. setLocationRelativeTo(null);
  27.  
  28. JPanel panel = new JPanel();
  29. panel.setLayout(new GridBagLayout());
  30. panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  31.  
  32. Font customFont = loadCustomFont(24f);
  33. if (customFont == null) {
  34. customFont = new Font("Arial", Font.BOLD, 24);
  35. }
  36.  
  37. JLabel titleLabel = new JLabel("InfinityOrigin");
  38. titleLabel.setFont(customFont);
  39. titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
  40.  
  41. JLabel usernameLabel = new JLabel("Username:");
  42. usernameField = new JTextField(15);
  43.  
  44. JLabel passwordLabel = new JLabel("Password:");
  45. passwordField = new JPasswordField(15);
  46.  
  47. loginButton = new JButton("Login");
  48. loginButton.setBackground(new Color(0, 153, 255));
  49. loginButton.setForeground(Color.WHITE);
  50. loginButton.setFocusPainted(false);
  51. loginButton.setFont(new Font("Arial", Font.BOLD, 14));
  52. loginButton.setPreferredSize(new Dimension(100, 30));
  53.  
  54. loginButton.addActionListener(e -> {
  55. usernameField.setVisible(false);
  56. passwordField.setVisible(false);
  57. loginButton.setVisible(false);
  58. usernameLabel.setVisible(false);
  59. passwordLabel.setVisible(false);
  60. showLoadingAnimation();
  61. });
  62.  
  63. GridBagConstraints gbc = new GridBagConstraints();
  64. gbc.insets = new Insets(10, 10, 10, 10);
  65. gbc.gridx = 0;
  66. gbc.gridy = 0;
  67. gbc.gridwidth = 2;
  68. gbc.anchor = GridBagConstraints.CENTER;
  69. panel.add(titleLabel, gbc);
  70.  
  71. gbc.gridwidth = 1;
  72. gbc.gridy = 1;
  73. panel.add(usernameLabel, gbc);
  74.  
  75. gbc.gridx = 1;
  76. panel.add(usernameField, gbc);
  77.  
  78. gbc.gridx = 0;
  79. gbc.gridy = 2;
  80. panel.add(passwordLabel, gbc);
  81.  
  82. gbc.gridx = 1;
  83. panel.add(passwordField, gbc);
  84.  
  85. gbc.gridx = 0;
  86. gbc.gridy = 3;
  87. gbc.gridwidth = 2;
  88. panel.add(loginButton, gbc);
  89.  
  90. add(panel);
  91. setVisible(true);
  92. }
  93.  
  94. @SuppressWarnings("InfiniteLoopStatement")
  95. private void showLoadingAnimation() {
  96. InputStream gifStream = getClass().getResourceAsStream("/loading.gif");
  97. if (gifStream == null) {
  98. JOptionPane.showMessageDialog(this, "GIF 文件未找到", "错误", JOptionPane.ERROR_MESSAGE);
  99. return;
  100. }
  101.  
  102. Image loadingImage = new ImageIcon("loading.gif").getImage();
  103. ImageIcon loadingGif = new ImageIcon(loadingImage.getScaledInstance(100, 100, Image.SCALE_DEFAULT));
  104. JLabel loadingGifLabel = new JLabel(loadingGif);
  105. loadingGifLabel.setHorizontalAlignment(SwingConstants.CENTER);
  106.  
  107. Font customFont = loadCustomFont(18f);
  108. if (customFont == null) {
  109. customFont = new Font("Arial", Font.BOLD, 18);
  110. }
  111. JLabel loadingLabel = new JLabel("加载中,请稍等...");
  112. loadingLabel.setFont(customFont);
  113. loadingLabel.setHorizontalAlignment(SwingConstants.CENTER);
  114.  
  115. JPanel loadingPanel = new JPanel(new BorderLayout());
  116. loadingPanel.add(loadingGifLabel, BorderLayout.CENTER);
  117. loadingPanel.add(loadingLabel, BorderLayout.SOUTH);
  118.  
  119. getContentPane().removeAll();
  120. getContentPane().add(loadingPanel);
  121. revalidate();
  122. repaint();
  123.  
  124. new SwingWorker<Void, Void>() {
  125. @Override
  126. protected Void doInBackground() throws Exception {
  127. Thread.sleep(300);
  128. return null;
  129. }
  130.  
  131. @Override
  132. protected void done() {
  133. String username = usernameField.getText();
  134. String password = new String(passwordField.getPassword());
  135.  
  136. if (!isStatus200()){
  137. JOptionPane.showMessageDialog(InfinityOriginLogin.this, "服务器异常,请联系腐竹", "错误", JOptionPane.ERROR_MESSAGE);
  138. }
  139.  
  140. if (isSuccessfulLogin(username, password)) {
  141. JOptionPane.showMessageDialog(InfinityOriginLogin.this, "登录成功!");
  142. getContentPane().removeAll();
  143. getContentPane().add(new JPanel());
  144. revalidate();
  145. repaint();
  146. setVisible(false);
  147. dispose();
  148. successful = true;
  149. } else {
  150. JOptionPane.showMessageDialog(InfinityOriginLogin.this, "用户名或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
  151. System.err.println("InfinityOrigin: Error login.");
  152. getContentPane().removeAll();
  153. getContentPane().add(new JPanel());
  154. revalidate();
  155. repaint();
  156. setVisible(false);
  157. dispose();
  158. }
  159.  
  160.  
  161. }
  162. }.execute();
  163. }
  164.  
  165. private Font loadCustomFont(float size) {
  166. try (InputStream fontStream = getClass().getResourceAsStream("/" + "MCAE.ttf")) {
  167. if (fontStream == null) {
  168. System.err.println("字体文件未找到: " + "MCAE.ttf");
  169. return null;
  170. }
  171. Font font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
  172. return font.deriveFont(size);
  173. } catch (Exception e) {
  174. System.err.println("加载字体失败: " + e.getMessage());
  175. return null;
  176. }
  177. }
  178.  
  179. public static void start() {
  180. SwingUtilities.invokeLater(InfinityOriginLogin::new);
  181. }
  182.  
  183. public boolean isSuccessfulLogin(String username, String password) {
  184. boolean value = false;
  185. try {
  186. String urlString = verifyAddress+"/login?username=" + username + "&password=" + password;
  187. URL url = new URL(urlString);
  188. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  189. connection.setRequestMethod("GET");
  190.  
  191. BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  192. String inputLine;
  193. StringBuilder response = new StringBuilder();
  194.  
  195. while ((inputLine = in.readLine()) != null) {
  196. response.append(inputLine);
  197. }
  198. in.close();
  199.  
  200. if (response.toString().equals("true")) {
  201. value = true;
  202. }
  203.  
  204. } catch (Exception e) {
  205. e.printStackTrace();
  206. }
  207. return value;
  208. }
  209.  
  210. public boolean isStatus200() {
  211. try {
  212. URL url = new URL(verifyAddress+"/status");
  213. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  214. connection.setRequestMethod("GET");
  215. connection.setConnectTimeout(5000);
  216. connection.setReadTimeout(5000);
  217. int responseCode = connection.getResponseCode();
  218. return responseCode >= 200 && responseCode < 300;
  219. } catch (IOException e) {
  220. return false;
  221. }
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement