Skip to content

Commit

Permalink
添加猫娘工作行为
Browse files Browse the repository at this point in the history
  • Loading branch information
CSneko committed Feb 5, 2025
1 parent 69fc961 commit a98150b
Show file tree
Hide file tree
Showing 13 changed files with 445 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ public void removeAlias(UUID owner, String alias) {
}

public void addXp(UUID owner, int xp) {
addLevel((double) xp / 1000.00d);
double level = (double) xp / 1000.00d;
addLevel(level);
getNeko(owner).addLevel(level);
processOwners(owner, o -> o.xp += xp);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.cneko.toneko.common.api.Messaging;
import org.cneko.toneko.common.mod.ai.Prompts;
import org.cneko.toneko.common.mod.api.events.ChatEvents;
import org.cneko.toneko.common.mod.commands.arguments.CustomStringArgument;
import org.cneko.toneko.common.mod.commands.arguments.NekoArgument;
import org.cneko.toneko.common.mod.events.CommonChatEvent;
import org.cneko.toneko.common.mod.util.PlayerUtil;
Expand All @@ -25,6 +26,10 @@ public static void bootstrap() {
ArgumentTypeRegistry.registerArgumentType(
toNekoLoc("neko"),
NekoArgument.class, SingletonArgumentInfo.contextFree(NekoArgument::neko));
ArgumentTypeRegistry.registerArgumentType(
toNekoLoc("custom_string"),
CustomStringArgument.class,SingletonArgumentInfo.contextFree(CustomStringArgument::replaceWord)
);
Prompts.init();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.cneko.toneko.common.Bootstrap;
import org.cneko.toneko.common.api.NekoQuery;
import org.cneko.toneko.common.api.Permissions;
import org.cneko.toneko.common.mod.commands.arguments.CustomStringArgument;
import org.cneko.toneko.common.mod.commands.arguments.NekoArgument;
import org.cneko.toneko.common.mod.commands.arguments.NekoSuggestionProvider;
import org.cneko.toneko.common.mod.commands.arguments.WordSuggestionProvider;
Expand Down Expand Up @@ -49,7 +50,7 @@ public static void init(){
.suggests(new NekoSuggestionProvider(true))
//-------------------------------------add---------------------------------------
.then(literal("add")
.then(argument("aliases", StringArgumentType.word())
.then(argument("aliases", CustomStringArgument.blockWord())
.executes(ToNekoCommand::AliasesAdd)
)
).then(literal("remove")
Expand All @@ -67,8 +68,8 @@ public static void init(){
.suggests(new NekoSuggestionProvider(true))
//--------------------------------add------------------------------------
.then(literal("add")
.then(argument("block",StringArgumentType.word())
.then(argument("replace",StringArgumentType.word())
.then(argument("block",CustomStringArgument.blockWord())
.then(argument("replace",CustomStringArgument.replaceWord())
.then(argument("method",StringArgumentType.word())
.suggests((context, builder) -> {
builder.suggest("all");
Expand All @@ -82,7 +83,7 @@ public static void init(){
)
//----------------------------remove----------------------------------
.then(literal("remove")
.then(argument("block",StringArgumentType.word())
.then(argument("block",CustomStringArgument.blockWord())
.executes(ToNekoCommand::removeBlock)
.suggests(WordSuggestionProvider.blockWord())
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@
public class CustomStringArgument implements ArgumentType<String> {
private final Pattern pattern;
private final boolean allowEmpty;
// 支持所有字符(不包括Emoji),长度限制为1~20
private static final Pattern NO_EMOJI = Pattern.compile("^[\\x00-\\x7F\\u0080-\\uFFFF]{1,20}$");
// 支持所有字符(包括Emoji),长度限制为1~40
public static final Pattern ALLOW_EMOJI = Pattern.compile("^.{1,40}$\n");
public static final Pattern ALLOW_EMOJI = Pattern.compile("^.{1,40}$");

private CustomStringArgument(Pattern pattern, boolean allowEmpty) {
this.pattern = pattern;
this.allowEmpty = allowEmpty;
}

public static CustomStringArgument blockWord() {
return new CustomStringArgument(NO_EMOJI, false);
return new CustomStringArgument(ALLOW_EMOJI, false);
}
public static CustomStringArgument replaceWord() {
return new CustomStringArgument(ALLOW_EMOJI, true);
Expand Down Expand Up @@ -52,8 +50,7 @@ public String parse(StringReader reader) throws CommandSyntaxException {
throw new SimpleCommandExceptionType(Component.literal("输入不能为空")).create();
}

// 正则验证
if (pattern != null && !pattern.matcher(result).matches()) {
if (pattern == null || !pattern.matcher(result).matches()) {
throw new SimpleCommandExceptionType(Component.literal("输入格式无效")).create();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import org.cneko.toneko.common.mod.entities.ai.goal.NekoCropGatheringGoal;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
Expand All @@ -25,6 +26,12 @@ public AdventurerNeko(EntityType<? extends NekoEntity> entityType, Level level)
}


@Override
public void registerGoals() {
super.registerGoals();
this.goalSelector.addGoal(10, new NekoCropGatheringGoal(this));
}

@Override
public boolean isFavoriteItem(ItemStack stack) {
return super.isFavoriteItem(stack) || stack.is(ItemTags.SWORDS) || stack.is(ItemTags.CHEST_ARMOR) || stack.is(ItemTags.HEAD_ARMOR) || stack.is(ItemTags.LEG_ARMOR) || stack.is(ItemTags.FOOT_ARMOR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ public void setMoeTags(List<String> moeTags){
this.entityData.set(MOE_TAGS_ID, String.join(":", moeTags));
}

// 采集动力
protected int gatheringPower = 0;
// 添加采集动力
public void addGatheringPower(int power) {
this.gatheringPower += power;
}
// 获取采集动力
public int getGatheringPower() {
return this.gatheringPower;
}
// 消耗采集动力
public void consumeGatheringPower(int amount) {
this.gatheringPower = Math.max(0, this.gatheringPower - amount);
}


// 最喜欢的物品
public boolean isFavoriteItem(ItemStack stack){
return stack.is(ToNekoItems.CATNIP_TAG);
Expand All @@ -280,6 +296,8 @@ public boolean giftItem(Player player, int slot){
public boolean giftItem(Player player, ItemStack stack){
// 如果是喜欢的物品
if (this.isLikedItem(stack) && player instanceof ServerPlayer sp){
// 增长动力
this.addGatheringPower(20);
// 达成进度
ToNekoCriteria.GIFT_NEKO.trigger(sp);
// 如果是猫薄荷,则吃下它
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,42 @@ public void tick() {
}

public boolean add(ItemStack stack) {
return this.add(-1, stack);
if (stack.isEmpty()){
return false;
}

// 尝试与已有的同类物品进行合并
for (ItemStack currentStack : this.items) {
// 判断槽位中已有物品,并且与待加入物品为同种(包括NBT数据)
if (!currentStack.isEmpty() && ItemStack.isSameItemSameComponents(currentStack, stack)) {
int maxStackSize = stack.getMaxStackSize();
int availableSpace = maxStackSize - currentStack.getCount();
if (availableSpace > 0) {
int toAdd = Math.min(availableSpace, stack.getCount());
currentStack.grow(toAdd);
stack.shrink(toAdd);
// 如果待加入物品已经全部合并,则返回成功
if (stack.isEmpty()) {
return true;
}
}
}
}

// 如果合并后仍有剩余的物品,则寻找空槽位存放剩余部分
for (int i = 0; i < this.items.size(); ++i) {
if (this.items.get(i).isEmpty()) {
// 注意这里需要复制一份stack,防止引用问题
this.items.set(i, stack.copy());
// 清空原来的stack
stack.setCount(0);
return true;
}
}
return false;
}


public boolean add(int slot, ItemStack stack) {
if (stack.isEmpty()) {
return false;
Expand Down
Loading

0 comments on commit a98150b

Please sign in to comment.