Skip to content

Commit

Permalink
feat(tsl): include metadata in DROP action
Browse files Browse the repository at this point in the history
  • Loading branch information
iGoodie committed Sep 30, 2019
1 parent 21d2c0d commit a402f2c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -5,7 +5,7 @@ org.gradle.daemon=false

mod_id=twitchspawn
mod_group=net.programmer.igoodie
mod_version=0.3.0
mod_version=0.3.1

minecraft_version=1.12.2
forge_version=14.23.3.2655
Expand Down
Expand Up @@ -17,6 +17,7 @@
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.programmer.igoodie.twitchspawn.client.gui.StatusIndicatorOverlay;
import net.programmer.igoodie.twitchspawn.command.ItemDataCommand;
import net.programmer.igoodie.twitchspawn.command.TwitchSpawnCommand;
import net.programmer.igoodie.twitchspawn.configuration.ConfigManager;
import net.programmer.igoodie.twitchspawn.network.NetworkManager;
Expand Down Expand Up @@ -75,6 +76,7 @@ public void onServerAboutToStart(FMLServerAboutToStartEvent event) {
@Mod.EventHandler
public void onServerStarting(FMLServerStartingEvent event) {
event.registerServerCommand(new TwitchSpawnCommand());
event.registerServerCommand(new ItemDataCommand());
LOGGER.info("onServerStarting()");
}

Expand Down
@@ -0,0 +1,39 @@
package net.programmer.igoodie.twitchspawn.command;

import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;

public class ItemDataCommand extends CommandBase {

@Override
public String getName() {
return "itemdata";
}

@Override
public String getUsage(ICommandSender sender) {
return "/itemdata";
}

/* ------------------------------------------------- */

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
if (!(sender instanceof EntityPlayerMP)) {
System.out.printf("Sender's type is a(n) %s\n", sender.getClass().getSimpleName());
return;
}

EntityPlayerMP player = (EntityPlayerMP) sender.getCommandSenderEntity();
ItemStack heldItemstack = player.inventory.mainInventory.get(player.inventory.currentItem);

System.out.printf("Holding: %s, %s\n",
heldItemstack.getDisplayName(),
heldItemstack.serializeNBT());
}

}
Expand Up @@ -13,34 +13,38 @@ public class DropAction extends TSLAction {

private String itemRaw;
private int itemAmount;
private int itemDamage;

/*
* Exemplar valid params:
* minecraft:diamond_block 123
* stone_block 321
* diamond_sword{Enchantments:[{id:smite,lvl:2},{id:sweeping,lvl:2},{id:unbreaking,lvl:3}]}
* Params:
* 0 - item: minecraft:diamond_block{someNBT:"Data"}
* 1 - amount: 1
* 2? - damage: 0
*
* Possible param count: [2,3]
*/
public DropAction(List<String> words) throws TSLSyntaxError {
this.message = TSLParser.parseMessage(words);
List<String> actionWords = actionPart(words);

if (actionWords.size() != 1 && actionWords.size() != 2)
if (actionWords.size() != 2 && actionWords.size() != 3)
throw new TSLSyntaxError("Invalid length of words: " + actionWords);

this.itemRaw = actionWords.get(0);

try {
this.itemAmount = actionWords.size() != 2 ? 1 : Integer.parseInt(actionWords.get(1));

} catch (NumberFormatException e) {
throw new TSLSyntaxError("Expected an integer, found instead -> %s", actionWords.get(1));
}
this.itemAmount = parseInt(actionWords.get(1));
this.itemDamage = actionWords.size() >= 3 ? parseInt(actionWords.get(2)) : 0;

// Check if given item word is parse-able
if (!new ItemParser(this.itemRaw).isValid())
throw new TSLSyntaxError("Invalid item text");
}

private int parseInt(String string) throws TSLSyntaxError {
try { return Integer.parseInt(string); } catch (NumberFormatException e) {
throw new TSLSyntaxError("Expected an integer, found instead -> %s", string);
}
}

@Override
protected void performAction(EntityPlayerMP player, EventArguments args) {
ItemStack itemStack = createItemStack(args);
Expand All @@ -49,7 +53,9 @@ protected void performAction(EntityPlayerMP player, EventArguments args) {

private ItemStack createItemStack(EventArguments args) {
String input = replaceExpressions(itemRaw, args);
return new ItemParser(input).generateItemStack(itemAmount);
ItemStack itemStack = new ItemParser(input).generateItemStack(itemAmount);
itemStack.setItemDamage(itemDamage);
return itemStack;
}

@Override
Expand Down

0 comments on commit a402f2c

Please sign in to comment.