Skip to content

Commit f58d440

Browse files
committed
fix: mixins issues with the mod ForgifiedFabricAPI
1 parent e4225cc commit f58d440

11 files changed

Lines changed: 143 additions & 101 deletions

File tree

patches/net/minecraft/server/level/ServerLevel.java.patch

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,21 @@
343343
}
344344
}
345345

346-
@@ -872,18 +_,40 @@
346+
@@ -870,20 +_,48 @@
347+
return list.isEmpty() ? null : list.get(this.random.nextInt(list.size()));
348+
}
347349

350+
+ // Magma
351+
+ private org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason magma$spawnReason = org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DEFAULT;
352+
+ public void setSpawnReason(org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason reason) {
353+
+ magma$spawnReason = reason;
354+
+ }
355+
+
348356
@Override
349357
public boolean addFreshEntity(Entity p_8837_) {
350358
- return this.addEntity(p_8837_);
351359
+ // CraftBukkit start
352-
+ return this.addFreshEntity(p_8837_, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DEFAULT);
360+
+ return this.addFreshEntity(p_8837_, magma$spawnReason);
353361
+ }
354362
+
355363
+ @Override

patches/net/minecraft/server/level/ServerPlayer.java.patch

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,61 +33,18 @@
3333
public ServerPlayer(MinecraftServer p_254143_, ServerLevel p_254435_, GameProfile p_253651_, ClientInformation p_301997_) {
3434
super(p_254435_, p_254435_.getSharedSpawnPos(), p_254435_.getSharedSpawnAngle(), p_253651_);
3535
this.textFilter = p_254143_.createTextFilterForPlayer(this);
36-
@@ -287,7 +_,53 @@
36+
@@ -287,6 +_,11 @@
3737
this.moveTo(this.adjustSpawnLocation(p_254435_, p_254435_.getSharedSpawnPos()).getBottomCenter(), 0.0F, 0.0F);
3838
this.updateOptions(p_301997_);
3939
this.object = null;
40-
- }
4140
+
4241
+ // CraftBukkit start
4342
+ this.displayName = this.getScoreboardName();
4443
+ this.bukkitPickUpLoot = true;
4544
+ this.maxHealthCache = this.getMaxHealth();
46-
+ }
47-
+
48-
+ // Use method to resend items in hands in case of client desync, because the item use got cancelled.
49-
+ // For example, when cancelling the leash event
50-
+ public void resendItemInHands() {
51-
+ containerMenu.findSlot(getInventory(), getInventory().selected).ifPresent(s -> {
52-
+ containerSynchronizer.sendSlotChange(containerMenu, s, getMainHandItem());
53-
+ });
54-
+ containerSynchronizer.sendSlotChange(inventoryMenu, net.minecraft.world.inventory.InventoryMenu.SHIELD_SLOT, getOffhandItem());
55-
+ }
56-
+
57-
+ // Yes, this doesn't match Vanilla, but it's the best we can do for now.
58-
+ // If this is an issue, PRs are welcome
59-
+ public final BlockPos getSpawnPoint(ServerLevel worldserver) {
60-
+ BlockPos blockposition = worldserver.getSharedSpawnPos();
61-
+ if (worldserver.dimensionType().hasSkyLight() && worldserver.serverLevelData.getGameType() != GameType.ADVENTURE) {
62-
+ int i = Math.max(0, this.server.getSpawnRadius(worldserver));
63-
+ int j = Mth.floor(worldserver.getWorldBorder().getDistanceToBorder((double) blockposition.getX(), (double) blockposition.getZ()));
64-
+ if (j < i) {
65-
+ i = j;
66-
+ }
67-
+ if (j <= 1) {
68-
+ i = 1;
69-
+ }
70-
+ long k = (long) (i * 2 + 1);
71-
+ long l = k * k;
72-
+ int i1 = l > 2147483647L ? Integer.MAX_VALUE : (int) l;
73-
+ int j1 = this.getCoprime(i1);
74-
+ int k1 = RandomSource.create().nextInt(i1);
75-
+ for (int l1 = 0; l1 < i1; ++l1) {
76-
+ int i2 = (k1 + j1 * l1) % i1;
77-
+ int j2 = i2 % (i * 2 + 1);
78-
+ int k2 = i2 / (i * 2 + 1);
79-
+ BlockPos blockposition1 = PlayerRespawnLogic.getOverworldRespawnPos(worldserver, blockposition.getX() + j2 - i, blockposition.getZ() + k2 - i);
80-
+ if (blockposition1 != null) {
81-
+ return blockposition1;
82-
+ }
83-
+ }
84-
+ }
85-
+ return blockposition;
86-
+ }
87-
+ // CraftBukkit end
45+
}
8846

8947
@Override
90-
public BlockPos adjustSpawnLocation(ServerLevel p_352206_, BlockPos p_352202_) {
9148
@@ -357,11 +_,20 @@
9249
if (p_9131_.contains("recipeBook", 10)) {
9350
this.recipeBook.fromNbt(p_9131_.getCompound("recipeBook"), this.server.getRecipeManager());
@@ -914,7 +871,7 @@
914871
}
915872

916873
private static float calculateLookAtYaw(Vec3 p_348686_, BlockPos p_348467_) {
917-
@@ -1968,4 +_,140 @@
874+
@@ -1968,4 +_,180 @@
918875
return (float)Mth.wrapDegrees(Mth.atan2(vec3.z, vec3.x) * 180.0F / (float)Math.PI - 90.0);
919876
}
920877
}
@@ -1053,5 +1010,45 @@
10531010
+ public org.bukkit.craftbukkit.entity.CraftPlayer getBukkitEntity() {
10541011
+ return (org.bukkit.craftbukkit.entity.CraftPlayer) super.getBukkitEntity();
10551012
+ }
1013+
+
1014+
+ // Use method to resend items in hands in case of client desync, because the item use got cancelled.
1015+
+ // For example, when cancelling the leash event
1016+
+ public void resendItemInHands() {
1017+
+ containerMenu.findSlot(getInventory(), getInventory().selected).ifPresent(s -> {
1018+
+ containerSynchronizer.sendSlotChange(containerMenu, s, getMainHandItem());
1019+
+ });
1020+
+ containerSynchronizer.sendSlotChange(inventoryMenu, net.minecraft.world.inventory.InventoryMenu.SHIELD_SLOT, getOffhandItem());
1021+
+ }
1022+
+
1023+
+ // Yes, this doesn't match Vanilla, but it's the best we can do for now.
1024+
+ // If this is an issue, PRs are welcome
1025+
+ public final BlockPos getSpawnPoint(ServerLevel worldserver) {
1026+
+ BlockPos blockposition = worldserver.getSharedSpawnPos();
1027+
+ if (worldserver.dimensionType().hasSkyLight() && worldserver.serverLevelData.getGameType() != GameType.ADVENTURE) {
1028+
+ int i = Math.max(0, this.server.getSpawnRadius(worldserver));
1029+
+ int j = Mth.floor(worldserver.getWorldBorder().getDistanceToBorder((double) blockposition.getX(), (double) blockposition.getZ()));
1030+
+ if (j < i) {
1031+
+ i = j;
1032+
+ }
1033+
+ if (j <= 1) {
1034+
+ i = 1;
1035+
+ }
1036+
+ long k = (long) (i * 2 + 1);
1037+
+ long l = k * k;
1038+
+ int i1 = l > 2147483647L ? Integer.MAX_VALUE : (int) l;
1039+
+ int j1 = this.getCoprime(i1);
1040+
+ int k1 = RandomSource.create().nextInt(i1);
1041+
+ for (int l1 = 0; l1 < i1; ++l1) {
1042+
+ int i2 = (k1 + j1 * l1) % i1;
1043+
+ int j2 = i2 % (i * 2 + 1);
1044+
+ int k2 = i2 / (i * 2 + 1);
1045+
+ BlockPos blockposition1 = PlayerRespawnLogic.getOverworldRespawnPos(worldserver, blockposition.getX() + j2 - i, blockposition.getZ() + k2 - i);
1046+
+ if (blockposition1 != null) {
1047+
+ return blockposition1;
1048+
+ }
1049+
+ }
1050+
+ }
1051+
+ return blockposition;
1052+
+ }
10561053
+ // CraftBukkit end
10571054
}

