Jump to content

Recommended Posts

Posted (edited)

Hello I created a command And I would that all player have access to this command

 

package fr.atonha.saofrmod.commands;

import java.util.List;

import fr.atonha.saofrmod.ExtendedPlayer;
import fr.atonha.saofrmod.Main;
import fr.atonha.saofrmod.network.PacketAttributeGui;
import fr.atonha.saofrmod.network.PacketSync;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.IAttributeInstance;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;

public class MainCommand extends CommandBase {
	
	String name = "saofr";

	@Override
	public String getCommandName() {
		return name;
	}

	@Override
	public String getCommandUsage(ICommandSender p_71518_1_) {
		return "/" + name;
	}

	@Override
	public void processCommand(ICommandSender sender, String[] args) {
		if(args.length == 0) {
			displayHelp(sender);
		}
		else if(args.length >= 1 && args[0].equalsIgnoreCase("open")) {
			Main.getPacketHandler().sendTo(new PacketAttributeGui(), (EntityPlayerMP) sender);
			ExtendedPlayer data = ExtendedPlayer.get((EntityPlayer)sender);
			Main.getPacketHandler().sendTo(new PacketSync(data.getLevel(), data.healthPoints, data.damagePoints, data.getPoint()), (EntityPlayerMP)sender);
		}
		else if(args.length >= 1 && args[0].equalsIgnoreCase("reset")) {
			if(!MinecraftServer.getServer().getConfigurationManager().func_152596_g(((EntityPlayer) sender).getGameProfile())){
				sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "[" + Main.MODID + "]" + " Tu n'as pas la permission pour utiliser cette commande"));
				return;
			}
			if(args.length >= 2 && this.isOnline(sender.getEntityWorld(), args[1])) {
				EntityPlayer argPlayer = this.getPlayer(sender.getEntityWorld(), args[1]);
				ExtendedPlayer pData = ExtendedPlayer.get(argPlayer);
				argPlayer.experienceLevel = 1;
				pData.setLevel(1);
				pData.setPoints(0);
				pData.damagePoints = 0;
				pData.healthPoints = 0;
				pData.setHP(ExtendedPlayer.startHP);
				pData.setDamage(ExtendedPlayer.startDamage);
				IAttributeInstance healthAttribute = argPlayer.getEntityAttribute(SharedMonsterAttributes.maxHealth);
		   		IAttributeInstance damageAttribute = argPlayer.getEntityAttribute(SharedMonsterAttributes.attackDamage);
		   		if(healthAttribute.getModifier(pData.healthModifier) != null)
		   			healthAttribute.removeModifier(healthAttribute.getModifier(pData.healthModifier));
		   		if(damageAttribute.getModifier(pData.damageModifier) != null)
		   			damageAttribute.removeModifier(damageAttribute.getModifier(pData.damageModifier));
				sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN + "[" + Main.MODID + "]" + " Donnees du joueur reinitialiser"));
			}
			else {
				sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "[" + Main.MODID + "]" + " Ce joueur n'est pas connecte"));
				return;
			}
		}
		else if(args.length >= 1 && args[0].equalsIgnoreCase("addPoints")) {
			if(!MinecraftServer.getServer().getConfigurationManager().func_152596_g(((EntityPlayer) sender).getGameProfile())){
				sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "[" + Main.MODID + "]" + " Tu n'as pas la permission pour utiliser cette commande"));
				return;
			}
			if(args.length >= 2 && this.isOnline(sender.getEntityWorld(), args[1]))
			{
				if(args.length >= 3 && this.isInteger(args[2])) {
					if(Integer.parseInt(args[2]) > 0) {
						EntityPlayer argPlayer = this.getPlayer(sender.getEntityWorld(), args[1]);
						ExtendedPlayer data = ExtendedPlayer.get((EntityPlayer) argPlayer);
						data.setPoints(data.getPoint() + Integer.parseInt(args[2]));
						sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN + "[" + Main.MODID + "]" + " Points ajouté avec succès"));
					}
					else {
						sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "[" + Main.MODID + "]" + " Vous devez entrer un nombre > 0"));
						return;
					}
				}
				else
				{
					sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "[" + Main.MODID + "]" + " Vous devez entrer un nombre"));
					return;
				}
			}
			else {
				sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "[" + Main.MODID + "]" + " Ce joueur n'est pas connecte"));
				return;
			}
		}
		else 
			displayHelp(sender);
		
	}
	
	
	public void displayHelp(ICommandSender sender) {
		sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN + "SaoFr main commands help:"));
		sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "-/saofr open " + EnumChatFormatting.WHITE + "-" + EnumChatFormatting.GOLD + " ouvre l'interface"));
		sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "-/saofr reset <Pseudo> " + EnumChatFormatting.WHITE + "-" + EnumChatFormatting.GOLD + " reinitialise les donnees d'un joueur"));
		sender.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + "-/saofr addpoints <Pseudo> <Points>" + EnumChatFormatting.WHITE + "-" + EnumChatFormatting.GOLD + " ajoute des points"));
	}
	
	
	public boolean isOnline(World world, String name) {
		for(Object player : world.playerEntities) {
			EntityPlayer p  = (EntityPlayer) player;
			if(p.getCommandSenderName().equalsIgnoreCase(name))
				return true;
		}
		return false;	
	}
	
	public static boolean isInteger(String s) {
	    try { 
	        Integer.parseInt(s); 
	    } catch(NumberFormatException e) { 
	        return false; 
	    } catch(NullPointerException e) {
	        return false;
	    }
	    return true;
	}
	
	public EntityPlayer getPlayer(World world, String name) {
		for(Object player : world.playerEntities) {
			EntityPlayer p  = (EntityPlayer) player;
			if(p.getCommandSenderName().equalsIgnoreCase(name))
				return p;
		}
		return null;	
	}
	
    @Override 
    public boolean canCommandSenderUseCommand(ICommandSender var1) 
    { 
        return true;
    } 
    
    @Override  
    public List addTabCompletionOptions(ICommandSender var1, String[] var2) 
    { 
        return null; 
    } 

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

}

 

but players not op doesn't not have access to command I do not understand thanks in advance .

Edited by INeoxz
Posted
9 minutes ago, INeoxz said:

ChatComponentText

 

9 minutes ago, INeoxz said:

canCommandSenderUseCommand

 

10 minutes ago, INeoxz said:

import net.minecraft.util.EnumChatFormatting;

What version of the game are you using?

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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