Jump to content

[1.10.2] Subcommand only works first time


JimiIT92

Recommended Posts

I'm making a server side only mod that introduce a command with subcommands. One of this will give a specific item to the player, the item is defined by config. But when i run the command i get the item only the first time i run it, the next times i get nothing.

This is how i get the item

Configuration config = new Configuration(new File("config/ForgeGuard/config.cfg"));
	config.load();
	String item = config.getString("selector", "Selector Item Properties", "stick",
			"The item to use as region selector");
	String color = config.getString("color", "Selector Item Properties", "light purple",
			"The color of the name of the region selector item");
	String name = config.getString("name", "Selector Item Properties", "Region Selector",
			"The name of the region selector item");

	TextFormatting nameColor = TextFormatting.getValueByName(color);
	String displayName = "";
	if (nameColor != null)
		displayName += nameColor;
	displayName += name;
	ForgeGuard.SELECTOR = new ItemStack(Item.getByNameOrId(item), 1).setStackDisplayName(displayName);

	config.save();

 

Notice that ForgeGuard.SELECTOR is a static ItemStack declared in the main class. This code is also called from the preInit method

 

This is the command class, in the execute method i check if there are args on the command. If there are and one of this is "selector" than give the item to the player. As i said this works once but not the other times

package com.forgeguard.commands;

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

import com.forgeguard.ForgeGuard;
import com.forgeguard.region.Region;

import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.CommandResultStats;
import net.minecraft.command.EntitySelector;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.SoundEvents;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextFormatting;

public class CommandRegion extends CommandBase {

private ArrayList<String> subCommands = new ArrayList<String>();
private ArrayList<String> flags = new ArrayList<String>();

public CommandRegion() {
	this.subCommands.add("selector");
	this.subCommands.add("save");
	this.subCommands.add("flag");

	this.flags.add("mobs");
	this.flags.add("chests");
	this.flags.add("edit");
	this.flags.add("pvp");
}

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

@Override
public String getCommandUsage(ICommandSender sender) {
	return "/region [subcommand][flag]";
}

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
	if (sender.getCommandSenderEntity() != null) {
		EntityPlayer player = getPlayer(server, sender, sender.getName());

		/**
		 * /region
		 */
		if (args.length == 0) {
			Region r = ForgeGuard.UTILS.getRegion(player.dimension, player.getPosition());
			if (r != null)
				ForgeGuard.UTILS.sendMessage(player, r.getName());
			else
				ForgeGuard.UTILS.sendMessage(player, TextFormatting.RED + "There's no region here!");
		} else {
			String subcommand = args[0].toLowerCase();

			/**
			 * /region selector
			 */
			if (subcommand.equalsIgnoreCase(this.subCommands.get(0))) {
				boolean flag = player.inventory.addItemStackToInventory(ForgeGuard.SELECTOR);
				if (flag) {
					player.worldObj.playSound((EntityPlayer) null, player.posX, player.posY, player.posZ,
							SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F,
							((player.getRNG().nextFloat() - player.getRNG().nextFloat()) * 0.7F + 1.0F) * 2.0F);
					player.inventoryContainer.detectAndSendChanges();
				} else {
					sender.setCommandStat(CommandResultStats.Type.AFFECTED_ITEMS, 1);
					EntityItem entityitem = player.dropItem(ForgeGuard.SELECTOR, false);

					if (entityitem != null) {
						entityitem.setNoPickupDelay();
						entityitem.setOwner(player.getName());
					}
				}

				notifyCommandListener(sender, this, "commands.give.success",
						new Object[] { ForgeGuard.SELECTOR.getTextComponent(), 1, player.getName() });
			}
			/**
			 * /region save
			 */
			else if (subcommand.equalsIgnoreCase(this.subCommands.get(1))) {
				ForgeGuard.UTILS.saveRegion(player);
			}

			/**
			 * /region flag
			 */
			else if (subcommand.equalsIgnoreCase(this.subCommands.get(2))) {
				Region r = ForgeGuard.UTILS.getPendingRegion(player);
				if (r != null) {
					String flag = args[1];
					if (flag.equalsIgnoreCase(this.flags.get(0)))
						r.setMobs(Boolean.valueOf(args[2]));
					if (flag.equalsIgnoreCase(this.flags.get(1)))
						r.setChests(Boolean.valueOf(args[2]));
					if (flag.equalsIgnoreCase(this.flags.get(2)))
						r.setEdit(Boolean.valueOf(args[2]));
					if (flag.equalsIgnoreCase(this.flags.get(3)))
						r.setPvp(Boolean.valueOf(args[2]));
				} else
					ForgeGuard.UTILS.sendMessage(player, TextFormatting.RED + "Select a region first!");
			}
		}

	}
}

@Override
public boolean checkPermission(MinecraftServer server, ICommandSender sender) {
	boolean flag;
	if(sender.getCommandSenderEntity() == null)
		flag = true;
	else
		flag = server.getServer().getPlayerList().getOppedPlayers().getEntry(((EntityPlayer)sender.getCommandSenderEntity()).getGameProfile()) != null;
	return flag;
}

@Override
public List<String> getTabCompletionOptions(MinecraftServer server, ICommandSender sender, String[] args,
		BlockPos pos) {
	if(args[0].equalsIgnoreCase("flag") && !args[1].isEmpty())
	{
		ArrayList<String> bool = new ArrayList<String>();
		bool.add("true");
		bool.add("false");
		return bool;
	}
	if(args[0].equalsIgnoreCase("flag"))
		return this.flags;
	if(args.length > 3)
		return new ArrayList<String>();
	return this.subCommands;
}

}

 

The command is registered in the main class by doing this

@EventHandler
    public void serverLoad(FMLServerStartingEvent event)
    {
       event.registerServerCommand(new CommandRegion());
    }

 

So what could cause this issue? Thanks to all who will help me :)

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

Instead of adding ForgeGuard.SELECTOR to the inventory add ForgeGuard.SELECTOR.copy() to the inventory. I ran into this problem once. I was using the FurnaceRecipes and just straight up set the slot to the FurnaceRecipes, but then whenever I increased the stackSize I would get more for every process, and once taken out of the furnace I would get a stack of the item, but with a stackSize of zero. That was one of the best debugging moments of my life...

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.