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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • today i downloaded forge 1.20.1 version 47.2.0 installed it in my .minecraft and installed the server, with and without mod it literally gives the same error, so i went to see other versions like 1.17 and it worked normally   https://paste.ee/p/VxmfF
    • If you're seeking an unparalleled solution to recover lost or stolen cryptocurrency, let me introduce you to GearHead Engineers Solutions. Their exceptional team of cybersecurity experts doesn't just restore your funds; they restore your peace of mind. With a blend of cutting-edge technology and unparalleled expertise, GearHead Engineers swiftly navigates the intricate web of the digital underworld to reclaim what's rightfully yours. In your moment of distress, they become your steadfast allies, guiding you through the intricate process of recovery with transparency, trustworthiness, and unwavering professionalism. Their team of seasoned web developers and cyber specialists possesses the acumen to dissect the most sophisticated schemes, leaving no stone unturned in their quest for justice. They don't just stop at recovering your assets; they go the extra mile to identify and track down the perpetrators, ensuring they face the consequences of their deceitful actions. What sets  GearHead Engineers apart is not just their technical prowess, but their unwavering commitment to their clients. From the moment you reach out to them, you're met with compassion, understanding, and a resolute determination to right the wrongs inflicted upon you. It's not just about reclaiming lost funds; it's about restoring faith in the digital landscape and empowering individuals to reclaim control over their financial futures. If you find yourself ensnared in the clutches of cybercrime, don't despair. Reach out to GearHead Engineers and let them weave their magic. With their expertise by your side, you can turn the tide against adversity and emerge stronger than ever before. In the realm of cybersecurity, GearHead Engineers reigns supreme. Don't just take my word for it—experience their unparalleled excellence for yourself. Your journey to recovery starts here.
    • Ok so this specific code freezes the game on world creation. This is what gets me so confused, i get that it might not be the best thing, but is it really so generation heavy?
    • Wizard web recovery has exhibited unparalleled strength in the realm of recovery. They stand out as the premier team to collaborate with if you encounter withdrawal difficulties from the platform where you’ve invested. Recently, I engaged with them to recover over a million dollars trapped in an investment platform I’d been involved with for months. I furnished their team with every detail of the investment, including accounts, names, and wallet addresses to which I sent the funds. This decision proved to be the best I’ve made, especially after realizing the company had scammed me.   Wizard web recovery ensures exemplary service delivery and ensures the perpetrators face justice. They employ advanced techniques to ensure you regain access to your funds. Understandably, many individuals who have fallen victim to investment scams may still regret engaging in online services again due to the trauma of being scammed. However, I implore you to take action. Seek assistance from Wizard Web Recovery today and witness their remarkable capabilities. I am grateful that I resisted their enticements, and despite the time it took me to discover Wizard web recovery, they ultimately fulfilled my primary objective. Without wizard web recovery intervention, I would have remained despondent and perplexed indefinitely.
    • I've tested the same code on three different envionrments (Desktop win10, desktop Linux and Laptop Linux) and it kinda blows up all the same. Gonna try this code and see if i can tune it
  • Topics

×
×
  • Create New...

Important Information

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