Skip to content

Commit

Permalink
Update to 24w09a
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Mar 5, 2024
1 parent 6b3485c commit 19e311d
Show file tree
Hide file tree
Showing 23 changed files with 248 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ public void render(DrawContext context, int mouseX, int mouseY,
float partialTicks)
{
MatrixStack matrixStack = context.getMatrices();
renderBackground(context, mouseX, mouseY, partialTicks);

listGui.render(context, mouseX, mouseY, partialTicks);

matrixStack.push();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public void render(DrawContext context, int mouseX, int mouseY,
float partialTicks)
{
MatrixStack matrixStack = context.getMatrices();
renderBackground(context, mouseX, mouseY, partialTicks);

listGui.render(context, mouseX, mouseY, partialTicks);

context.drawCenteredTextWithShadow(client.textRenderer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public void render(DrawContext context, int mouseX, int mouseY,
float partialTicks)
{
MatrixStack matrixStack = context.getMatrices();
renderBackgroundTexture(context);
renderBackground(context, mouseX, mouseY, partialTicks);

matrixStack.push();
matrixStack.translate(0, 0, 300);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public void render(DrawContext context, int mouseX, int mouseY,
float partialTicks)
{
MatrixStack matrixStack = context.getMatrices();
renderBackground(context, mouseX, mouseY, partialTicks);

listGui.render(context, mouseX, mouseY, partialTicks);

matrixStack.push();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public void render(DrawContext context, int mouseX, int mouseY,
float partialTicks)
{
MatrixStack matrixStack = context.getMatrices();
renderBackground(context, mouseX, mouseY, partialTicks);

listGui.render(context, mouseX, mouseY, partialTicks);

context.drawCenteredTextWithShadow(client.textRenderer,
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/net/wurstclient/commands/AuthorCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/
package net.wurstclient.commands;

import net.minecraft.item.Item;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.WrittenBookContentComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtString;
import net.wurstclient.command.CmdError;
import net.wurstclient.command.CmdException;
import net.wurstclient.command.CmdSyntaxError;
Expand All @@ -33,15 +33,20 @@ public void call(String[] args) throws CmdException
if(!MC.player.getAbilities().creativeMode)
throw new CmdError("Creative mode only.");

ItemStack heldItem = MC.player.getInventory().getMainHandStack();
int heldItemID = Item.getRawId(heldItem.getItem());
int writtenBookID = Item.getRawId(Items.WRITTEN_BOOK);

if(heldItemID != writtenBookID)
ItemStack heldStack = MC.player.getInventory().getMainHandStack();
if(!heldStack.isOf(Items.WRITTEN_BOOK))
throw new CmdError(
"You must hold a written book in your main hand.");

WrittenBookContentComponent oldData = heldStack.getComponents()
.get(DataComponentTypes.WRITTEN_BOOK_CONTENT);
if(oldData == null)
throw new CmdError("Can't find book data.");

String author = String.join(" ", args);
heldItem.setSubNbt("author", NbtString.of(author));
WrittenBookContentComponent newData =
new WrittenBookContentComponent(oldData.title(), author,
oldData.generation(), oldData.pages(), oldData.resolved());
heldStack.set(DataComponentTypes.WRITTEN_BOOK_CONTENT, newData);
}
}
3 changes: 1 addition & 2 deletions src/main/java/net/wurstclient/commands/EnchantCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import net.wurstclient.command.CmdSyntaxError;
import net.wurstclient.command.Command;
import net.wurstclient.util.ChatUtils;
import net.wurstclient.util.ItemUtils;

public final class EnchantCmd extends Command
{
Expand Down Expand Up @@ -71,7 +70,7 @@ private void enchant(ItemStack stack, int level)
continue;
}

ItemUtils.addEnchantment(stack, enchantment, level);
stack.addEnchantment(enchantment, level);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/wurstclient/commands/GiveCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import com.mojang.brigadier.exceptions.CommandSyntaxException;

import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
Expand Down Expand Up @@ -83,7 +85,7 @@ public void call(String[] args) throws CmdException
try
{
NbtCompound tag = StringNbtReader.parse(nbt);
stack.setNbt(tag);
NbtComponent.set(DataComponentTypes.CUSTOM_DATA, stack, tag);

}catch(CommandSyntaxException e)
{
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/net/wurstclient/commands/ModifyCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;

import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.StringNbtReader;
Expand Down Expand Up @@ -77,16 +79,19 @@ public void call(String[] args) throws CmdException

private void add(ItemStack stack, String[] args) throws CmdError
{
String nbt = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
nbt = nbt.replace("$", "\u00a7").replace("\u00a7\u00a7", "$");
String nbtString =
String.join(" ", Arrays.copyOfRange(args, 1, args.length))
.replace("$", "\u00a7").replace("\u00a7\u00a7", "$");

if(!stack.hasNbt())
stack.setNbt(new NbtCompound());
NbtCompound itemNbt = stack
.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT)
.copyNbt();

try
{
NbtCompound tag = StringNbtReader.parse(nbt);
stack.getNbt().copyFrom(tag);
NbtCompound parsedNbt = StringNbtReader.parse(nbtString);
itemNbt.copyFrom(parsedNbt);
stack.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(itemNbt));

}catch(CommandSyntaxException e)
{
Expand All @@ -103,7 +108,7 @@ private void set(ItemStack stack, String[] args) throws CmdError
try
{
NbtCompound tag = StringNbtReader.parse(nbt);
stack.setNbt(tag);
stack.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(tag));

}catch(CommandSyntaxException e)
{
Expand All @@ -117,12 +122,15 @@ private void remove(ItemStack stack, String[] args) throws CmdException
if(args.length > 2)
throw new CmdSyntaxError();

NbtPath path = parseNbtPath(stack.getNbt(), args[1]);
NbtPath path = parseNbtPath(stack
.getOrDefault(DataComponentTypes.CUSTOM_DATA, NbtComponent.DEFAULT)
.copyNbt(), args[1]);

if(path == null)
throw new CmdError("The path does not exist.");

path.base.remove(path.key);
stack.set(DataComponentTypes.CUSTOM_DATA, NbtComponent.of(path.base));
}

private NbtPath parseNbtPath(NbtCompound tag, String path)
Expand Down
108 changes: 48 additions & 60 deletions src/main/java/net/wurstclient/commands/PotionCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
*/
package net.wurstclient.commands;

import java.util.List;
import java.util.ArrayList;
import java.util.Optional;

import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.PotionContentsComponent;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.item.ItemStack;
import net.minecraft.item.PotionItem;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.potion.Potion;
import net.minecraft.registry.Registries;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;
import net.wurstclient.command.CmdError;
Expand Down Expand Up @@ -58,16 +61,23 @@ public void call(String[] args) throws CmdException
if((args.length - 1) % 3 != 0)
throw new CmdSyntaxError();

PotionContentsComponent oldContents = stack.getComponents()
.getOrDefault(DataComponentTypes.POTION_CONTENTS,
PotionContentsComponent.DEFAULT);

// get effects to start with
NbtList effects;
ArrayList<StatusEffectInstance> effects;
Optional<RegistryEntry<Potion>> potion;
switch(args[0].toLowerCase())
{
case "add":
effects = convertEffectsToNbt(stack);
effects = new ArrayList<>(oldContents.customEffects());
potion = oldContents.potion();
break;

case "set":
effects = new NbtList();
effects = new ArrayList<>();
potion = Optional.empty();
break;

default:
Expand All @@ -77,76 +87,54 @@ public void call(String[] args) throws CmdException
// add new effects
for(int i = 0; i < (args.length - 1) / 3; i++)
{
NbtCompound effect = new NbtCompound();

effect.putString("id", parseEffectId(args[1 + i * 3]));
effect.putInt("amplifier", parseInt(args[2 + i * 3]) - 1);
effect.putInt("duration", parseInt(args[3 + i * 3]) * 20);
RegistryEntry<StatusEffect> effect = parseEffect(args[1 + i * 3]);
int amplifier = parseInt(args[2 + i * 3]) - 1;
int duration = parseInt(args[3 + i * 3]) * 20;

effects.add(effect);
effects.add(new StatusEffectInstance(effect, duration, amplifier));
}

NbtCompound nbt = new NbtCompound();
nbt.put("custom_potion_effects", effects);
stack.setNbt(nbt);
stack.set(DataComponentTypes.POTION_CONTENTS,
new PotionContentsComponent(potion, oldContents.customColor(),
effects));
ChatUtils.message("Potion modified.");
}

private NbtList convertEffectsToNbt(ItemStack stack)
{
NbtList nbt = new NbtList();
List<StatusEffectInstance> effects =
PotionContentsComponent.getCustomPotionEffects(stack);

for(StatusEffectInstance effect : effects)
{
NbtCompound tag = new NbtCompound();

String id = Registries.STATUS_EFFECT
.getId(effect.getEffectType().value()).toString();
tag.putString("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
{
if(args.length != 2)
throw new CmdSyntaxError();

String id = parseEffectId(args[1]);
RegistryEntry<StatusEffect> targetEffect = parseEffect(args[1]);

List<StatusEffectInstance> oldEffects =
PotionContentsComponent.getCustomPotionEffects(stack);
PotionContentsComponent oldContents = stack.getComponents()
.getOrDefault(DataComponentTypes.POTION_CONTENTS,
PotionContentsComponent.DEFAULT);

NbtList newEffects = new NbtList();
for(StatusEffectInstance oldEffect : oldEffects)
{
String oldId = Registries.STATUS_EFFECT
.getId(oldEffect.getEffectType().value()).toString();

if(oldId.equals(id))
continue;

NbtCompound effect = new NbtCompound();
effect.putString("id", oldId);
effect.putInt("amplifier", oldEffect.getAmplifier());
effect.putInt("duration", oldEffect.getDuration());
newEffects.add(effect);
}
boolean mainPotionContainsTargetEffect =
oldContents.potion().isPresent()
&& oldContents.potion().get().value().getEffects().stream()
.anyMatch(effect -> effect.getEffectType() == targetEffect);

ArrayList<StatusEffectInstance> newEffects = new ArrayList<>();
if(mainPotionContainsTargetEffect)
oldContents.getEffects().forEach(newEffects::add);
else
oldContents.customEffects().forEach(newEffects::add);
newEffects.removeIf(effect -> effect.getEffectType() == targetEffect);

Optional<RegistryEntry<Potion>> newPotion =
mainPotionContainsTargetEffect ? Optional.empty()
: oldContents.potion();
stack.set(DataComponentTypes.POTION_CONTENTS,
new PotionContentsComponent(newPotion, oldContents.customColor(),
newEffects));

NbtCompound nbt = new NbtCompound();
nbt.put("custom_potion_effects", newEffects);
stack.setNbt(nbt);
ChatUtils.message("Effect removed.");
}

private String parseEffectId(String input) throws CmdSyntaxError
private RegistryEntry<StatusEffect> parseEffect(String input)
throws CmdSyntaxError
{
StatusEffect effect;

Expand All @@ -166,7 +154,7 @@ private String parseEffectId(String input) throws CmdSyntaxError
if(effect == null)
throw new CmdSyntaxError("Invalid effect: " + input);

return Registries.STATUS_EFFECT.getId(effect).toString();
return Registries.STATUS_EFFECT.getEntry(effect);
}

private int parseInt(String s) throws CmdSyntaxError
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/net/wurstclient/commands/RenameCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package net.wurstclient.commands;

import net.minecraft.component.DataComponentTypes;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.wurstclient.command.CmdError;
Expand Down Expand Up @@ -38,12 +39,12 @@ public void call(String[] args) throws CmdException
message += " " + args[i];

message = message.replace("$", "\u00a7").replace("\u00a7\u00a7", "$");
ItemStack item = MC.player.getInventory().getMainHandStack();
ItemStack stack = MC.player.getInventory().getMainHandStack();

if(item == null)
if(stack == null)
throw new CmdError("There is no item in your hand.");

item.setCustomName(Text.literal(message));
ChatUtils.message("Renamed item to \"" + message + "\u00a7r\".");
stack.set(DataComponentTypes.CUSTOM_NAME, Text.literal(message));
ChatUtils.message("Renamed item to \"\u00a7o" + message + "\u00a7r\".");
}
}
Loading

0 comments on commit 19e311d

Please sign in to comment.