Skip to content

Commit

Permalink
Merge branch '1.20.1-temp' into feature/1.20.1/leaves_optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
supermassimo committed Sep 9, 2024
2 parents 98c2c48 + 3cd4cea commit 914f23c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 12 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
modName=DynamicTrees
modId=dynamictrees
modVersion=1.3.0-BETA15
modVersion=1.3.2

group=com.ferreusveritas.dynamictrees

Expand All @@ -16,7 +16,7 @@ ccVersion=1.109.4
ssVersion=4761603
jadeVersion=4986594

versionType=beta
versionType=release

# Path of update checker relative to version info file
updateCheckerPath=DynamicTrees.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public V decode(final JsonObject jsonObject) {

}

public static JsonObject putJsonRegistryName(final JsonObject jsonObject, final ResourceLocation registryName) {
public synchronized static JsonObject putJsonRegistryName(final JsonObject jsonObject, final ResourceLocation registryName) {
jsonObject.add(Resources.RESOURCE_LOCATION.toString(), new JsonPrimitive(registryName.toString()));
return jsonObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -576,19 +577,30 @@ public void onRemove(BlockState state, Level level, BlockPos pos, BlockState new
return;
}

if (toBlock == Blocks.AIR) { // Block was set to air improperly.
level.setBlock(pos, state, 0); // Set the block back and attempt a proper breaking.
this.sloppyBreak(level, pos, DestroyType.VOID);
this.setBlockStateIgnored(level, pos, BlockStates.AIR, 2); // Set back to air in case the sloppy break failed to do so.
return;
boolean foundFire = toBlockState.is(BlockTags.FIRE);
if (!foundFire){
for (Direction offset : Direction.values()){
BlockPos offPos = pos.offset(offset.getNormal());
if (level.getBlockState(offPos).is(BlockTags.FIRE)){
foundFire = true;
break;
}
}
}
if (toBlock == Blocks.FIRE) { // Block has burned.

if (foundFire) { // Block has burned.
level.setBlock(pos, state, 0); // Set the branch block back and attempt a proper breaking.
this.sloppyBreak(level, pos, DestroyType.FIRE); // Applies fire effects to falling branches.
//this.setBlockStateIgnored(level, pos, Blocks.FIRE.getDefaultState(), 2); // Disabled because the fire is too aggressive.
this.setBlockStateIgnored(level, pos, BlockStates.AIR, 2); // Set back to air instead.
return;
} else if (toBlock == Blocks.AIR){
level.setBlock(pos, state, 0); // Set the block back and attempt a proper breaking.
this.sloppyBreak(level, pos, DestroyType.VOID);
this.setBlockStateIgnored(level, pos, BlockStates.AIR, 2); // Set back to air in case the sloppy break failed to do so.
return;
}

if (/*!toBlock.entity(toBlockState) && */level.getBlockEntity(pos) == null) { // Block seems to be a pure BlockState based block.
level.setBlock(pos, state, 0); // Set the branch block back and attempt a proper breaking.
this.sloppyBreak(level, pos, DestroyType.VOID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
import com.ferreusveritas.dynamictrees.entity.FallingTreeEntity;
import com.ferreusveritas.dynamictrees.init.DTConfigs;
import com.ferreusveritas.dynamictrees.tree.species.Species;
import com.ferreusveritas.dynamictrees.util.BranchDestructionData;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.BlockParticleOption;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.util.Mth;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
Expand All @@ -22,7 +26,6 @@
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.joml.Vector3f;

import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -77,6 +80,79 @@ protected void playFallThroughWaterSound(FallingTreeEntity entity){
}
}

private Vec3 rotateAroundAxis(Vec3 in, Vec3 axis, double theta){
double x = in.x;
double y = in.y;
double z = in.z;
double u = axis.x;
double v = axis.y;
double w = axis.z;
double v1 = u * x + v * y + w * z;
double xPrime = u* v1 *(1d - Math.cos(theta))
+ x*Math.cos(theta)
+ (-w*y + v*z)*Math.sin(theta);
double yPrime = v* v1 *(1d - Math.cos(theta))
+ y*Math.cos(theta)
+ (w*x - u*z)*Math.sin(theta);
double zPrime = w* v1 *(1d - Math.cos(theta))
+ z*Math.cos(theta)
+ (-v*x + u*y)*Math.sin(theta);
return new Vec3(xPrime, yPrime, zPrime);
}

protected void flingLeavesParticles(FallingTreeEntity entity, float fallSpeed){
int bounces = getData(entity).bounces;
if (bounces > 1) return;
int maxParticleBlocks = DTConfigs.MAX_FALLING_TREE_LEAVES_PARTICLES.get();
if (maxParticleBlocks == 0) return;

BranchDestructionData data = entity.getDestroyData();
Direction.Axis toolAxis = data.toolDir.getAxis();
if (toolAxis == Direction.Axis.Y) return; //this one isn't possible anyways

double limitChance = 1;
if (entity.getDestroyData().getNumLeaves() > maxParticleBlocks)
limitChance = maxParticleBlocks / (double)entity.getDestroyData().getNumLeaves();
limitChance *= Math.exp(-bounces);

RandomSource rand = entity.level().random;
int particleCount = bounces == 0 ? (int)(fallSpeed*5) : 1;

Vec3 angularVel = entity.getForward().scale(fallSpeed * -data.toolDir.getAxisDirection().getStep());
//on the X axis, the entity forward is rotated, so we rotate the angular velocity back
if (toolAxis == Direction.Axis.X) angularVel = new Vec3(angularVel.z, angularVel.x, angularVel.y);

for (int i=0; i<data.getNumLeaves(); i++){
BlockPos leaves = data.getLeavesRelPos(i).offset(data.basePos);
double r = leaves.getY() - data.basePos.getY();
Vec3 velocity = angularVel.scale(r);
BlockState leavesState = entity.getDestroyData().getLeavesBlockState(i);

spawnParticlesAtLeaves(entity, leaves, leavesState, velocity, rand, particleCount, limitChance);
}
}

protected void spawnParticlesAtLeaves(FallingTreeEntity entity, BlockPos leavesPos, BlockState leavesState, Vec3 velocity, RandomSource rand, int particleCount, double limitChance){
Vec3 newPos = getRelativeLeavesPosition(entity, leavesPos.getCenter());
for (int j=0; j<particleCount; j++){
if (rand.nextDouble() < limitChance){
if (leavesState != null)
entity.level().addParticle(new BlockParticleOption(ParticleTypes.BLOCK, leavesState),
newPos.x+rand.nextFloat(), newPos.y+rand.nextFloat(), newPos.z+rand.nextFloat(),
velocity.x+rand.nextFloat(), velocity.y+rand.nextFloat(), velocity.z+rand.nextFloat());
}
}
}

protected Vec3 getRelativeLeavesPosition(FallingTreeEntity entity, Vec3 leaves){
BranchDestructionData data = entity.getDestroyData();
float angle = (data.toolDir.getAxis() == Direction.Axis.X ? entity.getYRot() : entity.getXRot()) * -data.toolDir.getAxisDirection().getStep() * 0.0174533f;
return rotateAroundAxis(
leaves.subtract(data.basePos.getCenter()),
new Vec3(-data.toolDir.getStepZ(),0, data.toolDir.getStepX()),
angle).add(data.basePos.getCenter()).subtract(0.5,0.5,0.5);
}

@Override
public void initMotion(FallingTreeEntity entity) {
entity.dataAnimationHandler = new HandlerData();
Expand All @@ -97,7 +173,7 @@ public void handleMotion(FallingTreeEntity entity) {

if (entity.onGround()) {
float height = (float) entity.getMassCenter().y * 2;
fallSpeed += (0.2 / height);
fallSpeed += (float) (0.2 / height);
addRotation(entity, fallSpeed);
}

Expand Down Expand Up @@ -132,6 +208,7 @@ public void handleMotion(FallingTreeEntity entity) {

if (fallSpeed > 0 && testCollision(entity)) {
playEndSound(entity);
flingLeavesParticles(entity, fallSpeed);
addRotation(entity, -fallSpeed);//pull back to before the collision
getData(entity).bounces++;
fallSpeed *= -AnimationConstants.TREE_ELASTICITY;//bounce with elasticity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.config.ModConfigEvent;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.fml.loading.FMLPaths;

import java.io.File;
Expand Down Expand Up @@ -62,6 +61,7 @@ public class DTConfigs {
public static final ForgeConfigSpec.BooleanValue REPLACE_VANILLA_SAPLING;
public static final ForgeConfigSpec.BooleanValue REPLACE_NYLIUM_FUNGI;
public static final ForgeConfigSpec.BooleanValue CANCEL_VANILLA_VILLAGE_TREES;
public static final ForgeConfigSpec.IntValue MAX_FALLING_TREE_LEAVES_PARTICLES;

public static final ForgeConfigSpec.BooleanValue PODZOL_GEN;

Expand Down Expand Up @@ -167,6 +167,8 @@ public class DTConfigs {
define("replaceNyliumFungi", true);
CANCEL_VANILLA_VILLAGE_TREES = COMMON_BUILDER.comment("If enabled, cancels the non-dynamic trees that spawn with vanilla villages.").
define("cancelVanillaVillageTrees", true);
MAX_FALLING_TREE_LEAVES_PARTICLES = SERVER_BUILDER.comment("The maximum number of leaves blocks that will fling particles when a falling tree crashes into the ground. Higher values might have a performance impact.").
defineInRange("growthFolding", 400, 0, 4096);
COMMON_BUILDER.pop();

SERVER_BUILDER.comment("World Generation Settings").push("world");
Expand Down

0 comments on commit 914f23c

Please sign in to comment.