From c04933d2b1442ecb9fa0db5e811a13fbfedb6164 Mon Sep 17 00:00:00 2001 From: Globox1997 Date: Sat, 31 Aug 2024 14:59:49 +0200 Subject: [PATCH] villager test --- .../mayor/init/Entities.java | 116 +++++++++++++++++- .../mayor/mixin/TakeJobSiteTaskMixin.java | 43 +++++++ src/main/resources/mayor.mixins.json | 1 + 3 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/fabricatedatelier/mayor/mixin/TakeJobSiteTaskMixin.java diff --git a/src/main/java/io/fabricatedatelier/mayor/init/Entities.java b/src/main/java/io/fabricatedatelier/mayor/init/Entities.java index 28e6a0c..c89d805 100644 --- a/src/main/java/io/fabricatedatelier/mayor/init/Entities.java +++ b/src/main/java/io/fabricatedatelier/mayor/init/Entities.java @@ -1,23 +1,44 @@ package io.fabricatedatelier.mayor.init; import com.google.common.collect.ImmutableSet; +import com.mojang.datafixers.util.Pair; import io.fabricatedatelier.mayor.Mayor; +import net.fabricmc.fabric.api.event.player.UseItemCallback; +import net.fabricmc.fabric.api.object.builder.v1.trade.TradeOfferHelper; import net.fabricmc.fabric.api.object.builder.v1.world.poi.PointOfInterestHelper; +import net.fabricmc.fabric.mixin.content.registry.VillagerEntityAccessor; import net.minecraft.block.Block; +import net.minecraft.entity.Entity; +import net.minecraft.entity.ai.brain.task.FindPointOfInterestTask; +import net.minecraft.entity.passive.VillagerEntity; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.SoundEvent; import net.minecraft.sound.SoundEvents; -import net.minecraft.village.VillagerProfession; +import net.minecraft.util.Hand; +import net.minecraft.util.Identifier; +import net.minecraft.util.TypedActionResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.random.Random; +import net.minecraft.village.*; +import net.minecraft.world.World; +import net.minecraft.world.poi.PointOfInterestStorage; import net.minecraft.world.poi.PointOfInterestType; +import net.minecraft.world.poi.PointOfInterestTypes; import org.jetbrains.annotations.Nullable; import java.util.Iterator; +import java.util.Set; import java.util.function.Predicate; +import java.util.stream.Collectors; public class Entities { @@ -25,13 +46,40 @@ public class Entities { public static final PointOfInterestType BUILDER_POI = PointOfInterestHelper.register(Mayor.identifierOf("builder"), 1, 1, Blocks.CAMERA_DEBUG); - public static final VillagerProfession BUILDER = register("builder", entry -> entry.matchesKey(BUILDER_POI_KEY), entry -> entry.matchesKey(BUILDER_POI_KEY), ImmutableSet.of(), ImmutableSet.of(), SoundEvents.ENTITY_VILLAGER_WORK_MASON); + public static final VillagerProfession BUILDER = register("builder", entry -> entry.value().equals(BUILDER_POI), entry -> entry.value().equals(BUILDER_POI), ImmutableSet.of(), ImmutableSet.of(), SoundEvents.ENTITY_VILLAGER_WORK_MASON); + +//FindPointOfInterestTask +// RegistryKey +// public static final VillagerProfession BUILDER = register("builder", entry -> entry.matchesKey(RegistryKey.of(RegistryKeys.POINT_OF_INTEREST_TYPE,Identifier.of("builder"))), entry -> entry.matchesKey(RegistryKey.of(RegistryKeys.POINT_OF_INTEREST_TYPE,Identifier.of("builder"))), ImmutableSet.of(), ImmutableSet.of(), SoundEvents.ENTITY_VILLAGER_WORK_MASON); + + +// public static VillagerProfession registerProfession(String name, RegistryKey workStation) { +// var id = new Identifier(RoguesMod.NAMESPACE, name); +// return Registry.register(Registries.VILLAGER_PROFESSION, new Identifier(RoguesMod.NAMESPACE, name), new VillagerProfession( +// id.toString(), +// (entry) -> { +// return entry.matchesKey(workStation); +// }, +// (entry) -> { +// return entry.matchesKey(workStation); +// }, +// ImmutableSet.of(), +// ImmutableSet.of(), +// SoundHelper.WORKBENCH) +// ); +// } + + +// VillagerType.PLAINS private static VillagerProfession register(String id, Predicate> heldWorkstation, Predicate> acquirableWorkstation, ImmutableSet gatherableItems, ImmutableSet secondaryJobSites, @Nullable SoundEvent workSound) { return Registry.register(Registries.VILLAGER_PROFESSION, Mayor.identifierOf(id), new VillagerProfession(id, heldWorkstation, acquirableWorkstation, gatherableItems, secondaryJobSites, workSound)); } +// return PointOfInterestHelper.register(new Identifier(RoguesMod.NAMESPACE, name), +// 1, 10, ImmutableSet.copyOf(block.getStateManager().getStates())); + // private static RegistryKey of(String id) { // return RegistryKey.of(RegistryKeys.POINT_OF_INTEREST_TYPE, Mayor.identifierOf(id)); // } @@ -57,10 +105,68 @@ private static VillagerProfession register(String id, Predicate iterator = Registries.VILLAGER_PROFESSION.stream().iterator(); -// while(iterator.hasNext()) { -// System.out.println(iterator.next()); + Iterator iterator = Registries.VILLAGER_PROFESSION.stream().iterator(); + while (iterator.hasNext()) { + VillagerProfession villagerProfession = iterator.next(); +// System.out.println(villagerProfession.acquirableWorkstation().test(Registries.POINT_OF_INTEREST_TYPE.getEntry(BUILDER_POI))+ " : "+villagerProfession.id()); + } + + TradeOfferHelper.registerVillagerOffers(BUILDER, 1, factories -> { + factories.add(new SimpleTradeFactory(new TradeOffer(new TradedItem(net.minecraft.item.Items.DIAMOND, 5), new ItemStack(Items.GLOW_ITEM_FRAME), 3, 4, 0.15F))); + factories.add(new SimpleTradeFactory(new TradeOffer(new TradedItem(Items.SADDLE, 5), new ItemStack(Items.DANDELION), 3, 4, 0.15F))); + }); + TradeOfferHelper.registerVillagerOffers(BUILDER, 2, factories -> { + factories.add(new SimpleTradeFactory(new TradeOffer(new TradedItem(net.minecraft.item.Items.DIAMOND, 5), new ItemStack(Items.GLOW_ITEM_FRAME), 3, 4, 0.15F))); + factories.add(new SimpleTradeFactory(new TradeOffer(new TradedItem(Items.SADDLE, 5), new ItemStack(Items.DANDELION), 3, 4, 0.15F))); + }); + // Todo: FindPointOfInterestTask + // Todo: ServerWorld getPointOfInterestStorage() +// public PointOfInterestStorage getPointOfInterestStorage() { +// return this.getChunkManager().getPointOfInterestStorage(); // } + + UseItemCallback.EVENT.register((player, world, hand) -> { + if (world instanceof ServerWorld serverWorld) { + System.out.println(serverWorld.getPointOfInterestStorage()+ " : "+PointOfInterestTypes.isPointOfInterest(player.getSteppingBlockState())+ " : "+player.getSteppingBlockState()); + +// Set, BlockPos>> set = (Set, BlockPos>>)serverWorld.getPointOfInterestStorage().getSortedTypesAndPositions( +// poiPredicate, predicate2, player.getBlockPos(), 48, PointOfInterestStorage.OccupationStatus.HAS_SPACE +// ) +// .limit(5L) +// .collect(Collectors.toSet()); + } + return TypedActionResult.pass(player.getMainHandStack()); + }); + + +// TradeOfferHelper. + +// VillagerEntity.POINTS_OF_INTEREST.forEach((a,b)->{ +// System.out.println(b); +// }); +// VillagerEntityAccessor.fabric_setGatherableItems(); +// System.out.println(VillagerEntity.POINTS_OF_INTEREST); + +// Iterator iterator = Registries.POINT_OF_INTEREST_TYPE.stream().iterator(); +// while (iterator.hasNext()) { +//// System.out.println(iterator.next()); +// } + +// System.out.println(PointOfInterestTypes.isPointOfInterest(Blocks.CAMERA_DEBUG.getDefaultState())+ " : "+PointOfInterestTypes.getTypeForState(Blocks.CAMERA_DEBUG.getDefaultState())); + } + + private static class SimpleTradeFactory implements TradeOffers.Factory { + private final TradeOffer offer; + + SimpleTradeFactory(TradeOffer offer) { + this.offer = offer; + } + + @Override + public TradeOffer create(Entity entity, Random random) { + // ALWAYS supply a copy of the offer. + return this.offer.copy(); + } } } diff --git a/src/main/java/io/fabricatedatelier/mayor/mixin/TakeJobSiteTaskMixin.java b/src/main/java/io/fabricatedatelier/mayor/mixin/TakeJobSiteTaskMixin.java new file mode 100644 index 0000000..5249013 --- /dev/null +++ b/src/main/java/io/fabricatedatelier/mayor/mixin/TakeJobSiteTaskMixin.java @@ -0,0 +1,43 @@ +package io.fabricatedatelier.mayor.mixin; + +import io.fabricatedatelier.mayor.Mayor; +import net.minecraft.block.Blocks; +import net.minecraft.block.SmithingTableBlock; +import net.minecraft.entity.ai.brain.MemoryQueryResult; +import net.minecraft.entity.ai.brain.task.TakeJobSiteTask; +import net.minecraft.entity.ai.brain.task.TaskTriggerer; +import net.minecraft.entity.passive.VillagerEntity; +import net.minecraft.registry.entry.RegistryEntry; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.poi.PointOfInterestType; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; + +import java.util.Optional; + +@Mixin(TakeJobSiteTask.class) +public class TakeJobSiteTaskMixin { + + // @Inject(method = "method_47211",at = @At(value = "INVOKE",target = "Lnet/minecraft/server/world/ServerWorld;getPointOfInterestStorage()Lnet/minecraft/world/poi/PointOfInterestStorage;")) +// @Inject(method = "method_47211",at = @At(value = "RETURN",ordinal = 3),locals = LocalCapture.CAPTURE_FAILSOFT) +// private static void canUseJobSite(TaskTriggerer.TaskContext taskContext, MemoryQueryResult memoryQueryResult, MemoryQueryResult memoryQueryResult2, MemoryQueryResult memoryQueryResult3, MemoryQueryResult memoryQueryResult4, float f, ServerWorld world, VillagerEntity entity, long time, CallbackInfoReturnable cir, BlockPos blockPos, Optional optional) { +// System.out.println(entity+ " : "+optional+ " : "+blockPos); +//// memoryQueryResult. +//// Mayor.LOGGER.error(taskContext); +//// SmithingTableBlock +// } + +// Blocks + @Inject(method = "method_47211",at = @At(value = "HEAD")) + private static void canUseJobSite(TaskTriggerer.TaskContext taskContext, MemoryQueryResult memoryQueryResult, MemoryQueryResult memoryQueryResult2, MemoryQueryResult memoryQueryResult3, MemoryQueryResult memoryQueryResult4, float f, ServerWorld world, VillagerEntity entity, long time, CallbackInfoReturnable cir) { +// System.out.println(entity+ " : "+memoryQueryResult+" : "+memoryQueryResult.getValue()); + +// memoryQueryResult. +// Mayor.LOGGER.error(taskContext); +// SmithingTableBlock + } +} diff --git a/src/main/resources/mayor.mixins.json b/src/main/resources/mayor.mixins.json index 6b2fabc..fd70a31 100644 --- a/src/main/resources/mayor.mixins.json +++ b/src/main/resources/mayor.mixins.json @@ -13,6 +13,7 @@ "ServerPlayerEntityMixin", "VillagerEntityMixin", "CarvedPumpkinBlockMixin", + "TakeJobSiteTaskMixin", "access.JigsawStructureAccess" ], "injectors": {