View difference between Paste ID: 0wSKDk2t and jd5a3A5B
SHOW: | | - or go back to the newest paste.
1
/**
2
 * Feel free to change/modify this if you know how,
3
 * but please don't distribute it without giving me credit!
4
 * Do the right thing!
5
 * 
6
 * Creator: dankydrank
7
 * Jan 11, 2014
8
 */
9
10
import javax.swing.*;
11
import java.awt.*;
12
import java.awt.event.*;
13
import java.awt.datatransfer.*;
14
15
public class CommandCompressor implements KeyListener, ActionListener
16
{
17
	private JFrame frame;
18
	private JTextArea input, output;
19
	
20
	public static void main(String[]args)
21
	{
22
		//call CommandCompressor's constructor
23
		new CommandCompressor();
24
	}
25
26
	public CommandCompressor()
27
	{
28
		//instructions
29
		JLabel instructions = new JLabel("Press the Button to Convert Your Text and Copy It. Place each command on a new line.");
30
		JLabel outputLabel = new JLabel("Output:");
31
		//button
32
		JButton copy = new JButton("Convert and Copy to Clipboard");
33
		//frame
34
		frame = new JFrame("Command Compressor");
35
		//Just the sizes I found best
36
		input = new JTextArea(13, 31);
37
		output = new JTextArea(13, 23);
38
		//turn JTextAreas into JScrollPanes for window scrolling
39
		JScrollPane outPane = new JScrollPane(output);
40
		JScrollPane inPane = new JScrollPane(input);
41
		//add Key Listeners
42
		input.addKeyListener(this);
43
		output.addKeyListener(this);
44
		//user can't edit output pane
45
		output.setEditable(false);
46
		//output text wraps within the pane
47
		output.setLineWrap(true);
48
		//add listener to button
49
		copy.addActionListener(this);
50
		//FlowLayout works best
51
		frame.setLayout(new FlowLayout());
52
		//add components
53
		frame.add(instructions);
54
		frame.add(inPane);
55
		frame.add(outputLabel);
56
		frame.add(outPane);
57
		frame.add(copy);
58
		//set frame size and visibility
59
		frame.setSize(675, 325);
60
		frame.setVisible(true);
61
		//set default close operation
62
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
63
	}
64
65
	public void keyPressed(KeyEvent e) {
66
		//I updated the app so you no longer need to press F3, but it still works. F3's key code is 114
67
		int key = e.getKeyCode();
68
		//check for F3
69
		if(key==114)
70
			generateAndCopy();
71
	}
72
73
	public void actionPerformed(ActionEvent e)
74
	{
75
		generateAndCopy();
76
	}
77
	
78
	public void generateAndCopy()
79
	{
80
		//instantiate string
81
		String s = "summon MinecartCommandBlock ~ ~1 ~ {";
82
		//split text from input window into array--each element is one line of input
83
		String[] lines = input.getText().split("\n");
84
		//for each command the player entered, add "Riding:}id:MinecartCommandBlock," to the output string
85
		for(int i = 0; i < lines.length; i++)
86
			s+= "Riding:{id:MinecartCommandBlock,";
87
		//rail
88
		s+= "Riding:{id:FallingSand,TileID:157,Time:1}";
89
		//loop over output string and add commands from lines[]
90
		for(String temp : lines)
91
		{
92
			if(temp != null)
93
				s+= ",Command:" + temp + "}";
94
		}
95
		//add lava to the end
96
		s+= ",Command:setblock ~ ~ ~ lava 7}";
97
		//set text in window to the output string
98
		output.setText(s);
99
		//copy text to clipboard
100
		StringSelection selection = new StringSelection(output.getText());
101
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
102
		clipboard.setContents(selection, selection);
103
	}
104
105
	public void keyReleased(KeyEvent e) 
106
	{
107
		//Do nothing
108
	}
109
110
	public void keyTyped(KeyEvent e) 
111
	{
112
		//Do nothing
113
	}
114
}