|
| 1 | +package galena.oreganized.compat.supplementaries; |
| 2 | + |
| 3 | +import net.mehvahdjukaar.amendments.common.tile.WallLanternBlockTile; |
| 4 | +import net.minecraft.sounds.SoundEvents; |
| 5 | +import net.minecraft.sounds.SoundSource; |
| 6 | +import net.minecraft.world.InteractionResult; |
| 7 | +import net.minecraft.world.item.Items; |
| 8 | +import net.minecraft.world.level.block.CandleBlock; |
| 9 | +import net.minecraft.world.level.block.state.properties.BlockStateProperties; |
| 10 | +import net.minecraftforge.common.MinecraftForge; |
| 11 | +import net.minecraftforge.event.entity.player.PlayerInteractEvent; |
| 12 | + |
| 13 | +public class AmendmentsCompat { |
| 14 | + |
| 15 | + public static void register() { |
| 16 | + MinecraftForge.EVENT_BUS.addListener(AmendmentsCompat::onBlockInteract); |
| 17 | + } |
| 18 | + |
| 19 | + private static void onBlockInteract(PlayerInteractEvent.RightClickBlock event) { |
| 20 | + var pos = event.getPos(); |
| 21 | + var level = event.getLevel(); |
| 22 | + var be = level.getBlockEntity(pos); |
| 23 | + var held = event.getItemStack(); |
| 24 | + var player = event.getEntity(); |
| 25 | + |
| 26 | + if(!(be instanceof WallLanternBlockTile lantern)) return; |
| 27 | + |
| 28 | + var state = lantern.getHeldBlock(); |
| 29 | + var lit = state.getValue(CandleBlock.LIT); |
| 30 | + |
| 31 | + InteractionResult result = InteractionResult.PASS; |
| 32 | + |
| 33 | + if(held.is(Items.FLINT_AND_STEEL) && !lit) { |
| 34 | + level.playSound(player, pos, SoundEvents.FLINTANDSTEEL_USE, SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.4F + 0.8F); |
| 35 | + lantern.setHeldBlock(state.setValue(BlockStateProperties.LIT, true)); |
| 36 | + if (player != null) { |
| 37 | + held.hurtAndBreak(1, player, (p_41303_) -> { |
| 38 | + p_41303_.broadcastBreakEvent(event.getHand()); |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + result = InteractionResult.sidedSuccess(level.isClientSide()); |
| 43 | + } else if(held.isEmpty() && lit) { |
| 44 | + level.playSound(player, pos, SoundEvents.CANDLE_EXTINGUISH, SoundSource.BLOCKS, 1.0F, 1.0F); |
| 45 | + lantern.setHeldBlock(state.setValue(BlockStateProperties.LIT, false)); |
| 46 | + |
| 47 | + result = InteractionResult.sidedSuccess(level.isClientSide()); |
| 48 | + } |
| 49 | + |
| 50 | + if(result != InteractionResult.PASS) { |
| 51 | + event.setCancellationResult(result); |
| 52 | + event.setCanceled(true); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments