From 15733a18e41e53961e486001b7720b36b77c20ad Mon Sep 17 00:00:00 2001 From: ChampionAsh5357 Date: Tue, 8 Sep 2020 17:00:09 -0400 Subject: [PATCH] New hook for better custom scaffolding movement (#7261) --- build.gradle | 1 + .../minecraft/entity/LivingEntity.java.patch | 9 ++ src/generated_test/resources/.cache/cache | 1 + .../blockstates/scaffolding_method_test.json | 10 +++ .../common/extensions/IForgeBlock.java | 14 ++++ .../common/extensions/IForgeBlockState.java | 11 +++ .../debug/block/ScaffoldingTest.java | 83 +++++++++++++++++++ src/test/resources/META-INF/mods.toml | 4 +- 8 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/generated_test/resources/assets/scaffolding_test/blockstates/scaffolding_method_test.json create mode 100644 src/test/java/net/minecraftforge/debug/block/ScaffoldingTest.java diff --git a/build.gradle b/build.gradle index d59597aff..cb07cdc5a 100644 --- a/build.gradle +++ b/build.gradle @@ -351,6 +351,7 @@ project(':forge') { '--mod', 'data_gen_test', '--mod', 'piston_event_test', '--mod', 'global_loot_test', + '--mod', 'scaffolding_test', '--output', rootProject.file('src/generated_test/resources/'), '--existing', sourceSets.main.resources.srcDirs[0] } diff --git a/patches/minecraft/net/minecraft/entity/LivingEntity.java.patch b/patches/minecraft/net/minecraft/entity/LivingEntity.java.patch index a0cbd44ef..82005c861 100644 --- a/patches/minecraft/net/minecraft/entity/LivingEntity.java.patch +++ b/patches/minecraft/net/minecraft/entity/LivingEntity.java.patch @@ -343,6 +343,15 @@ float f4 = this.field_70122_E ? f3 * 0.91F : 0.91F; Vector3d vector3d5 = this.func_233633_a_(p_213352_1_, f3); double d2 = vector3d5.field_72448_b; +@@ -2049,7 +2081,7 @@ + double d0 = MathHelper.func_151237_a(p_213362_1_.field_72450_a, (double)-0.15F, (double)0.15F); + double d1 = MathHelper.func_151237_a(p_213362_1_.field_72449_c, (double)-0.15F, (double)0.15F); + double d2 = Math.max(p_213362_1_.field_72448_b, (double)-0.15F); +- if (d2 < 0.0D && !this.func_213339_cH().func_203425_a(Blocks.field_222420_lI) && this.func_230491_ea_() && this instanceof PlayerEntity) { ++ if (d2 < 0.0D && !this.func_213339_cH().isScaffolding(this) && this.func_230491_ea_() && this instanceof PlayerEntity) { + d2 = 0.0D; + } + @@ -2077,6 +2109,7 @@ } diff --git a/src/generated_test/resources/.cache/cache b/src/generated_test/resources/.cache/cache index b6c98ea82..556aa2d5c 100644 --- a/src/generated_test/resources/.cache/cache +++ b/src/generated_test/resources/.cache/cache @@ -66,6 +66,7 @@ a012d6d92bab1c91913bd0f2dc0492a0fce916f2 assets/minecraft/blockstates/wall_torch 273e0ed992d227f09f1c83bc22d066fb68d03c84 assets/piston_event_test/blockstates/shiftonmove.json d2c8e860521c5e738ed0798337f5728d610825ff assets/piston_event_test/models/block/shiftonmove.json f51f026a75e27a0a53a76771d553c0e3385a2966 assets/piston_event_test/models/item/shiftonmove.json +cf16f861eaf5815238c2278eb48bde0688cb73b7 assets/scaffolding_test/blockstates/scaffolding_method_test.json 4fbaf6f4a3ea05cc071076e27f44ac81f9cc50e3 data/data_gen_test/advancements/conditional.json ed4cbf1a3a2f5d8969f6346fdc9acdbe81d0c919 data/data_gen_test/recipes/conditional.json 40208299608468b044f64317995f9182ec219d90 data/data_gen_test/tags/blocks/test.json diff --git a/src/generated_test/resources/assets/scaffolding_test/blockstates/scaffolding_method_test.json b/src/generated_test/resources/assets/scaffolding_test/blockstates/scaffolding_method_test.json new file mode 100644 index 000000000..aca5b491d --- /dev/null +++ b/src/generated_test/resources/assets/scaffolding_test/blockstates/scaffolding_method_test.json @@ -0,0 +1,10 @@ +{ + "variants": { + "bottom=false": { + "model": "minecraft:block/scaffolding_stable" + }, + "bottom=true": { + "model": "minecraft:block/scaffolding_unstable" + } + } +} \ No newline at end of file diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeBlock.java b/src/main/java/net/minecraftforge/common/extensions/IForgeBlock.java index 148e5ff2b..809ff078a 100644 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeBlock.java +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeBlock.java @@ -911,4 +911,18 @@ public interface IForgeBlock else if(toolType == ToolType.HOE) return HoeItem.getHoeTillingState(state); else return toolType == ToolType.SHOVEL ? ShovelItem.getShovelPathingState(state) : null; } + + /** + * Checks if a player or entity handles movement on this block like scaffolding. + * + * @param state The current state + * @param world The current world + * @param pos The block position in world + * @param entity The entity on the scaffolding + * @return True if the block should act like scaffolding + */ + default boolean isScaffolding(BlockState state, IWorldReader world, BlockPos pos, LivingEntity entity) + { + return state.isIn(Blocks.SCAFFOLDING); + } } diff --git a/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java b/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java index d548c976b..424723a75 100644 --- a/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java +++ b/src/main/java/net/minecraftforge/common/extensions/IForgeBlockState.java @@ -798,4 +798,15 @@ public interface IForgeBlockState BlockState eventState = net.minecraftforge.event.ForgeEventFactory.onToolUse(getBlockState(), world, pos, player, stack, toolType); return eventState != getBlockState() ? eventState : getBlockState().getBlock().getToolModifiedState(getBlockState(), world, pos, player, stack, toolType); } + + /** + * Checks if a player or entity handles movement on this block like scaffolding. + * + * @param entity The entity on the scaffolding + * @return True if the block should act like scaffolding + */ + default boolean isScaffolding(LivingEntity entity) + { + return getBlockState().getBlock().isScaffolding(getBlockState(), entity.world, entity.func_233580_cy_(), entity); + } } diff --git a/src/test/java/net/minecraftforge/debug/block/ScaffoldingTest.java b/src/test/java/net/minecraftforge/debug/block/ScaffoldingTest.java new file mode 100644 index 000000000..69e32a1dc --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/block/ScaffoldingTest.java @@ -0,0 +1,83 @@ +package net.minecraftforge.debug.block; + +import net.minecraft.block.AbstractBlock; +import net.minecraft.block.Block; +import net.minecraft.block.BlockState; +import net.minecraft.block.ScaffoldingBlock; +import net.minecraft.block.SoundType; +import net.minecraft.block.material.Material; +import net.minecraft.block.material.MaterialColor; +import net.minecraft.data.DataGenerator; +import net.minecraft.entity.LivingEntity; +import net.minecraft.tags.BlockTags; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IWorldReader; +import net.minecraftforge.client.model.generators.BlockStateProvider; +import net.minecraftforge.client.model.generators.ConfiguredModel; +import net.minecraftforge.client.model.generators.ModelFile; +import net.minecraftforge.common.Tags.IOptionalNamedTag; +import net.minecraftforge.common.data.ExistingFileHelper; +import net.minecraftforge.eventbus.api.IEventBus; +import net.minecraftforge.fml.RegistryObject; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.event.lifecycle.GatherDataEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.registries.DeferredRegister; +import net.minecraftforge.registries.ForgeRegistries; + +/** + * This test mod allows a custom scaffolding to move down + * while sneaking through a method. + */ +@Mod(ScaffoldingTest.MODID) +public class ScaffoldingTest +{ + static final String MODID = "scaffolding_test"; + static final DeferredRegister BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID); + static final IOptionalNamedTag SCAFFOLDING = BlockTags.createOptional(new ResourceLocation("forge", "scaffolding")); + + static final RegistryObject SCAFFOLDING_METHOD_TEST = BLOCKS.register("scaffolding_method_test", () -> new ScaffoldingMethodTestBlock(AbstractBlock.Properties.create(Material.MISCELLANEOUS, MaterialColor.SAND).doesNotBlockMovement().sound(SoundType.SCAFFOLDING).variableOpacity())); + + public ScaffoldingTest() + { + IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); + BLOCKS.register(modBus); + modBus.addListener(this::gatherData); + } + + private void gatherData(final GatherDataEvent event) + { + DataGenerator gen = event.getGenerator(); + if(event.includeClient()) gen.addProvider(new ScaffoldingBlockState(gen, MODID, event.getExistingFileHelper())); + } + + static class ScaffoldingBlockState extends BlockStateProvider + { + public ScaffoldingBlockState(DataGenerator gen, String modid, ExistingFileHelper exFileHelper) + { + super(gen, modid, exFileHelper); + } + + @Override + protected void registerStatesAndModels() + { + this.getVariantBuilder(SCAFFOLDING_METHOD_TEST.get()).forAllStatesExcept((state) -> ConfiguredModel.builder().modelFile(state.get(ScaffoldingBlock.field_220120_c) ? new ModelFile.ExistingModelFile(new ResourceLocation("block/scaffolding_unstable"), this.models().existingFileHelper) : new ModelFile.ExistingModelFile(new ResourceLocation("block/scaffolding_stable"), this.models().existingFileHelper)).build(), ScaffoldingBlock.field_220118_a, ScaffoldingBlock.WATERLOGGED); + } + } + + static class ScaffoldingMethodTestBlock extends ScaffoldingBlock + { + + public ScaffoldingMethodTestBlock(Properties properties) + { + super(properties); + } + + @Override + public boolean isScaffolding(BlockState state, IWorldReader world, BlockPos pos, LivingEntity entity) + { + return true; + } + } +} diff --git a/src/test/resources/META-INF/mods.toml b/src/test/resources/META-INF/mods.toml index 0491585ab..d0ad2eb55 100644 --- a/src/test/resources/META-INF/mods.toml +++ b/src/test/resources/META-INF/mods.toml @@ -79,4 +79,6 @@ license="LGPL v2.1" [[mods]] modId="custom_elytra_test" [[mods]] - modId="finite_water_test" \ No newline at end of file + modId="finite_water_test" +[[mods]] + modId="scaffolding_test"