Skip to content

Commit

Permalink
villager test
Browse files Browse the repository at this point in the history
  • Loading branch information
Globox1997 committed Aug 31, 2024
1 parent 71d3797 commit c04933d
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 5 deletions.
116 changes: 111 additions & 5 deletions src/main/java/io/fabricatedatelier/mayor/init/Entities.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,85 @@
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 {

public static final RegistryKey<PointOfInterestType> BUILDER_POI_KEY = RegistryKey.of(RegistryKeys.POINT_OF_INTEREST_TYPE, Mayor.identifierOf("builder"));

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<PointOfInterestType> 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<RegistryEntry<PointOfInterestType>> heldWorkstation, Predicate<RegistryEntry<PointOfInterestType>> acquirableWorkstation, ImmutableSet<Item> gatherableItems, ImmutableSet<Block> 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<PointOfInterestType> of(String id) {
// return RegistryKey.of(RegistryKeys.POINT_OF_INTEREST_TYPE, Mayor.identifierOf(id));
// }
Expand All @@ -57,10 +105,68 @@ private static VillagerProfession register(String id, Predicate<RegistryEntry<Po
public static void initialize() {
// static initialisation

// Iterator<VillagerProfession> iterator = Registries.VILLAGER_PROFESSION.stream().iterator();
// while(iterator.hasNext()) {
// System.out.println(iterator.next());
Iterator<VillagerProfession> 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<Pair<RegistryEntry<PointOfInterestType>, BlockPos>> set = (Set<Pair<RegistryEntry<PointOfInterestType>, 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<PointOfInterestType> 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();
}
}

}
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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<Boolean> cir) {
// System.out.println(entity+ " : "+memoryQueryResult+" : "+memoryQueryResult.getValue());

// memoryQueryResult.
// Mayor.LOGGER.error(taskContext);
// SmithingTableBlock
}
}
1 change: 1 addition & 0 deletions src/main/resources/mayor.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"ServerPlayerEntityMixin",
"VillagerEntityMixin",
"CarvedPumpkinBlockMixin",
"TakeJobSiteTaskMixin",
"access.JigsawStructureAccess"
],
"injectors": {
Expand Down

0 comments on commit c04933d

Please sign in to comment.