Skip to content

Commit

Permalink
Add entity attack functionality to LeftClickBlock event
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswolff96 committed Feb 19, 2024
1 parent c243f59 commit 6939c47
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/main/java/me/jameswolff/notthegrass/NotTheGrass.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

import com.mojang.logging.LogUtils;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.config.ModConfig;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent.LeftClickBlock;

import java.util.List;

import org.slf4j.Logger;

// The value here should match an entry in the META-INF/mods.toml file
Expand All @@ -35,14 +42,14 @@ public NotTheGrass(IEventBus modEventBus)
}

// Checks to make sure the block clicked is not in the {@link Config#blockIgnoreList} and then whether it has one of the tags
// in the {@link Config#tagList}, if so cancel the event.
// in the {@link Config#tagList}, if so check to see if that block contains any entities and if so attack the first one.
// If the config option {@link Config#cancelAction} is true then cancel the original LeftClickBlockEvent.
@SubscribeEvent
public void onLeftClickBlockEvent(LeftClickBlock event)
{
if (event == null) return;
if (event.getAction() == LeftClickBlock.Action.START)
{
LOGGER.info("Left click block start");
if(event.getLevel() == null) return;
BlockState blockState = event.getLevel().getBlockState(event.getPos());
if (blockState == null) return;
Expand All @@ -52,8 +59,23 @@ public void onLeftClickBlockEvent(LeftClickBlock event)
.filter(tag -> tag.isFor(BuiltInRegistries.BLOCK.key()))
.noneMatch(tag -> Config.tagList.contains(tag.location())))
return;
LOGGER.info("Cancelling left click block event");
event.setCanceled(true);
Player player = event.getEntity();
List<Entity> entitiesInBlock = event.getLevel().getEntities(player, new AABB(event.getPos()));
if (entitiesInBlock == null || entitiesInBlock.isEmpty()) return;
entitiesInBlock.forEach(entity -> LOGGER.info("Entity in block: " + entity));
for (Entity e : entitiesInBlock)
{
if (e instanceof ItemEntity)
{
continue;
}
else
{
player.attack(e);
break;
}
}
event.setCanceled(Config.cancelAction);
}
}
}

0 comments on commit 6939c47

Please sign in to comment.