Skip to content

Commit 2766c23

Browse files
committed
Allow the flame-thrower to ignite blocks using the new FIRESTARTER_LIGHT item ability
1 parent 6f7c187 commit 2766c23

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

src/main/java/mekanism/common/entity/EntityFlame.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import net.minecraft.world.entity.projectile.ProjectileUtil;
2727
import net.minecraft.world.item.BlockItem;
2828
import net.minecraft.world.item.ItemStack;
29+
import net.minecraft.world.item.Items;
30+
import net.minecraft.world.item.context.UseOnContext;
2931
import net.minecraft.world.item.crafting.RecipeHolder;
3032
import net.minecraft.world.item.crafting.RecipeType;
3133
import net.minecraft.world.item.crafting.SingleRecipeInput;
@@ -34,18 +36,16 @@
3436
import net.minecraft.world.level.Level;
3537
import net.minecraft.world.level.block.BaseFireBlock;
3638
import net.minecraft.world.level.block.Block;
37-
import net.minecraft.world.level.block.CampfireBlock;
38-
import net.minecraft.world.level.block.CandleBlock;
39-
import net.minecraft.world.level.block.CandleCakeBlock;
4039
import net.minecraft.world.level.block.LevelEvent;
4140
import net.minecraft.world.level.block.TntBlock;
4241
import net.minecraft.world.level.block.state.BlockState;
43-
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
4442
import net.minecraft.world.phys.BlockHitResult;
4543
import net.minecraft.world.phys.EntityHitResult;
4644
import net.minecraft.world.phys.HitResult;
4745
import net.minecraft.world.phys.HitResult.Type;
4846
import net.minecraft.world.phys.Vec3;
47+
import net.neoforged.neoforge.common.ItemAbilities;
48+
import net.neoforged.neoforge.common.ItemAbility;
4949
import net.neoforged.neoforge.common.NeoForge;
5050
import net.neoforged.neoforge.common.util.BlockSnapshot;
5151
import net.neoforged.neoforge.entity.IEntityWithComplexSpawn;
@@ -163,15 +163,17 @@ protected void onHitBlock(@NotNull BlockHitResult blockRayTrace) {
163163
} else if (mode == FlamethrowerMode.INFERNO) {
164164
Entity owner = getOwner();
165165
BlockPos sidePos = hitPos.relative(hitSide);
166-
if (CampfireBlock.canLight(hitState) || CandleBlock.canLight(hitState) || CandleCakeBlock.canLight(hitState)) {
167-
tryPlace(owner, hitPos, hitSide, hitState.setValue(BlockStateProperties.LIT, true));
168-
} else if (BaseFireBlock.canBePlacedAt(level(), sidePos, hitSide)) {
169-
tryPlace(owner, sidePos, hitSide, BaseFireBlock.getState(level(), sidePos));
170-
} else if (hitState.isFlammable(level(), hitPos, hitSide)) {
171-
//TODO: Is there some event we should/can be firing here?
172-
hitState.onCaughtFire(level(), hitPos, hitSide, owner instanceof LivingEntity livingEntity ? livingEntity : null);
173-
if (hitState.getBlock() instanceof TntBlock) {
174-
level().removeBlock(hitPos, false);
166+
UseOnContext igniterContext = new UseOnContext(level(), owner instanceof Player player ? player : null,
167+
InteractionHand.MAIN_HAND, new ItemStack(Items.FIRE_CHARGE), blockRayTrace);
168+
if (!tryModify(owner, hitPos, hitSide, hitState, igniterContext, ItemAbilities.FIRESTARTER_LIGHT)) {
169+
if (BaseFireBlock.canBePlacedAt(level(), sidePos, hitSide)) {
170+
tryPlace(owner, sidePos, hitSide, BaseFireBlock.getState(level(), sidePos));
171+
} else if (hitState.isFlammable(level(), hitPos, hitSide)) {
172+
//TODO: Is there some event we should/can be firing here?
173+
hitState.onCaughtFire(level(), hitPos, hitSide, owner instanceof LivingEntity livingEntity ? livingEntity : null);
174+
if (hitState.getBlock() instanceof TntBlock) {
175+
level().removeBlock(hitPos, false);
176+
}
175177
}
176178
}
177179
}
@@ -183,6 +185,21 @@ protected void onHitBlock(@NotNull BlockHitResult blockRayTrace) {
183185
discard();
184186
}
185187

188+
private boolean tryModify(@Nullable Entity shooter, BlockPos pos, Direction hitSide, BlockState state, UseOnContext context, ItemAbility ability) {
189+
BlockSnapshot blockSnapshot = BlockSnapshot.create(level().dimension(), level(), pos);
190+
BlockState modifiedState = state.getToolModifiedState(context, ability, false);
191+
if (modifiedState != null) {
192+
level().setBlockAndUpdate(pos, modifiedState);
193+
}
194+
if (modifiedState == null || EventHooks.onBlockPlace(shooter, blockSnapshot, hitSide)) {
195+
level().restoringBlockSnapshots = true;
196+
blockSnapshot.restore(blockSnapshot.getFlags() | Block.UPDATE_CLIENTS);
197+
level().restoringBlockSnapshots = false;
198+
return false;
199+
}
200+
return true;
201+
}
202+
186203
private boolean tryPlace(@Nullable Entity shooter, BlockPos pos, Direction hitSide, BlockState newState) {
187204
BlockSnapshot blockSnapshot = BlockSnapshot.create(level().dimension(), level(), pos);
188205
level().setBlockAndUpdate(pos, newState);

0 commit comments

Comments
 (0)