Jump to content

[1.7.10] [Server Mod] Error, when comparing strings


DCNick3

Recommended Posts

I have a command class :

 

package com.dcnick.servmod;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ChatComponentText;

public class AreaCommand implements ICommand {

private List<String> aliases;

protected AreaCommand() {
	aliases = new ArrayList<String>();
	aliases.add("area");
	aliases.add("ar");
}

@Override
public int compareTo(Object arg0) {
	return 0;
}

@Override
public String getCommandName() {
	return "area";
}

@Override
public String getCommandUsage(ICommandSender cs) {
	return "Using: /area [create] [name]";
}

@Override
public List getCommandAliases() {
	return aliases;
}

@Override
public void processCommand(ICommandSender cs, String[] arg) {
	EntityPlayer player;

	if (arg.length < 1) {
		cs.addChatMessage(new ChatComponentText("§4Not Enought Arguments§f"));
		return;
	}

	if (cs instanceof EntityPlayer) {
		player = (EntityPlayer) cs;
	} else {
		cs.addChatMessage(new ChatComponentText("§4Player Only Command§f"));
		return;
	}
	if (arg[0] == "create")
	{
		if (arg.length < 2) {
			cs.addChatMessage(new ChatComponentText(
					"§4Not Enought Arguments§f"));
			return;
		}
		if (player.getEntityData().getBoolean("pos1") && player.getEntityData().getBoolean("pos2"))
		{
			double x1 = player.getEntityData().getInteger("pos1X");
			double y1 = player.getEntityData().getInteger("pos1Y");
			double z1 = player.getEntityData().getInteger("pos1Z");
			double x2 = player.getEntityData().getInteger("pos2X");
			double y2 = player.getEntityData().getInteger("pos2Y");
			double z2 = player.getEntityData().getInteger("pos2Z");
			String name = arg[1];
			cs.addChatMessage(new ChatComponentText(
					"§fCreating Region named '§e" + name
					+ "§f' with Point1 at (§e" + x1 + "§f,§e" + y1
					+ "§f,§e" + z1 + "§f) and Point2 at (§e" + x2
					+ "§f,§e" + y2 + "§f,§e" + z2 + "§f) ..." + "§f"));
		}
		else
		{
			cs.addChatMessage(new ChatComponentText(
					"§4Positions not setted! Use /pos command to do this§f"));
			return;
		}
	}
	else
	{
		cs.addChatMessage(new ChatComponentText(
				"§4Error at argument 1 it can be: create, remove, info§f"));
		return;
	}
}

@Override
public boolean canCommandSenderUseCommand(ICommandSender cs) {
	return true;
}

@Override
public List addTabCompletionOptions(ICommandSender cs, String[] ar) {
	return null;
}

@Override
public boolean isUsernameIndex(String[] p_82358_1_, int p_82358_2_) {
	return false;
}

}

 

 

And when I comparing arg[0] with "create" Program Jumps to else instruction, but I enter "create" in minecraft, Help!!!

I'm Developing Some mods. It's all.

Link to comment
Share on other sites

That is not how you compare strings.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

This is pretty basic Java, but to compare arbitrary objects, you have to use the

equals

method.

 

In Java, every object variable is actually a reference. That means object variables only store the memory location of the objects, not the objects themselves. If you use "A == B" with two objects, it compares the addresses of A and B, not A and B themselves. (They are kind of like pointers in C.)

 

Edit:

(I meant

equals

, not

compare

.)

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.



×
×
  • Create New...

Important Information

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