Skip to content

Commit

Permalink
Merge pull request #647
Browse files Browse the repository at this point in the history
* Merge pull request #1 from TartaricAcid/1.20

* maid can directly back to handItem when need to swap handItem and the…

* 修正一处错误
  • Loading branch information
Azumic authored Jan 21, 2025
1 parent 2d22eb7 commit c8e6e0b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static ImmutableList<MemoryModuleType<?>> getMemoryTypes() {
MemoryModuleType.WALK_TARGET,
MemoryModuleType.ATTACK_TARGET,
MemoryModuleType.ATTACK_COOLING_DOWN,
InitEntities.TARGET_POS.get()
InitEntities.TARGET_POS.get(),
InitEntities.CURRENT_ITEMSTACK.get()
);
ExtraMaidBrainManager.EXTRA_MAID_BRAINS.forEach(extra -> defaultTypes.addAll(extra.getExtraMemoryTypes()));
return ImmutableList.copyOf(defaultTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private void eatBreatheItem(EntityMaid maid) {
maid.setItemInHand(eanHand, foodStack);
backpackInv.setStackInSlot(i, ItemStack.EMPTY);
ItemHandlerHelper.insertItemStacked(backpackInv, handStack, false);
maid.memoryHandItemStack(handStack);
itemInHand = maid.getItemInHand(eanHand);
hasFood = true;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected void start(ServerLevel serverLevel, EntityMaid maid, long gameTime) {
backpackInv.setStackInSlot(i, ItemStack.EMPTY);
ItemHandlerHelper.insertItemStacked(backpackInv, handStack, false);
itemInHand = maid.getItemInHand(eanHand);
maid.memoryHandItemStack(handStack);
hasFood = true;
break swapItemCheck;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ protected void start(ServerLevel serverLevel, EntityMaid maid, long gameTime) {
// 先对把手上的物品放入背包做预处理:如果放入背包后,手上还有剩余,那就不执行后续吃的逻辑并添加气泡提示
ItemStack itemInHand = maid.getItemInHand(eanHand);
IItemHandlerModifiable availableInv = maid.getAvailableBackpackInv();
ItemStack leftoverStack = ItemHandlerHelper.insertItemStacked(availableInv, itemInHand.copy(), true);
ItemStack handItemCopy = itemInHand.copy();
ItemStack leftoverStack = ItemHandlerHelper.insertItemStacked(availableInv, handItemCopy, true);
if (!leftoverStack.isEmpty()) {
ChatBubbleManger.addInnerChatText(maid, "chat_bubble.touhou_little_maid.inner.home_meal.two_hand_is_full");
return;
Expand Down Expand Up @@ -106,11 +107,12 @@ protected void start(ServerLevel serverLevel, EntityMaid maid, long gameTime) {
candidateFood.intStream().skip(skipCount).findFirst().ifPresent(slotIndex -> {
ItemStack outputStack = handler.extractItem(slotIndex, 1, false);
this.tmpPicnicMat.refresh();
ItemHandlerHelper.insertItemStacked(availableInv, itemInHand.copy(), false);
ItemHandlerHelper.insertItemStacked(availableInv, handItemCopy, false);
maid.setItemInHand(hand, outputStack);
ItemStack refreshItemInHand = maid.getItemInHand(hand);
for (IMaidMeal maidMeal : maidMeals) {
if (maidMeal.canMaidEat(maid, refreshItemInHand, hand)) {
maid.memoryHandItemStack(handItemCopy);
maidMeal.onMaidEat(maid, refreshItemInHand, hand);
if (maid.getOwner() instanceof ServerPlayer serverPlayer) {
InitTrigger.MAID_EVENT.trigger(serverPlayer, TriggerType.MAID_PICNIC_EAT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.tartaricacid.touhoulittlemaid.entity.favorability.Type;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import com.github.tartaricacid.touhoulittlemaid.entity.task.meal.MaidMealManager;
import com.github.tartaricacid.touhoulittlemaid.init.InitEntities;
import com.google.common.collect.ImmutableMap;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -79,6 +80,7 @@ protected void start(ServerLevel serverLevel, EntityMaid maid, long gameTime) {
maid.setItemInHand(eanHand, foodStack);
backpackInv.setStackInSlot(i, ItemStack.EMPTY);
ItemHandlerHelper.insertItemStacked(backpackInv, handStack, false);
maid.memoryHandItemStack(handStack);
itemInHand = maid.getItemInHand(eanHand);
hasFood = true;
break swapItemCheck;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,35 @@ protected void dropEquipment() {
protected void completeUsingItem() {
this.getSwimManager().resetEatBreatheItem();
super.completeUsingItem();
this.backCurrentHandItemStack();
}

/**
* 当需要临时调换手中物品和背包内物品时,可调用此方法
* 当置换后的物品使用完后会自动将之前的手中物品再次返回到手上
*
* @param itemStack 当前手上的物品(必须是能使用--需要持续使用的物品)
*/
public void memoryHandItemStack(ItemStack itemStack) {
this.getBrain().setMemory(InitEntities.CURRENT_ITEMSTACK.get(), itemStack);
}

/**
* 将之前临时存在背包里的物品再次放在对应的手上
*/
public void backCurrentHandItemStack() {
this.getBrain().getMemory(InitEntities.CURRENT_ITEMSTACK.get()).ifPresent(itemStack -> {
InteractionHand usedItemHand = this.getUsedItemHand();
ItemStack itemInHand = this.getItemInHand(usedItemHand);
this.swapHandItem(usedItemHand, itemInHand, itemStack);
this.getBrain().eraseMemory(InitEntities.CURRENT_ITEMSTACK.get());
});
}

private void swapHandItem(InteractionHand hand, ItemStack itemInHand, ItemStack backpackItem) {
ItemStack handItemCopy = itemInHand.copy();
this.setItemInHand(hand, backpackItem.split(backpackItem.getCount()));
ItemHandlerHelper.insertItemStacked(this.getMaidInv(), handItemCopy, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.minecraft.world.entity.schedule.Activity;
import net.minecraft.world.entity.schedule.Schedule;
import net.minecraft.world.entity.schedule.ScheduleBuilder;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
Expand Down Expand Up @@ -65,6 +66,7 @@ public final class InitEntities {

public static RegistryObject<MemoryModuleType<List<Entity>>> VISIBLE_PICKUP_ENTITIES = MEMORY_MODULE_TYPES.register("visible_pickup_entities", () -> new MemoryModuleType<>(Optional.empty()));
public static RegistryObject<MemoryModuleType<PositionTracker>> TARGET_POS = MEMORY_MODULE_TYPES.register("target_pos", () -> new MemoryModuleType<>(Optional.empty()));
public static RegistryObject<MemoryModuleType<ItemStack>> CURRENT_ITEMSTACK = MEMORY_MODULE_TYPES.register("current_itemstack", () -> new MemoryModuleType<>(Optional.empty()));
public static RegistryObject<SensorType<MaidNearestLivingEntitySensor>> MAID_NEAREST_LIVING_ENTITY_SENSOR = SENSOR_TYPES.register("maid_nearest_living_entity", () -> new SensorType<>(MaidNearestLivingEntitySensor::new));
public static RegistryObject<SensorType<MaidHostilesSensor>> MAID_HOSTILES_SENSOR = SENSOR_TYPES.register("maid_hostiles", () -> new SensorType<>(MaidHostilesSensor::new));
public static RegistryObject<SensorType<MaidPickupEntitiesSensor>> MAID_PICKUP_ENTITIES_SENSOR = SENSOR_TYPES.register("maid_pickup_entities", () -> new SensorType<>(MaidPickupEntitiesSensor::new));
Expand Down

0 comments on commit c8e6e0b

Please sign in to comment.