Skip to content

Commit

Permalink
feat: update to 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunekaer committed Jun 10, 2023
1 parent ce122db commit e37d046
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -56,13 +58,13 @@ private static int remove(CommandSourceStack source, int size, String filter) th
var removalCheck = RemovalPredicate.getFromName(filter).orElse(RemovalPredicate.JUST_ORES);
Predicate<BlockState> customCheck = null;
if (filter.startsWith("#")) {
customCheck = state -> state.is(TagKey.create(Registry.BLOCK_REGISTRY, new ResourceLocation(filter.replace("#", ""))));
customCheck = state -> state.is(TagKey.create(Registries.BLOCK, new ResourceLocation(filter.replace("#", ""))));
} else if(filter.contains(":")) {
customCheck = state -> Registry.BLOCK.getKey(state.getBlock()).toString().equalsIgnoreCase(filter);
customCheck = state -> BuiltInRegistries.BLOCK.getKey(state.getBlock()).toString().equalsIgnoreCase(filter);
}

ServerLevel level = source.getLevel();
source.sendSuccess(Component.translatable("commands.toolkit.remove.lagwarring"), true);
source.sendSuccess(() -> Component.translatable("commands.toolkit.remove.lagwarring"), true);

// We're removing 1 to make it so 1 = 0, 2 - 1, etc, this means we'll have correct radius 0 = 1x1, 1 = 3x3, 2 = 5x5
var range = size - 1;
Expand Down Expand Up @@ -98,33 +100,32 @@ private static void removeChunk(ServerLevel level, ChunkPos chunkPos, Predicate<
LevelChunk chunk = level.getChunk(chunkPos.x, chunkPos.z);
List<LevelChunkSection> sections = Arrays.stream(chunk.getSections()).filter(e -> !e.hasOnlyAir()).toList();

for (LevelChunkSection section : sections) {
for (int y = section.bottomBlockY(); y < section.bottomBlockY() + 16; y ++) {
// For people that don't know what bit shifting is, google it...
for (int x = chunkPos.x << 4; x < (chunkPos.x << 4) + (1 << 4); x ++) {
for (int z = chunkPos.z << 4; z < (chunkPos.z << 4) + (1 << 4); z ++) {
final BlockPos pos = new BlockPos(x, y, z);
final BlockState state = level.getBlockState(pos);

// Don't remove bedrock and skip air, it's a waste of computation
if (state.isAir() || state.getBlock() == Blocks.BEDROCK) {
continue;
}
for (int y = level.getMinBuildHeight(); y < level.getMaxBuildHeight(); y ++) {
// For people that don't know what bit shifting is, google it...
for (int x = chunkPos.x << 4; x < (chunkPos.x << 4) + (1 << 4); x ++) {
for (int z = chunkPos.z << 4; z < (chunkPos.z << 4) + (1 << 4); z ++) {
final BlockPos pos = new BlockPos(x, y, z);
final BlockState state = level.getBlockState(pos);

if (blockCheck.test(state)) {
continue;
}
// Don't remove bedrock and skip air, it's a waste of computation
if (state.isAir() || state.getBlock() == Blocks.BEDROCK) {
continue;
}

level.setBlock(pos, Blocks.AIR.defaultBlockState(), Block.UPDATE_ALL);
if (blockCheck.test(state)) {
continue;
}

level.setBlock(pos, Blocks.AIR.defaultBlockState(), Block.UPDATE_ALL);
}
}
}
}

private enum RemovalPredicate {
JUST_ORES(state -> state.is(ToolkitPlatform.getOresTag())),
ORES_AND_MODDED(state -> state.is(ToolkitPlatform.getOresTag()) && Registry.BLOCK.getKey(state.getBlock()).getNamespace().equals("minecraft"));
ORES_AND_MODDED(state -> state.is(ToolkitPlatform.getOresTag()) && BuiltInRegistries.BLOCK.getKey(state.getBlock()).getNamespace().equals("minecraft"));

public static final List<RemovalPredicate> VALUES = Arrays.asList(values());
public static final String[] NAMES = VALUES.stream().map(e -> e.toString().toLowerCase()).toArray(String[]::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.*;
import com.sunekaer.toolkit.utils.EnchantmentHacks;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.ItemEnchantmentArgument;
import net.minecraft.commands.arguments.ResourceArgument;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
Expand All @@ -21,61 +24,64 @@ public class CommandEnchant {
private static final DynamicCommandExceptionType ERROR_INCOMPATIBLE = new DynamicCommandExceptionType(object -> Component.translatable("commands.enchant.failed.incompatible", object));
private static final DynamicCommandExceptionType ERROR_NO_ITEM = new DynamicCommandExceptionType(object -> Component.translatable("commands.enchant.failed.itemless", object));

public static ArgumentBuilder<CommandSourceStack, ?> register() {
public static ArgumentBuilder<CommandSourceStack, ?> register(CommandBuildContext commandBuildContext) {
return Commands.literal("enchant")
.requires(cs -> cs.hasPermission(2))
.then(Commands.literal("add")
.then(Commands.argument("enchantment", ItemEnchantmentArgument.enchantment())
.then(Commands.argument("enchantment", ResourceArgument.resource(commandBuildContext, Registries.ENCHANTMENT))
.then(Commands.argument("level", IntegerArgumentType.integer(0))
.executes(context -> enchant(
context,
ItemEnchantmentArgument.getEnchantment(context, "enchantment"),
ResourceArgument.getEnchantment(context, "enchantment"),
IntegerArgumentType.getInteger(context, "level")
))
)
)
)
.then(Commands.literal("remove")
.then(Commands.argument("enchantment", ItemEnchantmentArgument.enchantment())
.executes(context -> removeEnchantment(context, ItemEnchantmentArgument.getEnchantment(context, "enchantment"))))
.then(Commands.argument("enchantment", ResourceArgument.resource(commandBuildContext, Registries.ENCHANTMENT))
.executes(context -> removeEnchantment(context, ResourceArgument.getEnchantment(context, "enchantment"))))
);
}

private static int enchant(CommandContext<CommandSourceStack> context, Enchantment enchantment, int level) throws CommandSyntaxException {
private static int enchant(CommandContext<CommandSourceStack> context, Holder.Reference<Enchantment> enchantment, int level) throws CommandSyntaxException {
CommandSourceStack source = context.getSource();

var enchant = enchantment.value();
var player = source.getPlayer();
var mainHandItem = getItemInHand(player);
if (mainHandItem == null) {
throw ERROR_MISSING_PLAYER.create();
}

if (!enchantment.canEnchant(mainHandItem) || !EnchantmentHelper.isEnchantmentCompatible(EnchantmentHelper.getEnchantments(mainHandItem).keySet(), enchantment)) {
if (!enchant.canEnchant(mainHandItem) || !EnchantmentHelper.isEnchantmentCompatible(EnchantmentHelper.getEnchantments(mainHandItem).keySet(), enchant)) {
throw ERROR_INCOMPATIBLE.create(mainHandItem.getItem().getName(mainHandItem).getString());
}

EnchantmentHacks.enchantItem(mainHandItem, enchantment, (short) level);
source.sendSuccess(Component.translatable("commands.toolkit.enchant.success", mainHandItem.getItem().getName(mainHandItem).getString(), enchantment.getFullname(level).getString()), false);
EnchantmentHacks.enchantItem(mainHandItem, enchant, (short) level);
source.sendSuccess(() -> Component.translatable("commands.toolkit.enchant.success", mainHandItem.getItem().getName(mainHandItem).getString(), enchant.getFullname(level).getString()), false);
return 1;
}

private static int removeEnchantment(CommandContext<CommandSourceStack> context, Enchantment enchantment) throws CommandSyntaxException {
private static int removeEnchantment(CommandContext<CommandSourceStack> context, Holder.Reference<Enchantment> enchantment) throws CommandSyntaxException {
var source = context.getSource();
var player = source.getPlayer();
var mainHandItem = getItemInHand(player);
if (mainHandItem == null) {
throw ERROR_MISSING_PLAYER.create();
}

if (!EnchantmentHelper.getEnchantments(mainHandItem).containsKey(enchantment)) {
throw ERROR_MISSING_ENCHANTMENT.create(mainHandItem.getItem().getName(mainHandItem).getString(), enchantment.getFullname(1));
var enchant = enchantment.value();

if (!EnchantmentHelper.getEnchantments(mainHandItem).containsKey(enchant)) {
throw ERROR_MISSING_ENCHANTMENT.create(mainHandItem.getItem().getName(mainHandItem).getString(), enchant.getFullname(1));
}

boolean success = EnchantmentHacks.removeEnchantment(mainHandItem, enchantment);
boolean success = EnchantmentHacks.removeEnchantment(mainHandItem, enchant);
if (success) {
source.sendSuccess(Component.translatable("commands.toolkit.remove_enchant.success", mainHandItem.getItem().getName(mainHandItem).getString(), enchantment.getFullname(1).getString()), false);
source.sendSuccess(() -> Component.translatable("commands.toolkit.remove_enchant.success", mainHandItem.getItem().getName(mainHandItem).getString(), enchant.getFullname(1).getString()), false);
} else {
source.sendFailure(Component.translatable("commands.toolkit.remove_enchant.failed", mainHandItem.getItem().getName(mainHandItem).getString(), enchantment.getFullname(1).getString()));
source.sendFailure(Component.translatable("commands.toolkit.remove_enchant.failed", mainHandItem.getItem().getName(mainHandItem).getString(), enchant.getFullname(1).getString()));
}
return success ? 1 : 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.TagKey;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

import java.util.ArrayList;
Expand All @@ -39,8 +41,8 @@ private static int getHand(CommandSourceStack source, Player player) {
return 0;
}

String itemName = Objects.requireNonNull(Registry.ITEM.getKey(stack.getItem())).toString();
List<TagKey> tags = new ArrayList<>(stack.getTags().collect(Collectors.toList()));
String itemName = Objects.requireNonNull(BuiltInRegistries.ITEM.getKey(stack.getItem())).toString();
List<TagKey<Item>> tags = stack.getTags().toList();

String withNBT = "";
CompoundTag nbt = stack.save(new CompoundTag());
Expand All @@ -51,14 +53,14 @@ private static int getHand(CommandSourceStack source, Player player) {
String combinedItemNBT = itemName + withNBT;


source.sendSuccess(Component.literal(combinedItemNBT).withStyle(ChatFormatting.YELLOW), true);
source.sendSuccess(() -> Component.literal(combinedItemNBT).withStyle(ChatFormatting.YELLOW), true);
Handler.CHANNEL.sendToPlayer((ServerPlayer) player, new SetCopy(combinedItemNBT));

if (!tags.isEmpty()) {
MutableComponent tagText = Component.literal("Tags: ");
MutableComponent tagsText = Component.literal(tags.stream().map(TagKey::toString).collect(Collectors.joining(", ")));
tagsText.withStyle(ChatFormatting.RED);
source.sendSuccess(tagText.append(tagsText), true);
source.sendSuccess(() -> tagText.append(tagsText), true);
}
return 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -35,7 +36,7 @@ private static int getHotbar(CommandSourceStack source, Player player) {
continue;
}

String itemName = Objects.requireNonNull(Registry.ITEM.getKey(stack.getItem())).toString();
String itemName = Objects.requireNonNull(BuiltInRegistries.ITEM.getKey(stack.getItem())).toString();

String withNBT = "";
CompoundTag nbt = stack.save(new CompoundTag());
Expand All @@ -46,7 +47,7 @@ private static int getHotbar(CommandSourceStack source, Player player) {
clipboard.append(itemName).append(withNBT).append(CommandUtils.NEW_LINE);
}

source.sendSuccess(Component.translatable("commands.toolkit.clipboard.copied"), true);
source.sendSuccess(() -> Component.translatable("commands.toolkit.clipboard.copied"), true);
Handler.CHANNEL.sendToPlayer((ServerPlayer) player, new SetCopy(clipboard.toString()));

return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -35,7 +36,7 @@ private static int getHotbar(CommandSourceStack source, Player player) {
continue;
}

String itemName = Objects.requireNonNull(Registry.ITEM.getKey(stack.getItem())).toString();
String itemName = Objects.requireNonNull(BuiltInRegistries.ITEM.getKey(stack.getItem())).toString();

String withNBT = "";
CompoundTag nbt = stack.save(new CompoundTag());
Expand All @@ -46,7 +47,7 @@ private static int getHotbar(CommandSourceStack source, Player player) {
clipboard.append(itemName).append(withNBT).append(CommandUtils.NEW_LINE);
}

source.sendSuccess(Component.translatable("commands.toolkit.clipboard.copied"), true);
source.sendSuccess(() -> Component.translatable("commands.toolkit.clipboard.copied"), true);
Handler.CHANNEL.sendToPlayer((ServerPlayer) player, new SetCopy(clipboard.toString()));

return 1;
Expand Down
27 changes: 12 additions & 15 deletions common/src/main/java/com/sunekaer/toolkit/commands/CommandKill.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.Codec;
import dev.architectury.registry.registries.Registries;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntitySummonArgument;
import net.minecraft.commands.arguments.ResourceArgument;
import net.minecraft.commands.arguments.StringRepresentableArgument;
import net.minecraft.commands.synchronization.SuggestionProviders;
import net.minecraft.core.Registry;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.entity.Entity;
Expand Down Expand Up @@ -65,24 +65,21 @@ public static KillType getKillType(CommandContext<CommandSourceStack> source, St
}
}

public static ArgumentBuilder<CommandSourceStack, ?> register() {
public static ArgumentBuilder<CommandSourceStack, ?> register(CommandBuildContext commandBuildContext) {
return Commands.literal("kill")
.requires(cs -> cs.hasPermission(2))
.then(Commands.argument("type", KillTypeArgument.killType())
.executes(context -> kill(KillTypeArgument.getKillType(context, "type"), context.getSource())))
.then(Commands.literal("by").then(Commands.argument("entity", EntitySummonArgument.id()).suggests(SuggestionProviders.SUMMONABLE_ENTITIES).executes(context -> killByEntity(context, EntitySummonArgument.getSummonableEntity(context, "entity")))));
.then(Commands.literal("by").then(Commands.argument("entity", ResourceArgument.resource(commandBuildContext, Registries.ENTITY_TYPE)).suggests(SuggestionProviders.SUMMONABLE_ENTITIES).executes(context -> killByEntity(context, ResourceArgument.getSummonableEntityType(context, "entity")))));
}

private static int killByEntity(CommandContext<CommandSourceStack> context, ResourceLocation entityId) throws CommandSyntaxException {
private static int killByEntity(CommandContext<CommandSourceStack> context, Holder.Reference<EntityType<?>> entityId) throws CommandSyntaxException {
CommandSourceStack source = context.getSource();
var level = source.getLevel();

EntityType<?> entityType = Registry.ENTITY_TYPE.get(entityId);
if (entityType == null) {
return -1;
}
EntityType<?> entityType = entityId.value();

source.sendSuccess(Component.translatable("commands.toolkit.kill.start", entityId), true);
source.sendSuccess(() -> Component.translatable("commands.toolkit.kill.start", entityId), true);
var entitiesKilled = yeetEntities((player, entity) -> entity.getType().equals(entityType), level, source.getPlayerOrException());
yeetedEntitiesMessage(source, entitiesKilled, entityId.toString());

Expand All @@ -94,7 +91,7 @@ private static int kill(KillType type, CommandSourceStack source) throws Command
int entitiesKilled = 0;

String typeName = Component.translatable("commands.toolkit.kill.type." + type.name()).getString();
source.sendSuccess(Component.translatable("commands.toolkit.kill.start", typeName), true);
source.sendSuccess(() -> Component.translatable("commands.toolkit.kill.start", typeName), true);

if (type == KillType.me || type == KillType.players) {
for (Player player : level.getPlayers(e -> type.checker.test(e, e))) {
Expand All @@ -112,9 +109,9 @@ private static int kill(KillType type, CommandSourceStack source) throws Command

private static void yeetedEntitiesMessage(CommandSourceStack source, int yeetedAmount, String typeName) {
if (yeetedAmount > 0) {
source.sendSuccess(Component.translatable("commands.toolkit.kill.done", yeetedAmount), true);
source.sendSuccess(() -> Component.translatable("commands.toolkit.kill.done", yeetedAmount), true);
} else {
source.sendSuccess(Component.translatable("commands.toolkit.kill.no", typeName), true);
source.sendSuccess(() -> Component.translatable("commands.toolkit.kill.no", typeName), true);
}
}

Expand Down
Loading

0 comments on commit e37d046

Please sign in to comment.