Skip to content

Commit

Permalink
refactored entity package and created new CameraTargetEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
JR1811 committed Sep 14, 2024
1 parent d9de019 commit a901cf2
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 152 deletions.
1 change: 1 addition & 0 deletions src/main/java/io/fabricatedatelier/mayor/Mayor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void onInitialize() {
Blocks.initialize();
Items.initialize();
Entities.initialize();
VillagerUtilities.initialize();
ItemGroups.initialize();
BlockEntities.initialize();
NetworkPayloads.initialize();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.fabricatedatelier.mayor.entity.custom;

import io.fabricatedatelier.mayor.init.Entities;
import io.fabricatedatelier.mayor.util.NbtKeys;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.UUID;

public class CameraTargetEntity extends Entity {
private static final TrackedData<Optional<UUID>> USER = DataTracker.registerData(CameraTargetEntity.class, TrackedDataHandlerRegistry.OPTIONAL_UUID);

public CameraTargetEntity(EntityType<?> type, World world) {
super(type, world);
}

public CameraTargetEntity(World world, @Nullable ServerPlayerEntity player) {
this(Entities.CAMERA_TARGET, world);
if (player == null) {
setUser(null);
} else {
setUser(player.getUuid());
}
}

public Optional<UUID> getUser() {
return this.dataTracker.get(USER);
}

public void setUser(@Nullable UUID user) {
this.dataTracker.set(USER, Optional.ofNullable(user));
}

@Override
protected void initDataTracker(DataTracker.Builder builder) {
builder.add(USER, Optional.empty());
}

@Override
protected void readCustomDataFromNbt(NbtCompound nbt) {
if (nbt.contains(NbtKeys.USER_UUID)) {
setUser(nbt.getUuid(NbtKeys.USER_UUID));
} else {
setUser(null);
}
}

@Override
protected void writeCustomDataToNbt(NbtCompound nbt) {
getUser().ifPresentOrElse(userUUID -> {
nbt.putUuid(NbtKeys.USER_UUID, userUUID);
}, () -> nbt.remove(NbtKeys.USER_UUID));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.fabricatedatelier.mayor.entity.access;
package io.fabricatedatelier.mayor.entity.villager.access;

import net.minecraft.inventory.SimpleInventory;
import net.minecraft.nbt.NbtCompound;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.fabricatedatelier.mayor.entity.task;
package io.fabricatedatelier.mayor.entity.villager.task;

import com.google.common.collect.ImmutableMap;
import io.fabricatedatelier.mayor.entity.access.Builder;
import io.fabricatedatelier.mayor.entity.villager.access.Builder;
import io.fabricatedatelier.mayor.state.ConstructionData;
import io.fabricatedatelier.mayor.state.MayorVillageState;
import io.fabricatedatelier.mayor.state.VillageData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.fabricatedatelier.mayor.entity.task;
package io.fabricatedatelier.mayor.entity.villager.task;

import com.google.common.collect.ImmutableMap;
import io.fabricatedatelier.mayor.block.entity.VillageContainerBlockEntity;
import io.fabricatedatelier.mayor.entity.access.Builder;
import io.fabricatedatelier.mayor.entity.villager.access.Builder;
import io.fabricatedatelier.mayor.state.ConstructionData;
import io.fabricatedatelier.mayor.state.MayorVillageState;
import io.fabricatedatelier.mayor.state.VillageData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.fabricatedatelier.mayor.entity.task;
package io.fabricatedatelier.mayor.entity.villager.task;

import com.google.common.collect.ImmutableList;
import com.mojang.datafixers.util.Pair;
import io.fabricatedatelier.mayor.init.Entities;
import io.fabricatedatelier.mayor.init.VillagerUtilities;
import net.minecraft.entity.ai.brain.MemoryModuleType;
import net.minecraft.entity.ai.brain.task.*;
import net.minecraft.entity.passive.VillagerEntity;
Expand All @@ -12,7 +13,7 @@ public class BuilderTaskListProvider {

public static ImmutableList<Pair<Integer, ? extends Task<? super VillagerEntity>>> createBuildingTasks(VillagerProfession profession, float speed) {
VillagerWorkTask villagerWorkTask;
if (profession == Entities.BUILDER) {
if (profession == VillagerUtilities.BUILDER) {
villagerWorkTask = new BuilderWorkTask();
} else {
return ImmutableList.of();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.fabricatedatelier.mayor.entity.task;
package io.fabricatedatelier.mayor.entity.villager.task;

import com.google.common.collect.ImmutableList;
import java.util.List;
Expand Down
150 changes: 12 additions & 138 deletions src/main/java/io/fabricatedatelier/mayor/init/Entities.java
Original file line number Diff line number Diff line change
@@ -1,154 +1,28 @@
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.UseEntityCallback;
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.client.render.entity.feature.VillagerClothingFeatureRenderer;
import io.fabricatedatelier.mayor.entity.custom.CameraTargetEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.ai.brain.Activity;
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.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
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.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.hit.EntityHitResult;
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 EntityType<CameraTargetEntity> CAMERA_TARGET = register("camera_target",
EntityType.Builder.<CameraTargetEntity>create(CameraTargetEntity::new, SpawnGroup.MISC)
.dimensions(0.5f, 0.5f)
.spawnableFarFromPlayer()
.makeFireImmune()
.disableSummon()
.build());

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.value().equals(BUILDER_POI), entry -> entry.value().equals(BUILDER_POI), ImmutableSet.of(), ImmutableSet.of(), SoundEvents.ENTITY_VILLAGER_WORK_MASON);

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));
private static <E extends Entity, T extends EntityType<E>> T register(String name, T entityType) {
return Registry.register(Registries.ENTITY_TYPE, Mayor.identifierOf(name), entityType);
}

public static final Activity BUILDING = Registry.register(Registries.ACTIVITY, Mayor.identifierOf("building"), new Activity("building"));

// VillagerClothingFeatureRenderer
// public static final RegistryKey<PointOfInterestType> BUILDER_POI_KEY = RegistryKey.of(RegistryKeys.POINT_OF_INTEREST_TYPE, Identifier.ofVanilla("builder"));
//
// public static final PointOfInterestType BUILDER_POI = PointOfInterestHelper.register( Identifier.ofVanilla("builder"), 1, 1, Blocks.CAMERA_DEBUG);
//
// 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);
//
// 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, Identifier.ofVanilla(id), new VillagerProfession(id, heldWorkstation, acquirableWorkstation, gatherableItems, secondaryJobSites, workSound));
// }


public static void initialize() {
// static initialisation

// 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)));
});
// public PointOfInterestStorage getPointOfInterestStorage() {
// return this.getChunkManager().getPointOfInterestStorage();
// }

// FindPointOfInterestTask
// System.out.println(Registries.VILLAGER_PROFESSION.getId(BUILDER));

// UseEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {
// if(entity instanceof VillagerEntity villagerEntity){
// System.out.println(villagerEntity.getVillagerData().getProfession());
// if(!world.isClient()){
// villagerEntity.setVillagerData(new VillagerData(villagerEntity.getVillagerData().getType(),BUILDER,2));
//
// System.out.println(villagerEntity.getVillagerData().getProfession());
// }
// }
// return ActionResult.PASS;
// });
//
// 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();
}
}

}
Loading

0 comments on commit a901cf2

Please sign in to comment.