Skip to content

Commit

Permalink
添加区域划分与女仆传送问题的检查
Browse files Browse the repository at this point in the history
  • Loading branch information
TartaricAcid committed Dec 27, 2023
1 parent 2455fbd commit 6a6c786
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.schedule.Activity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;
Expand Down Expand Up @@ -40,6 +41,10 @@ public static void onRender(RenderLevelStageEvent event) {
if (!ItemKappaCompass.hasKappaCompassData(stack)) {
return;
}
ResourceLocation dimension = ItemKappaCompass.getDimension(stack);
if (dimension != null && !mc.player.level.dimension().location().equals(dimension)) {
return;
}
BlockPos workPos = ItemKappaCompass.getPoint(Activity.WORK, stack);
Vec3 camera = event.getCamera().getPosition().reverse();
event.getPoseStack().pushPose();
Expand All @@ -49,10 +54,10 @@ public static void onRender(RenderLevelStageEvent event) {
double radius = MaidConfig.MAID_WORK_RANGE.get() + 0.1;
VertexConsumer buffer = mc.renderBuffers().bufferSource().getBuffer(RenderType.LINES);
RenderHelper.renderCylinder(event.getPoseStack(), buffer, centerPos, radius, 16, 1.0F, 0, 0);
Vec3 textPos = new Vec3(workPos.getX() + 0.5, workPos.getY() + 0.5, workPos.getZ() + 0.5);
Vec3 textPos = new Vec3(workPos.getX() + 0.5, workPos.getY() + 2, workPos.getZ() + 0.5);
String text = I18n.get("message.touhou_little_maid.kappa_compass.work_area");
RenderHelper.renderFloatingText(event.getPoseStack(), text, textPos, 0xff1111, 0.15f, -5);
RenderHelper.renderFloatingText(event.getPoseStack(), "▼", textPos, 0xff1111, 0.15f, 5);
RenderHelper.renderFloatingText(event.getPoseStack(), text, textPos.x, textPos.y, textPos.z, 0xff1111, 0.15f, true, -5, false);
RenderHelper.renderFloatingText(event.getPoseStack(), "▼", textPos.x, textPos.y, textPos.z, 0xff1111, 0.15f, true, 5, false);
}

BlockPos idlePos = ItemKappaCompass.getPoint(Activity.IDLE, stack);
Expand All @@ -61,16 +66,16 @@ public static void onRender(RenderLevelStageEvent event) {
double radius = MaidConfig.MAID_IDLE_RANGE.get();
VertexConsumer buffer = mc.renderBuffers().bufferSource().getBuffer(RenderType.LINES);
RenderHelper.renderCylinder(event.getPoseStack(), buffer, centerPos, radius, 16, 0, 1.0F, 0);
Vec3 textPos = new Vec3(idlePos.getX() + 0.5, idlePos.getY() + 0.5, idlePos.getZ() + 0.5);
Vec3 textPos = new Vec3(idlePos.getX() + 0.5, idlePos.getY() + 2, idlePos.getZ() + 0.5);
if (idlePos.equals(workPos)) {
textPos = textPos.add(0, 1, 0);
} else if (workPos != null) {
Vec3 prePos = camera.add(workPos.getX() + 0.5, workPos.getY() + 0.5, workPos.getZ() + 0.5);
RenderHelper.renderLine(event.getPoseStack(), buffer, centerPos, prePos, 1.0f, 1.0f, 1.0f);
}
String text = I18n.get("message.touhou_little_maid.kappa_compass.idle_area");
RenderHelper.renderFloatingText(event.getPoseStack(), text, textPos, 0x11ff11, 0.15f, -5);
RenderHelper.renderFloatingText(event.getPoseStack(), "▼", textPos, 0x11ff11, 0.15f, 5);
RenderHelper.renderFloatingText(event.getPoseStack(), text, textPos.x, textPos.y, textPos.z, 0x11ff11, 0.15f, true, -5, false);
RenderHelper.renderFloatingText(event.getPoseStack(), "▼", textPos.x, textPos.y, textPos.z, 0x11ff11, 0.15f, true, 5, false);
}

BlockPos resetPos = ItemKappaCompass.getPoint(Activity.REST, stack);
Expand All @@ -79,16 +84,18 @@ public static void onRender(RenderLevelStageEvent event) {
double radius = MaidConfig.MAID_SLEEP_RANGE.get() - 0.1;
VertexConsumer buffer = mc.renderBuffers().bufferSource().getBuffer(RenderType.LINES);
RenderHelper.renderCylinder(event.getPoseStack(), buffer, centerPos, radius, 16, 0, 0, 1.0F);
Vec3 textPos = new Vec3(resetPos.getX() + 0.5, resetPos.getY() + 0.5, resetPos.getZ() + 0.5);
Vec3 textPos = new Vec3(resetPos.getX() + 0.5, resetPos.getY() + 2, resetPos.getZ() + 0.5);
if (resetPos.equals(idlePos)) {
textPos = textPos.add(0, 2, 0);
} else if (idlePos != null) {
} else if (idlePos != null && workPos != null) {
Vec3 prePos = camera.add(idlePos.getX() + 0.5, idlePos.getY() + 0.5, idlePos.getZ() + 0.5);
RenderHelper.renderLine(event.getPoseStack(), buffer, centerPos, prePos, 1.0f, 1.0f, 1.0f);
prePos = camera.add(workPos.getX() + 0.5, workPos.getY() + 0.5, workPos.getZ() + 0.5);
RenderHelper.renderLine(event.getPoseStack(), buffer, centerPos, prePos, 1.0f, 1.0f, 1.0f);
}
String text = I18n.get("message.touhou_little_maid.kappa_compass.sleep_area");
RenderHelper.renderFloatingText(event.getPoseStack(), text, textPos, 0x1111ff, 0.15f, -5);
RenderHelper.renderFloatingText(event.getPoseStack(), "▼", textPos, 0x1111ff, 0.15f, 5);
RenderHelper.renderFloatingText(event.getPoseStack(), text, textPos.x, textPos.y, textPos.z, 0x1111ff, 0.15f, true, -5, false);
RenderHelper.renderFloatingText(event.getPoseStack(), "▼", textPos.x, textPos.y, textPos.z, 0x1111ff, 0.15f, true, 5, false);
}
event.getPoseStack().popPose();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.tartaricacid.touhoulittlemaid.client.gui.entity.maid;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.util.FormattedCharSequence;

import java.util.List;

public class CheckSchedulePosGui extends Screen {
private final AbstractMaidContainerGui<?> parent;
private final Component tips;
private int middleX;
private int middleY;

public CheckSchedulePosGui(AbstractMaidContainerGui<?> parent, Component tips) {
super(Component.literal("Check Schedule Pos Screen"));
this.parent = parent;
this.tips = tips;
}

@Override
protected void init() {
this.middleX = this.width / 2;
this.middleY = this.height / 2;
Button returnButton = Button.builder(Component.translatable("button.touhou_little_maid.maid.return"), (b) -> getMinecraft().setScreen(this.parent))
.pos(middleX - 100, middleY + 10).size(200, 20).build();
this.addRenderableWidget(returnButton);
}

@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) {
renderBackground(graphics);
List<FormattedCharSequence> split = font.split(tips, 300);
int startY = middleY - 10 - split.size() * font.lineHeight;
for (FormattedCharSequence text : split) {
graphics.drawCenteredString(font, text, middleX, startY, 0xFFFFFF);
startY += font.lineHeight;
}
super.render(graphics, mouseX, mouseY, partialTicks);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.tartaricacid.touhoulittlemaid.entity.ai.brain.task;

import com.github.tartaricacid.touhoulittlemaid.entity.item.EntitySit;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import com.google.common.collect.ImmutableMap;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -9,6 +10,8 @@
import net.minecraft.world.entity.schedule.Activity;

public class MaidUpdateActivityFromSchedule extends Behavior<EntityMaid> {
private Activity cacheActivity;

public MaidUpdateActivityFromSchedule() {
super(ImmutableMap.of());
}
Expand All @@ -18,7 +21,11 @@ protected void start(ServerLevel level, EntityMaid maid, long gameTime) {
long dayTime = level.getDayTime();
if (gameTime - brain.lastScheduleUpdate > 20L) {
Activity activity = brain.getSchedule().getActivityAt((int) (dayTime % 24000L));
if (!brain.isActive(activity)) {
if (this.cacheActivity == null) {
this.cacheActivity = activity;
}
if (!this.cacheActivity.equals(activity) && !maid.isSleeping() && !(maid.getVehicle() instanceof EntitySit)) {
this.cacheActivity = activity;
maid.getSchedulePos().restrictTo(maid);
BehaviorUtils.setWalkAndLookTargetMemories(maid, maid.getRestrictCenter(), 0.7f, 3);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ protected EntityMaid(EntityType<EntityMaid> type, Level world) {
this.getNavigation().setCanFloat(true);
this.setPathfindingMalus(BlockPathTypes.COCOA, -1.0F);
this.favorabilityManager = new FavorabilityManager(this);
this.schedulePos = new SchedulePos(BlockPos.ZERO);
this.schedulePos = new SchedulePos(BlockPos.ZERO, world.dimension().location());
}

public EntityMaid(Level worldIn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.schedule.Activity;

import javax.annotation.Nullable;

public final class SchedulePos {
private BlockPos workPos;
private BlockPos idlePos;
private BlockPos sleepPos;
private ResourceLocation dimension;
private boolean configured = false;

public SchedulePos(BlockPos workPos, BlockPos idlePos, BlockPos sleepPos) {
public SchedulePos(BlockPos workPos, BlockPos idlePos, BlockPos sleepPos, ResourceLocation dimension) {
this.workPos = workPos;
this.idlePos = idlePos;
this.sleepPos = sleepPos;
this.dimension = dimension;
}

public SchedulePos(BlockPos workPos, BlockPos idlePos) {
this(workPos, idlePos, idlePos);
public SchedulePos(BlockPos workPos, BlockPos idlePos, ResourceLocation dimension) {
this(workPos, idlePos, idlePos, dimension);
}

public SchedulePos(BlockPos workPos) {
this(workPos, workPos);
public SchedulePos(BlockPos workPos, ResourceLocation dimension) {
this(workPos, workPos, dimension);
}

public void setWorkPos(BlockPos workPos) {
Expand All @@ -39,11 +44,17 @@ public void setSleepPos(BlockPos sleepPos) {
this.sleepPos = sleepPos;
}

public void setDimension(ResourceLocation dimension) {
this.dimension = dimension;
}

public void save(CompoundTag compound) {
CompoundTag data = new CompoundTag();
data.put("Work", NbtUtils.writeBlockPos(this.workPos));
data.put("Idle", NbtUtils.writeBlockPos(this.idlePos));
data.put("Sleep", NbtUtils.writeBlockPos(this.sleepPos));
data.putString("Dimension", this.dimension.toString());
data.putBoolean("Configured", this.configured);
compound.put("MaidSchedulePos", data);
}

Expand All @@ -53,6 +64,8 @@ public void load(CompoundTag compound, EntityMaid maid) {
this.workPos = NbtUtils.readBlockPos(data.getCompound("Work"));
this.idlePos = NbtUtils.readBlockPos(data.getCompound("Idle"));
this.sleepPos = NbtUtils.readBlockPos(data.getCompound("Sleep"));
this.dimension = new ResourceLocation(data.getString("Dimension"));
this.configured = data.getBoolean("Configured");
this.restrictTo(maid);
}
}
Expand Down Expand Up @@ -92,10 +105,15 @@ public boolean isConfigured() {
return configured;
}

public ResourceLocation getDimension() {
return dimension;
}

public void clear(EntityMaid maid) {
this.idlePos = this.workPos;
this.sleepPos = this.workPos;
this.configured = false;
this.dimension = maid.level.dimension().location();
this.restrictTo(maid);
}

Expand All @@ -104,7 +122,27 @@ public void setHomeModeEnable(EntityMaid maid, BlockPos pos) {
this.workPos = pos;
this.idlePos = pos;
this.sleepPos = pos;
this.dimension = maid.level.dimension().location();
}
this.restrictTo(maid);
}

@Nullable
public BlockPos getNearestPos(EntityMaid maid) {
if (this.configured) {
BlockPos pos = this.workPos;
double workDistance = maid.blockPosition().distSqr(this.workPos);
double idleDistance = maid.blockPosition().distSqr(this.idlePos);
double sleepDistance = maid.blockPosition().distSqr(this.sleepPos);
if (workDistance > idleDistance) {
pos = this.idlePos;
workDistance = idleDistance;
}
if (workDistance > sleepDistance) {
pos = this.sleepPos;
}
return pos;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
Expand All @@ -33,6 +34,11 @@ public static void addPoint(Activity activity, BlockPos pos, ItemStack compass)
tag.put(activity.getName(), NbtUtils.writeBlockPos(pos));
}

public static void addDimension(ResourceLocation dimension, ItemStack compass) {
CompoundTag tag = compass.getOrCreateTagElement("KappaCompassData");
tag.putString("Dimension", dimension.toString());
}

@Nullable
public static BlockPos getPoint(Activity activity, ItemStack compass) {
CompoundTag tag = compass.getTagElement("KappaCompassData");
Expand All @@ -53,6 +59,15 @@ public static BlockPos getPoint(Activity activity, ItemStack compass) {
return null;
}

@Nullable
public static ResourceLocation getDimension(ItemStack compass) {
CompoundTag tag = compass.getTagElement("KappaCompassData");
if (tag != null) {
return new ResourceLocation(tag.getString("Dimension"));
}
return null;
}

public static int getRecordCount(ItemStack compass) {
CompoundTag tag = compass.getTagElement("KappaCompassData");
int count = 0;
Expand Down Expand Up @@ -84,7 +99,13 @@ public InteractionResult interactLivingEntity(ItemStack compass, Player player,
return InteractionResult.SUCCESS;
}
CompoundTag tag = compass.getTagElement("KappaCompassData");
if (tag != null) {
ResourceLocation dimension = getDimension(compass);
if (tag != null || dimension != null) {
if (!maid.level.dimension().location().equals(dimension)) {
player.sendSystemMessage(Component.translatable("message.touhou_little_maid.kappa_compass.maid_dimension_check"));
return InteractionResult.CONSUME;
}
maid.getSchedulePos().setDimension(dimension);
BlockPos point = getPoint(Activity.WORK, compass);
if (point != null) {
maid.getSchedulePos().setWorkPos(point);
Expand All @@ -104,7 +125,7 @@ public InteractionResult interactLivingEntity(ItemStack compass, Player player,
return InteractionResult.SUCCESS;
}
player.sendSystemMessage(Component.translatable("message.touhou_little_maid.kappa_compass.no_data"));
return InteractionResult.FAIL;
return InteractionResult.CONSUME;
}
return super.interactLivingEntity(compass, player, livingEntity, hand);
}
Expand Down Expand Up @@ -144,6 +165,7 @@ public InteractionResult useOn(UseOnContext context) {
addPoint(Activity.WORK, clickedPos, compass);
player.sendSystemMessage(Component.translatable("message.touhou_little_maid.kappa_compass.work", clickedPos.getX(), clickedPos.getY(), clickedPos.getZ()));
}
addDimension(player.level.dimension().location(), compass);
}
player.level.playSound(null, player.blockPosition(), InitSounds.COMPASS_POINT.get(), SoundSource.PLAYERS, 0.8f, 1.5f);
return InteractionResult.SUCCESS;
Expand All @@ -152,9 +174,13 @@ public InteractionResult useOn(UseOnContext context) {
@Override
public void appendHoverText(ItemStack stack, @Nullable Level pLevel, List<Component> components, TooltipFlag pIsAdvanced) {
if (hasKappaCompassData(stack)) {
ResourceLocation dimension = getDimension(stack);
BlockPos workPos = getPoint(Activity.WORK, stack);
BlockPos idlePos = getPoint(Activity.IDLE, stack);
BlockPos sleepPos = getPoint(Activity.REST, stack);
if (dimension != null) {
components.add(Component.translatable("tooltips.touhou_little_maid.fox_scroll.dimension", dimension.toString()).withStyle(ChatFormatting.GRAY));
}
if (workPos != null) {
components.add(Component.translatable("message.touhou_little_maid.kappa_compass.work", workPos.getX(), workPos.getY(), workPos.getZ()).withStyle(ChatFormatting.GRAY));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public static void init() {
Optional.of(NetworkDirection.PLAY_TO_CLIENT));
CHANNEL.registerMessage(26, SetScrollData.class, SetScrollData::encode, SetScrollData::decode, SetScrollData::handle,
Optional.of(NetworkDirection.PLAY_TO_SERVER));
CHANNEL.registerMessage(27, CheckSchedulePosMessage.class, CheckSchedulePosMessage::encode, CheckSchedulePosMessage::decode, CheckSchedulePosMessage::handle,
Optional.of(NetworkDirection.PLAY_TO_CLIENT));
}

public static void sendToClientPlayer(Object message, Player player) {
Expand Down
Loading

0 comments on commit 6a6c786

Please sign in to comment.