Skip to content

Commit 1f98090

Browse files
committed
添加监听事件,设置猫娘生命值,修改基础信息
1 parent f531e7e commit 1f98090

File tree

6 files changed

+84
-18
lines changed

6 files changed

+84
-18
lines changed

.idea/workspace.xml

+14-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

-1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,5 @@ kotlin {
108108
javadoc {
109109
options {
110110
encoding = 'UTF-8' // 设置编码格式
111-
charset = 'UTF-8'
112111
}
113112
}

gradle.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ org.gradle.jvmargs=-Xmx1G
88
loader_version=0.14.23
99

1010
# Mod Properties
11-
mod_version = 0.2.7
11+
mod_version = 0.2.8
1212
maven_group = com.crystalneko
13-
archives_base_name = toNekoFabric
13+
archives_base_name = toNeko
1414

1515
# Dependencies
1616
# check this on https://modmuss50.me/fabric.html

src/main/java/com/crystalneko/tonekofabric/api/NekoEntityEvents.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import com.crystalneko.tonekofabric.entity.nekoEntity;
44
import net.fabricmc.fabric.api.event.Event;
55
import net.fabricmc.fabric.api.event.EventFactory;
6+
import net.minecraft.entity.Entity;
67
import net.minecraft.entity.player.PlayerEntity;
78
import net.minecraft.util.ActionResult;
89
import net.minecraft.util.Hand;
10+
import org.bukkit.event.world.EntitiesLoadEvent;
11+
912
public class NekoEntityEvents {
1013

1114
/**
@@ -40,7 +43,27 @@ public class NekoEntityEvents {
4043
for (GrowUpEvent listener : listeners) {
4144
listener.onGrowUp(entity, age, overGrow);
4245
}
43-
});
46+
});
47+
48+
/**
49+
* 猫娘实体被攻击时的回调<br>
50+
* 它会在执行原本的代码前被执行<br>
51+
* 如果返回true,则不会继续往下执行,攻击将不会生效<br>
52+
* 参数:<br>
53+
* - neko: 猫腻实体<br>
54+
* - attacker: 攻击者
55+
*/
56+
public static Event<OnAttackEvent> ON_ATTACK = EventFactory.createArrayBacked(OnAttackEvent.class,
57+
(listeners) -> (neko,attacker) -> {
58+
for (OnAttackEvent listener : listeners) {
59+
listener.onAttack(neko,attacker);
60+
boolean result = listener.onAttack(neko,attacker);
61+
if(result){
62+
return true;
63+
}
64+
}
65+
return false;
66+
});
4467

4568
public interface InteractEvent {
4669
ActionResult onInteract(nekoEntity entity, PlayerEntity player, Hand hand);
@@ -49,4 +72,7 @@ public interface InteractEvent {
4972
public interface GrowUpEvent {
5073
void onGrowUp(nekoEntity entity, int age, boolean overGrow);
5174
}
75+
public interface OnAttackEvent {
76+
boolean onAttack(nekoEntity entity, Entity attacker);
77+
}
5278
}

src/main/java/com/crystalneko/tonekofabric/entity/ai/FollowAndAttackPlayerGoal.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ public class FollowAndAttackPlayerGoal extends Goal {
1717
private final double speed;
1818
private final double minDistanceSq;
1919
private final double maxDistanceSq;
20+
private float amount;
2021

21-
public FollowAndAttackPlayerGoal(AnimalEntity mobEntity, PlayerEntity targetPlayer, double speed, double minDistance, double maxDistance) {
22+
public FollowAndAttackPlayerGoal(AnimalEntity mobEntity, PlayerEntity targetPlayer, double speed, double minDistance, double maxDistance, float amount) {
2223
this.mobEntity = mobEntity;
2324
this.targetPlayer = targetPlayer;
2425
this.speed = speed;
2526
this.minDistanceSq = minDistance * minDistance;
2627
this.maxDistanceSq = maxDistance * maxDistance;
28+
this.amount = amount;
2729
this.setControls(EnumSet.of(Control.MOVE, Control.LOOK));
2830
}
2931

@@ -56,7 +58,7 @@ public void tick() {
5658
// 如果攻击目标还活着,就判断距离目标的位置
5759
double distance = mobEntity.distanceTo(targetPlayer);
5860
if (distance <= 1.2) {
59-
targetPlayer.damage(mobEntity.getDamageSources().generic(),2.0F);
61+
targetPlayer.damage(mobEntity.getDamageSources().generic(),amount);
6062
if(!targetPlayer.isAlive()){
6163
targetPlayer.sendMessage(Text.translatable("attack.death.normal_rod"));
6264
}

src/main/java/com/crystalneko/tonekofabric/entity/nekoEntity.java

+37-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
import com.crystalneko.tonekofabric.api.NekoEntityEvents;
66
import com.crystalneko.tonekofabric.entity.ai.FollowAndAttackPlayerGoal;
77
import com.crystalneko.tonekofabric.libs.base;
8+
import net.minecraft.entity.Entity;
89
import net.minecraft.entity.EntityType;
910
import net.minecraft.entity.LivingEntity;
1011
import net.minecraft.entity.MovementType;
11-
import net.minecraft.entity.ai.goal.AnimalMateGoal;
12-
import net.minecraft.entity.ai.goal.LookAtEntityGoal;
13-
import net.minecraft.entity.ai.goal.SwimGoal;
14-
import net.minecraft.entity.ai.goal.TemptGoal;
12+
import net.minecraft.entity.ai.goal.*;
13+
import net.minecraft.entity.attribute.EntityAttribute;
1514
import net.minecraft.entity.attribute.EntityAttributes;
1615
import net.minecraft.entity.effect.StatusEffectInstance;
1716
import net.minecraft.entity.effect.StatusEffects;
@@ -49,6 +48,11 @@
4948
<li><code>{@link nekoEntity#nekoEntity}</code></li>
5049
</ul>
5150
</li>
51+
<li>实体属性
52+
<ul>
53+
<li><code>{@link nekoEntity#getAttributeValue}</code></li>
54+
</ul>
55+
</li>
5256
<li>繁殖
5357
<ul>
5458
<li>判断繁殖物品是否有效:<code>{@link nekoEntity#isBreedingItem(ItemStack)}</code></li>
@@ -63,6 +67,7 @@
6367
</li>
6468
<li>玩家操作
6569
<ul>
70+
<li>攻击时的操作: <code>{@link nekoEntity#handleAttack(Entity)}</code></li>
6671
<li>右键点击操作:<code>{@link nekoEntity#interactMob(PlayerEntity, Hand)}</code></li>
6772
<li>骑行:
6873
<ul>
@@ -145,6 +150,7 @@ public class nekoEntity extends AnimalEntity implements GeoEntity {
145150
public nekoEntity(EntityType<? extends AnimalEntity> entityType, World world) {
146151
super(entityType, world);
147152
Objects.requireNonNull(this.getAttributeInstance(EntityAttributes.GENERIC_MOVEMENT_SPEED)).setBaseValue(0.6D);
153+
this.speed = 0.6F;
148154
//设置碰撞箱
149155
if (this.isBaby()){
150156
//如果是baby,则为成体的一半
@@ -153,6 +159,15 @@ public nekoEntity(EntityType<? extends AnimalEntity> entityType, World world) {
153159
this.setBoundingBox(this.boundingBox);
154160
}
155161
}
162+
// ---------------------------------------------------------属性-------------------------------------------------
163+
@Override
164+
public double getAttributeValue(EntityAttribute attribute) {
165+
if(attribute == EntityAttributes.GENERIC_MAX_HEALTH){
166+
// 最大生命值
167+
return 15.0;
168+
}
169+
return this.getAttributes().getValue(attribute);
170+
}
156171

157172
// --------------------------------------------------------繁殖-----------------------------------------------------
158173
@Override
@@ -184,9 +199,26 @@ protected void initGoals() {
184199
this.goalSelector.add(2, temptGoal);
185200
this.goalSelector.add(12, new LookAtEntityGoal(this, PlayerEntity.class, 10.0F));
186201
this.goalSelector.add(3, new AnimalMateGoal(this, 0.8));
202+
this.goalSelector.add(10,new EscapeDangerGoal(this, this.speed*0.5));
187203
}
188204

189205
// -----------------------------------------------------玩家操作----------------------------------------------------
206+
// 玩家攻击实体时
207+
@Override
208+
public boolean handleAttack(Entity entity){
209+
// 注册监听事件
210+
boolean result = NekoEntityEvents.ON_ATTACK.invoker().onAttack(this, entity);
211+
if(result){
212+
return true;
213+
}
214+
if (entity instanceof PlayerEntity player){
215+
// 添加仇恨
216+
this.increaseHatred(player,100);
217+
return false;
218+
}
219+
return super.handleAttack(entity);
220+
}
221+
190222
// 玩家右键点击
191223
@Override
192224
public ActionResult interactMob(PlayerEntity player, Hand hand) {
@@ -250,7 +282,7 @@ public void increaseHatred(LivingEntity target, int amount) {
250282
int currentHatred = hatredMap.getOrDefault(target, 0);
251283
if(target instanceof PlayerEntity targetPlayer){
252284
//让实体尝试跟随目标
253-
neko.goalSelector.add(1,new FollowAndAttackPlayerGoal(neko,targetPlayer,0.8D,0.1F,100.0F));
285+
neko.goalSelector.add(1,new FollowAndAttackPlayerGoal(neko,targetPlayer,this.speed*2,0.1F,100.0F,2.0F));
254286
}
255287
hatredMap.put(target, currentHatred + amount);
256288
}

0 commit comments

Comments
 (0)