Jump to content

Questin about Strings


Moritz

Recommended Posts

Hello.

 

I got an idea for a mod and want to start with it.

I make the whole mod mobile (the writing thing)

because i like to look how much i know and how much i do not know.

But these informations are not needed here.

 

Now informations what i think they are important:

 

I want to make items and these work with Strings:

 

Like this:

 

String Username = "Null";

String par1 = "";

String par2 = "";

 

Now the problem:

 

i want to make a String Checking lib like this:

 

if(Username.startsWith("C")

{

    than do this this and this;

}

else if(Username.startsWith("G")

{

    than do this but not that

}

else

{

    do nothing

}

 

 

now all letters together are 26

My Problem is i do not want to do 26 if states.

How can make a (maybe) enum for ever letter

or something which stores the letters and i can say:

 

enumname:

 

0 = "A", "G", "Z", "K", "D", "I", "Q"

 

if(username.startswith(enumname.0)

{

    than do that

}

else

{

    do this

}

 

So who can help me?

Link to comment
Share on other sites

I am a little bit new. I want to use minecraft to learn how things work. (The understanding)

 

I know that i can not learn java from

minecraft.

 

But now to the answer you mean it like this:

 

public class ItemTest extends Item

{

 

    String[] letters = new String[5] {"A", "X", "Y", "G", "T"};

    String username;

    String var1; //I will not tell what this parameter do

 

 

      public ItemTest(int id)

      {

          super(id);

          setHasSubtypes(true);

      }

 

      public ItemStack onRightClickItem(ItemStack par1, World par2, EntityPlayer par3)

      {

          if(username == null)

          {

                username = par3.username;

          }

          return par1;

      }

 

      public void onUpdate(ItemStack par1, World par2, Entity par3, int par4, boolean par5)

      {

            if(par5)

            {

                for(int i = 0; i < 5; i++)

                {

                    if(var1 == null)

                    {

                            if(username.startsWith(letters)

                            {

                                var1 = "This";

                            }

                            else

                            {

                                  var1 = "That";

                              }

 

 

                    }

                    else

                    {

                      //Do nothing

                    }

                }

            }

      }

 

}

 

Does this work?

   

Link to comment
Share on other sites

In the loop you aren't using the elements,you should: letters

 

But for understanding from minecraft is a really terrible thing,because of the variable names.

 

You should start making you own games,like a simple top-down shooter games,platform games,pacman any easy game do it.

 

And i recommend you LWJGL and Slick2D

Link to comment
Share on other sites

personaly i'd use a case using a char

 

function (void or return type) checkName(String name){
    char nameChar[] = name.toLowerCase().toCharArray();
    
    switch nameChar[0] {
        case 'a' :
              <function to do here>
              break; // if void, else return from the above function
        case 'b' :
              <function to do here>
              break;
        // ect..
        case default :
              System.out.println("you fucked up");
              break;
    }
}

Link to comment
Share on other sites

Ok now i have to explain what i want to do.

 

I have no name for the mod. But it has something todo with blood:

When you are rightclick the item. The player get Hurt (half heart damage) and the item got the username.

Then the item checks what letter it starts and choose from that which bloodgroup the player has.

xD and with that you can make potions and whatever xD

Link to comment
Share on other sites

Guys we're dealing with strings....

public static final String blood_O = "aqtik";
public static final String blood_Opos  = "rhnsb";

/**pass the first char of username*/
public void handleBlood(char c){
if(blood_O.contains(c))
//dostuff;
else if(blood_Opos.contains(c))
//dostuff;
} 

Fast and legible

I think its my java of the variables.

Link to comment
Share on other sites

You did understand nothing.

 

here my codeidea:

 

private String dna = "null";

private String username = "null";

 

 

public ItemStack onRightClickItem(ItemStack par1, World par2, EntityPlayer par3)

{

      if(username == "null")

      {

          username = par3.username;

      }

return par1;

}

 

public void onUpdate(....boolean par5)

{

    if(par5)

    {

        checkDNA();

    }

}

 

    public void checkDNA();

    {

        if(dna == "null" && !username == "null")

        {

              if(username.startsWith("a")

            {

                // do stuff;

            }

            else

            {

                //dostuff

            }

        }

    }

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.