Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added growth of baby animals when crouching and hold sneak to continuous crop growth #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '1.3.8'
version = '1.4.1'
group = 'com.ticticbooom.twerkitmeal' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'TwerkItMeal'

Expand Down
65 changes: 63 additions & 2 deletions src/main/java/com/ticticboooom/twerkitmeal/TwerkItMeal.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.ticticboooom.twerkitmeal.dynamictrees.DTProxy;
import com.ticticboooom.twerkitmeal.helper.FilterListHelper;
import net.minecraft.block.*;
import net.minecraft.entity.AgeableEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.BoneMealItem;
Expand All @@ -13,6 +15,7 @@
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
Expand All @@ -25,6 +28,7 @@
import net.minecraftforge.fml.config.ModConfig;
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nullable;
import java.util.*;


Expand Down Expand Up @@ -53,6 +57,7 @@ public static class RegistryEvents {
private final Map<UUID, Integer> crouchCount = new HashMap<>();
private final Map<UUID, Boolean> prevSneaking = new HashMap<>();
private final Map<UUID, Integer> playerDistance = new HashMap<>();
private final Map<UUID, Integer> playerHoldTimer = new HashMap<>();

@SubscribeEvent
public void onTwerk(TickEvent.PlayerTickEvent event) {
Expand All @@ -72,6 +77,10 @@ public void onTwerk(TickEvent.PlayerTickEvent event) {
triggerGrowth(event, uuid);
}

if (event.player.isSneaking()){
triggerGrowth(event, uuid);
}

boolean wasPlayerSneaking = prevSneaking.get(uuid);
int playerCrouchCount = crouchCount.get(uuid);
if (!event.player.isSneaking()) {
Expand All @@ -95,6 +104,7 @@ private void triggerGrowth(TickEvent.PlayerTickEvent event, UUID uuid) {
crouchCount.put(uuid, 0);
List<BlockPos> growables = getNearestBlocks(event.player.world, event.player.getPosition());
Set<BlockPos> grownDT = new HashSet<>();

for (BlockPos growablePos : growables) {
BlockState blockState = event.player.world.getBlockState(growablePos);
if (!FilterListHelper.shouldAllow(blockState.getBlock().getRegistryName().toString())) {
Expand All @@ -114,14 +124,25 @@ private void triggerGrowth(TickEvent.PlayerTickEvent event, UUID uuid) {
} else if (DTProxy.getProxy().blockAllowed(blockState.getBlock())) {
DTProxy.getProxy().grow(event.player.world, growablePos, grownDT);
}
((ServerWorld)event.player.world).spawnParticle((ServerPlayerEntity) event.player, ParticleTypes.HAPPY_VILLAGER, false, growablePos.getX() + event.player.world.rand.nextDouble(), growablePos.getY() + event.player.world.rand.nextDouble(), growablePos.getZ() + event.player.world.rand.nextDouble(), 10, 0, 0, 0, 3);
SpawnParticles(event.player, growablePos);
}

if (!TwerkConfig.growBabies) return;

List<AgeableEntity> entities = getNearestAgeableEntities(event.player.world, event.player.getPosition());

for (AgeableEntity entity:
entities) {
entity.addGrowth(8000 / 20);

SpawnParticles(event.player, entity.getPosition());
}
}

private List<BlockPos> getNearestBlocks(World world, BlockPos pos) {
List<BlockPos> list = new ArrayList<>();
for (int x = -TwerkConfig.effectRadius; x <= TwerkConfig.effectRadius; x++)
for (int y = -2; y <= 2; y++)
for (int y = -TwerkConfig.effectRadius; y <= TwerkConfig.effectRadius; y++)
for (int z = -TwerkConfig.effectRadius; z <= TwerkConfig.effectRadius; z++) {
Block block = world.getBlockState(new BlockPos(x + pos.getX(), y + pos.getY(), z + pos.getZ())).getBlock();
if (block instanceof IGrowable || DTProxy.getProxy().blockAllowed(block)) {
Expand All @@ -133,6 +154,46 @@ private List<BlockPos> getNearestBlocks(World world, BlockPos pos) {
return list;
}

private List<AgeableEntity> getNearestAgeableEntities(World world, BlockPos pos) {
List<AgeableEntity> list = new ArrayList<>();

for (int x = -TwerkConfig.effectRadius; x <= TwerkConfig.effectRadius; x++)
for (int y = -2; y <= 2; y++)
for (int z = -TwerkConfig.effectRadius; z <= TwerkConfig.effectRadius; z++) {
List<AgeableEntity> posEntitiesList =
world.getEntitiesWithinAABB(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use an AxisAlignedBB for the whole area instead of block by block. This can replace your whole method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested yet, but could work as well.
I thought of block by block because inside a same block ther can be multiple entities.
All in all, this is my first attempt at modding Minecraft. I am still adapting to the API. Any suggestions are welcome of course

AgeableEntity.class,
new AxisAlignedBB(
new BlockPos(
x + pos.getX(),
y + pos.getY(),
z + pos.getZ()
)
)
);
for (AgeableEntity entity :
posEntitiesList) {
if (entity.getGrowingAge() < 0) {
list.add(entity);
}
}
}

return list;
}

private void SpawnParticles(PlayerEntity player, BlockPos pos){
((ServerWorld)player.world).spawnParticle(
(ServerPlayerEntity) player,
ParticleTypes.HAPPY_VILLAGER,
false,
pos.getX() + player.world.rand.nextDouble(),
pos.getY() + player.world.rand.nextDouble(),
pos.getZ() + player.world.rand.nextDouble(),
10, 0, 0, 0, 3
);
}

private CompoundNBT createCompoundTag(BlockPos pos) {
CompoundNBT nbt = new CompoundNBT();
nbt.putInt("x", pos.getX());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class CommonConfig {
public final ForgeConfigSpec.BooleanValue saplingsOnly;
public final ForgeConfigSpec.DoubleValue sprintGrowChance;
public final ForgeConfigSpec.DoubleValue crouchGrowChance;
public final ForgeConfigSpec.BooleanValue growBabies;

public CommonConfig(ForgeConfigSpec.Builder builder) {
List<String> defaultBlackList = new ArrayList<>();
Expand Down Expand Up @@ -44,5 +45,7 @@ public CommonConfig(ForgeConfigSpec.Builder builder) {
.defineInRange("sprintGrowChance", 0.15, 0, 1);
crouchGrowChance = builder.comment("The chance of growth effect being applied from any source")
.defineInRange("crouchGrowChance", 0.5, 0, 1);
growBabies = builder.comment("Whether to grow up baby animals, villagers and other ageable mobs or not when twerking")
.define("growBabies", true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class TwerkConfig {
public static int distanceSprintedToGrow;
public static double sprintGrowChance;
public static double crouchGrowChance;
public static boolean growBabies;

public static void bake(ModConfig config) {
showParticles = TwerkItMeal.COMMON_CONFIG.showParticles.get();
Expand All @@ -27,5 +28,6 @@ public static void bake(ModConfig config) {
saplingsOnly = TwerkItMeal.COMMON_CONFIG.saplingsOnly.get();
sprintGrowChance = TwerkItMeal.COMMON_CONFIG.sprintGrowChance.get();
crouchGrowChance = TwerkItMeal.COMMON_CONFIG.crouchGrowChance.get();
growBabies = TwerkItMeal.COMMON_CONFIG.growBabies.get();
}
}