Skip to content

Commit

Permalink
Add mutable marker to command string
Browse files Browse the repository at this point in the history
Signed-off-by: TheSilkMiner <thesilkminer@outlook.com>
  • Loading branch information
TheSilkMiner committed Oct 27, 2021
1 parent c56859a commit 9f3f9bd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
Expand Up @@ -4,6 +4,7 @@
import com.blamejared.crafttweaker.api.item.IIngredient;
import com.blamejared.crafttweaker.api.managers.IRecipeManager;
import com.blamejared.crafttweaker.api.util.InstantiationUtil;
import com.blamejared.crafttweaker.impl.helper.ItemStackHelper;
import com.blamejared.crafttweaker.impl.item.MCItemStackMutable;
import net.minecraft.item.crafting.IRecipe;
import org.openzen.zenscript.codemodel.Modifiers;
Expand All @@ -28,7 +29,7 @@ public String dumpToCommandString(final IRecipeManager manager, final IRecipe<?>
return String.format(
"~~ Recipe name: %s, Outputs: %s, Inputs: [%s], Recipe Class: %s, Recipe Serializer: %s ~~",
recipe.getId(),
new MCItemStackMutable(recipe.getRecipeOutput()).getCommandString(),
ItemStackHelper.getCommandString(recipe.getRecipeOutput()),
recipe.getIngredients()
.stream()
.map(IIngredient::fromIngredient)
Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.blamejared.crafttweaker.impl.commands.CommandUtilities;
import com.blamejared.crafttweaker.impl.data.MapData;
import com.blamejared.crafttweaker.impl.fluid.MCFluidStackMutable;
import com.blamejared.crafttweaker.impl.helper.ItemStackHelper;
import com.blamejared.crafttweaker.impl.item.MCItemStackMutable;
import com.blamejared.crafttweaker.impl.tag.MCTag;
import com.blamejared.crafttweaker.impl.tag.manager.TagManager;
Expand Down Expand Up @@ -161,7 +162,7 @@ private static void subCommand(final String name, final String desc, final Comma
// <editor-fold desc="CT Functions">
private static void sendBasicItemInformation(final PlayerEntity player, final ItemStack target) {

final String output = new MCItemStackMutable(target).getCommandString();
final String output = ItemStackHelper.getCommandString(target);
sendCopyingHand(player, "Item", output);
}

Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.blamejared.crafttweaker.api.CraftTweakerAPI;
import com.blamejared.crafttweaker.impl.commands.CTCommands;
import com.blamejared.crafttweaker.impl.commands.CommandUtilities;
import com.blamejared.crafttweaker.impl.helper.ItemStackHelper;
import com.blamejared.crafttweaker.impl.item.MCItemStackMutable;
import com.blamejared.crafttweaker.impl.tag.MCTag;
import com.blamejared.crafttweaker.impl.tag.manager.TagManagerItem;
Expand Down Expand Up @@ -42,7 +43,7 @@ public static void registerInventoryCommands() {
final String inventoryContents = IntStream.range(0, inventory.getSlots())
.mapToObj(inventory::getStackInSlot)
.filter(it -> !it.isEmpty())
.map(it -> Pair.of(new MCItemStackMutable(it).getCommandString(), TagManagerItem.INSTANCE.getAllTagsFor(it.getItem())))
.map(it -> Pair.of(ItemStackHelper.getCommandString(it), TagManagerItem.INSTANCE.getAllTagsFor(it.getItem())))
.map(it -> it.getFirst() + '\n' + stringify(it.getSecond()))
.collect(Collectors.joining("\n", "Inventory item tags\n", ""));

Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.blamejared.crafttweaker.api.item.IIngredient;
import com.blamejared.crafttweaker.api.item.tooltip.ITooltipFunction;
import com.blamejared.crafttweaker.impl.entity.MCEntityType;
import com.blamejared.crafttweaker.impl.helper.ItemStackHelper;
import com.blamejared.crafttweaker.impl.item.MCItemStackMutable;
import com.blamejared.crafttweaker.impl.util.text.MCTextComponent;
import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -67,7 +68,7 @@ public void handleTooltips(ItemTooltipEvent e) {
String.format(
"Unable to run one of the tooltip functions for %s on %s due to an error (for experts, refer to %s)",
ingredient.getCommandString(),
new MCItemStackMutable(e.getItemStack()).getCommandString(),
ItemStackHelper.getCommandString(e.getItemStack()),
function.getClass().getName()
),
exception
Expand Down
Expand Up @@ -8,33 +8,47 @@
public final class ItemStackHelper {

public static String getCommandString(final ItemStack stack) {

return getCommandString(stack, false);
}

public static String getCommandString(final ItemStack stack, final boolean mutable) {

final StringBuilder sb = new StringBuilder("<item:");
sb.append(stack.getItem().getRegistryName());
sb.append('>');

if(stack.getTag() != null) {

MapData data = (MapData) NBTConverter.convert(stack.getTag()).copyInternal();
//Damage is special case, if we have more special cases we can handle them here.
if(stack.getItem().isDamageable()) {

data.remove("Damage");
}
if(!data.isEmpty()) {

sb.append(".withTag(");
sb.append(data.asString());
sb.append(')');
}
}

if(stack.getDamage() > 0) {

sb.append(".withDamage(").append(stack.getDamage()).append(')');
}

if(!stack.isEmpty()) {
if(stack.getCount() != 1) {
sb.append(" * ").append(stack.getCount());
}
if(!stack.isEmpty() && stack.getCount() != 1) {

sb.append(" * ").append(stack.getCount());
}

if (mutable) {

sb.append(".mutable()");
}

return sb.toString();
}

Expand Down
Expand Up @@ -138,7 +138,7 @@ public IItemStack withoutTag() {
@Override
public String getCommandString() {

return ItemStackHelper.getCommandString(this.getInternal());
return ItemStackHelper.getCommandString(this.getInternal(), true);
}

@Override
Expand Down

0 comments on commit 9f3f9bd

Please sign in to comment.