Files
gradle
src
build.gradle
gradle.properties
settings.gradle
Code
10 733
src/main/java/com/kodari/gothbaddie/GothBaddieMod.java
package com.kodari.gothbaddie;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;
import net.fabricmc.fabric.api.message.v1.ServerMessageEvents;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricEntityTypeBuilder;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.loot.LootPool;
import net.minecraft.loot.condition.RandomChanceLootCondition;
import net.minecraft.loot.entry.ItemEntry;
import net.minecraft.loot.provider.number.ConstantLootNumberProvider;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.server.MinecraftServer;
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.Identifier;
import net.minecraft.world.World;
public final class GothBaddieMod implements ModInitializer {
public static final String MOD_ID = "gothbaddie";
public static final EntityType<GothBaddieEntity> GOTH_BADDIE = Registry.register(
Registries.ENTITY_TYPE,
id("goth_baddie"),
FabricEntityTypeBuilder.create(SpawnGroup.CREATURE, GothBaddieEntity::new)
.dimensions(net.minecraft.entity.EntityDimensions.fixed(0.6F, 1.95F))
.trackRangeBlocks(10)
.trackedUpdateRate(3)
.build()
);
public static final Item LEASH_ITEM = Registry.register(
Registries.ITEM,
id("goth_baddie_leash"),
new Item(new FabricItemSettings().maxCount(1))
);
public static Identifier id(String path) {
return new Identifier(MOD_ID, path);
}
@Override
public void onInitialize() {
FabricDefaultAttributeRegistry.register(
GOTH_BADDIE,
GothBaddieEntity.createGothAttributes()
);
LootTableEvents.MODIFY.register((key, tableBuilder, source) -> {
if (key.getValue().getPath().startsWith("chests/")) {
tableBuilder.pool(LootPool.builder()
.rolls(ConstantLootNumberProvider.create(1.0F))
.with(ItemEntry.builder(LEASH_ITEM))
.conditionally(RandomChanceLootCondition.builder(0.25F)));
}
});
UseEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
if (!(entity instanceof GothBaddieEntity companion)
|| !player.getStackInHand(hand).isOf(LEASH_ITEM)) {
return ActionResult.PASS;
}
if (!world.isClient && player instanceof ServerPlayerEntity serverPlayer) {
companion.startLeashing(serverPlayer);
player.getStackInHand(hand).decrement(1);
}
return ActionResult.success(world.isClient);
});
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
ensureCompanion(handler.player));
ServerLivingEntityEvents.AFTER_DEATH.register((entity, source) -> {
if (entity instanceof ServerPlayerEntity player) {
GothBaddieEntity companion = findCompanion(player.getServer(), player.getUuid());
if (companion == null) {
companion = spawnCompanion(player);
} else {
companion.refreshPositionAndAngles(player.getX() + 1.5D, player.getY(), player.getZ(),
player.getYaw(), 0.0F);
}
companion.tellOwner("Du stirbst sogar, wenn ich kurz nicht hinsehe. Enttäuschend.");
}
});
ServerEntityWorldChangeEvents.AFTER_PLAYER_CHANGE_WORLD.register((player, origin, destination) -> {
GothBaddieEntity companion = findCompanion(player.getServer(), player.getUuid());
if (companion == null) {
spawnCompanion(player);
return;
}
GothBaddieEntity replacement = GOTH_BADDIE.create(destination);
if (replacement == null) {
return;
}
companion.copyStateTo(replacement);
replacement.refreshPositionAndAngles(player.getX() + 1.5D, player.getY(), player.getZ(),
player.getYaw(), 0.0F);
companion.discard();
destination.spawnEntity(replacement);
});
ServerMessageEvents.ALLOW_CHAT_MESSAGE.register((message, sender, parameters) -> {
String text = message.getContent();
GothBaddieEntity companion = findCompanion(sender.getServer(), sender.getUuid());
if (companion != null) {
if (isLeashRequest(text)) {
companion.startLeashing(sender);
} else {
companion.reactToChat(sender, text);
}
}
return true;
});
}
private static void ensureCompanion(ServerPlayerEntity player) {
GothBaddieEntity companion = findCompanion(player.getServer(), player.getUuid());
if (companion == null) {
spawnCompanion(player);
} else if (companion.getWorld() != player.getWorld()) {
GothBaddieEntity replacement = GOTH_BADDIE.create(player.getServerWorld());
if (replacement != null) {
companion.copyStateTo(replacement);
replacement.refreshPositionAndAngles(player.getX() + 1.5D, player.getY(), player.getZ(),
player.getYaw(), 0.0F);
companion.discard();
player.getServerWorld().spawnEntity(replacement);
}
}
}
private static GothBaddieEntity spawnCompanion(ServerPlayerEntity player) {
GothBaddieEntity companion = GOTH_BADDIE.create(player.getServerWorld());
if (companion == null) {
return null;
}
companion.setOwnerUuid(player.getUuid());
companion.refreshPositionAndAngles(player.getX() + 1.5D, player.getY(), player.getZ(),
player.getYaw(), 0.0F);
player.getServerWorld().spawnEntity(companion);
return companion;
}
private static GothBaddieEntity findCompanion(MinecraftServer server, java.util.UUID ownerUuid) {
if (server == null) {
return null;
}
for (ServerWorld world : server.getWorlds()) {
GothBaddieEntity found = world.getEntitiesByType(GOTH_BADDIE,
entity -> ownerUuid.equals(entity.getOwnerUuid())).stream().findFirst().orElse(null);
if (found != null) {
return found;
}
}
return null;
}
private static boolean isLeashRequest(String message) {
String text = message.toLowerCase(java.util.Locale.ROOT)
.replace('ü', 'u').replace('ö', 'o').replace('ä', 'a').replace('ß', 's');
boolean leashIntent = text.contains("lein") || text.contains("fuehr")
|| text.contains("fuhr") || text.contains("leine") || text.contains("leash");
boolean requestIntent = text.contains("bitte") || text.contains("nimm") || text.contains("mach")
|| text.contains("will") || text.contains("mochte") || text.contains("kannst")
|| text.contains("leg") || text.contains("anlegen") || text.contains("anleinen");
return leashIntent && requestIntent;
}
}