Time and Date

0 comments

The handler:

Class for getting time and date.

The documentation explains it, that's why its in the tutorial section.

I already know theres shorter ways of doing this, but I honestly don't give a shit.

Code: [Select]
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Gets the time and date.
 *
 * @author Ryan Greene
 *
 */
public class Time {

        /**
         * Instance for the time.
         */
        private static Time time = new Time();

        /**
         * Gets the time.
         *
         * @return The time.
         */
        public static Time getTime() {
                return time;
        }

        /**
         * Instance for the calendar.
         */
        Calendar cal = new GregorianCalendar();

        /**
         * The day.
         */
        private int day = cal.get(Calendar.DAY_OF_MONTH);

        /**
         * The month.
         */
        private int month = cal.get(Calendar.MONTH);

        /**
         * The year.
         */
        private int year = cal.get(Calendar.YEAR);

        /**
         * The hour.
         */
        private int hour = cal.get(Calendar.HOUR);

        /**
         * The minute.
         */
        private int min = cal.get(Calendar.MINUTE);

        /**
         * The second.
         */
        private int sec = cal.get(Calendar.SECOND);

        /**
         * Gets the day.
         *
         * @return The day.
         */
        public int getDay() {
                return day;
        }

        /**
         * Gets the month.
         *
         * @return The month.
         */
        public int getMonth() {
                return month;
        }

        /**
         * Gets the year.
         *
         * @return The the year.
         */
        public int getYear() {
                return year;
        }

        /**
         * Gets the hour.
         *
         * @return The hour.
         */
        public int getHour() {
                return hour;
        }

        /**
         * Gets the minute.
         *
         * @return The minute.
         */
        public int getMin() {
                return min;
        }

        /**
         * Gets the second.
         *
         * @return The second.
         */
        public int getSec() {
                return sec;
        }

        /**
         * Holds the month names.
         */
        private String[] monthNames = { "January", "Febuary", "March", "April",
                        "May", "June", "July", "Augest", "September", "October",
                        "November", "December" };

        /**
         * Gets the month name.
         *
         * @return The month name.
         */
        public String getMonthName() {
                return monthNames[getMonth()];
        }
}

Example on how to use this:
Code: [Select]
                logger.info("Year: " + Time.getTime().getYear() + " Month: "
                                + Time.getTime().getMonth() + " Day: "
                                + Time.getTime().getDay() + " Hour: "
                                + Time.getTime().getHour() + " Minute: "
                                + Time.getTime().getMin() + " Month name: "
                                + Time.getTime().getMonthName());

Examples of way to use this in a RSPS:

Using my dialogue handler witch can be found at the end of the tutorial:

Replace dialogueData with this:
Code: [Select]
        /**
         * Array to store dialogue data. <next dialgouge ID, head animation ID, type
         * of dialogue, dialogue text>.
         */
        private static Object[][] dialogueData = {
                        { 1, 591, "NPC", "Good day, how can I help you?" },
                        { -1, -1, "OPTION", "I'd like to access my bank account, please.",
                                        "I'd like to check my PIN settings." },
                        { -1, 591, "PLAYER", "I'd like to access my bank account, please." },
                        { -1, 591, "PLAYER", "I'd like to check my PIN settings." },
                        { 5, 591, "NPC", "Oy matey! What would you like to do?" },
                        { -1, 591, "OPTION", "I'd like to do fishing trawler.",
                                        "I'd like to ride your boat." }, // 5
                        { -1, 591, "NPC", "Argh, I'm sorry matey.",
                                        "That minigame is still under construction!" },
                        { -1, 591, "NPC", "Oy, and where would you like to go?" },
                        { -1, 591, "NPC", "Hello, I can take you in my wheel barrow",
                                        "to various places.", "Where would you like to go?" },
                        { 10, 591, "NPC", "Hello, what would you like to do?" },
                        { -1, 591, "OPTION", "View server time.", "Nevermind." },
                        { 10, 591, "NPC", "Sure! It is currently: " + Time.getTime().getHour() + ":"
                                        + Time.getTime().getMin() }
        };
Replace handleOptions with this:

Code: [Select]
        /**
         * Handles dialogue options.
         *
         * @param player
         *            The player.
         * @param actionButton
         *            The action button.
         */
        public static void handleOptions(Player player, int actionButton) {
                int d = player.getDialogueId();
                if (actionButton == 2461) { // 1 2
                        switch (d) {
                        case 1:
                                sendDialogue(player, 2);
                                break;
                        case 5:
                                sendDialogue(player, 6);
                                break;
                        case 10:
                                sendDialogue(player, 11);
                        default:
                                player.getInterfaceState().interfaceClosed();
                                break;
                        }
                } else if (actionButton == 2462) { // 2 2
                        switch (d) {
                        case 1:
                                sendDialogue(player, 3);
                                break;
                        case 5:
                                sendDialogue(player, 7);
                                break;
                        default:
                                player.getInterfaceState().interfaceClosed();
                                break;
                        }
                }
        }

comments (0)

Post a Comment