Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.taco.plex.msg.GUI;
- import java.awt.BorderLayout;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.util.Enumeration;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
- import java.util.zip.ZipOutputStream;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.SwingUtilities;
- import javax.swing.UIManager;
- import org.objectweb.asm.ClassReader;
- import org.objectweb.asm.ClassWriter;
- import org.objectweb.asm.tree.ClassNode;
- import org.objectweb.asm.tree.InsnList;
- import org.objectweb.asm.tree.LdcInsnNode;
- import org.objectweb.asm.tree.MethodInsnNode;
- import org.objectweb.asm.tree.MethodNode;
- @SuppressWarnings("serial")
- public class onDisableGUI extends JFrame {
- private JTextField field;
- private JTextField messagetoinject;
- private static boolean messagegotinjected;
- public static void main(String[] args) {
- createGUI();
- }
- public static void createGUI() {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- try {
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }
- catch (Exception ex) {}
- onDisableGUI onDisablegui = new onDisableGUI();
- onDisablegui.setTitle("Bukkit Plugin Message onDisablegui");
- onDisablegui.setResizable(false);
- onDisablegui.setSize(440, 135);
- onDisablegui.setLocationRelativeTo(null);
- onDisablegui.setDefaultCloseOperation(3);
- onDisablegui.getContentPane().setLayout(new FlowLayout());
- JLabel label = new JLabel("Select File:");
- JLabel label2 = new JLabel("Inject Msg: ");
- onDisablegui.field = new JTextField();
- onDisablegui.field.setEditable(true);
- onDisablegui.field.setColumns(18);
- onDisablegui.messagetoinject = new JTextField();
- onDisablegui.messagetoinject.setEditable(true);
- onDisablegui.messagetoinject.setColumns(25);
- JButton selectButton = new JButton("Select");
- selectButton.setToolTipText("Select jar file");
- selectButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- if (onDisablegui.field.getText() != null && !onDisablegui.field.getText().isEmpty()) {
- chooser.setSelectedFile(new File(onDisablegui.field.getText()));
- }
- chooser.setMultiSelectionDisabled(false);
- chooser.setFileSelectionMode(0);
- int result = chooser.showOpenDialog(onDisablegui);
- if (result == 0) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- onDisablegui.field.setText(chooser.getSelectedFile().getAbsolutePath());
- }
- });
- }
- }
- });
- JButton startButton = new JButton("Start");
- startButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (onDisablegui.field.getText() == null || onDisablegui.field.getText().isEmpty() || !onDisablegui.field.getText().endsWith(".jar")) {
- JOptionPane.showMessageDialog(null, "You must select a valid jar file!", "Error", 0);
- return;
- }
- if (onDisablegui.messagetoinject.getText() == null || onDisablegui.messagetoinject.getText().isEmpty()) {
- JOptionPane.showMessageDialog(null, "You must enter a message to be injected!", "Error", 0);
- return;
- }
- File output = null;
- try {
- File input = new File(onDisablegui.field.getText());
- if (!input.getName().endsWith(".jar")) {
- throw new IllegalArgumentException("File must be a jar.");
- }
- if (!input.exists()) {
- throw new FileNotFoundException("The file " + input.getName() + " doesn't exist.");
- }
- output = new File(String.format("%s-Output.jar", input.getAbsolutePath().substring(0, input.getAbsolutePath().lastIndexOf("."))));
- if (output.exists()) {
- output.delete();
- }
- process(input, output, onDisablegui.messagetoinject.getText());
- checkFile(output);
- JOptionPane.showMessageDialog(null, "Done: " + output.getAbsolutePath(), "Done", 1);
- }
- catch (Throwable t) {
- JOptionPane.showMessageDialog(null, t, "Error", 0);
- t.printStackTrace();
- if (output != null) {
- output.delete();
- }
- }
- finally {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- onDisablegui.field.setText("");
- onDisablegui.messagetoinject.setText("");
- }
- });
- }
- }
- });
- JPanel panel = new JPanel(new FlowLayout());
- panel.add(label);
- panel.add(onDisablegui.field);
- panel.add(selectButton);
- JPanel panel2 = new JPanel(new FlowLayout());
- panel2.add(startButton);
- JPanel panel3 = new JPanel(new FlowLayout());
- panel3.add(label2);
- panel3.add(onDisablegui.messagetoinject);
- JPanel border = new JPanel(new BorderLayout());
- border.add(panel, "North");
- border.add(panel3, "Center");
- border.add(panel2, "South");
- onDisablegui.getContentPane().add(border);
- onDisablegui.setVisible(true);
- }
- });
- }
- private static void checkFile(File jarFile) throws Throwable {
- if (!jarFile.exists()) {
- throw new IllegalStateException("Output file not found.");
- }
- }
- private static void writeToFile(ZipOutputStream outputStream, InputStream inputStream) throws Throwable {
- byte[] buffer = new byte[4096];
- try {
- while (inputStream.available() > 0) {
- int data = inputStream.read(buffer);
- outputStream.write(buffer, 0, data);
- }
- }
- finally {
- inputStream.close();
- outputStream.closeEntry();
- }
- }
- private static void process(File jarFile, File outputFile, String message) throws Throwable {
- ZipFile zipFile = new ZipFile(jarFile);
- Enumeration<? extends ZipEntry> entries = zipFile.entries();
- ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile));
- try {
- while (entries.hasMoreElements()) {
- ZipEntry entry = (ZipEntry)entries.nextElement();
- if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
- try (InputStream in = zipFile.getInputStream(entry)) {
- ClassReader cr = new ClassReader(in);
- ClassNode classNode = new ClassNode();
- cr.accept(classNode, 0);
- messageInjector(classNode, message);
- ClassWriter cw = new ClassWriter(0);
- classNode.accept(cw);
- ZipEntry newEntry = new ZipEntry(entry.getName());
- newEntry.setTime(System.currentTimeMillis());
- out.putNextEntry(newEntry);
- writeToFile(out, new ByteArrayInputStream(cw.toByteArray()));
- }
- }
- else {
- entry.setTime(System.currentTimeMillis());
- out.putNextEntry(entry);
- writeToFile(out, zipFile.getInputStream(entry));
- }
- }
- }
- finally {
- zipFile.close();
- if (out != null) {
- out.close();
- }
- if (!messagegotinjected) {
- // throw new IllegalStateException("Could not find Bukkit onDisable or onLoad method.");
- throw new IllegalStateException("Could not find Bukkit onDisable method.");
- }
- }
- }
- private static void messageInjector(ClassNode classNode, String message) throws Throwable {
- if (classNode.superName.equals("org/bukkit/plugin/java/JavaPlugin")) {
- for (MethodNode methodNode : classNode.methods) {
- if (methodNode.name.equals("onDisable") /* || methodNode.name.equals("onLoad") */) { // Meh, we don't really need to inject onLoad. Most people use onDisable anyways.
- InsnList instructions = new InsnList();
- instructions.add(new MethodInsnNode(184, "org/bukkit/Bukkit", "getConsoleSender", "()Lorg/bukkit/command/ConsoleCommandSender;", false));
- instructions.add(new LdcInsnNode(message));
- instructions.add(new MethodInsnNode(185, "org/bukkit/command/ConsoleCommandSender", "sendMessage", "(Ljava/lang/String;)V", true));
- methodNode.instructions.insertBefore(methodNode.instructions.getFirst(), instructions);
- messagegotinjected = true;
- return;
- }
- }
- } // TODO: Bungee support
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement