From 3f089e3addf40a5427b459bbe05d2e44d65719dd Mon Sep 17 00:00:00 2001 From: mcenderdragon Date: Tue, 3 Sep 2019 22:20:44 +0200 Subject: [PATCH] Added gravity as attribute to Entities (#5630) --- .../minecraft/entity/LivingEntity.java.patch | 96 +++++++----- .../debug/entity/GravityAttributeTest.java | 137 ++++++++++++++++++ src/test/resources/META-INF/mods.toml | 4 +- 3 files changed, 199 insertions(+), 38 deletions(-) create mode 100644 src/test/java/net/minecraftforge/debug/entity/GravityAttributeTest.java diff --git a/patches/minecraft/net/minecraft/entity/LivingEntity.java.patch b/patches/minecraft/net/minecraft/entity/LivingEntity.java.patch index 52a9eb528..7e53985b6 100644 --- a/patches/minecraft/net/minecraft/entity/LivingEntity.java.patch +++ b/patches/minecraft/net/minecraft/entity/LivingEntity.java.patch @@ -1,24 +1,29 @@ --- a/net/minecraft/entity/LivingEntity.java +++ b/net/minecraft/entity/LivingEntity.java -@@ -105,6 +105,8 @@ +@@ -104,7 +104,12 @@ + public abstract class LivingEntity extends Entity { private static final UUID field_110156_b = UUID.fromString("662A6B8D-DA3E-4C1C-8813-96EA6097278D"); ++ private static final UUID SLOW_FALLING_ID = UUID.fromString("A5B6CF2A-2F7C-31EF-9022-7C3E7D5E6ABA"); private static final AttributeModifier field_110157_c = (new AttributeModifier(field_110156_b, "Sprinting speed boost", (double)0.3F, AttributeModifier.Operation.MULTIPLY_TOTAL)).func_111168_a(false); ++ private static final AttributeModifier SLOW_FALLING = new AttributeModifier(SLOW_FALLING_ID, "Slow falling acceleration reduction", -0.07, AttributeModifier.Operation.ADDITION).func_111168_a(false); // Add -0.07 to 0.08 so we get the vanilla default of 0.01 + public static final net.minecraft.entity.ai.attributes.IAttribute SWIM_SPEED = new net.minecraft.entity.ai.attributes.RangedAttribute(null, "forge.swimSpeed", 1.0D, 0.0D, 1024.0D).func_111112_a(true); + public static final net.minecraft.entity.ai.attributes.IAttribute NAMETAG_DISTANCE = new net.minecraft.entity.ai.attributes.RangedAttribute(null, "forge.nameTagDistance", 64.0D, 0.0D, Float.MAX_VALUE).func_111112_a(true); ++ public static final net.minecraft.entity.ai.attributes.IAttribute ENTITY_GRAVITY = new net.minecraft.entity.ai.attributes.RangedAttribute(null, "forge.entity_gravity", 0.08D, -8.0D, 8.0D).func_111112_a(true); protected static final DataParameter field_184621_as = EntityDataManager.func_187226_a(LivingEntity.class, DataSerializers.field_187191_a); private static final DataParameter field_184632_c = EntityDataManager.func_187226_a(LivingEntity.class, DataSerializers.field_187193_c); private static final DataParameter field_184633_f = EntityDataManager.func_187226_a(LivingEntity.class, DataSerializers.field_187192_b); -@@ -228,6 +230,8 @@ +@@ -228,6 +233,9 @@ this.func_110140_aT().func_111150_b(SharedMonsterAttributes.field_111263_d); this.func_110140_aT().func_111150_b(SharedMonsterAttributes.field_188791_g); this.func_110140_aT().func_111150_b(SharedMonsterAttributes.field_189429_h); + this.func_110140_aT().func_111150_b(SWIM_SPEED); + this.func_110140_aT().func_111150_b(NAMETAG_DISTANCE); ++ this.func_110140_aT().func_111150_b(ENTITY_GRAVITY); } protected void func_184231_a(double p_184231_1_, boolean p_184231_3_, BlockState p_184231_4_, BlockPos p_184231_5_) { -@@ -237,9 +241,10 @@ +@@ -237,9 +245,10 @@ if (!this.field_70170_p.field_72995_K && this.field_70143_R > 3.0F && p_184231_3_) { float f = (float)MathHelper.func_76123_f(this.field_70143_R - 3.0F); @@ -30,7 +35,7 @@ ((ServerWorld)this.field_70170_p).func_195598_a(new BlockParticleData(ParticleTypes.field_197611_d, p_184231_4_), this.field_70165_t, this.field_70163_u, this.field_70161_v, i, 0.0D, 0.0D, 0.0D, (double)0.15F); } } -@@ -303,7 +308,7 @@ +@@ -303,7 +312,7 @@ } } @@ -39,7 +44,7 @@ this.func_184210_p(); } } else if (this.func_70086_ai() < this.func_205010_bg()) { -@@ -388,6 +393,7 @@ +@@ -388,6 +397,7 @@ if (!this.field_70170_p.field_72995_K && (this.func_70684_aJ() || this.field_70718_bc > 0 && this.func_146066_aG() && this.field_70170_p.func_82736_K().func_223586_b(GameRules.field_223602_e))) { int i = this.func_70693_a(this.field_70717_bb); @@ -65,7 +70,7 @@ iterator.remove(); this.func_70688_c(effectinstance); } -@@ -640,8 +646,10 @@ +@@ -640,8 +650,10 @@ this.func_82142_c(false); } else { Collection collection = this.field_70713_bf.values(); @@ -78,7 +83,7 @@ this.func_82142_c(this.func_70644_a(Effects.field_76441_p)); } -@@ -705,7 +713,10 @@ +@@ -705,7 +717,10 @@ boolean flag; for(flag = false; iterator.hasNext(); flag = true) { @@ -90,7 +95,7 @@ iterator.remove(); } -@@ -735,6 +746,7 @@ +@@ -735,6 +750,7 @@ return false; } else { EffectInstance effectinstance = this.field_70713_bf.get(p_195064_1_.func_188419_a()); @@ -98,7 +103,7 @@ if (effectinstance == null) { this.field_70713_bf.put(p_195064_1_.func_188419_a(), p_195064_1_); this.func_70670_a(p_195064_1_); -@@ -749,6 +761,9 @@ +@@ -749,6 +765,9 @@ } public boolean func_70687_e(EffectInstance p_70687_1_) { @@ -108,7 +113,7 @@ if (this.func_70668_bt() == CreatureAttribute.field_223223_b_) { Effect effect = p_70687_1_.func_188419_a(); if (effect == Effects.field_76428_l || effect == Effects.field_76436_u) { -@@ -769,6 +784,7 @@ +@@ -769,6 +788,7 @@ } public boolean func_195063_d(Effect p_195063_1_) { @@ -116,7 +121,7 @@ EffectInstance effectinstance = this.func_184596_c(p_195063_1_); if (effectinstance != null) { this.func_70688_c(effectinstance); -@@ -805,6 +821,8 @@ +@@ -805,6 +825,8 @@ } public void func_70691_i(float p_70691_1_) { @@ -125,7 +130,7 @@ float f = this.func_110143_aJ(); if (f > 0.0F) { this.func_70606_j(f + p_70691_1_); -@@ -821,6 +839,7 @@ +@@ -821,6 +843,7 @@ } public boolean func_70097_a(DamageSource p_70097_1_, float p_70097_2_) { @@ -133,7 +138,7 @@ if (this.func_180431_b(p_70097_1_)) { return false; } else if (this.field_70170_p.field_72995_K) { -@@ -887,8 +906,8 @@ +@@ -887,8 +910,8 @@ if (entity1 instanceof PlayerEntity) { this.field_70718_bc = 100; this.field_70717_bb = (PlayerEntity)entity1; @@ -144,7 +149,7 @@ if (wolfentity.func_70909_n()) { this.field_70718_bc = 100; LivingEntity livingentity = wolfentity.func_70902_q(); -@@ -1070,6 +1089,7 @@ +@@ -1070,6 +1093,7 @@ } public void func_70645_a(DamageSource p_70645_1_) { @@ -152,7 +157,7 @@ if (!this.field_70729_aU) { Entity entity = p_70645_1_.func_76346_g(); LivingEntity livingentity = this.func_94060_bK(); -@@ -1091,7 +1111,7 @@ +@@ -1091,7 +1115,7 @@ this.func_213345_d(p_70645_1_); boolean flag = false; if (livingentity instanceof WitherEntity) { @@ -161,7 +166,7 @@ BlockPos blockpos = new BlockPos(this.field_70165_t, this.field_70163_u, this.field_70161_v); BlockState blockstate = Blocks.field_222388_bz.func_176223_P(); if (this.field_70170_p.func_180495_p(blockpos).func_196958_f() && blockstate.func_196955_c(this.field_70170_p, blockpos)) { -@@ -1114,13 +1134,10 @@ +@@ -1114,13 +1138,10 @@ protected void func_213345_d(DamageSource p_213345_1_) { Entity entity = p_213345_1_.func_76346_g(); @@ -178,7 +183,7 @@ boolean flag = this.field_70718_bc > 0; if (this.func_146066_aG() && this.field_70170_p.func_82736_K().func_223586_b(GameRules.field_223602_e)) { this.func_213354_a(p_213345_1_, flag); -@@ -1128,6 +1145,10 @@ +@@ -1128,6 +1149,10 @@ } this.func_213337_cE(); @@ -189,7 +194,7 @@ } protected void func_213337_cE() { -@@ -1157,6 +1178,9 @@ +@@ -1157,6 +1182,9 @@ } public void func_70653_a(Entity p_70653_1_, float p_70653_2_, double p_70653_3_, double p_70653_5_) { @@ -199,7 +204,7 @@ if (!(this.field_70146_Z.nextDouble() < this.func_110148_a(SharedMonsterAttributes.field_111266_c).func_111126_e())) { this.field_70160_al = true; Vec3d vec3d = this.func_213322_ci(); -@@ -1192,12 +1216,7 @@ +@@ -1192,12 +1220,7 @@ return false; } else { BlockState blockstate = this.func_213339_cH(); @@ -213,7 +218,7 @@ } } -@@ -1221,6 +1240,9 @@ +@@ -1221,6 +1244,9 @@ } public void func_180430_e(float p_180430_1_, float p_180430_2_) { @@ -223,7 +228,7 @@ super.func_180430_e(p_180430_1_, p_180430_2_); EffectInstance effectinstance = this.func_70660_b(Effects.field_76430_j); float f = effectinstance == null ? 0.0F : (float)(effectinstance.func_76458_c() + 1); -@@ -1233,7 +1255,7 @@ +@@ -1233,7 +1259,7 @@ int l = MathHelper.func_76128_c(this.field_70161_v); BlockState blockstate = this.field_70170_p.func_180495_p(new BlockPos(j, k, l)); if (!blockstate.func_196958_f()) { @@ -232,7 +237,7 @@ this.func_184185_a(soundtype.func_185842_g(), soundtype.func_185843_a() * 0.5F, soundtype.func_185847_b() * 0.75F); } } -@@ -1302,6 +1324,8 @@ +@@ -1302,6 +1328,8 @@ protected void func_70665_d(DamageSource p_70665_1_, float p_70665_2_) { if (!this.func_180431_b(p_70665_1_)) { @@ -241,7 +246,7 @@ p_70665_2_ = this.func_70655_b(p_70665_1_, p_70665_2_); p_70665_2_ = this.func_70672_c(p_70665_1_, p_70665_2_); float f = p_70665_2_; -@@ -1312,10 +1336,11 @@ +@@ -1312,10 +1340,11 @@ ((ServerPlayerEntity)p_70665_1_.func_76346_g()).func_195067_a(Stats.field_212735_F, Math.round(f1 * 10.0F)); } @@ -254,7 +259,7 @@ this.func_110149_m(this.func_110139_bj() - p_70665_2_); } } -@@ -1357,6 +1382,8 @@ +@@ -1357,6 +1386,8 @@ } public void func_184609_a(Hand p_184609_1_) { @@ -263,7 +268,7 @@ if (!this.field_82175_bq || this.field_110158_av >= this.func_82166_i() / 2 || this.field_110158_av < 0) { this.field_110158_av = -1; this.field_82175_bq = true; -@@ -1715,15 +1742,16 @@ +@@ -1715,15 +1746,16 @@ } this.field_70160_al = true; @@ -282,7 +287,24 @@ } protected float func_189749_co() { -@@ -1786,7 +1814,7 @@ +@@ -1733,11 +1765,15 @@ + public void func_213352_e(Vec3d p_213352_1_) { + if (this.func_70613_aW() || this.func_184186_bw()) { + double d0 = 0.08D; ++ IAttributeInstance gravity = this.func_110148_a(ENTITY_GRAVITY); + boolean flag = this.func_213322_ci().field_72448_b <= 0.0D; + if (flag && this.func_70644_a(Effects.field_204839_B)) { +- d0 = 0.01D; ++ if (!gravity.func_180374_a(SLOW_FALLING)) gravity.func_111121_a(SLOW_FALLING); + this.field_70143_R = 0.0F; ++ } else if (gravity.func_180374_a(SLOW_FALLING)) { ++ gravity.func_111124_b(SLOW_FALLING); + } ++ d0 = gravity.func_111126_e(); + + if (!this.func_70090_H() || this instanceof PlayerEntity && ((PlayerEntity)this).field_71075_bZ.field_75100_b) { + if (!this.func_180799_ab() || this instanceof PlayerEntity && ((PlayerEntity)this).field_71075_bZ.field_75100_b) { +@@ -1786,7 +1822,7 @@ } } else { BlockPos blockpos = new BlockPos(this.field_70165_t, this.func_174813_aQ().field_72338_b - 1.0D, this.field_70161_v); @@ -291,7 +313,7 @@ float f7 = this.field_70122_E ? f5 * 0.91F : 0.91F; this.func_213309_a(this.func_213335_r(f5), p_213352_1_); this.func_213317_d(this.func_213362_f(this.func_213322_ci())); -@@ -1848,6 +1876,7 @@ +@@ -1848,6 +1884,7 @@ f = 0.96F; } @@ -299,7 +321,7 @@ this.func_213309_a(f1, p_213352_1_); this.func_213315_a(MoverType.SELF, this.func_213322_ci()); Vec3d vec3d1 = this.func_213322_ci(); -@@ -1923,6 +1952,7 @@ +@@ -1923,6 +1960,7 @@ } public void func_70071_h_() { @@ -307,7 +329,7 @@ super.func_70071_h_(); this.func_184608_ct(); this.func_205014_p(); -@@ -1954,7 +1984,9 @@ +@@ -1954,7 +1992,9 @@ ItemStack itemstack1 = this.func_184582_a(equipmentslottype); if (!ItemStack.func_77989_b(itemstack1, itemstack)) { @@ -317,7 +339,7 @@ if (!itemstack.func_190926_b()) { this.func_110140_aT().func_111148_a(itemstack.func_111283_C(equipmentslottype)); } -@@ -2403,13 +2435,22 @@ +@@ -2403,13 +2443,22 @@ private void func_184608_ct() { if (this.func_184587_cr()) { @@ -342,7 +364,7 @@ this.func_71036_o(); } } else { -@@ -2443,8 +2484,10 @@ +@@ -2443,8 +2492,10 @@ public void func_184598_c(Hand p_184598_1_) { ItemStack itemstack = this.func_184586_b(p_184598_1_); if (!itemstack.func_190926_b() && !this.func_184587_cr()) { @@ -354,7 +376,7 @@ if (!this.field_70170_p.field_72995_K) { this.func_204802_c(1, true); this.func_204802_c(2, p_184598_1_ == Hand.OFF_HAND); -@@ -2504,6 +2547,9 @@ +@@ -2504,6 +2555,9 @@ vec3d1 = vec3d1.func_178789_a(-this.field_70125_A * ((float)Math.PI / 180F)); vec3d1 = vec3d1.func_178785_b(-this.field_70177_z * ((float)Math.PI / 180F)); vec3d1 = vec3d1.func_72441_c(this.field_70165_t, this.field_70163_u + (double)this.func_70047_e(), this.field_70161_v); @@ -364,7 +386,7 @@ this.field_70170_p.func_195594_a(new ItemParticleData(ParticleTypes.field_197591_B, p_195062_1_), vec3d1.field_72450_a, vec3d1.field_72448_b, vec3d1.field_72449_c, vec3d.field_72450_a, vec3d.field_72448_b + 0.05D, vec3d.field_72449_c); } -@@ -2512,7 +2558,9 @@ +@@ -2512,7 +2566,9 @@ protected void func_71036_o() { if (!this.field_184627_bm.func_190926_b() && this.func_184587_cr()) { this.func_184584_a(this.field_184627_bm, 16); @@ -375,7 +397,7 @@ this.func_184602_cy(); } -@@ -2532,7 +2580,11 @@ +@@ -2532,6 +2588,11 @@ public void func_184597_cx() { if (!this.field_184627_bm.func_190926_b()) { @@ -387,7 +409,7 @@ if (this.field_184627_bm.func_222122_m()) { this.func_184608_ct(); } -@@ -2687,16 +2739,16 @@ +@@ -2687,16 +2747,16 @@ private boolean func_213359_p() { return this.func_213374_dv().map((p_213347_1_) -> { @@ -408,7 +430,7 @@ BlockPos blockpos = p_213368_1_.func_177984_a(); return new Vec3d((double)blockpos.func_177958_n() + 0.5D, (double)blockpos.func_177956_o() + 0.1D, (double)blockpos.func_177952_p() + 0.5D); }); -@@ -2711,8 +2763,13 @@ +@@ -2711,8 +2771,13 @@ @Nullable @OnlyIn(Dist.CLIENT) public Direction func_213376_dz() { @@ -423,7 +445,7 @@ } public boolean func_70094_T() { -@@ -2779,4 +2836,58 @@ +@@ -2779,4 +2844,58 @@ public void func_213334_d(Hand p_213334_1_) { this.func_213361_c(p_213334_1_ == Hand.MAIN_HAND ? EquipmentSlotType.MAINHAND : EquipmentSlotType.OFFHAND); } diff --git a/src/test/java/net/minecraftforge/debug/entity/GravityAttributeTest.java b/src/test/java/net/minecraftforge/debug/entity/GravityAttributeTest.java new file mode 100644 index 000000000..277955871 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/entity/GravityAttributeTest.java @@ -0,0 +1,137 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2019. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.debug.entity; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import com.google.common.collect.Multimap; + +import net.minecraft.client.world.ClientWorld; +import net.minecraft.entity.Entity; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.ai.attributes.AttributeModifier; +import net.minecraft.entity.ai.attributes.AttributeModifier.Operation; +import net.minecraft.entity.ai.attributes.IAttributeInstance; +import net.minecraft.inventory.EquipmentSlotType; +import net.minecraft.item.Item; +import net.minecraft.item.ItemGroup; +import net.minecraft.item.Rarity; +import net.minecraft.util.math.AxisAlignedBB; +import net.minecraft.world.ServerWorld; +import net.minecraft.world.World; +import net.minecraft.world.biome.Biome.Category; +import net.minecraft.world.chunk.ServerChunkProvider; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.RegistryEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.gameevent.TickEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; + +@Mod("gravity_attribute_test") +public class GravityAttributeTest +{ + public static final boolean ENABLE = true; + private static Logger logger; + private int ticks; + private static final UUID REDUCED_GRAVITY_ID = UUID.fromString("DEB06000-7979-4242-8888-00000DEB0600"); + private static final AttributeModifier REDUCED_GRAVITY = (new AttributeModifier(REDUCED_GRAVITY_ID, "Reduced gravity", (double)-0.80, Operation.MULTIPLY_TOTAL)).setSaved(false); + + + public GravityAttributeTest() { + if (ENABLE) { + logger = LogManager.getLogger(); + MinecraftForge.EVENT_BUS.register(this); + FMLJavaModLoadingContext.get().getModEventBus().register(this); + } + } + + @SubscribeEvent + public void worldTick(TickEvent.WorldTickEvent event) { + if (ENABLE && !event.world.isRemote) { + if (ticks++ > 60) { + ticks = 0; + World w = event.world; + List list; + if(w.isRemote) { + ClientWorld cw = (ClientWorld) w; + list = new ArrayList<>(100); + for(Entity e : cw.func_217416_b()) { + if(e.isAlive()) + { + if(e instanceof LivingEntity) { + list.add((LivingEntity) e); + } + } + } + } else { + ServerWorld sw = (ServerWorld) w; + Stream s = sw.getEntities().filter(Entity::isAlive).filter(e -> e instanceof LivingEntity).map(e -> (LivingEntity)e); + list = s.collect(Collectors.toList()); + } + for(LivingEntity liv : list) { + IAttributeInstance grav = liv.getAttribute(LivingEntity.ENTITY_GRAVITY); + boolean inPlains = liv.world.getBiome(liv.getPosition()).getCategory() == Category.PLAINS; + if (inPlains && !grav.hasModifier(REDUCED_GRAVITY)) { + logger.info("Granted low gravity to Entity: {}", liv); + grav.applyModifier(REDUCED_GRAVITY); + } else if (!inPlains && grav.hasModifier(REDUCED_GRAVITY)) { + logger.info("Removed low gravity from Entity: {}", liv); + grav.removeModifier(REDUCED_GRAVITY); + } + } + } + + } + } + + @SubscribeEvent + public void registerItems(RegistryEvent.Register event) { + event.getRegistry().register(new ItemGravityStick(new Item.Properties().group(ItemGroup.TOOLS).rarity(Rarity.RARE)).setRegistryName("gravity_attribute_test:gravity_stick")); + } + + public static class ItemGravityStick extends Item + { + private static final UUID GRAVITY_MODIFIER = UUID.fromString("DEB06001-7979-4242-8888-10000DEB0601"); + + public ItemGravityStick(Properties properties) { + super(properties); + } + + @Override + public Multimap getAttributeModifiers(EquipmentSlotType slot) { + @SuppressWarnings("deprecation") + Multimap multimap = super.getAttributeModifiers(slot); + if (slot == EquipmentSlotType.MAINHAND) { + multimap.put(LivingEntity.ENTITY_GRAVITY.getName(), new AttributeModifier(GRAVITY_MODIFIER, "More Gravity", 1.0D, Operation.ADDITION)); + } + + return multimap; + } + } +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index 842f8d52d..d950081cc 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -43,9 +43,11 @@ loaderVersion="[28,)" modId="command_event_test" [[mods]] modId="entity_selector_test" +[[mods]] + modId="gravity_attribute_test" [[mods]] modId="new_fluid_test" [[mods]] modId="data_gen_test" [[mods]] - modId="piston_event_test" \ No newline at end of file + modId="piston_event_test"