If your client has donator options, needs a file to be downloaded or anything to get from internet, then this might come in useful.
The outcome of this is creating your own file type (ie it has a custom extension).To start, I created a new project. This new project would contain all of the class files for the file creator.
public static void main(String args[]) {/** Creating Main JFrame */JFrame frame = new JFrame("[SFS]"); //Initialize JFrameframe.setVisible(true); //Make the JFrame visibleframe.setTitle("Secure File System"); //Set the title for JFrameframe.setSize(400, 200); //Set the size of the frameframe.setLayout(new FlowLayout()); //Set a flowing layoutframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create a default exit of the programJButton Make = new JButton("Make"); //Create a JButton called Make//JButton Unzip = new JButton("Unzip"); //Creates a JButton to unzip/open the fileMake.setSize(new Dimension(40, 40)); //Sets the size for the buttons//Unzip.setSize(new Dimension(40, 40)); //^^frame.add(Make); //Adds the button to the JFrame//frame.add(Unzip); //^^//Unzip.addActionListener(new ActionButtonListener()); //Adds a listenerMake.addActionListener(new ActionButtonListener()); //^^}
Go ahead and open up a new project and create... 2 packages. Name them to whatever you want.
Mine is Gui and Files.
The gui will contain the gui.. (duh).. and the files will have the files and stuff..
To start, lets create a new class in the Gui package. Lets call this class A_ScreenGui.The functions should go underneath the JFrame main void unless you want it to add it in another class to keep organized.
/** Functions */public static void Serialize() {FileOne f1 = new FileOne(); //Creates an object classf1.username = "Wampwire"; //Sets a String username to be "Wampwire"f1.donator = "true"; //String donator = "true"f1.version = 1; //versionf1.something = 1; //Something equals to 1 :)try { //Use a try catch method FileOutputStream fileOut = new FileOutputStream("FileOne.sfsx"); //Creates a File called FileOne with the extension of sfsxObjectOutputStream out = new ObjectOutputStream(fileOut); //Creates the FileOut into an Object Output Stream (OOS)out.writeObject(f1); //Writes the object f1 (FileOne class) via OOS out.close(); //Closes Object Output Stream (because were memory efficient fuckers :P) fileOut.close(); //Closes the FileOutputStream } catch(IOException e) { //Catches any input output exceptions and prints them out in the consolee.printStackTrace();}}Lets create the basis of FileOne class. Go ahead and create a new class in the File package.
public class FileOne implements java****.Serializable{ //Makes class serializablepublic String username; //Creates a String called usernamepublic String donator; //String called donatorpublic int version; //Int Versionpublic transient int something; //Int Number (transient) doesnt serialize (would return 0)}We need this so that we can listen (see) any changes done to the buttons. Either is pressed or not.
public class ActionButtonListener implements ActionListener { //Makes the class behaviour the same as ActionListener public ActionButtonListener(){} //Leave empty but still initialize it public void actionPerformed(ActionEvent e) { //Get the action and name it as e if(e.getActionCommand().equals("Make")) { //If button "Make" was pressed, call void A_ScreenGui.Serialize(); //Calling the void we created } //if(e.getActionCommand().equals("Unzip")) { //You havent added this in to this project so dont worry //A_ScreenGui.Deserialize(); //} }}Ok. Youre almost done. All you have to do is create a class file, or use my method where the FileOne is used as a template and serialize it.
Upload it to the internet and use IOStreams/BufferedReaders or Sockets to download the file.Now, you might think... Whats the point of this if we cant read it, or java cant read it... Well, we gotta create a deserializer (Sounds fancy, doesnt it? :P) Go to your minecraft client, create a new class and create this void.
public static void Deserialize() { //Creates a voidFileOne f1 = null; //Sets the object FileOne to f1 which equals to nulltry { //Try catch statementFileInputStream fileIn = new FileInputStream("FileOne.sfsx"); //Grabs the file (put in the url to where your file is downloaded)ObjectInputStream in = new ObjectInputStream(fileIn); //Creates fileIn into Object and grabs itf1 = (FileOne)in.readObject(); //Reads Object FileOnein.close(); //Close ObjectInputStream (efficiency fuckers) fileIn.close(); //Close FileInputStream (Even more efficiency)} catch(IOException e) { //Catch input output exceptionse.printStackTrace(); //Prints the exception in the console return; //Return method .-.} catch(ClassNotFoundException c) { //Catch FileNotFound exceptionSystem.out.println("FileOne class not found"); //Prints out to console c.printStackTrace(); //Prints exception to console return; //Return Method}System.out.println("Deserialized FileOne..."); //Print to console System.out.println("Name: " + f1.name); //Print to console System.out.println("Donator: " + f1.donator); //Screw this shitSystem.out.println("Version: " + f1.Version); //...............System.out.println("Something: " + f1.something); //Returns zero as its not serializable variable}Now call this void from a place which suits you in your MC Client.
You can also add this void in your main program which serializes the file in the first place to test it (Thats why I commented a lot of things out at first).Advantage to using this:
Sort of obfuscates the file, allowing safer donator statuses which arent hard coded into the client itself.
Allows all sorts of data to be used. Even allows voids (y)Disadvantage to using this:
Easily "cracked" by creating identical program and deserializing the file.To get rid of the disadvantage, I used string encryption and decryption. First it encrypts the strings, then it writes it to a file. Once it gets the file, its deserialized and then decrypted.
Maybe this will be usefull to some people to just look at it... Sorry about the comments, I wrote all them in and didnt bother to check how it looked so now I said screw formating..
Outcome of the Program, File, and Output in the console:
Friday, 5 July 2013
[Tutorial] Serialize Files
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment