Files
src
build.gradle
gradle.properties
settings.gradle
Code
22 606
src/main/java/com/kodari/mythicbeasts/MythicBeasts.java
package com.kodari.mythicbeasts;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.projectile.SmallFireballEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.scoreboard.ServerScoreboard;
import net.minecraft.scoreboard.Team;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.world.World;
import java.util.Collection;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public final class MythicBeasts implements ModInitializer {
public static final String MOD_ID = "mythicbeasts";
private static final Map<Monster, Item> CRYSTALS = new EnumMap<>(Monster.class);
private static final Map<UUID, Monster> PLAYER_MONSTERS = new HashMap<>();
private static final Map<UUID, PowerState> POWER_STATES = new HashMap<>();
private static final Map<UUID, FlightState> FLIGHT_STATES = new HashMap<>();
private static final Item POWER_CORE = registerItem("power_core");
private static final RegistryKey<ItemGroup> COMBAT_GROUP = RegistryKey.of(
RegistryKeys.ITEM_GROUP,
Identifier.of("minecraft", "combat")
);
private static long serverTick;
@Override
public void onInitialize() {
for (Monster monster : Monster.values()) {
CRYSTALS.put(monster, registerItem(monster.crystalItem));
}
ItemGroupEvents.modifyEntriesEvent(COMBAT_GROUP).register(entries -> {
entries.add(POWER_CORE);
for (Item crystal : CRYSTALS.values()) {
entries.add(crystal);
}
});
UseItemCallback.EVENT.register((player, world, hand) -> {
ItemStack stack = player.getStackInHand(hand);
if (!isOurItem(stack)) {
return ActionResult.PASS;
}
if (!(player instanceof ServerPlayerEntity serverPlayer)) {
return ActionResult.SUCCESS;
}
Monster crystalType = monsterForCrystal(stack);
if (crystalType != null) {
teleportToMonster(serverPlayer, crystalType);
} else if (stack.isOf(POWER_CORE)) {
usePowerCore(serverPlayer);
}
return ActionResult.SUCCESS;
});
ServerPlayerEvents.COPY_FROM.register((oldPlayer, newPlayer, alive) -> copyCrystals(oldPlayer, newPlayer));
ServerTickEvents.END_SERVER_TICK.register(MythicBeasts::onServerTick);
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(
CommandManager.literal("mythic")
.requires(CommandManager.requirePermissionLevel(CommandManager.GAMEMASTERS_CHECK))
.then(CommandManager.literal("arena")
.then(CommandManager.argument("monster", StringArgumentType.word())
.executes(MythicBeasts::summonMonster)))
.then(CommandManager.literal("give")
.then(CommandManager.argument("players", EntityArgumentType.players())
.then(CommandManager.argument("monster", StringArgumentType.word())
.executes(MythicBeasts::giveMonsterKit))))
.then(CommandManager.literal("power")
.then(CommandManager.argument("players", EntityArgumentType.players())
.executes(MythicBeasts::givePowerCore)))
.then(CommandManager.literal("help")
.executes(context -> {
context.getSource().sendFeedback(() -> Text.of(
"/mythic arena <sky|ember|warden|titan> | /mythic give <players> <monster> | /mythic power <players>"
), false);
return Command.SINGLE_SUCCESS;
}))
));
}
private static Item registerItem(String path) {
Identifier id = Identifier.of(MOD_ID, path);
RegistryKey<Item> key = RegistryKey.of(RegistryKeys.ITEM, id);
return Registry.register(Registries.ITEM, key, new Item(new Item.Settings().registryKey(key)));
}
private static int summonMonster(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
Monster monster = Monster.fromId(StringArgumentType.getString(context, "monster"));
if (monster == null) {
context.getSource().sendFeedback(() -> Text.of("نوع الوحش غير معروف. استخدم sky أو ember أو warden أو titan."), false);
return 0;
}
ServerPlayerEntity player = context.getSource().getPlayerOrThrow();
Entity boss = monster.entityType.spawn(context.getSource().getWorld(), player.getBlockPos().up(), SpawnReason.COMMAND);
if (boss == null) {
context.getSource().sendFeedback(() -> Text.of("تعذر استدعاء الوحش هنا."), false);
return 0;
}
boss.addCommandTag(monster.bossTag);
boss.setCustomName(Text.of(monster.bossName));
boss.setCustomNameVisible(true);
if (boss instanceof LivingEntity living) {
EntityAttributeInstance maxHealth = living.getAttributeInstance(EntityAttributes.MAX_HEALTH);
if (maxHealth != null) {
maxHealth.setBaseValue(monster.maxHealth);
}
living.setHealth(monster.maxHealth);
}
context.getSource().sendFeedback(() -> Text.of("تم استدعاء " + monster.bossName + " في الساحة."), true);
return Command.SINGLE_SUCCESS;
}
private static int giveMonsterKit(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
Monster monster = Monster.fromId(StringArgumentType.getString(context, "monster"));
if (monster == null) {
context.getSource().sendFeedback(() -> Text.of("نوع الوحش غير معروف. استخدم sky أو ember أو warden أو titan."), false);
return 0;
}
Collection<ServerPlayerEntity> players = EntityArgumentType.getPlayers(context, "players");
for (ServerPlayerEntity player : players) {
Item crystal = CRYSTALS.get(monster);
if (!containsItem(player, crystal)) {
player.getInventory().insertStack(new ItemStack(crystal));
}
if (!containsItem(player, POWER_CORE)) {
player.getInventory().insertStack(new ItemStack(POWER_CORE));
}
PLAYER_MONSTERS.put(player.getUuid(), monster);
assignColor(context.getSource().getServer(), player, monster);
}
context.getSource().sendFeedback(() -> Text.of("تم توزيع كريستال وقدرة " + monster.bossName + " على " + players.size() + " لاعب."), true);
return players.size();
}
private static int givePowerCore(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
Collection<ServerPlayerEntity> players = EntityArgumentType.getPlayers(context, "players");
int given = 0;
for (ServerPlayerEntity player : players) {
if (!containsItem(player, POWER_CORE)) {
player.getInventory().insertStack(new ItemStack(POWER_CORE));
}
given++;
}
int delivered = given;
context.getSource().sendFeedback(() -> Text.of("تم توزيع نواة القدرة على " + delivered + " لاعب."), true);
return delivered;
}