Skip to content

Commit

Permalink
goal stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
MehVahdJukaar committed Oct 31, 2024
1 parent 133a7ca commit b22ce27
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 59 deletions.
4 changes: 4 additions & 0 deletions src/main/java/galena/doom_and_gloom/DoomAndGloom.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.serialization.Codec;
import com.teamabnormals.blueprint.core.util.registry.RegistryHelper;
import galena.doom_and_gloom.compat.moonlight.MoonlightCompat;
import galena.doom_and_gloom.compat.supplementaries.AmendmentsCompat;
import galena.doom_and_gloom.content.entity.holler.Holler;
import galena.doom_and_gloom.data.OBlockStates;
Expand Down Expand Up @@ -114,6 +115,9 @@ public DoomAndGloom() {
if (ModList.get().isLoaded("amendments")) {
AmendmentsCompat.register();
}
if(ModList.get().isLoaded("moonlight")) {
MoonlightCompat.init();
}
}

private void registerAttributes(EntityAttributeCreationEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package galena.doom_and_gloom.compat.moonlight;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import galena.doom_and_gloom.index.OTags;
import galena.doom_and_gloom.index.OVillagerTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.core.GlobalPos;
import net.minecraft.resources.ResourceKey;
Expand All @@ -11,49 +12,49 @@
import net.minecraft.world.entity.ai.sensing.Sensor;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;

import java.util.List;
import java.util.Set;

public class CandlePoiSensor extends Sensor<Villager> {
public class CandleSensor extends Sensor<Villager> {

public CandlePoiSensor() {
public CandleSensor() {
super(200);
}

@Override
protected void doTick(ServerLevel pLevel, Villager pEntity) {
if (pEntity.isBaby()) {
if (!pEntity.isBaby() && pEntity.getVillagerData().getProfession() == OVillagerTypes.GRAVETENDER.get()) {
ResourceKey<Level> resourcekey = pLevel.dimension();
BlockPos blockpos = pEntity.blockPosition();
List<GlobalPos> list = Lists.newArrayList();
int i = 4;
GlobalPos found = null;
int rad = 4;

for (int j = -4; j <= 4; ++j) {
for (int j = -rad; j <= rad; ++j) {
for (int k = -2; k <= 2; ++k) {
for (int l = -4; l <= 4; ++l) {
for (int l = -rad; l <= rad; ++l) {
BlockPos blockpos1 = blockpos.offset(j, k, l);
if (pLevel.getBlockState(blockpos1).is(Blocks.PUMPKIN)) {
list.add(GlobalPos.of(resourcekey, blockpos1));
if (pLevel.getBlockState(blockpos1).is(OTags.Blocks.GRAVETENDER_LIGHTABLE)) {
found = (GlobalPos.of(resourcekey, blockpos1));
break;
}
}
}
}


Brain<?> brain = pEntity.getBrain();
if (!list.isEmpty()) {
brain.setMemory(MoonlightCompat.NEAREST_PUMPKIN.get(), list.get(0));

if (found != null) {
brain.setMemory(MoonlightCompat.NEAREST_UNLIT_CANDLE.get(), found);
} else {
brain.eraseMemory(MoonlightCompat.NEAREST_PUMPKIN.get());
brain.eraseMemory(MoonlightCompat.NEAREST_UNLIT_CANDLE.get());
}
}

}

@Override
public Set<MemoryModuleType<?>> requires() {
return ImmutableSet.of(MoonlightCompat.NEAREST_PUMPKIN.get());
return ImmutableSet.of(MoonlightCompat.NEAREST_UNLIT_CANDLE.get());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,118 @@
package galena.doom_and_gloom.compat.moonlight;

public class LightUpCandles {
public LightUpCandles(float v) {
import com.google.common.collect.ImmutableMap;
import com.mojang.authlib.GameProfile;
import galena.doom_and_gloom.index.OTags;
import net.mehvahdjukaar.moonlight.api.platform.PlatHelper;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.GlobalPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.ai.behavior.Behavior;
import net.minecraft.world.entity.ai.behavior.BlockPosTracker;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.memory.WalkTarget;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.util.FakePlayerFactory;
import org.jetbrains.annotations.Nullable;

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

public class LightUpCandles extends Behavior<Villager> {
private final float speedModifier;
private int ticksSinceReached = 0;
private int cooldown = 20 * 30;
protected int lastBreakProgress = -1;
protected GlobalPos targetPos = null;

public LightUpCandles(float speed) {
super(ImmutableMap.of(
MemoryModuleType.INTERACTION_TARGET, MemoryStatus.VALUE_ABSENT,
MoonlightCompat.NEAREST_UNLIT_CANDLE.get(), MemoryStatus.VALUE_PRESENT,
MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT),
270, 350);
this.speedModifier = speed * 1.1f;

}

@Override
protected boolean checkExtraStartConditions(ServerLevel pLevel, Villager pOwner) {
if (cooldown-- > 0) return false;
if (!PlatHelper.isMobGriefingOn(pLevel, pOwner)) {
cooldown = 20 * 60;
return false;
}
GlobalPos globalpos = pOwner.getBrain().getMemory(MoonlightCompat.NEAREST_UNLIT_CANDLE.get()).get();
targetPos = globalpos;
return globalpos.dimension() == pLevel.dimension();
}

@Override
protected void start(ServerLevel pLevel, Villager pEntity, long pGameTime) {
this.cooldown = 20 * (5 + pLevel.random.nextInt(20)) + pLevel.random.nextInt(20);
this.ticksSinceReached = 0;
this.lastBreakProgress = -1;

pEntity.getBrain().eraseMemory(MemoryModuleType.INTERACTION_TARGET);
pEntity.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(targetPos.pos(), this.speedModifier, 1));
// AskCandy.displayAsHeldItem(pEntity, new ItemStack(Items.IRON_AXE));
}

@Override
protected void stop(ServerLevel pLevel, Villager pEntity, long pGameTime) {
super.stop(pLevel, pEntity, pGameTime);
// AskCandy.clearHeldItem(pEntity);
}

@Override
protected boolean canStillUse(ServerLevel pLevel, Villager pEntity, long pGameTime) {
return pEntity.getBrain().hasMemoryValue(MoonlightCompat.NEAREST_UNLIT_CANDLE.get());
}

private static final GameProfile GRAVETENDER = new GameProfile(UUID.fromString("f3f3f3f3-2233-f3f3-f3f3-f3f3f3f3f3f3"), "[Gravetender]");

@Override
protected void tick(ServerLevel pLevel, Villager pOwner, long pGameTime) {
BlockPos pos = targetPos.pos();

//hax
pOwner.getBrain().eraseMemory(MemoryModuleType.INTERACTION_TARGET);
pOwner.getBrain().setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(pos, this.speedModifier, 2));

pOwner.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new BlockPosTracker(pos));
if (pos.closerToCenterThan(pOwner.position(), 2.3)) {
this.ticksSinceReached++;

BlockState state = pLevel.getBlockState(pos);
if (!state.is(OTags.Blocks.GRAVETENDER_LIGHTABLE)) {
pOwner.getBrain().eraseMemory(MoonlightCompat.NEAREST_UNLIT_CANDLE.get());
} else {
//breaking animation. same as fodder lol. might have the same issues
int k = (int) ((float) this.ticksSinceReached / (float) 20 * 10.0F);
if (k != this.lastBreakProgress) {
this.lastBreakProgress = k;
}

//TODO: this task is run for candles that are already on too. We would need to clear them off first and validate thatthey canbe extinguished
if (ticksSinceReached > 20) {
ServerPlayer player = FakePlayerFactory.get(pLevel, GRAVETENDER);
player.setItemInHand(InteractionHand.MAIN_HAND, Items.FLINT_AND_STEEL.getDefaultInstance());
BlockHitResult hit = new BlockHitResult(Vec3.atBottomCenterOf(pos), Direction.UP, pos, false);
state.use(pLevel, player, InteractionHand.MAIN_HAND, hit);
}
}

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,49 @@
import galena.doom_and_gloom.index.OVillagerTypes;
import net.mehvahdjukaar.moonlight.api.entity.VillagerAIHooks;
import net.mehvahdjukaar.moonlight.api.events.IVillagerBrainEvent;
import net.mehvahdjukaar.moonlight.api.platform.PlatHelper;
import net.mehvahdjukaar.moonlight.api.platform.RegHelper;
import net.minecraft.core.GlobalPos;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.ai.sensing.SensorType;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.schedule.Activity;

import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

public class MoonlightCompat {

public static final Supplier<MemoryModuleType<GlobalPos>> PUMPKIN_POS =
RegHelper.registerMemoryModule(DoomAndGloom.modLoc("pumpkin_pos"), () ->
new MemoryModuleType<>(Optional.of(GlobalPos.CODEC)));
public static final Supplier<MemoryModuleType<List<GlobalPos>>> CANDLES_I_LIT =
RegHelper.registerMemoryModule(DoomAndGloom.modLoc("candles_i_lit"), () ->
new MemoryModuleType<>(Optional.of(GlobalPos.CODEC.listOf())));

public static final Supplier<MemoryModuleType<GlobalPos>> NEAREST_PUMPKIN =
RegHelper.registerMemoryModule(DoomAndGloom.modLoc("nearest_pumpkin"), () ->
//list as these could be lit
public static final Supplier<MemoryModuleType<GlobalPos>> NEAREST_UNLIT_CANDLE =
RegHelper.registerMemoryModule(DoomAndGloom.modLoc("nearest_candle"), () ->
new MemoryModuleType<>(Optional.empty()));


public static final Supplier<SensorType<CandlePoiSensor>> PUMPKIN_POI_SENSOR =
public static final Supplier<SensorType<CandleSensor>> CANDLES_SENSOR =
RegHelper.registerSensor(DoomAndGloom.modLoc("candle_poi"), () ->
new SensorType<>(CandlePoiSensor::new));
new SensorType<>(CandleSensor::new));

public static void init() {
VillagerAIHooks.addBrainModification(MoonlightCompat::onVillagerBrainInitialize);
VillagerAIHooks.registerMemory(PUMPKIN_POS.get());
VillagerAIHooks.registerMemory(NEAREST_PUMPKIN.get());
PlatHelper.addCommonSetup(() -> {
VillagerAIHooks.registerMemory(CANDLES_I_LIT.get());
VillagerAIHooks.registerMemory(NEAREST_UNLIT_CANDLE.get());
});
}

public static void onVillagerBrainInitialize(IVillagerBrainEvent event) {
Villager villager = event.getVillager();

if (!villager.isBaby() && villager.getVillagerData().getProfession() == OVillagerTypes.GRAVETENDER.get()) {
// event.addTaskToActivity(Activity.WORK, Pair.of(3, new LightUpCandles(0.5f)));
event.addTaskToActivity(Activity.WORK, Pair.of(3, new LightUpCandles(0.5f)));
event.addTaskToActivity(Activity.IDLE, Pair.of(3, new TurnOffCandles(0.5f)));
event.addSensor(CANDLES_SENSOR.get());
}
}
}
Loading

0 comments on commit b22ce27

Please sign in to comment.