Skip to content

Commit

Permalink
Fix 'hand' argument in MaterialOrHand selector
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioGr committed Apr 6, 2022
1 parent 30abd3d commit 1c6562a
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,22 +301,11 @@ public AdminEditCommands(final NotQuests main, PaperCommandManager<CommandSender
final boolean glow = context.flags().isPresent("glow");

final MaterialOrHand materialOrHand = context.get("material");
ItemStack takeItem;
if (materialOrHand.hand) { //"hand"
if (context.getSender() instanceof Player player) {
takeItem = player.getInventory().getItemInMainHand();
} else {
context.getSender().sendMessage(main.parse(
"<error>This command must be run by a player."
));
return;
}
final ItemStack takeItem;
if (materialOrHand.material.equalsIgnoreCase("any")) {
takeItem = new ItemStack(Material.BOOK, 1);
} else {
if (materialOrHand.material.equalsIgnoreCase("any")) {
takeItem = new ItemStack(Material.BOOK, 1);
} else {
takeItem = main.getItemsManager().getItemStack(materialOrHand.material);
}
takeItem = main.getItemsManager().getItemStack(materialOrHand.material);
}
if (glow) {
takeItem.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,14 @@ public AdminItemsCommands(final NotQuests main, PaperCommandManager<CommandSende

final MaterialOrHand materialOrHand = context.get("material");

ItemStack itemStack;
if (materialOrHand.hand) { //"hand"
if (context.getSender() instanceof Player player) {
itemStack = player.getInventory().getItemInMainHand().clone();
itemStack.setAmount(1);
} else {
context.getSender().sendMessage(main.parse(
"<error>This must be run by a player."
));
return;
}
} else {
if (materialOrHand.material.equalsIgnoreCase("any")) {
context.getSender().sendMessage(main.parse(
"<error>You cannot use <highlight>'any'</highlight> here!"
));
return;
}
itemStack = main.getItemsManager().getItemStack(materialOrHand.material);
final ItemStack itemStack;
if (materialOrHand.material.equalsIgnoreCase("any")) {
context.getSender().sendMessage(main.parse(
"<error>You cannot use <highlight>'any'</highlight> here!"
));
return;
}
itemStack = main.getItemsManager().getItemStack(materialOrHand.material);

NQItem nqItem = new NQItem(main, itemName, itemStack);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import cloud.commandframework.exceptions.parsing.ParserException;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import rocks.gravili.notquests.paper.NotQuests;
Expand Down Expand Up @@ -162,9 +163,14 @@ public MaterialParser(
final MaterialOrHand materialOrHand = new MaterialOrHand();

if (input.equalsIgnoreCase("hand")) {
materialOrHand.hand = true;
inputQueue.remove();
return ArgumentParseResult.success(materialOrHand);
if (commandContext.getSender() instanceof final Player player) {
materialOrHand.material = player.getInventory().getItemInMainHand().getType().name();
inputQueue.remove();
return ArgumentParseResult.success(materialOrHand);
} else {
return ArgumentParseResult.failure(new MaterialOrHandArgument.MaterialParseException(input, commandContext));
}

} else if (input.equalsIgnoreCase("any")) {
materialOrHand.material = "any";
inputQueue.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@

public class MaterialOrHand {
public String material;
public boolean hand = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,14 @@ public static void handleCommands(NotQuests main, PaperCommandManager<CommandSen
final int itemRewardAmount = context.get("amount");

ItemStack itemStack;
if (materialOrHand.hand) { //"hand"
if (context.getSender() instanceof Player player) {
itemStack = player.getInventory().getItemInMainHand().clone();
itemStack.setAmount(itemRewardAmount);
} else {
context.getSender().sendMessage(main.parse(
"<error>This must be run by a player."
));
return;
}
} else {
if (materialOrHand.material.equalsIgnoreCase("any")) {
context.getSender().sendMessage(main.parse(
"<error>You cannot use <highlight>'any'</highlight> here!"
));
return;
}
itemStack = main.getItemsManager().getItemStack(materialOrHand.material);
itemStack.setAmount(itemRewardAmount);
if (materialOrHand.material.equalsIgnoreCase("any")) {
context.getSender().sendMessage(main.parse(
"<error>You cannot use <highlight>'any'</highlight> here!"
));
return;
}
itemStack = main.getItemsManager().getItemStack(materialOrHand.material);
itemStack.setAmount(itemRewardAmount);

GiveItemAction giveItemAction = new GiveItemAction(main);
if(main.getItemsManager().getItem(materialOrHand.material) != null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,10 @@ public static void handleCommands(NotQuests main, PaperCommandManager<CommandSen

final MaterialOrHand materialOrHand = context.get("material");
final String materialToBreak;
if (materialOrHand.hand) { //"hand"
if (context.getSender() instanceof Player player) {
materialToBreak = player.getInventory().getItemInMainHand().getType().name();
} else {
context.getSender().sendMessage(main.parse(
"<error>This must be run by a player."
));
return;
}
} else {
if (!materialOrHand.material.equalsIgnoreCase("any")) {
materialToBreak = main.getItemsManager().getMaterial(materialOrHand.material).name();
}else{
materialToBreak = "any";
}

if (!materialOrHand.material.equalsIgnoreCase("any")) {
materialToBreak = main.getItemsManager().getMaterial(materialOrHand.material).name();
}else{
materialToBreak = "any";
}

BreakBlocksObjective breakBlocksObjective = new BreakBlocksObjective(main);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,11 @@ public static void handleCommands(NotQuests main, PaperCommandManager<CommandSen

final MaterialOrHand materialOrHand = context.get("material");
ItemStack itemToCollect;
if (materialOrHand.hand) { //"hand"
if (context.getSender() instanceof Player player) {
itemToCollect = player.getInventory().getItemInMainHand();
} else {
context.getSender().sendMessage(main.parse(
"<error>This must be run by a player."
));
return;
}
if (materialOrHand.material.equalsIgnoreCase("any")) {
collectAnyItem = true;
itemToCollect = null;
} else {
if (materialOrHand.material.equalsIgnoreCase("any")) {
collectAnyItem = true;
itemToCollect = null;
} else {
itemToCollect = main.getItemsManager().getItemStack(materialOrHand.material);
}
itemToCollect = main.getItemsManager().getItemStack(materialOrHand.material);
}

CollectItemsObjective collectItemsObjective = new CollectItemsObjective(main);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,11 @@ public static void handleCommands(NotQuests main, PaperCommandManager<CommandSen

final MaterialOrHand materialOrHand = context.get("material");
ItemStack itemToConsume;
if (materialOrHand.hand) { //"hand"
if (context.getSender() instanceof Player player) {
itemToConsume = player.getInventory().getItemInMainHand();
} else {
context.getSender().sendMessage(main.parse(
"<error>This must be run by a player."
));
return;
}
if (materialOrHand.material.equalsIgnoreCase("any")) {
consumeAnyItem = true;
itemToConsume = null;
} else {
if (materialOrHand.material.equalsIgnoreCase("any")) {
consumeAnyItem = true;
itemToConsume = null;
} else {
itemToConsume = main.getItemsManager().getItemStack(materialOrHand.material);
}
itemToConsume = main.getItemsManager().getItemStack(materialOrHand.material);
}

ConsumeItemsObjective consumeItemsObjective = new ConsumeItemsObjective(main);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,11 @@ public static void handleCommands(NotQuests main, PaperCommandManager<CommandSen
boolean craftAnyItem = false;
final MaterialOrHand materialOrHand = context.get("material");
ItemStack itemToCraft;
if (materialOrHand.hand) { //"hand"
if (context.getSender() instanceof Player player) {
itemToCraft = player.getInventory().getItemInMainHand();
} else {
context.getSender().sendMessage(main.parse(
"<error>This must be run by a player."
));
return;
}
if (materialOrHand.material.equalsIgnoreCase("any")) {
craftAnyItem = true;
itemToCraft = null;
} else {
if (materialOrHand.material.equalsIgnoreCase("any")) {
craftAnyItem = true;
itemToCraft = null;
} else {
itemToCraft = main.getItemsManager().getItemStack(materialOrHand.material);
}
itemToCraft = main.getItemsManager().getItemStack(materialOrHand.material);
}

CraftItemsObjective craftItemsObjective = new CraftItemsObjective(main);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,11 @@ public static void handleCommands(NotQuests main, PaperCommandManager<CommandSen

final MaterialOrHand materialOrHand = context.get("material");
ItemStack itemToDeliver;
if (materialOrHand.hand) { //"hand"
if (context.getSender() instanceof Player player) {
itemToDeliver = player.getInventory().getItemInMainHand();
} else {
context.getSender().sendMessage(main.parse(
"<error>This must be run by a player."
));
return;
}
if (materialOrHand.material.equalsIgnoreCase("any")) {
deliverAnyItem = true;
itemToDeliver = null;
} else {
if (materialOrHand.material.equalsIgnoreCase("any")) {
deliverAnyItem = true;
itemToDeliver = null;
} else {
itemToDeliver = main.getItemsManager().getItemStack(materialOrHand.material);
}
itemToDeliver = main.getItemsManager().getItemStack(materialOrHand.material);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,10 @@ public static void handleCommands(NotQuests main, PaperCommandManager<CommandSen
final MaterialOrHand materialOrHand = context.get("material");
final String materialToPlace;

if (materialOrHand.hand) { //"hand"
if (context.getSender() instanceof Player player) {
materialToPlace = player.getInventory().getItemInMainHand().getType().name();
} else {
context.getSender().sendMessage(main.parse(
"<error>This must be run by a player."
));
return;
}
} else {
if (!materialOrHand.material.equalsIgnoreCase("any")) {
materialToPlace = main.getItemsManager().getMaterial(materialOrHand.material).name();
}else{
materialToPlace = "any";
}
if (!materialOrHand.material.equalsIgnoreCase("any")) {
materialToPlace = main.getItemsManager().getMaterial(materialOrHand.material).name();
}else{
materialToPlace = "any";
}

PlaceBlocksObjective placeBlocksObjective = new PlaceBlocksObjective(main);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,11 @@ public static void handleCommands(NotQuests main, PaperCommandManager<CommandSen

final MaterialOrHand materialOrHand = context.get("material");
ItemStack itemToSmelt;
if (materialOrHand.hand) { //"hand"
if (context.getSender() instanceof Player player) {
itemToSmelt = player.getInventory().getItemInMainHand();
} else {
context.getSender().sendMessage(main.parse(
"<error>This must be run by a player."
));
return;
}
if (materialOrHand.material.equalsIgnoreCase("any")) {
smeltAnyItem = true;
itemToSmelt = null;
} else {
if (materialOrHand.material.equalsIgnoreCase("any")) {
smeltAnyItem = true;
itemToSmelt = null;
} else {
itemToSmelt = main.getItemsManager().getItemStack(materialOrHand.material);
}
itemToSmelt = main.getItemsManager().getItemStack(materialOrHand.material);
}

SmeltObjective smeltObjective = new SmeltObjective(main);
Expand Down

0 comments on commit 1c6562a

Please sign in to comment.