Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
59 935
src/main/java/br/kodari/arcanawand/ArcanaWand.java
package br.kodari.arcanawand;
import java.util.List;
import com.mojang.serialization.Codec;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.fabricmc.fabric.api.loot.v3.LootTableEvents;
import net.minecraft.core.Registry;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.network.Filterable;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.WrittenBookItem;
import net.minecraft.world.item.component.WrittenBookContent;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.entries.LootItem;
import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
public class ArcanaWand implements ModInitializer {
private static ResourceKey<Item> key(String path) {
return ResourceKey.create(Registries.ITEM, Identifier.fromNamespaceAndPath("arcanawand", path));
}
private static final ResourceKey<Item> WAND_KEY = key("wand");
public static final Item WAND = Registry.register(BuiltInRegistries.ITEM, WAND_KEY, new Item(new Item.Properties().setId(WAND_KEY).stacksTo(1)));
public static final Item PULL = book("grimoire_pull", "Grimório da Atração", "§5Atração\n\nSímbolo: linha horizontal, em qualquer direção.\n\nAlcance: 25 blocos. Puxa uma criatura em sua direção imediatamente. Não atravessa paredes. Sem recarga.");
public static final Item PUSH = book("grimoire_push", "Grimório da Repulsão", "§5Repulsão\n\nSímbolo: linha vertical de baixo para cima.\n\nAlcance: 10 blocos. Empurra imediatamente e causa 10 pontos de dano (5 corações), antes de reduções. Sem recarga.");
public static final Item FREEZE = book("grimoire_freeze", "Grimório das Correntes", "§5Imobilização\n\nSímbolo: círculo fechado.\n\nAlcance: 10 blocos. Impede o movimento durante 5 segundos. Não atravessa paredes. Sem recarga.");
public static final Item FATAL = book("grimoire_fatal", "Grimório Proibido", "§2Raio fatal\n\nSímbolo: raio em zigue-zague, de cima para baixo.\n\nAlcance: 10 blocos. Mata imediatamente. Não atravessa paredes. A cada dez usos, reduz sua honra em um nível. Sem recarga.");
public static final Item SHIELD = book("grimoire_shield", "Grimório do Escudo", "§5Escudo arcano\n\nSímbolo: escudo fechado: trace o topo da esquerda à direita, desça até a ponta central e volte ao início.\n\nBloqueia um golpe. Dura até ser atingido. Três cargas; recupera uma a cada 60 segundos jogados.");
public static final Item BLINK = book("grimoire_blink", "Grimório do Cristal", "§bSalto cristalino\n\nDesenhe um losango fechado começando no topo: direita, baixo, esquerda e topo. Teletransporta até 10 blocos na direção da mira, se houver espaço livre.");
public static final Item MARK = book("grimoire_mark", "Grimório dos Marcos", "§eMarcar local\n\nDesenhe uma linha vertical de cima para baixo. Salva sua posição e dimensão para usar com Aparatação. Até 20 pontos.");
public static final Item APPARATE = book("grimoire_apparate", "Grimório da Aparatação", "§dAparatação\n\nTrace do canto superior direito até o canto inferior esquerdo, suba pelo lado esquerdo e termine no canto inferior direito. Abre a lista de pontos salvos para escolher um destino.");
public static final Item KEY_COMMON = item("key_common");
public static final Item KEY_DARK = item("key_dark");
private static final AttachmentType<Integer> LEARNED = AttachmentRegistry.create(Identifier.fromNamespaceAndPath("arcanawand", "learned_spells"), b -> b.persistent(Codec.INT).copyOnDeath().initializer(() -> 0));
private static int spellBit(String spell) {
return switch (spell) {
case "pull" -> 1;
case "push" -> 2;
case "freeze" -> 4;
case "fatal" -> 8;
case "shield" -> 16;
case "blink" -> 32;
case "mark" -> 64;
case "apparate" -> 128;
default -> 0;
};
}
public static boolean hasLearned(ServerPlayer player, String spell) {
int bit = spellBit(spell);
return bit != 0 && (player.getAttachedOrCreate(LEARNED) & bit) != 0;
}
private static Item item(String name) {
ResourceKey<Item> id = key(name);
return Registry.register(BuiltInRegistries.ITEM, id, new Item(new Item.Properties().setId(id)));
}
private static Item book(String name, String title, String details) {
ResourceKey<Item> id = key(name);
return Registry.register(BuiltInRegistries.ITEM, id, new WrittenBookItem(new Item.Properties().setId(id).stacksTo(1).component(DataComponents.WRITTEN_BOOK_CONTENT,
new WrittenBookContent(Filterable.passThrough(title), "Mestre Arcano", 0, List.of(
Filterable.passThrough(Component.literal("§5Como lançar\n\nMire no alvo, segure a varinha e clique com o botão direito. Segure o botão esquerdo, desenhe o símbolo e solte. Esc cancela.")),
Filterable.passThrough(Component.literal(details))), false))));
}
@Override
public void onInitialize() {
CreativeModeTabEvents.modifyOutputEvent(ResourceKey.create(Registries.CREATIVE_MODE_TAB, Identifier.fromNamespaceAndPath("minecraft", "tools_and_utilities"))).register(output -> {
output.accept(WAND);
output.accept(PULL); output.accept(PUSH); output.accept(FREEZE); output.accept(FATAL); output.accept(SHIELD);
output.accept(BLINK); output.accept(MARK); output.accept(APPARATE);
output.accept(KEY_COMMON); output.accept(KEY_DARK);
});
UseItemCallback.EVENT.register((player, level, hand) -> {
Item held = player.getItemInHand(hand).getItem();
Item required = held == FATAL ? KEY_DARK : (held == PULL || held == PUSH || held == FREEZE || held == SHIELD || held == BLINK || held == MARK || held == APPARATE ? KEY_COMMON : null);
if (required != null && !player.getInventory().contains(stack -> stack.getItem() == required)) {
player.sendOverlayMessage(Component.literal("§cVocê precisa da chave correspondente no inventário."));
return InteractionResult.FAIL;
}
if (required != null && player instanceof ServerPlayer serverPlayer) {
String spell = held == PULL ? "pull" : held == PUSH ? "push" : held == FREEZE ? "freeze" : held == FATAL ? "fatal" : held == SHIELD ? "shield" : held == BLINK ? "blink" : held == MARK ? "mark" : "apparate";
int bit = spellBit(spell);
int learned = serverPlayer.getAttachedOrCreate(LEARNED);
if ((learned & bit) == 0) {
serverPlayer.setAttached(LEARNED, learned | bit);
serverPlayer.sendOverlayMessage(Component.literal("§aFeitiço desbloqueado ao abrir o grimório!"));
}
}
return InteractionResult.PASS;
});
LootTableEvents.MODIFY.register((key, table, source, registries) -> {
if (!source.isBuiltin() || !key.identifier().getNamespace().equals("minecraft") || !key.identifier().getPath().startsWith("chests/")) return;
for (Item book : List.of(PULL, PUSH, FREEZE, FATAL, SHIELD, BLINK, MARK, APPARATE)) table.withPool(LootPool.lootPool().add(LootItem.lootTableItem(book)).when(LootItemRandomChanceCondition.randomChance(.02f)));
table.withPool(LootPool.lootPool().add(LootItem.lootTableItem(KEY_COMMON)).when(LootItemRandomChanceCondition.randomChance(.07f)));
table.withPool(LootPool.lootPool().add(LootItem.lootTableItem(KEY_DARK)).when(LootItemRandomChanceCondition.randomChance(.05f)));
});
PayloadTypeRegistry.serverboundPlay().register(SpellPayload.TYPE, SpellPayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(ShieldPayload.TYPE, ShieldPayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(HonorPayload.TYPE, HonorPayload.CODEC);
PayloadTypeRegistry.clientboundPlay().register(DestinationsPayload.TYPE, DestinationsPayload.CODEC);
ServerPlayNetworking.registerGlobalReceiver(SpellPayload.TYPE, (payload, context) -> SpellCasting.cast(context.player(), payload.spell()));
ArcaneShield.initialize();
Honor.initialize();
}
}