0
0
mirror of https://github.com/Wurst-Imperium/Wurst7.git synced 2024-09-20 01:12:13 +02:00

Fix PotionCmd

This commit is contained in:
Alexander01998 2024-03-27 13:41:01 +01:00
parent 7297743b20
commit 877576494e

View File

@ -7,15 +7,15 @@
*/ */
package net.wurstclient.commands; package net.wurstclient.commands;
import java.util.List; import java.util.ArrayList;
import net.minecraft.entity.effect.StatusEffect; import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance; import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.PotionItem; import net.minecraft.item.PotionItem;
import net.minecraft.nbt.NbtCompound; import net.minecraft.potion.Potion;
import net.minecraft.nbt.NbtList;
import net.minecraft.potion.PotionUtil; import net.minecraft.potion.PotionUtil;
import net.minecraft.potion.Potions;
import net.minecraft.registry.Registries; import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException; import net.minecraft.util.InvalidIdentifierException;
@ -60,15 +60,15 @@ public final class PotionCmd extends Command
throw new CmdSyntaxError(); throw new CmdSyntaxError();
// get effects to start with // get effects to start with
NbtList effects; ArrayList<StatusEffectInstance> effects;
switch(args[0].toLowerCase()) switch(args[0].toLowerCase())
{ {
case "add": case "add":
effects = convertEffectsToNbt(stack); effects = new ArrayList<>(PotionUtil.getCustomPotionEffects(stack));
break; break;
case "set": case "set":
effects = new NbtList(); effects = new ArrayList<>();
break; break;
default: default:
@ -78,97 +78,74 @@ public final class PotionCmd extends Command
// add new effects // add new effects
for(int i = 0; i < (args.length - 1) / 3; i++) for(int i = 0; i < (args.length - 1) / 3; i++)
{ {
NbtCompound effect = new NbtCompound(); StatusEffect effect = parseEffect(args[1 + i * 3]);
int amplifier = parseInt(args[2 + i * 3]) - 1;
int duration = parseInt(args[3 + i * 3]) * 20;
effect.putInt("id", parseEffectId(args[1 + i * 3])); effects.add(new StatusEffectInstance(effect, duration, amplifier));
effect.putInt("amplifier", parseInt(args[2 + i * 3]) - 1);
effect.putInt("duration", parseInt(args[3 + i * 3]) * 20);
effects.add(effect);
} }
NbtCompound nbt = new NbtCompound(); setCustomPotionEffects(stack, effects);
nbt.put("custom_potion_effects", effects);
stack.setNbt(nbt);
ChatUtils.message("Potion modified."); ChatUtils.message("Potion modified.");
} }
private NbtList convertEffectsToNbt(ItemStack stack)
{
NbtList nbt = new NbtList();
List<StatusEffectInstance> effects =
PotionUtil.getCustomPotionEffects(stack);
for(StatusEffectInstance effect : effects)
{
NbtCompound tag = new NbtCompound();
int id = Registries.STATUS_EFFECT.getRawId(effect.getEffectType());
tag.putInt("id", id);
tag.putInt("amplifier", effect.getAmplifier());
tag.putInt("duration", effect.getDuration());
nbt.add(tag);
}
return nbt;
}
private void remove(ItemStack stack, String[] args) throws CmdSyntaxError private void remove(ItemStack stack, String[] args) throws CmdSyntaxError
{ {
if(args.length != 2) if(args.length != 2)
throw new CmdSyntaxError(); throw new CmdSyntaxError();
int id = parseEffectId(args[1]); StatusEffect targetEffect = parseEffect(args[1]);
List<StatusEffectInstance> oldEffects = Potion oldPotion = PotionUtil.getPotion(stack);
PotionUtil.getCustomPotionEffects(stack); boolean mainPotionContainsTargetEffect = oldPotion.getEffects().stream()
.anyMatch(effect -> effect.getEffectType() == targetEffect);
NbtList newEffects = new NbtList(); ArrayList<StatusEffectInstance> newEffects = new ArrayList<>();
for(StatusEffectInstance oldEffect : oldEffects) if(mainPotionContainsTargetEffect)
{ PotionUtil.getPotionEffects(stack).forEach(newEffects::add);
int oldId = else
Registries.STATUS_EFFECT.getRawId(oldEffect.getEffectType()); PotionUtil.getCustomPotionEffects(stack).forEach(newEffects::add);
newEffects.removeIf(effect -> effect.getEffectType() == targetEffect);
if(oldId == id) Potion newPotion =
continue; mainPotionContainsTargetEffect ? Potions.EMPTY : oldPotion;
NbtCompound effect = new NbtCompound(); PotionUtil.setPotion(stack, newPotion);
effect.putInt("id", oldId); setCustomPotionEffects(stack, newEffects);
effect.putInt("amplifier", oldEffect.getAmplifier());
effect.putInt("duration", oldEffect.getDuration());
newEffects.add(effect);
}
NbtCompound nbt = new NbtCompound();
nbt.put("custom_potion_effects", newEffects);
stack.setNbt(nbt);
ChatUtils.message("Effect removed."); ChatUtils.message("Effect removed.");
} }
private int parseEffectId(String input) throws CmdSyntaxError private StatusEffect parseEffect(String input) throws CmdSyntaxError
{ {
int id = 0; StatusEffect effect;
if(MathUtils.isInteger(input)) if(MathUtils.isInteger(input))
id = Integer.parseInt(input); effect = Registries.STATUS_EFFECT.get(Integer.parseInt(input));
else else
try try
{ {
Identifier identifier = new Identifier(input); Identifier identifier = new Identifier(input);
StatusEffect effect = Registries.STATUS_EFFECT.get(identifier); effect = Registries.STATUS_EFFECT.get(identifier);
id = Registries.STATUS_EFFECT.getRawId(effect);
}catch(InvalidIdentifierException e) }catch(InvalidIdentifierException e)
{ {
throw new CmdSyntaxError("Invalid effect: " + input); throw new CmdSyntaxError("Invalid effect: " + input);
} }
if(id < 1) if(effect == null)
throw new CmdSyntaxError(); throw new CmdSyntaxError("Invalid effect: " + input);
return id; return Registries.STATUS_EFFECT.getEntry(effect).value();
}
private void setCustomPotionEffects(ItemStack stack,
ArrayList<StatusEffectInstance> effects)
{
// PotionUtil doesn't remove effects when passing an empty list to it
if(effects.isEmpty())
stack.removeSubNbt("custom_potion_effects");
else
PotionUtil.setCustomPotionEffects(stack, effects);
} }
private int parseInt(String s) throws CmdSyntaxError private int parseInt(String s) throws CmdSyntaxError