Skip to content

Commit 79fcc78

Browse files
committed
Added /potion add and /potion remove commands
1 parent 20549e2 commit 79fcc78

3 files changed

Lines changed: 62 additions & 30 deletions

File tree

src/main/java/clashsoft/brewingapi/command/CommandPotion.java

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package clashsoft.brewingapi.command;
22

33
import java.util.ArrayList;
4-
import java.util.Arrays;
54
import java.util.List;
65

76
import clashsoft.brewingapi.BrewingAPI;
7+
import clashsoft.brewingapi.potion.type.IPotionType;
88
import clashsoft.brewingapi.potion.type.PotionType;
99

1010
import net.minecraft.command.CommandBase;
11+
import net.minecraft.command.CommandNotFoundException;
1112
import net.minecraft.command.ICommandSender;
1213
import net.minecraft.command.NumberInvalidException;
1314
import net.minecraft.entity.item.EntityItem;
14-
import net.minecraft.entity.player.EntityPlayerMP;
15+
import net.minecraft.entity.player.EntityPlayer;
1516
import net.minecraft.item.ItemStack;
1617
import net.minecraft.potion.Potion;
1718
import net.minecraft.potion.PotionEffect;
1819
import net.minecraft.server.MinecraftServer;
19-
import net.minecraft.util.ChatComponentText;
2020