patches/net/minecraft/world/entity/LivingEntity.java.patch

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@
466466
} else if (this.level().isClientSide) {
467467
return false;
468468
- } else if (this.isDeadOrDying()) {
469-
+ } else if (this.isRemoved() || this.dead || this.getHealth() <= 0.0F) { // CraftBukkit - Don't allow entities that got set to dead/killed elsewhere to get damaged and die
469+
+ } else if (this.isRemoved() || this.dead || this.isDeadOrDying()) { // CraftBukkit - Don't allow entities that got set to dead/killed elsewhere to get damaged and die
470470
return false;
471471
} else if (p_21016_.is(DamageTypeTags.IS_FIRE) && this.hasEffect(MobEffects.FIRE_RESISTANCE)) {
472472
return false;
@@ -941,51 +941,80 @@
941941
}
942942

943943
return p_21194_;
944-
@@ -1677,13 +_,192 @@
944+
@@ -1677,13 +_,221 @@
945945
}
946946
}
947947

948948
- protected void actuallyHurt(DamageSource p_21240_, float p_21241_) {
949949
+ // Magma - rewriten this method to work with neoforge without breaking the original code and to many patches
950950
+ private org.bukkit.event.entity.EntityDamageEvent handleEntityDamage(final DamageSource damagesource, float f) {
951951
+ float originalDamage = f;
952-
+ com.google.common.base.Function<Double, Double> freezing = f4 -> {
953-
+ if (damagesource.is(net.minecraft.tags.DamageTypeTags.IS_FREEZING) && net.minecraft.world.entity.LivingEntity.this.getType().is(net.minecraft.tags.EntityTypeTags.FREEZE_HURTS_EXTRA_TYPES)) {
954-
+ return -(f4 - (f4 * 5.0F));
952+
+ com.google.common.base.Function<Double, Double> freezing = new com.google.common.base.Function<Double, Double>() {
953+
+ @Override
954+
+ public Double apply(Double f4) {
955+
+ if (damagesource.is(net.minecraft.tags.DamageTypeTags.IS_FREEZING) && net.minecraft.world.entity.LivingEntity.this.getType().is(net.minecraft.tags.EntityTypeTags.FREEZE_HURTS_EXTRA_TYPES)) {
956+
+ return Double.valueOf(-(f4 - (f4 * 5.0F)));
957+
+ }
958+
+ return Double.valueOf(-0.0);
955959
+ }
956-
+ return -0.0;
957960
+ };
958961
+ float freezingModifier = freezing.apply((double) f).floatValue();
959962
+ f += freezingModifier;
960-
+ com.google.common.base.Function<Double, Double> hardHat = f5 -> {
961-
+ if (damagesource.is(net.minecraft.tags.DamageTypeTags.DAMAGES_HELMET) && !net.minecraft.world.entity.LivingEntity.this.getItemBySlot(net.minecraft.world.entity.EquipmentSlot.HEAD).isEmpty()) {
962-
+ return -(f5 - (f5 * 0.75F));
963+
+ com.google.common.base.Function<Double, Double> hardHat = new com.google.common.base.Function<Double, Double>() {
964+
+ @Override
965+
+ public Double apply(Double f5) {
966+
+ if (damagesource.is(net.minecraft.tags.DamageTypeTags.DAMAGES_HELMET) && !net.minecraft.world.entity.LivingEntity.this.getItemBySlot(net.minecraft.world.entity.EquipmentSlot.HEAD).isEmpty()) {
967+
+ return Double.valueOf(-(f5 - (f5 * 0.75F)));
968+
+ }
969+
+ return Double.valueOf(-0.0);
963970
+ }
964-
+ return -0.0;
965971
+ };
966972
+ float hardHatModifier = hardHat.apply((double) f).floatValue();
967973
+ f += hardHatModifier;
968-
+ com.google.common.base.Function<Double, Double> blocking = f6 -> -((net.minecraft.world.entity.LivingEntity.this.isDamageSourceBlocked(damagesource)) ? f6 : 0.0);
974+
+ com.google.common.base.Function<Double, Double> blocking = new com.google.common.base.Function<Double, Double>() {
975+
+ @Override
976+
+ public Double apply(Double f6) {
977+
+ return Double.valueOf(-((net.minecraft.world.entity.LivingEntity.this.isDamageSourceBlocked(damagesource)) ? f6 : 0.0));
978+
+ }
979+
+ };
969980
+ float blockingModifier = blocking.apply((double) f).floatValue();
970981
+ f += blockingModifier;
971-
+ com.google.common.base.Function<Double, Double> armor = f7 -> -(f7 - net.minecraft.world.entity.LivingEntity.this.getDamageAfterArmorAbsorb(damagesource, f7.floatValue()));
982+
+ com.google.common.base.Function<Double, Double> armor = new com.google.common.base.Function<Double, Double>() {
983+
+ @Override
984+
+ public Double apply(Double f7) {
985+
+ return Double.valueOf(-(f7 - net.minecraft.world.entity.LivingEntity.this.getDamageAfterArmorAbsorb(damagesource, f7.floatValue())));
986+
+ }
987+
+ };
972988
+ float armorModifier = armor.apply((double) f).floatValue();
973989
+ f += armorModifier;
974-
+ com.google.common.base.Function<Double, Double> resistance = f8 -> {
975-
+ if (!damagesource.is(net.minecraft.tags.DamageTypeTags.BYPASSES_EFFECTS) && net.minecraft.world.entity.LivingEntity.this.hasEffect(net.minecraft.world.effect.MobEffects.DAMAGE_RESISTANCE) && !damagesource.is(net.minecraft.tags.DamageTypeTags.BYPASSES_RESISTANCE)) {
976-
+ int i = (net.minecraft.world.entity.LivingEntity.this.getEffect(net.minecraft.world.effect.MobEffects.DAMAGE_RESISTANCE).getAmplifier() + 1) * 5;
977-
+ int j = 25 - i;
978-
+ float f1 = f8.floatValue() * (float) j;
979-
+ return -(f8 - (f1 / 25.0F));
990+
+ com.google.common.base.Function<Double, Double> resistance = new com.google.common.base.Function<Double, Double>() {
991+
+ @Override
992+
+ public Double apply(Double f8) {
993+
+ if (!damagesource.is(net.minecraft.tags.DamageTypeTags.BYPASSES_EFFECTS) && net.minecraft.world.entity.LivingEntity.this.hasEffect(net.minecraft.world.effect.MobEffects.DAMAGE_RESISTANCE) && !damagesource.is(net.minecraft.tags.DamageTypeTags.BYPASSES_RESISTANCE)) {
994+
+ int i = (net.minecraft.world.entity.LivingEntity.this.getEffect(net.minecraft.world.effect.MobEffects.DAMAGE_RESISTANCE).getAmplifier() + 1) * 5;
995+
+ int j = 25 - i;
996+
+ float f1 = f8.floatValue() * (float) j;
997+
+ return Double.valueOf(-(f8 - (f1 / 25.0F)));
998+
+ }
999+
+ return Double.valueOf(-0.0);
9801000
+ }
981-
+ return -0.0;
9821001
+ };
9831002
+ float resistanceModifier = resistance.apply((double) f).floatValue();
9841003
+ f += resistanceModifier;
985-
+ com.google.common.base.Function<Double, Double> magic = f9 -> -(f9 - net.minecraft.world.entity.LivingEntity.this.getDamageAfterMagicAbsorb(damagesource, f9.floatValue()));
1004+
+ com.google.common.base.Function<Double, Double> magic = new com.google.common.base.Function<Double, Double>() {
1005+
+ @Override
1006+
+ public Double apply(Double f9) {
1007+
+ return Double.valueOf(-(f9 - net.minecraft.world.entity.LivingEntity.this.getDamageAfterMagicAbsorb(damagesource, f9.floatValue())));
1008+
+ }
1009+
+ };
9861010
+ float magicModifier = magic.apply((double) f).floatValue();
9871011
+ f += magicModifier;
988-
+ com.google.common.base.Function<Double, Double> absorption = f10 -> -(Math.max(f10 - Math.max(f10 - net.minecraft.world.entity.LivingEntity.this.getAbsorptionAmount(), 0.0F), 0.0F));
1012+
+ com.google.common.base.Function<Double, Double> absorption = new com.google.common.base.Function<Double, Double>() {
1013+
+ @Override
1014+
+ public Double apply(Double f10) {
1015+
+ return Double.valueOf(-(Math.max(f10 - Math.max(f10 - net.minecraft.world.entity.LivingEntity.this.getAbsorptionAmount(), 0.0F), 0.0F)));
1016+
+ }
1017+
+ };
9891018
+ float absorptionModifier = absorption.apply((double) f).floatValue();
9901019
+
9911020
+ return org.bukkit.craftbukkit.event.CraftEventFactory.handleLivingEntityDamageEvent(this, damagesource, originalDamage, freezingModifier, hardHatModifier, blockingModifier, armorModifier, resistanceModifier, magicModifier, absorptionModifier, freezing, hardHat, blocking, armor, resistance, magic, absorption);

patches/net/minecraft/world/entity/Mob.java.patch

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -245,32 +245,38 @@
245245
return p_21437_;
246246
}
247247

248-
@@ -1270,8 +_,15 @@
248+
@@ -1270,6 +_,20 @@
249249
return this.restrictRadius != -1.0F;
250250
}
251251

252252
+ // CraftBukkit start
253-
@Nullable
254-
public <T extends Mob> T convertTo(EntityType<T> p_21407_, boolean p_21408_) {
255-
+ return this.convertTo(p_21407_, p_21408_, org.bukkit.event.entity.EntityTransformEvent.TransformReason.UNKNOWN, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DEFAULT);
253+
+ private org.bukkit.event.entity.EntityTransformEvent.TransformReason magma$transformReason = org.bukkit.event.entity.EntityTransformEvent.TransformReason.UNKNOWN;
254+
+ private org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason magma$spawnReason = org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DEFAULT;
255+
+ @Nullable
256+
+ public <T extends Mob> T convertToCB(EntityType<T> p_21407_, boolean p_21408_, org.bukkit.event.entity.EntityTransformEvent.TransformReason transformReason, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason spawnReason) {
257+
+ magma$transformReason = transformReason;
258+
+ magma$spawnReason = spawnReason;
259+
+ var mob = this.convertTo(p_21407_, p_21408_);
260+
+ magma$transformReason = org.bukkit.event.entity.EntityTransformEvent.TransformReason.UNKNOWN;
261+
+ magma$spawnReason = org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DEFAULT;
262+
+ return mob;
256263
+ }
264+
+ // CraftBukkit end
257265
+
258-
+ @Nullable
259-
+ public <T extends Mob> T convertTo(EntityType<T> p_21407_, boolean p_21408_, org.bukkit.event.entity.EntityTransformEvent.TransformReason transformReason, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason spawnReason) {
260-
+ // CraftBukkit end
266+
@Nullable
267+
public <T extends Mob> T convertTo(EntityType<T> p_21407_, boolean p_21408_) {
261268
if (this.isRemoved()) {
262-
return null;
263-
} else {
264-
@@ -1304,13 +_,19 @@
269+
@@ -1304,13 +_,21 @@
265270
}
266271
}
267272

268-
- this.level().addFreshEntity(t);
269273
+ // CraftBukkit start
270-
+ if (org.bukkit.craftbukkit.event.CraftEventFactory.callEntityTransformEvent(this, t, transformReason).isCancelled()) {
274+
+ if (org.bukkit.craftbukkit.event.CraftEventFactory.callEntityTransformEvent(this, t, magma$transformReason).isCancelled()) {
271275
+ return null;
272276
+ }
273-
+ this.level().addFreshEntity(t, spawnReason);
277+
+ this.level().setSpawnReason(magma$spawnReason);
278+
this.level().addFreshEntity(t);
279+
+ this.level().setSpawnReason(org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DEFAULT);
274280
+ // CraftBukkit end
275281
if (this.isPassenger()) {
276282
Entity entity = this.getVehicle();

patches/net/minecraft/world/entity/monster/Skeleton.java.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
protected void doFreezeConversion() {
77
- this.convertTo(EntityType.STRAY, true);
88
+ if (!net.neoforged.neoforge.event.EventHooks.canLivingConvert(this, EntityType.STRAY, (timer) -> this.conversionTime = timer)) return;
9-
+ Stray stray = this.convertTo(EntityType.STRAY, true, org.bukkit.event.entity.EntityTransformEvent.TransformReason.FROZEN, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.FROZEN); // CraftBukkit - add spawn and transform reasons
9+
+ Stray stray = this.convertToCB(EntityType.STRAY, true, org.bukkit.event.entity.EntityTransformEvent.TransformReason.FROZEN, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.FROZEN); // CraftBukkit - add spawn and transform reasons
1010
+ if (stray != null) net.neoforged.neoforge.event.EventHooks.onLivingConvert(this, stray);
1111
if (!this.isSilent()) {
1212
this.level().levelEvent(null, 1048, this.blockPosition(), 0);

patches/net/minecraft/world/entity/monster/Zombie.java.patch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
protected void convertToZombieType(EntityType<? extends Zombie> p_34311_) {
5858
- Zombie zombie = this.convertTo(p_34311_, true);
59-
+ Zombie zombie = this.convertTo(p_34311_, true, org.bukkit.event.entity.EntityTransformEvent.TransformReason.DROWNED, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DROWNED);
59+
+ Zombie zombie = this.convertToCB(p_34311_, true, org.bukkit.event.entity.EntityTransformEvent.TransformReason.DROWNED, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DROWNED);
6060
if (zombie != null) {
6161
zombie.handleAttributes(zombie.level().getCurrentDifficultyAt(zombie.blockPosition()).getSpecialMultiplier());
6262
zombie.setCanBreakDoors(zombie.supportsBreakDoorGoal() && this.canBreakDoors());
@@ -116,7 +116,7 @@
116116
+ public static ZombieVillager zombifyVillager(ServerLevel p_219160_, Villager villager, net.minecraft.core.BlockPos blockPosition, LivingEntity p_219161_, boolean silent, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason spawnReason) {
117117
+
118118
+ {
119-
+ ZombieVillager zombievillager = (ZombieVillager) villager.convertTo(EntityType.ZOMBIE_VILLAGER, false, org.bukkit.event.entity.EntityTransformEvent.TransformReason.INFECTION, spawnReason);
119+
+ ZombieVillager zombievillager = (ZombieVillager) villager.convertToCB(EntityType.ZOMBIE_VILLAGER, false, org.bukkit.event.entity.EntityTransformEvent.TransformReason.INFECTION, spawnReason);
120120
+ // CraftBukkit end
121121
if (zombievillager != null) {
122122
zombievillager.finalizeSpawn(

0 commit comments

Comments
 (0)