diff options
Diffstat (limited to 'src/main/kotlin/moe/nea/potatocrime')
6 files changed, 221 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/potatocrime/PotatoCrime.kt b/src/main/kotlin/moe/nea/potatocrime/PotatoCrime.kt new file mode 100644 index 0000000..a421e35 --- /dev/null +++ b/src/main/kotlin/moe/nea/potatocrime/PotatoCrime.kt @@ -0,0 +1,19 @@ +package moe.nea.potatocrime + +import moe.nea.potatocrime.registry.PotatoRegistry +import net.fabricmc.api.ModInitializer +import org.slf4j.LoggerFactory + +object PotatoCrime : ModInitializer { + + val modId = "potato-crime" + private val logger = LoggerFactory.getLogger("potato-crime") + + override fun onInitialize() { + // This code runs as soon as Minecraft is in a mod-load-ready state. + // However, some things (like resources) may still be uninitialized. + // Proceed with mild caution. + logger.info("Hello Fabric world!") + PotatoRegistry.registerAll() + } +}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/potatocrime/PotatoCrimeDataGenerator.kt b/src/main/kotlin/moe/nea/potatocrime/PotatoCrimeDataGenerator.kt new file mode 100644 index 0000000..09c9469 --- /dev/null +++ b/src/main/kotlin/moe/nea/potatocrime/PotatoCrimeDataGenerator.kt @@ -0,0 +1,68 @@ +package moe.nea.potatocrime + +import moe.nea.potatocrime.registry.PotatoRegistry +import moe.nea.potatocrime.registry.PotatoTranslations +import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint +import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator +import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput +import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider +import net.fabricmc.fabric.api.datagen.v1.provider.FabricModelProvider +import net.fabricmc.fabric.api.datagen.v1.provider.FabricRecipeProvider +import net.minecraft.data.client.BlockStateModelGenerator +import net.minecraft.data.client.ItemModelGenerator +import net.minecraft.data.client.Models +import net.minecraft.data.server.recipe.RecipeExporter +import net.minecraft.data.server.recipe.ShapelessRecipeJsonBuilder +import net.minecraft.item.Items +import net.minecraft.recipe.book.RecipeCategory +import net.minecraft.registry.RegistryWrapper +import java.util.concurrent.CompletableFuture + +class PotatoCrimeDataGenerator : DataGeneratorEntrypoint { + override fun onInitializeDataGenerator(fabricDataGenerator: FabricDataGenerator) { + val pack = fabricDataGenerator.createPack() + pack.addProvider(::NameProvider) + pack.addProvider(::RecipeProvider) + pack.addProvider(::DefaultModels) + } +} + +class RecipeProvider( + output: FabricDataOutput?, + registriesFuture: CompletableFuture<RegistryWrapper.WrapperLookup>? +) : FabricRecipeProvider(output, registriesFuture) { + override fun generate(exporter: RecipeExporter) { + ShapelessRecipeJsonBuilder.create(RecipeCategory.TOOLS, PotatoRegistry.contraband) + .input(Items.PAPER, 3) + .input(Items.BROWN_DYE) + .input(Items.CARROT) + .criterion(hasItem(Items.PAPER), conditionsFromItem(Items.PAPER)) + .criterion(hasItem(Items.CARROT), conditionsFromItem(Items.CARROT)) + .offerTo(exporter) + } +} + +class DefaultModels(output: FabricDataOutput?) : FabricModelProvider(output) { + override fun generateBlockStateModels(blockStateModelGenerator: BlockStateModelGenerator) { + + } + + override fun generateItemModels(itemModelGenerator: ItemModelGenerator) { + itemModelGenerator.register(PotatoRegistry.contraband, Models.GENERATED) + } + +} + + +class NameProvider(dataOutput: FabricDataOutput, registryLookup: CompletableFuture<RegistryWrapper.WrapperLookup>) : + FabricLanguageProvider(dataOutput, registryLookup) { + override fun generateTranslations( + registryLookup: RegistryWrapper.WrapperLookup, + translationBuilder: TranslationBuilder + ) { + translationBuilder.add(PotatoRegistry.contraband, "Contraband") + PotatoTranslations.allTranslations.forEach { + translationBuilder.add(it.translationKey, it.default) + } + } +}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/potatocrime/entity/PotatoGuard.kt b/src/main/kotlin/moe/nea/potatocrime/entity/PotatoGuard.kt new file mode 100644 index 0000000..b7282ea --- /dev/null +++ b/src/main/kotlin/moe/nea/potatocrime/entity/PotatoGuard.kt @@ -0,0 +1,3 @@ +package moe.nea.potatocrime.entity + +import net.minecraft.entity.LivingEntity diff --git a/src/main/kotlin/moe/nea/potatocrime/item/ContrabandItem.kt b/src/main/kotlin/moe/nea/potatocrime/item/ContrabandItem.kt new file mode 100644 index 0000000..728d84a --- /dev/null +++ b/src/main/kotlin/moe/nea/potatocrime/item/ContrabandItem.kt @@ -0,0 +1,70 @@ +package moe.nea.potatocrime.item + +import moe.nea.potatocrime.registry.PotatoRegistry +import moe.nea.potatocrime.registry.PotatoTranslations +import net.minecraft.client.item.TooltipContext +import net.minecraft.entity.player.PlayerEntity +import net.minecraft.item.Item +import net.minecraft.item.ItemStack +import net.minecraft.item.Items +import net.minecraft.server.network.ServerPlayerEntity +import net.minecraft.text.Text +import net.minecraft.util.Formatting +import net.minecraft.util.Hand +import net.minecraft.util.TypedActionResult +import net.minecraft.world.World + +class ContrabandItem : Item( + Settings().maxCount(1) +) { + + fun ItemStack.getContrabandCount(): Int { + return getOrDefault(PotatoRegistry.contrabandData, 0) + } + + fun ItemStack.setContrabandCount(count: Int) { + set(PotatoRegistry.contrabandData, count) + } + + fun dropCarrots(user: PlayerEntity, oldCount: Int): Int { + val carrotCount = oldCount.coerceAtMost(Items.CARROT.maxCount) + val stack = ItemStack(Items.CARROT, carrotCount) + if (user is ServerPlayerEntity) + user.dropItem(stack, false, true) + return oldCount - carrotCount + } + + override fun appendTooltip( + stack: ItemStack, + world: World?, + tooltip: MutableList<Text>, + context: TooltipContext + ) { + val count = stack.getContrabandCount() + tooltip.add( + PotatoTranslations.contrabandFillText.format(count.toString()).styled { it.withColor(Formatting.DARK_GRAY) }) + } + + override fun use(world: World, user: PlayerEntity, hand: Hand): TypedActionResult<ItemStack> { + val stack = user.getStackInHand(hand) + if (user.isSneaking) { + val count = stack.getContrabandCount() + if (count <= 0) + return TypedActionResult.fail(stack) + val newCount = dropCarrots(user, count) + stack.setContrabandCount(newCount) + return TypedActionResult.success(stack) + } else { + val count = stack.getContrabandCount() + val carrotSlot = user.inventory.getSlotWithStack(ItemStack(Items.CARROT)) + if (carrotSlot < 0) { + if (user is ServerPlayerEntity) + user.sendMessage(PotatoTranslations.noCarrotsToDeposit.format()) + return TypedActionResult.fail(stack) + } + val removed = user.inventory.removeStack(carrotSlot, (1000 - count).coerceAtLeast(0)) + stack.setContrabandCount(removed.count + count) + return TypedActionResult.pass(stack) + } + } +} diff --git a/src/main/kotlin/moe/nea/potatocrime/registry/PotatoRegistry.kt b/src/main/kotlin/moe/nea/potatocrime/registry/PotatoRegistry.kt new file mode 100644 index 0000000..2e5fa5f --- /dev/null +++ b/src/main/kotlin/moe/nea/potatocrime/registry/PotatoRegistry.kt @@ -0,0 +1,37 @@ +package moe.nea.potatocrime.registry + +import com.mojang.serialization.Codec +import moe.nea.potatocrime.PotatoCrime +import moe.nea.potatocrime.item.ContrabandItem +import net.minecraft.component.DataComponentType +import net.minecraft.item.Item +import net.minecraft.network.codec.PacketCodecs +import net.minecraft.registry.Registries +import net.minecraft.registry.Registry +import net.minecraft.util.Identifier + +object PotatoRegistry { + private val delayedRegistries = mutableListOf<() -> Unit>() + fun registerAll() { + delayedRegistries.forEach { it.invoke() } + } + + private fun <U, T : U> register(registry: Registry<U>, name: String, t: T): T { + delayedRegistries.add { + Registry.register(registry, Identifier(PotatoCrime.modId, name), t) + } + return t + } + + private fun <T : Item> item(name: String, t: T): T = register(Registries.ITEM, name, t) + private fun <T> dataComponent(name: String, block: (DataComponentType.Builder<T>) -> DataComponentType.Builder<T>) = + register( + Registries.DATA_COMPONENT_TYPE, name, + block(DataComponentType.builder()).build() + ) + + val contraband = item("contraband", ContrabandItem()) + val contrabandData = dataComponent("contraband_data") { + it.codec(Codec.INT).packetCodec(PacketCodecs.VAR_INT) + } +}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/potatocrime/registry/PotatoTranslations.kt b/src/main/kotlin/moe/nea/potatocrime/registry/PotatoTranslations.kt new file mode 100644 index 0000000..062c88d --- /dev/null +++ b/src/main/kotlin/moe/nea/potatocrime/registry/PotatoTranslations.kt @@ -0,0 +1,24 @@ +package moe.nea.potatocrime.registry + +import net.minecraft.text.MutableText +import net.minecraft.text.Text + +object PotatoTranslations { + val allTranslations = mutableListOf<PT>() + + data class PT(val name: String, val default: String) { + fun format(vararg params: String): MutableText { + return Text.translatable(translationKey, *params) + } + + val translationKey = "potato-crime.text.$name" + + init { + allTranslations.add(this) + } + } + + val noCarrotsToDeposit = PT("no-carrots", "No carrots to deposit.") + val contrabandFillText = PT("fill-level", "Hidden carrots: %s/1000.") + +}
\ No newline at end of file |