2121
public class CommandPotion extends CommandBase
2222
{
@@ -56,56 +56,82 @@ public String getCommandUsage(ICommandSender sender)
5656
@Override
5757
public void processCommand(ICommandSender sender, String[] args)
5858
{
59-
if ("give".equals(args[0]))
59+
if (!"give".equals(args[0]) && !"add".equals(args[0]) && !"remove".equals(args[0]))
60+
throw new CommandNotFoundException("commans.potion.notFound");
61+
62+
int index = 1;
63+
int id = this.getPotionID(args[index]);
64+
65+
if (id == -1)
6066
{
61-
this.givePotion(sender, Arrays.copyOfRange(args, 2, args.length));
67+
sender = sender.getEntityWorld().getPlayerEntityByName(args[index]);
68+
69+
index++;
70+
id = this.getPotionID(args[index]);
6271
}
63-
}
64-
65-
public void givePotion(ICommandSender sender, String[] args)
66-
{
67-
int id = this.getPotionID(args[0]);
72+
String name = sender.getCommandSenderName();
73+
EntityPlayer player = getCommandSenderAsPlayer(sender);
74+
6875
int duration = 600;
6976
int amplifier = 0;
7077
boolean splash = false;
7178

7279
if (id < 0 || id >= Potion.potionTypes.length || Potion.potionTypes[id] == null)
7380
{
74-
throw new NumberInvalidException("commands.effect.notFound", new Object[] { Integer.valueOf(id) });
81+
throw new NumberInvalidException("commands.potion.effect.notFound", new Object[] { Integer.valueOf(id) });
7582
}
7683

77-
if (args.length >= 2)
78-
{
79-
duration = parseIntBounded(sender, args[1], 0, 1000000) * 20;
80-
}
8184
if (Potion.potionTypes[id].isInstant())
8285
{
8386
duration = 1;
8487
}
88+
else if (args.length >= 2)
89+
{
90+
duration = parseIntBounded(sender, args[index + 1], 0, 1000000) * 20;
91+
}
92+
8593
if (args.length >= 3)
8694
{
87-
amplifier = parseIntBounded(sender, args[2], 1, 256) - 1;
95+
amplifier = parseIntBounded(sender, args[index + 2], 1, 256) - 1;
8896
}
8997
if (args.length >= 4)
9098
{
91-
splash = Boolean.parseBoolean(args[3]);
99+
splash = Boolean.parseBoolean(args[index + 3]);
92100
}
93101

94102
PotionEffect potioneffect = new PotionEffect(id, duration, amplifier);
95-
ItemStack stack = new ItemStack(BrewingAPI.potion2, 1, splash ? 2 : 1);
96-
PotionType.getFromEffect(potioneffect).apply(stack);
97-
98-
EntityItem entityitem = ((EntityPlayerMP) sender).dropPlayerItemWithRandomChoice(stack, false);
99-
entityitem.delayBeforeCanPickup = 0;
100-
String name = sender.getCommandSenderName();
103+
IPotionType potionType = PotionType.getFromEffect(potioneffect);
101104

102-
if (splash)
105+
if ("give".equals(args[0]))
106+
{
107+
ItemStack stack = new ItemStack(BrewingAPI.potion2, 1, splash ? 2 : 1);
108+
potionType.apply(stack);
109+
110+
EntityItem entityitem = player.dropPlayerItemWithRandomChoice(stack, false);
111+
entityitem.delayBeforeCanPickup = 0;
112+
113+
if (splash)
114+
{
115+
notifyAdmins(sender, "commands.potion.give.splash.success", potioneffect.getEffectName(), id, duration, amplifier, name);
116+
}
117+
else
118+
{
119+
notifyAdmins(sender, "commands.potion.give.success", potioneffect.getEffectName(), id, duration, amplifier, name);
120+
}
121+
}
122+
else if ("remove".equals(args[0]))
103123
{
104-
notifyAdmins(sender, "commands.potion.give.splash.success", new Object[] { new ChatComponentText(potioneffect.getEffectName()), Integer.valueOf(id), Integer.valueOf(duration), Integer.valueOf(amplifier), name });
124+
ItemStack stack = player.getHeldItem();
125+
potionType.remove(stack);
126+
127+
notifyAdmins(sender, "command.potion.remove.success", potioneffect.getEffectName(), id, duration, amplifier, name);
105128
}
106-
else
129+
else if ("add".equals(args[0]))
107130
{
108-
notifyAdmins(sender, "commands.potion.give.success", new Object[] { new ChatComponentText(potioneffect.getEffectName()), Integer.valueOf(id), Integer.valueOf(duration), Integer.valueOf(amplifier), name });
131+
ItemStack stack = player.getHeldItem();
132+
potionType.apply(stack);
133+
134+
notifyAdmins(sender, "command.potion.remove.success", potioneffect.getEffectName(), id, duration, amplifier, name);
109135
}
110136
}
111137

@@ -132,7 +158,7 @@ public List addTabCompletionOptions(ICommandSender sender, String[] args)
132158
{
133159
if (args.length == 1)
134160
{
135-
return getListOfStringsMatchingLastWord(args, "give");
161+
return getListOfStringsMatchingLastWord(args, "give", "add", "remove");
136162
}
137163
else if (args.length == 2)
138164
{

src/main/resources/assets/brewingapi/lang/de_DE.lang

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ itemGroup.mixedpotions=Vermischte Tränke
22

33
commands.potion.usage=/potion <sub-command> <player> [args...]
44
commands.potion.give.usage=/potion give <player> <effect> [seconds] [amplifier] [splash]
5-
commands.potion.give.success=Trank (%1$s (ID %2$d) level %4$d for %3$d seconds) an %5$s gegeben.
6-
commands.potion.give.success.splash=Werfbaren Trank (%1$s (ID %2$d) level %4$d for %3$d seconds) an %5$s gegeben.
5+
commands.potion.give.success=Trank (%1$s (ID %2$d) Level %4$d für %3$d Sekunden) an %5$s gegeben.
6+
commands.potion.give.success.splash=Werfbaren Trank (%1$s (ID %2$d) Level %4$d für %3$d Sekunden) an %5$s gegeben.
7+
commands.potion.add.success=Trankeffekt (%1$s (ID %2$d) Level %4$d für %3$d Sekunden) an %5$s gegeben.
8+
commands.potion.remove.success=Trankeffekt (%1$s (ID %2$d) Level %4$d für %3$d Sekunden) von %5$s entfernt.
9+
commands.potion.effect.notFound=Effekt konnte nicht gefunden werden.
710

811
potion.regeneration.description=Regeneriert Leben.
912
potion.moveSpeed.description=Erlaubt es, sich schneller zu bewegen.

src/main/resources/assets/brewingapi/lang/en_US.lang

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ commands.potion.usage=/potion <sub-command> <player> [args...]
44
commands.potion.give.usage=/potion give <player> <effect> [seconds] [amplifier] [splash]
55
commands.potion.give.success=Given Potion (%1$s (ID %2$d) level %4$d for %3$d seconds) to %5$s.
66
commands.potion.give.success.splash=Given Splash Potion (%1$s (ID %2$d) level %4$d for %3$d seconds) to %5$s.
7+
commands.potion.add.success=Added Potion (%1$s (ID %2$d) level %4$d for %3$d seconds) to %5$s.
8+
commands.potion.remove.success=Removed Potion (%1$s (ID %2$d) level %4$d for %3$d seconds) from %5$s.
9+
commands.potion.effect.notFound=Effect could not be found.
710

811
potion.goodeffects=Good Effects
912
potion.negativeEffects=Bad Effects

0 commit comments

Comments
 (0)